-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add preflight CWD check for npm #3355
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,8 +158,13 @@ function createApp(name, verbose, version, template) { | |
path.join(root, 'package.json'), | ||
JSON.stringify(packageJson, null, 2) | ||
); | ||
|
||
const useYarn = shouldUseYarn(); | ||
const originalDirectory = process.cwd(); | ||
process.chdir(root); | ||
if (!useYarn && !checkThatNpmCanReadCwd()) { | ||
process.exit(1); | ||
} | ||
|
||
if (!semver.satisfies(process.version, '>=6.0.0')) { | ||
console.log( | ||
|
@@ -172,7 +177,6 @@ function createApp(name, verbose, version, template) { | |
version = '[email protected]'; | ||
} | ||
|
||
const useYarn = shouldUseYarn(); | ||
if (!useYarn) { | ||
const npmInfo = checkNpmVersion(); | ||
if (!npmInfo.hasMinNpm) { | ||
|
@@ -200,7 +204,7 @@ function shouldUseYarn() { | |
} | ||
} | ||
|
||
function install(useYarn, dependencies, verbose, isOnline) { | ||
function install(root, useYarn, dependencies, verbose, isOnline) { | ||
return new Promise((resolve, reject) => { | ||
let command; | ||
let args; | ||
|
@@ -212,6 +216,14 @@ function install(useYarn, dependencies, verbose, isOnline) { | |
} | ||
[].push.apply(args, dependencies); | ||
|
||
// Explicitly set cwd() to work around issues like | ||
// https://github.com/facebookincubator/create-react-app/issues/3326. | ||
// Unfortunately we can only do this for Yarn because npm support for | ||
// equivalent --prefix flag doesn't help with this issue. | ||
// This is why for npm, we run checkThatNpmCanReadCwd() early instead. | ||
args.push('--cwd'); | ||
args.push(root); | ||
|
||
if (!isOnline) { | ||
console.log(chalk.yellow('You appear to be offline.')); | ||
console.log(chalk.yellow('Falling back to the local Yarn cache.')); | ||
|
@@ -275,7 +287,7 @@ function run( | |
); | ||
console.log(); | ||
|
||
return install(useYarn, allDependencies, verbose, isOnline).then( | ||
return install(root, useYarn, allDependencies, verbose, isOnline).then( | ||
() => packageName | ||
); | ||
}) | ||
|
@@ -606,6 +618,67 @@ function isSafeToCreateProjectIn(root, name) { | |
return false; | ||
} | ||
|
||
function checkThatNpmCanReadCwd() { | ||
const cwd = process.cwd(); | ||
let childOutput = null; | ||
try { | ||
// Note: intentionally using spawn over exec since | ||
// the problem doesn't reproduce otherwise. | ||
// `npm config list` is the only reliably way I could find | ||
// to reproduce the wrong path. Just printing process.cwd() | ||
// in a Node process was not enough. | ||
childOutput = spawn.sync('npm', ['config', 'list']).output.join(''); | ||
} catch (err) { | ||
// Something went wrong spawning node. | ||
// Not great, but it means we can't do this check. | ||
// We might fail later on, but let's continue. | ||
return true; | ||
} | ||
if (typeof childOutput !== 'string') { | ||
return true; | ||
} | ||
const lines = childOutput.split('\n'); | ||
// `npm config list` output includes the following line: | ||
// "; cwd = C:\path\to\current\dir" (unquoted) | ||
// I couldn't find an easier way to get it. | ||
const prefix = '; cwd = '; | ||
const line = lines.find(line => line.indexOf(prefix) === 0); | ||
if (typeof line !== 'string') { | ||
// Fail gracefully. They could remove it. | ||
return true; | ||
} | ||
const npmCWD = line.substring(prefix.length); | ||
if (npmCWD === cwd) { | ||
return true; | ||
} | ||
console.error( | ||
chalk.red( | ||
`Could not start an npm process in the right directory.\n\n` + | ||
`The current directory is: ${chalk.bold(cwd)}\n` + | ||
`However, a newly started npm process runs in: ${chalk.bold( | ||
npmCWD | ||
)}\n\n` + | ||
`This is probably caused by a miconfigured system terminal shell.` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. miSconfigured There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks |
||
) | ||
); | ||
if (process.platform === 'win32') { | ||
console.error( | ||
chalk.red(`On Windows, this can usually be fixed by running:\n\n`) + | ||
` ${chalk.cyan( | ||
'reg' | ||
)} delete "HKCU\\Software\\Microsoft\\Command Processor" /v AutoRun /f\n` + | ||
` ${chalk.cyan( | ||
'reg' | ||
)} delete "HKLM\\Software\\Microsoft\\Command Processor" /v AutoRun /f\n\n` + | ||
chalk.red(`Try to run the above two lines in the terminal.\n`) + | ||
chalk.red( | ||
`To learn more about this problem, read: https://blogs.msdn.microsoft.com/oldnewthing/20071121-00/?p=24433/` | ||
) | ||
); | ||
} | ||
return false; | ||
} | ||
|
||
function checkIfOnline(useYarn) { | ||
if (!useYarn) { | ||
// Don't ping the Yarn registry. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this needs to be more relaxed regarding case sensitivity. But we can probably release like this and then wait for issues to come up. I'd prefer it be more strict and relaxed later than vaguely relaxed just in case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Windows and MacOS are both case-insensitive, so I wonder if any strange issues will come up here. In the past I've encountered issues where some places used
c:\
while others usedC:\
.Does Node.js not have a method to determine if one path is a substring of another??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find one.