Skip to content

Commit

Permalink
cli: add NODE_RUN_PACKAGE_JSON_PATH env
Browse files Browse the repository at this point in the history
PR-URL: nodejs#53058
Refs: nodejs#52673
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
  • Loading branch information
anonrig committed May 21, 2024
1 parent d031987 commit 2aaeaa8
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
5 changes: 5 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,9 @@ changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/53032
description: NODE_RUN_SCRIPT_NAME environment variable is added.
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/53058
description: NODE_RUN_PACKAGE_JSON_PATH environment variable is added.
-->

> Stability: 1.1 - Active development
Expand Down Expand Up @@ -1907,6 +1910,8 @@ The following environment variables are set when running a script with `--run`:

* `NODE_RUN_SCRIPT_NAME`: The name of the script being run. For example, if
`--run` is used to run `test`, the value of this variable will be `test`.
* `NODE_RUN_PACKAGE_JSON_PATH`: The path to the `package.json` that is being
processed.

### `--secure-heap=n`

Expand Down
29 changes: 24 additions & 5 deletions src/node_task_runner.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "node_task_runner.h"
#include "util.h"

#include <filesystem>
#include <regex> // NOLINT(build/c++11)

namespace node::task_runner {
Expand All @@ -12,7 +13,8 @@ static constexpr const char* bin_path = "/node_modules/.bin";
#endif // _WIN32

ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
const std::string& script_name,
std::string_view package_json_path,
std::string_view script_name,
std::string_view command,
const PositionalArgs& positional_args) {
memset(&options_, 0, sizeof(uv_process_options_t));
Expand Down Expand Up @@ -52,7 +54,10 @@ ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
// callback.
process_.data = this;

SetEnvironmentVariables(current_bin_path, script_name);
SetEnvironmentVariables(current_bin_path,
std::string_view(cwd, cwd_size),
package_json_path,
script_name);

std::string command_str(command);

Expand Down Expand Up @@ -102,7 +107,9 @@ ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
}

void ProcessRunner::SetEnvironmentVariables(const std::string& current_bin_path,
const std::string& script_name) {
std::string_view cwd,
std::string_view package_json_path,
std::string_view script_name) {
uv_env_item_t* env_items;
int env_count;
CHECK_EQ(0, uv_os_environ(&env_items, &env_count));
Expand Down Expand Up @@ -132,7 +139,19 @@ void ProcessRunner::SetEnvironmentVariables(const std::string& current_bin_path,

// Add NODE_RUN_SCRIPT_NAME environment variable to the environment
// to indicate which script is being run.
env_vars_.push_back("NODE_RUN_SCRIPT_NAME=" + script_name);
env_vars_.push_back("NODE_RUN_SCRIPT_NAME=" + std::string(script_name));

// Add NODE_RUN_PACKAGE_JSON_PATH environment variable to the environment to
// indicate which package.json is being processed.
if (std::filesystem::path(package_json_path).is_absolute()) {
// TODO(anonrig): Traverse up the directory tree until we find a
// package.json
env_vars_.push_back("NODE_RUN_PACKAGE_JSON_PATH=" +
std::string(package_json_path));
} else {
auto path = std::filesystem::path(cwd) / std::string(package_json_path);
env_vars_.push_back("NODE_RUN_PACKAGE_JSON_PATH=" + path.string());
}

env = std::unique_ptr<char*[]>(new char*[env_vars_.size() + 1]);
options_.env = env.get();
Expand Down Expand Up @@ -284,7 +303,7 @@ void RunTask(std::shared_ptr<InitializationResultImpl> result,
}

auto runner =
ProcessRunner(result, std::string(command_id), command, positional_args);
ProcessRunner(result, path, command_id, command, positional_args);
runner.Run();
}

Expand Down
7 changes: 5 additions & 2 deletions src/node_task_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ using PositionalArgs = std::vector<std::string_view>;
class ProcessRunner {
public:
ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
const std::string& script_name,
std::string_view package_json_path,
std::string_view script_name,
std::string_view command_id,
const PositionalArgs& positional_args);
void Run();
Expand All @@ -45,7 +46,9 @@ class ProcessRunner {
// OnExit is the callback function that is called when the process exits.
void OnExit(int64_t exit_status, int term_signal);
void SetEnvironmentVariables(const std::string& bin_path,
const std::string& script_name);
std::string_view cwd,
std::string_view package_json_path,
std::string_view script_name);

#ifdef _WIN32
std::string file_ = "cmd.exe";
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/parallel/test-node-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ describe('node run [command]', () => {

it('should set special environment variables', async () => {
const scriptName = `special-env-variables${envSuffix}`;
const packageJsonPath = fixtures.path('run-script/package.json');
const child = await common.spawnPromisified(
process.execPath,
[ '--no-warnings', '--run', scriptName],
{ cwd: fixtures.path('run-script') },
);
assert.ok(child.stdout.includes(scriptName));
assert.ok(child.stdout.includes(packageJsonPath));
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
Expand Down

0 comments on commit 2aaeaa8

Please sign in to comment.