@@ -119,11 +119,12 @@ def _write_loader_script(ctx):
119119 is_executable = True ,
120120 )
121121
122- def _short_path_to_manifest_path (ctx , short_path ):
123- if short_path .startswith ("../" ):
124- return short_path [3 :]
122+ # Avoid writing non-normalized paths (workspace/../other_workspace/path)
123+ def _to_manifest_path (ctx , file ):
124+ if file .short_path .startswith ("../" ):
125+ return file .short_path [3 :]
125126 else :
126- return ctx .workspace_name + "/" + short_path
127+ return ctx .workspace_name + "/" + file . short_path
127128
128129def _nodejs_binary_impl (ctx ):
129130 node_modules = depset (ctx .files .node_modules )
@@ -147,14 +148,8 @@ def _nodejs_binary_impl(ctx):
147148
148149 _write_loader_script (ctx )
149150
150- # Avoid writing non-normalized paths (workspace/../other_workspace/path)
151- if ctx .outputs .loader .short_path .startswith ("../" ):
152- script_path = ctx .outputs .loader .short_path [len ("../" ):]
153- else :
154- script_path = "/" .join ([
155- ctx .workspace_name ,
156- ctx .outputs .loader .short_path ,
157- ])
151+ script_path = _to_manifest_path (ctx , ctx .outputs .loader )
152+
158153 env_vars = "export BAZEL_TARGET=%s\n " % ctx .label
159154 for k in ctx .attr .configuration_env_vars + ctx .attr .default_env_vars :
160155 if k in ctx .var .keys ():
@@ -165,61 +160,58 @@ def _nodejs_binary_impl(ctx):
165160 expected_exit_code = ctx .attr .expected_exit_code
166161
167162 node_tool_info = ctx .toolchains ["@build_bazel_rules_nodejs//toolchains/node:toolchain_type" ].nodeinfo
168- node_tool_files = []
169- if node_tool_info .target_tool_path == "" and not node_tool_info .target_tool :
163+
164+ # Make a copy so we don't try to mutate a frozen object
165+ node_tool_files = node_tool_info .tool_files [:]
166+ if node_tool_info .target_tool_path == "" :
170167 # If tool_path is empty and tool_target is None then there is no local
171168 # node tool, we will just print a nice error message if the user
172169 # attempts to do bazel run
173170 fail ("The node toolchain was not properly configured so %s cannot be executed. Make sure that target_tool_path or target_tool is set." % ctx .attr .name )
174- else :
175- node_tool = node_tool_info .target_tool_path
176- if node_tool_info .target_tool :
177- node_tool_files += node_tool_info .target_tool .files .to_list ()
178- node_tool = _short_path_to_manifest_path (ctx , node_tool_files [0 ].short_path )
179-
180- if not ctx .outputs .templated_args_file :
181- templated_args = ctx .attr .templated_args
182- else :
183- # Distribute the templated_args between the params file and the node options
184- params = []
185- templated_args = []
186- for a in ctx .attr .templated_args :
187- if a .startswith ("--node_options=" ):
188- templated_args .append (a )
189- else :
190- params .append (a )
191-
192- # Put the params into the params file
193- ctx .actions .write (
194- output = ctx .outputs .templated_args_file ,
195- content = "\n " .join ([expand_location_into_runfiles (ctx , p ) for p in params ]),
196- is_executable = False ,
197- )
198-
199- # after the node_options args, pass the params file arg
200- templated_args .append (ctx .outputs .templated_args_file .short_path )
201-
202- # also be sure to include the params file in the program inputs
203- node_tool_files += [ctx .outputs .templated_args_file ]
204171
205- substitutions = {
206- "TEMPLATED_args" : " " .join ([
207- expand_location_into_runfiles (ctx , a )
208- for a in templated_args
209- ]),
210- "TEMPLATED_env_vars" : env_vars ,
211- "TEMPLATED_expected_exit_code" : str (expected_exit_code ),
212- "TEMPLATED_node" : node_tool ,
213- "TEMPLATED_repository_args" : _short_path_to_manifest_path (ctx , ctx .file ._repository_args .short_path ),
214- "TEMPLATED_script_path" : script_path ,
215- }
216- ctx .actions .expand_template (
217- template = ctx .file ._launcher_template ,
218- output = ctx .outputs .script ,
219- substitutions = substitutions ,
220- is_executable = True ,
172+ if not ctx .outputs .templated_args_file :
173+ templated_args = ctx .attr .templated_args
174+ else :
175+ # Distribute the templated_args between the params file and the node options
176+ params = []
177+ templated_args = []
178+ for a in ctx .attr .templated_args :
179+ if a .startswith ("--node_options=" ):
180+ templated_args .append (a )
181+ else :
182+ params .append (a )
183+
184+ # Put the params into the params file
185+ ctx .actions .write (
186+ output = ctx .outputs .templated_args_file ,
187+ content = "\n " .join ([expand_location_into_runfiles (ctx , p ) for p in params ]),
188+ is_executable = False ,
221189 )
222190
191+ # after the node_options args, pass the params file arg
192+ templated_args .append (ctx .outputs .templated_args_file .short_path )
193+
194+ # also be sure to include the params file in the program inputs
195+ node_tool_files .append (ctx .outputs .templated_args_file )
196+
197+ substitutions = {
198+ "TEMPLATED_args" : " " .join ([
199+ expand_location_into_runfiles (ctx , a )
200+ for a in templated_args
201+ ]),
202+ "TEMPLATED_env_vars" : env_vars ,
203+ "TEMPLATED_expected_exit_code" : str (expected_exit_code ),
204+ "TEMPLATED_node" : node_tool_info .target_tool_path ,
205+ "TEMPLATED_repository_args" : _to_manifest_path (ctx , ctx .file ._repository_args ),
206+ "TEMPLATED_script_path" : script_path ,
207+ }
208+ ctx .actions .expand_template (
209+ template = ctx .file ._launcher_template ,
210+ output = ctx .outputs .script ,
211+ substitutions = substitutions ,
212+ is_executable = True ,
213+ )
214+
223215 runfiles = depset (node_tool_files + [ctx .outputs .loader , ctx .file ._repository_args ], transitive = [sources , node_modules ])
224216
225217 if is_windows (ctx ):
0 commit comments