Skip to content

Commit

Permalink
cli: ensure --check flag works for piped-in code
Browse files Browse the repository at this point in the history
Previously, the --check CLI flag had no effect when run on code piped
from stdin. This commit updates the bootstrap logic to handle the
--check flag the same way regardless of whether the code is piped from
stdin.

Fixes: nodejs#11680
  • Loading branch information
not-an-aardvark committed Apr 1, 2017
1 parent c68da89 commit 6e255ad
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
30 changes: 20 additions & 10 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,11 @@

// check if user passed `-c` or `--check` arguments to Node.
if (process._syntax_check_only != null) {
const vm = NativeModule.require('vm');
const fs = NativeModule.require('fs');
const internalModule = NativeModule.require('internal/module');
// read the source
const filename = Module._resolveFilename(process.argv[1]);
var source = fs.readFileSync(filename, 'utf-8');
// remove shebang and BOM
source = internalModule.stripBOM(source.replace(/^#!.*/, ''));
// wrap it
source = Module.wrap(source);
// compile the script, this will throw if it fails
new vm.Script(source, {filename: filename, displayErrors: true});
checkScriptSyntax(source);
process.exit(0);
}

Expand Down Expand Up @@ -184,8 +177,12 @@
});

process.stdin.on('end', function() {
process._eval = code;
evalScript('[stdin]');
if (process._syntax_check_only != null) {
checkScriptSyntax(code);
} else {
process._eval = code;
evalScript('[stdin]');
}
});
}
}
Expand Down Expand Up @@ -445,6 +442,19 @@
}
}

function checkScriptSyntax(source) {
const Module = NativeModule.require('module');
const vm = NativeModule.require('vm');
const internalModule = NativeModule.require('internal/module');

// remove shebang and BOM
source = internalModule.stripBOM(source.replace(/^#!.*/, ''));
// wrap it
source = Module.wrap(source);
// compile the script, this will throw if it fails
new vm.Script(source, {displayErrors: true});
}

// Below you find a minimal module system, which is used to load the node
// core modules found in lib/*.js. All core modules are compiled into the
// node binary, so they can be loaded faster.
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-cli-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,16 @@ const syntaxArgs = [
assert.strictEqual(c.status, 1, 'code == ' + c.status);
});
});

// should not execute code piped from stdin with --check
// loop each possible option, `-c` or `--check`
syntaxArgs.forEach(function(args) {
const stdin = 'throw new Error("should not get run");';
const c = spawnSync(node, args, {encoding: 'utf8', input: stdin});

// no stdout or stderr should be produced
assert.strictEqual(c.stdout, '', 'stdout produced');
assert.strictEqual(c.stderr, '', 'stderr produced');

assert.strictEqual(c.status, 0, 'code == ' + c.status);
});

0 comments on commit 6e255ad

Please sign in to comment.