@@ -44,6 +44,29 @@ repository so all files that the package manager depends on must be listed.
4444 doc = """Environment variables to set before calling the package manager.""" ,
4545 default = {},
4646 ),
47+ "generate_local_modules_build_files" : attr .bool (
48+ default = True ,
49+ doc = """Enables the BUILD files auto generation for local modules installed with `file:` (npm) or `link:` (yarn)
50+
51+ When using a monorepo it's common to have modules that we want to use locally and
52+ publish to an external package repository. This can be achieved using a `js_library` rule
53+ with a `package_name` attribute defined inside the local package `BUILD` file. However,
54+ if the project relies on the local package dependency with `file:` (npm) or `link:` (yarn) to be used outside Bazel, this
55+ could introduce a race condition with both `npm_install` or `yarn_install` rules.
56+
57+ In order to overcome it, a link could be created to the package `BUILD` file from the
58+ npm external Bazel repository (so we can use a local BUILD file instead of an auto generated one),
59+ which require us to set `generate_local_modules_build_files = False` and complete a last step which is writing the
60+ expected targets on that same `BUILD` file to be later used both by `npm_install` or `yarn_install`
61+ rules, which are: `<package_name__files>`, `<package_name__nested_node_modules>`,
62+ `<package_name__contents>`, `<package_name__typings>` and the last one just `<package_name>`. If you doubt what those targets
63+ should look like, check the generated `BUILD` file for a given node module.
64+
65+ When true, the rule will follow the default behaviour of auto generating BUILD files for each `node_module` at install time.
66+
67+ When False, the rule will not auto generate BUILD files for `node_modules` that are installed as symlinks for local modules.
68+ """ ,
69+ ),
4770 "included_files" : attr .string_list (
4871 doc = """List of file extensions to be included in the npm package targets.
4972
@@ -123,7 +146,7 @@ data attribute.
123146 ),
124147})
125148
126- def _create_build_files (repository_ctx , rule_type , node , lock_file ):
149+ def _create_build_files (repository_ctx , rule_type , node , lock_file , generate_local_modules_build_files ):
127150 repository_ctx .report_progress ("Processing node_modules: installing Bazel packages and generating BUILD files" )
128151 if repository_ctx .attr .manual_build_file_contents :
129152 repository_ctx .file ("manual_build_file_contents" , repository_ctx .attr .manual_build_file_contents )
@@ -137,6 +160,7 @@ def _create_build_files(repository_ctx, rule_type, node, lock_file):
137160 _workspace_root_prefix (repository_ctx ),
138161 str (repository_ctx .attr .strict_visibility ),
139162 "," .join (repository_ctx .attr .included_files ),
163+ str (generate_local_modules_build_files ),
140164 native .bazel_version ,
141165 repository_ctx .attr .package_path ,
142166 # double the default timeout in case of many packages, see #2231
@@ -344,7 +368,7 @@ cd /D "{root}" && "{npm}" {npm_args}
344368
345369 _symlink_node_modules (repository_ctx )
346370
347- _create_build_files (repository_ctx , "npm_install" , node , repository_ctx .attr .package_lock_json )
371+ _create_build_files (repository_ctx , "npm_install" , node , repository_ctx .attr .package_lock_json , repository_ctx . attr . generate_local_modules_build_files )
348372
349373npm_install = repository_rule (
350374 attrs = dict (COMMON_ATTRIBUTES , ** {
@@ -374,26 +398,7 @@ See npm CLI docs https://docs.npmjs.com/cli/install.html for complete list of su
374398
375399This rule will set the environment variable `BAZEL_NPM_INSTALL` to '1' (unless it
376400set to another value in the environment attribute). Scripts may use to this to
377- check if yarn is being run by the `npm_install` repository rule.
378-
379-
380- **LOCAL MODULES WITH THE NEED TO BE USED BOTH INSIDE AND OUTSIDE BAZEL**
381-
382- When using a monorepo it's common to have modules that we want to use locally and
383- publish to an external package repository. This can be achieved using a `js_library` rule
384- with a `package_name` attribute defined inside the local package `BUILD` file. However,
385- if the project relies on the local package dependency with `file:`, this could introduce a
386- race condition with the `npm_install` rule.
387-
388- In order to overcome it, a link will be created to the package `BUILD` file from the
389- npm external Bazel repository, which require us to complete a last step which is writing
390- the expected targets on that same `BUILD` file to be later used by the `npm_install`
391- rule, which are: `<package_name__files>`, `<package_name__nested_node_modules>`,
392- `<package_name__contents>`, `<package_name__typings>` and the last
393- one just `<package_name>`.
394-
395- If you doubt what those targets should look like, check the
396- generated `BUILD` file for a given node module.""" ,
401+ check if yarn is being run by the `npm_install` repository rule.""" ,
397402 implementation = _npm_install_impl ,
398403)
399404
@@ -499,7 +504,7 @@ cd /D "{root}" && "{yarn}" {yarn_args}
499504
500505 _symlink_node_modules (repository_ctx )
501506
502- _create_build_files (repository_ctx , "yarn_install" , node , repository_ctx .attr .yarn_lock )
507+ _create_build_files (repository_ctx , "yarn_install" , node , repository_ctx .attr .yarn_lock , repository_ctx . attr . generate_local_modules_build_files )
503508
504509yarn_install = repository_rule (
505510 attrs = dict (COMMON_ATTRIBUTES , ** {
@@ -550,25 +555,6 @@ to yarn so that the local cache is contained within the external repository.
550555
551556This rule will set the environment variable `BAZEL_YARN_INSTALL` to '1' (unless it
552557set to another value in the environment attribute). Scripts may use to this to
553- check if yarn is being run by the `yarn_install` repository rule.
554-
555-
556- **LOCAL MODULES WITH THE NEED TO BE USED BOTH INSIDE AND OUTSIDE BAZEL**
557-
558- When using a monorepo it's common to have modules that we want to use locally and
559- publish to an external package repository. This can be achieved using a `js_library` rule
560- with a `package_name` attribute defined inside the local package `BUILD` file. However,
561- if the project relies on the local package dependency with `link:`, this could introduce a
562- race condition with the `yarn_install` rule.
563-
564- In order to overcome it, a link will be created to the package `BUILD` file from the
565- npm external Bazel repository, which require us to complete a last step which is writing
566- the expected targets on that same `BUILD` file to be later used by the `yarn_install`
567- rule, which are: `<package_name__files>`, `<package_name__nested_node_modules>`,
568- `<package_name__contents>`, `<package_name__typings>` and the last
569- one just `<package_name>`.
570-
571- If you doubt what those targets should look like, check the
572- generated `BUILD` file for a given node module.""" ,
558+ check if yarn is being run by the `yarn_install` repository rule.""" ,
573559 implementation = _yarn_install_impl ,
574560)
0 commit comments