@@ -12,6 +12,7 @@ static constexpr const char* bin_path = "/node_modules/.bin";
1212#endif // _WIN32
1313
1414ProcessRunner::ProcessRunner (std::shared_ptr<InitializationResultImpl> result,
15+ const std::string& script_name,
1516 std::string_view command,
1617 const PositionalArgs& positional_args) {
1718 memset (&options_, 0 , sizeof (uv_process_options_t ));
@@ -51,39 +52,9 @@ ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
5152 // callback.
5253 process_.data = this ;
5354
54- std::string command_str (command );
55+ SetEnvironmentVariables (current_bin_path, script_name );
5556
56- // Set environment variables
57- uv_env_item_t * env_items;
58- int env_count;
59- CHECK_EQ (0 , uv_os_environ (&env_items, &env_count));
60- env = std::unique_ptr<char *[]>(new char *[env_count + 1 ]);
61- options_.env = env.get ();
62-
63- // Iterate over environment variables once to store them in the current
64- // ProcessRunner instance.
65- for (int i = 0 ; i < env_count; i++) {
66- std::string name = env_items[i].name ;
67- auto value = env_items[i].value ;
68-
69- #ifdef _WIN32
70- // We use comspec environment variable to find cmd.exe path on Windows
71- // Example: 'C:\\Windows\\system32\\cmd.exe'
72- // If we don't find it, we fallback to 'cmd.exe' for Windows
73- if (StringEqualNoCase (name.c_str (), " comspec" )) {
74- file_ = value;
75- }
76- #endif // _WIN32
77-
78- // Check if environment variable key is matching case-insensitive "path"
79- if (StringEqualNoCase (name.c_str (), " path" )) {
80- env_vars_.push_back (name + " =" + current_bin_path + value);
81- } else {
82- // Environment variables should be in "KEY=value" format
83- env_vars_.push_back (name + " =" + value);
84- }
85- }
86- uv_os_free_environ (env_items, env_count);
57+ std::string command_str (command);
8758
8859 // Use the stored reference on the instance.
8960 options_.file = file_.c_str ();
@@ -128,11 +99,47 @@ ProcessRunner::ProcessRunner(std::shared_ptr<InitializationResultImpl> result,
12899 options_.args [i] = const_cast <char *>(command_args_[i].c_str ());
129100 }
130101 options_.args [argc] = nullptr ;
102+ }
131103
104+ void ProcessRunner::SetEnvironmentVariables (const std::string& current_bin_path,
105+ const std::string& script_name) {
106+ uv_env_item_t * env_items;
107+ int env_count;
108+ CHECK_EQ (0 , uv_os_environ (&env_items, &env_count));
109+
110+ // Iterate over environment variables once to store them in the current
111+ // ProcessRunner instance.
132112 for (int i = 0 ; i < env_count; i++) {
113+ std::string name = env_items[i].name ;
114+ std::string value = env_items[i].value ;
115+
116+ #ifdef _WIN32
117+ // We use comspec environment variable to find cmd.exe path on Windows
118+ // Example: 'C:\\Windows\\system32\\cmd.exe'
119+ // If we don't find it, we fallback to 'cmd.exe' for Windows
120+ if (StringEqualNoCase (name.c_str (), " comspec" )) {
121+ file_ = value;
122+ }
123+ #endif // _WIN32
124+
125+ if (StringEqualNoCase (name.c_str (), " path" )) {
126+ // Add bin_path to the beginning of the PATH
127+ value = current_bin_path + value;
128+ }
129+ env_vars_.push_back (name + " =" + value);
130+ }
131+ uv_os_free_environ (env_items, env_count);
132+
133+ // Add NODE_RUN_SCRIPT_NAME environment variable to the environment
134+ // to indicate which script is being run.
135+ env_vars_.push_back (" NODE_RUN_SCRIPT_NAME=" + script_name);
136+
137+ env = std::unique_ptr<char *[]>(new char *[env_vars_.size () + 1 ]);
138+ options_.env = env.get ();
139+ for (size_t i = 0 ; i < env_vars_.size (); i++) {
133140 options_.env [i] = const_cast <char *>(env_vars_[i].c_str ());
134141 }
135- options_.env [env_count ] = nullptr ;
142+ options_.env [env_vars_. size () ] = nullptr ;
136143}
137144
138145// EscapeShell escapes a string to be used as a command line argument.
@@ -276,7 +283,8 @@ void RunTask(std::shared_ptr<InitializationResultImpl> result,
276283 return ;
277284 }
278285
279- auto runner = ProcessRunner (result, command, positional_args);
286+ auto runner =
287+ ProcessRunner (result, std::string (command_id), command, positional_args);
280288 runner.Run ();
281289}
282290
0 commit comments