Skip to content
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

Catch synchronous errors from spawning yarn #1204

Merged
merged 2 commits into from
Dec 8, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 33 additions & 24 deletions packages/create-react-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,38 +108,47 @@ function createApp(name, verbose, version) {
}

function install(packageToInstall, verbose, callback) {
var args = [
'add',
'--dev',
'--exact',
packageToInstall,
];
var proc = spawn('yarn', args, {stdio: 'inherit'});

var yarnExists = true;
proc.on('error', function (err) {
if (err.code === 'ENOENT') {
yarnExists = false;
}
});
proc.on('close', function (code) {
if (yarnExists) {
callback(code, 'yarn', args);
return;
}
// No Yarn installed, continuing with npm.
args = [
function fallbackToNpm() {
var npmArgs = [
'install',
verbose && '--verbose',
'--save-dev',
'--save-exact',
packageToInstall,
].filter(function(e) { return e; });
var npmProc = spawn('npm', args, {stdio: 'inherit'});
var npmProc = spawn('npm', npmArgs, {stdio: 'inherit'});
npmProc.on('close', function (code) {
callback(code, 'npm', args);
callback(code, 'npm', npmArgs);
});
});
}

var yarnArgs = [
'add',
'--dev',
'--exact',
packageToInstall,
];
var yarnProc;
var yarnExists = true;
try {
yarnProc = spawn('yarn', yarnArgs, {stdio: 'inherit'});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd only wrap this single line with try-catch, because that's the thing we need to catch the errors from. I don't want us to get stuck in an infinite loop if something else fails in the fallback npm command for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

yarnProc.on('error', function (err) {
if (err.code === 'ENOENT') {
yarnExists = false;
}
});
yarnProc.on('close', function (code) {
if (yarnExists) {
callback(code, 'yarn', yarnArgs);
return;
}
Copy link
Contributor

@fson fson Dec 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to call fallbackToNpm() here, so that we'll fall back to npm if the yarn process emitted the ENOENT error, which is asynchronous (handled above).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

});
} catch (err) {
// It's not clear why we end up here in some cases but we need this.
// https://github.com/facebookincubator/create-react-app/issues/1200
yarnExists = false;
fallbackToNpm();
}
}

function run(root, appName, version, verbose, originalDirectory) {
Expand Down