@@ -168,6 +168,37 @@ fine grained npm dependencies.
168168In most cases, this should be the directory of the package.json file so that the linker links the node_modules
169169in the same location they are found in the source tree. In a future release, this will default to the package.json
170170directory. This is planned for 4.0: https://github.com/bazelbuild/rules_nodejs/issues/2451""" ,
171+ ),
172+ "patch_args" : attr .string_list (
173+ default = ["-p0" ],
174+ doc =
175+ "The arguments given to the patch tool. Defaults to -p0, " +
176+ "however -p1 will usually be needed for patches generated by " +
177+ "git. If multiple -p arguments are specified, the last one will take effect." +
178+ "If arguments other than -p are specified, Bazel will fall back to use patch " +
179+ "command line tool instead of the Bazel-native patch implementation. When falling " +
180+ "back to patch command line tool and patch_tool attribute is not specified, " +
181+ "`patch` will be used." ,
182+ ),
183+ "patch_tool" : attr .string (
184+ default = "" ,
185+ doc = "The patch(1) utility to use. If this is specified, Bazel will use the specifed " +
186+ "patch tool instead of the Bazel-native patch implementation." ,
187+ ),
188+ "post_install_patches" : attr .label_list (
189+ doc = """Patch files to apply after running package manager.
190+
191+ This can be used to make changes to installed packages after the package manager runs.
192+
193+ File paths in patches should be relative to workspace root.""" ,
194+ ),
195+ "pre_install_patches" : attr .label_list (
196+ doc = """Patch files to apply before running package manager.
197+
198+ This can be used to make changes to package.json or other data files passed in before running the
199+ package manager.
200+
201+ File paths in patches should be relative to workspace root.""" ,
171202 ),
172203 "quiet" : attr .bool (
173204 default = True ,
@@ -205,6 +236,34 @@ data attribute.
205236 ),
206237})
207238
239+ def _apply_patches (repository_ctx , patches ):
240+ bash_exe = repository_ctx .os .environ ["BAZEL_SH" ] if "BAZEL_SH" in repository_ctx .os .environ else "bash"
241+
242+ patch_tool = repository_ctx .attr .patch_tool
243+ if not patch_tool :
244+ patch_tool = "patch"
245+ patch_args = repository_ctx .attr .patch_args
246+
247+ for patchfile in patches :
248+ command = "{patchtool} {patch_args} < {patchfile}" .format (
249+ patchtool = patch_tool ,
250+ patchfile = repository_ctx .path (patchfile ),
251+ patch_args = " " .join ([
252+ "'%s'" % arg
253+ for arg in patch_args
254+ ]),
255+ )
256+ st = repository_ctx .execute (
257+ [bash_exe , "-c" , command ],
258+ quiet = repository_ctx .attr .quiet ,
259+ # Working directory is _ which is where all files are copied to and
260+ # where the install is run; patches should be relative to workspace root.
261+ working_directory = "_" ,
262+ )
263+ if st .return_code :
264+ fail ("Error applying patch %s:\n %s%s" %
265+ (str (patchfile ), st .stderr , st .stdout ))
266+
208267def _create_build_files (repository_ctx , rule_type , node , lock_file , generate_local_modules_build_files ):
209268 repository_ctx .report_progress ("Processing node_modules: installing Bazel packages and generating BUILD files" )
210269 if repository_ctx .attr .manual_build_file_contents :
@@ -399,6 +458,7 @@ cd /D "{root}" && "{npm}" {npm_args}
399458 _copy_data_dependencies (repository_ctx )
400459 _add_scripts (repository_ctx )
401460 _add_node_repositories_info_deps (repository_ctx )
461+ _apply_patches (repository_ctx , repository_ctx .attr .pre_install_patches )
402462
403463 result = repository_ctx .execute (
404464 [node , "pre_process_package_json.js" , repository_ctx .path (repository_ctx .attr .package_json ), "npm" ],
@@ -439,6 +499,7 @@ cd /D "{root}" && "{npm}" {npm_args}
439499 fail ("remove_npm_absolute_paths failed: %s (%s)" % (result .stdout , result .stderr ))
440500
441501 _symlink_node_modules (repository_ctx )
502+ _apply_patches (repository_ctx , repository_ctx .attr .post_install_patches )
442503
443504 _create_build_files (repository_ctx , "npm_install" , node , repository_ctx .attr .package_lock_json , repository_ctx .attr .generate_local_modules_build_files )
444505
@@ -550,6 +611,7 @@ cd /D "{root}" && "{yarn}" {yarn_args}
550611 _copy_data_dependencies (repository_ctx )
551612 _add_scripts (repository_ctx )
552613 _add_node_repositories_info_deps (repository_ctx )
614+ _apply_patches (repository_ctx , repository_ctx .attr .pre_install_patches )
553615
554616 result = repository_ctx .execute (
555617 [node , "pre_process_package_json.js" , repository_ctx .path (repository_ctx .attr .package_json ), "yarn" ],
@@ -575,6 +637,7 @@ cd /D "{root}" && "{yarn}" {yarn_args}
575637 fail ("yarn_install failed: %s (%s)" % (result .stdout , result .stderr ))
576638
577639 _symlink_node_modules (repository_ctx )
640+ _apply_patches (repository_ctx , repository_ctx .attr .post_install_patches )
578641
579642 _create_build_files (repository_ctx , "yarn_install" , node , repository_ctx .attr .yarn_lock , repository_ctx .attr .generate_local_modules_build_files )
580643
0 commit comments