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 all commits
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
49 changes: 30 additions & 19 deletions packages/create-react-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,37 +108,48 @@ function createApp(name, verbose, version) {
}

function install(packageToInstall, verbose, callback) {
var args = [
function fallbackToNpm() {
var npmArgs = [
'install',
verbose && '--verbose',
'--save-dev',
'--save-exact',
packageToInstall,
].filter(function(e) { return e; });
var npmProc = spawn('npm', npmArgs, {stdio: 'inherit'});
npmProc.on('close', function (code) {
callback(code, 'npm', npmArgs);
});
}

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

var yarnProc;
var yarnExists = true;
proc.on('error', function (err) {
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

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

Expand Down