diff --git a/src/node_task_runner.cc b/src/node_task_runner.cc index 136dec24fa02..b9716846abfc 100644 --- a/src/node_task_runner.cc +++ b/src/node_task_runner.cc @@ -233,7 +233,9 @@ FindPackageJson(const std::filesystem::path& cwd) { directory_path = directory_path.parent_path()) { // Append "path/node_modules/.bin" to the env var, if it is a directory. auto node_modules_bin = directory_path / "node_modules" / ".bin"; - if (std::filesystem::is_directory(node_modules_bin)) { + // Skip directories that can't be inspected (e.g. permission denied). + std::error_code error_code; + if (std::filesystem::is_directory(node_modules_bin, error_code)) { path_env_var += ConvertPathToUTF8(node_modules_bin) + env_var_separator; } @@ -279,7 +281,15 @@ static void PrintScripts(FILE* out, void RunTask(const std::shared_ptr& result, std::string_view command_id, const std::vector& positional_args) { - auto cwd = std::filesystem::current_path(); + std::error_code error_code; + auto cwd = std::filesystem::current_path(error_code); + if (error_code) { + fprintf(stderr, + "Can't determine the current working directory: %s\n", + error_code.message().c_str()); + result->exit_code_ = ExitCode::kGenericUserError; + return; + } auto package_json = FindPackageJson(cwd); if (!package_json.has_value()) { diff --git a/test/parallel/test-node-run.js b/test/parallel/test-node-run.js index 57faa80b9f82..b276c54ca23e 100644 --- a/test/parallel/test-node-run.js +++ b/test/parallel/test-node-run.js @@ -12,6 +12,8 @@ const fixtures = require('../common/fixtures'); const tmpdir = require('../common/tmpdir'); const envSuffix = common.isWindows ? '-windows' : ''; +tmpdir.refresh(); + describe('node --run [command]', { concurrency: !process.env.TEST_PARALLEL }, () => { it('returns error on non-existent file', async () => { const child = await common.spawnPromisified( @@ -336,4 +338,45 @@ describe('node --run [command]', { concurrency: !process.env.TEST_PARALLEL }, () assert.strictEqual(child.stdout, ''); assert.strictEqual(child.code, 1); }); + + it('skips node_modules directories that cannot be inspected', { + skip: common.isWindows || process.getuid() === 0, + }, async () => { + const projectDir = tmpdir.resolve('unreadable-node-modules'); + const nodeModules = path.join(projectDir, 'node_modules'); + fs.mkdirSync(nodeModules, { recursive: true }); + fs.writeFileSync(path.join(projectDir, 'package.json'), JSON.stringify({ + scripts: { test: 'echo ok' }, + })); + fs.chmodSync(nodeModules, 0o000); + try { + const child = await common.spawnPromisified( + process.execPath, + [ '--run', 'test'], + { cwd: projectDir }, + ); + assert.strictEqual(child.stdout.trim(), 'ok'); + assert.strictEqual(child.stderr, ''); + assert.strictEqual(child.code, 0); + } finally { + fs.chmodSync(nodeModules, 0o755); + } + }); + + it('returns error when the current working directory is removed', { + // Windows, SunOS, AIX and IBM i refuse to remove a directory that is in + // use as a cwd. + skip: common.isWindows || common.isSunOS || common.isAIX || common.isIBMi, + }, async () => { + const removedDir = tmpdir.resolve('removed-cwd'); + fs.mkdirSync(removedDir); + const child = await common.spawnPromisified( + '/bin/sh', + [ '-c', 'rmdir "$PWD" && exec "$0" --run test', process.execPath], + { cwd: removedDir }, + ); + assert.match(child.stderr, /Can't determine the current working directory/); + assert.strictEqual(child.stdout, ''); + assert.strictEqual(child.code, 1); + }); });