Skip to content

Failed 'create-react-app .' deletes current directory #9176

Open
@10hendersonm

Description

@10hendersonm

Describe the bug

When CRA fails, it deletes the created directory. However, when calling create-react-app ., that directory is the current directory.

I see this is done intentionally, and CRA purposefully moves the node process out of the directory in order to delete it, but the shell process is still left in that directory.

Why is this a problem?

My user base works on a custom npm registry - which can cause failures with module/template/etc resolution when misconfigured - and npm chokes when run from a directory which does not exist. While npm's failure is not an issue in CRA, npm is just an example in this case. Many tools break when trying to run from a directory which does not exist, and users may attempt to use those tools to troubleshoot why CRA failed.

Duplicating

# create a temp space for easy cleanup
mkdir tmp-cra
cd tmp-cra

# make an incomplete template that will fail
mkdir cra-template-broken
cd cra-template-broken
echo '{"name": "cra-template-broken"}' > package.json
cd ..

# run CRA against that template
mkdir cra-working-directory
cd cra-working-directory
npx create-react-app@latest . --template file:../cra-template-broken

# assert on the current directory existing
pwd
ls $PWD
# npm will break when running inside of a directory which does not exist.
# When CRA fails due to registry or other npm configuration issues,
# you may not realize you're in a non-existent directory before attempting to troubleshoot the registry issue.
npm config get registry

# clean up
cd ../..
rm -r tmp-cra
Sample Output (click to expand)

image

Going back to that defensive code I mentioned earlier, it seems that process.chdir(...) is designed to change the working directory of the current process, but does not change the directory of the overarching shell process, meaning that CRA is successfully navigating up and out of this directory, but the shell is left behind. Apparently that's tough to do (I have no evidence to back that up), so my suggestion is to dodge it entirely by only removing the directory when the process is not in that directory.

My suggestion is to detect if root === process.cwd() and avoid fs.removeSyncing the directory (see below), however I also see that this code is in createReactApp.js, and as such is the untouchable stuff of legends.

-        if (!remainingFiles.length) {
+        if (!remainingFiles.length && root !== process.cwd()) {

⬆️ May require additional path.resolve(root)?

The node interaction can be further validated with this script:

echo "shell pwd before: $PWD"
node -e "
const path = require('path')
console.log('script cwd before', process.cwd())
process.chdir(path.resolve(process.cwd(), '..'))
console.log('script cwd after', process.cwd())
"
echo "shell pwd after: $PWD"
Sample Output (click to expand)

image

Did you try recovering your dependencies?

N/A

Which terms did you search for in User Guide?

dir(ectory)
remove
clean
fail
error

Environment

Environment Info:

  current version of create-react-app: 3.4.1
  running from /Users/z001gk0/.nvm/versions/node/v12.14.1/lib/node_modules/create-react-app

  System:
    OS: macOS Mojave 10.14.6
    CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
  Binaries:
    Node: 12.14.1 - ~/.nvm/versions/node/v12.14.1/bin/node
    Yarn: 1.22.4 - ~/.nvm/versions/node/v12.14.1/bin/yarn
    npm: 6.13.7 - ~/.nvm/versions/node/v12.14.1/bin/npm
  Browsers:
    Chrome: 83.0.4103.106
    Firefox: Not Found
    Safari: 13.1.1
  npmPackages:
    react: Not Found
    react-dom: Not Found
    react-scripts: Not Found
  npmGlobalPackages:
    create-react-app: 3.4.1

Steps to reproduce

# create a temp space for easy cleanup
mkdir tmp-cra
cd tmp-cra

# make an incomplete template that will fail
mkdir cra-template-broken
cd cra-template-broken
echo '{"name": "cra-template-broken"}' > package.json
cd ..

# run CRA against that template
mkdir cra-working-directory
cd cra-working-directory
npx create-react-app@latest . --template file:../cra-template-broken

# assert on the current directory existing
pwd
ls $PWD
# npm will break when running inside of a directory which does not exist.
# When CRA fails due to registry or other npm configuration issues,
# you may not realize you're in a non-existent directory before attempting to troubleshoot the registry issue.
npm config get registry

# clean up
cd ../..
rm -r tmp-cra

To re-iterate, the npm issue is beside the point and is only placed there as an example of one of many tools which will break when the current directory does not exist.

Expected behavior

Either:

  • CRA should move the shell process out of the working directory when deleting it
  • CRA should not delete the working directory when it is the same as the current directory

Actual behavior

CRA deletes the working directory (when it is the same as the current directory).

Reproducible demo

It's not really a runtime issue as much as an issue on generation, so Steps to reproduce may have to suffice.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions