Skip to content

Commit cfcde78

Browse files
anonrigtargos
authored andcommittedJun 1, 2024
cli: add NODE_RUN_PACKAGE_JSON_PATH env
PR-URL: #53058 Refs: #52673 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Daniel Lemire <daniel@lemire.me>
1 parent 793af1b commit cfcde78

File tree

6 files changed

+38
-7
lines changed

6 files changed

+38
-7
lines changed
 

‎doc/api/cli.md

+5
Original file line numberDiff line numberDiff line change
@@ -1879,6 +1879,9 @@ changes:
18791879
- version: REPLACEME
18801880
pr-url: https://github.com/nodejs/node/pull/53032
18811881
description: NODE_RUN_SCRIPT_NAME environment variable is added.
1882+
- version: REPLACEME
1883+
pr-url: https://github.com/nodejs/node/pull/53058
1884+
description: NODE_RUN_PACKAGE_JSON_PATH environment variable is added.
18821885
-->
18831886

18841887
> Stability: 1.1 - Active development
@@ -1925,6 +1928,8 @@ The following environment variables are set when running a script with `--run`:
19251928

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

19291934
### `--secure-heap=n`
19301935

‎src/node_task_runner.cc

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "node_task_runner.h"
22
#include "util.h"
33

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

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

1415
ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
15-
const std::string& script_name,
16+
std::string_view package_json_path,
17+
std::string_view script_name,
1618
std::string_view command,
1719
const PositionalArgs& positional_args) {
1820
memset(&options_, 0, sizeof(uv_process_options_t));
@@ -52,7 +54,10 @@ ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
5254
// callback.
5355
process_.data = this;
5456

55-
SetEnvironmentVariables(current_bin_path, script_name);
57+
SetEnvironmentVariables(current_bin_path,
58+
std::string_view(cwd, cwd_size),
59+
package_json_path,
60+
script_name);
5661

5762
std::string command_str(command);
5863

@@ -102,7 +107,9 @@ ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
102107
}
103108

104109
void ProcessRunner::SetEnvironmentVariables(const std::string& current_bin_path,
105-
const std::string& script_name) {
110+
std::string_view cwd,
111+
std::string_view package_json_path,
112+
std::string_view script_name) {
106113
uv_env_item_t* env_items;
107114
int env_count;
108115
CHECK_EQ(0, uv_os_environ(&env_items, &env_count));
@@ -132,7 +139,19 @@ void ProcessRunner::SetEnvironmentVariables(const std::string& current_bin_path,
132139

133140
// Add NODE_RUN_SCRIPT_NAME environment variable to the environment
134141
// to indicate which script is being run.
135-
env_vars_.push_back("NODE_RUN_SCRIPT_NAME=" + script_name);
142+
env_vars_.push_back("NODE_RUN_SCRIPT_NAME=" + std::string(script_name));
143+
144+
// Add NODE_RUN_PACKAGE_JSON_PATH environment variable to the environment to
145+
// indicate which package.json is being processed.
146+
if (std::filesystem::path(package_json_path).is_absolute()) {
147+
// TODO(anonrig): Traverse up the directory tree until we find a
148+
// package.json
149+
env_vars_.push_back("NODE_RUN_PACKAGE_JSON_PATH=" +
150+
std::string(package_json_path));
151+
} else {
152+
auto path = std::filesystem::path(cwd) / std::string(package_json_path);
153+
env_vars_.push_back("NODE_RUN_PACKAGE_JSON_PATH=" + path.string());
154+
}
136155

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

286305
auto runner =
287-
ProcessRunner(result, std::string(command_id), command, positional_args);
306+
ProcessRunner(result, path, command_id, command, positional_args);
288307
runner.Run();
289308
}
290309

‎src/node_task_runner.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ using PositionalArgs = std::vector<std::string_view>;
2323
class ProcessRunner {
2424
public:
2525
ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
26-
const std::string& script_name,
26+
std::string_view package_json_path,
27+
std::string_view script_name,
2728
std::string_view command_id,
2829
const PositionalArgs& positional_args);
2930
void Run();
@@ -45,7 +46,9 @@ class ProcessRunner {
4546
// OnExit is the callback function that is called when the process exits.
4647
void OnExit(int64_t exit_status, int term_signal);
4748
void SetEnvironmentVariables(const std::string& bin_path,
48-
const std::string& script_name);
49+
std::string_view cwd,
50+
std::string_view package_json_path,
51+
std::string_view script_name);
4952

5053
#ifdef _WIN32
5154
std::string file_ = "cmd.exe";

‎test/fixtures/run-script/node_modules/.bin/special-env-variables

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/run-script/node_modules/.bin/special-env-variables.bat

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/parallel/test-node-run.js

+2
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,14 @@ describe('node run [command]', () => {
8383

8484
it('should set special environment variables', async () => {
8585
const scriptName = `special-env-variables${envSuffix}`;
86+
const packageJsonPath = fixtures.path('run-script/package.json');
8687
const child = await common.spawnPromisified(
8788
process.execPath,
8889
[ '--no-warnings', '--run', scriptName],
8990
{ cwd: fixtures.path('run-script') },
9091
);
9192
assert.ok(child.stdout.includes(scriptName));
93+
assert.ok(child.stdout.includes(packageJsonPath));
9294
assert.strictEqual(child.stderr, '');
9395
assert.strictEqual(child.code, 0);
9496
});

0 commit comments

Comments
 (0)
Please sign in to comment.