Skip to content
Closed
Show file tree
Hide file tree
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
16 changes: 10 additions & 6 deletions src/node_task_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void ProcessRunner::Run() {
std::optional<std::tuple<std::filesystem::path, std::string, std::string>>
FindPackageJson(const std::filesystem::path& cwd) {
auto package_json_path = cwd / "package.json";
std::string raw_content;
std::optional<std::string> raw_content;
std::string path_env_var;
auto root_path = cwd.root_path();

Expand All @@ -231,22 +231,26 @@ FindPackageJson(const std::filesystem::path& cwd) {
path_env_var += ConvertPathToUTF8(node_modules_bin) + env_var_separator;
}

if (raw_content.empty()) {
if (!raw_content.has_value()) {
package_json_path = directory_path / "package.json";
// This is required for Windows because std::filesystem::path::c_str()
// returns wchar_t* on Windows, and char* on other platforms.
std::string contents = ConvertPathToUTF8(package_json_path);
USE(ReadFileSync(&raw_content, contents.c_str()) > 0);
std::string package_json_path_string =
ConvertPathToUTF8(package_json_path);
std::string contents;
if (ReadFileSync(&contents, package_json_path_string.c_str()) >= 0) {
raw_content.emplace(std::move(contents));
}
}
}

// This means that there is no package.json until the root directory.
// In this case, we just return nullopt, which will terminate the process..
if (raw_content.empty()) {
if (!raw_content.has_value()) {
return std::nullopt;
}

return {{package_json_path, raw_content, path_env_var}};
return {{package_json_path, std::move(raw_content.value()), path_env_var}};
}

void RunTask(const std::shared_ptr<InitializationResultImpl>& result,
Expand Down
Empty file.
13 changes: 13 additions & 0 deletions test/parallel/test-node-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,19 @@ describe('node --run [command]', () => {
assert.strictEqual(child.code, 1);
});

it('stops searching at an empty package.json', async () => {
const packageJsonPath = fixtures.path('run-script/empty-json/package.json');
const child = await common.spawnPromisified(
process.execPath,
[ '--run', 'test'],
{ cwd: fixtures.path('run-script/empty-json') },
);
assert.strictEqual(child.stdout, '');
assert.match(child.stderr, /Can't parse/);
assert(child.stderr.includes(packageJsonPath));
assert.strictEqual(child.code, 1);
});

it('returns error when there is no "scripts" field file', async () => {
const child = await common.spawnPromisified(
process.execPath,
Expand Down
Loading