Skip to content

Commit

Permalink
refactor: set symlink_node_modules default to False in yarn_install a…
Browse files Browse the repository at this point in the history
…nd npm_install
  • Loading branch information
gregmagolan committed Jan 11, 2022
1 parent fa84c43 commit d35ce7b
Show file tree
Hide file tree
Showing 44 changed files with 159 additions and 205 deletions.
2 changes: 0 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ workspace(
"@cypress_deps": ["packages/cypress/test/node_modules"],
"@internal_npm_install_test_patches_npm_symlinked": ["internal/npm_install/test/patches_npm_symlinked/node_modules"],
"@internal_npm_install_test_patches_yarn_symlinked": ["internal/npm_install/test/patches_yarn_symlinked/node_modules"],
"@internal_test_multi_linker_sub_deps": ["internal/linker/test/multi_linker/sub/node_modules"],
"@npm": ["node_modules"],
"@npm_node_patches": ["packages/node-patches/node_modules"],
},
)

Expand Down
36 changes: 18 additions & 18 deletions docs/Built-ins.md
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,14 @@ Defaults to `True`

(*Boolean*): Turn symlinking of node_modules on

When False, the package manager will run in the external repository
created by this rule.
This requires that any files required for it to run should be listed in the
`data` attribute. These files would include things like patch files that are
read by a postinstall lifecycle hook such as the `patch-package` package uses.
`package.json` and the lock file are already specified in dedicated attributes
of this rule and do not need to be included in the `data`.

When True, we run the package manager (npm or yarn) with the working directory
set in your source tree, in the folder containing the package.json file.
The resulting `node_modules` folder in the source tree will be symlinked to the
Expand Down Expand Up @@ -930,15 +938,7 @@ Using managed_directories will mean that
2. if the `node_modules` folder is deleted from the source tree, Bazel will re-run the
repository rule that creates it again on the next run.

When False, the package manager will run in the external repository
created by this rule.
This requires that any files required for it to run should be listed in the
`data` attribute. These files would include things like patch files that are
read by a postinstall lifecycle hook such as the `patch-package` package uses.
`package.json` and the lock file are already specified in dedicated attributes
of this rule and do not need to be included in the `data`.

Defaults to `True`
Defaults to `False`

<h4 id="npm_install-timeout">timeout</h4>

Expand Down Expand Up @@ -1586,6 +1586,14 @@ Defaults to `True`

(*Boolean*): Turn symlinking of node_modules on

When False, the package manager will run in the external repository
created by this rule.
This requires that any files required for it to run should be listed in the
`data` attribute. These files would include things like patch files that are
read by a postinstall lifecycle hook such as the `patch-package` package uses.
`package.json` and the lock file are already specified in dedicated attributes
of this rule and do not need to be included in the `data`.

When True, we run the package manager (npm or yarn) with the working directory
set in your source tree, in the folder containing the package.json file.
The resulting `node_modules` folder in the source tree will be symlinked to the
Expand Down Expand Up @@ -1617,15 +1625,7 @@ Using managed_directories will mean that
2. if the `node_modules` folder is deleted from the source tree, Bazel will re-run the
repository rule that creates it again on the next run.

When False, the package manager will run in the external repository
created by this rule.
This requires that any files required for it to run should be listed in the
`data` attribute. These files would include things like patch files that are
read by a postinstall lifecycle hook such as the `patch-package` package uses.
`package.json` and the lock file are already specified in dedicated attributes
of this rule and do not need to be included in the `data`.

Defaults to `True`
Defaults to `False`

<h4 id="yarn_install-timeout">timeout</h4>

Expand Down
49 changes: 36 additions & 13 deletions docs/dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ This approach also allows you to use the generated fine-grained npm package depe
which can significantly reduce the number of inputs to actions, making Bazel sand-boxing and
remote-execution faster if there are a large number of files under `node_modules`.

> Note that as of Bazel 0.26, and with the recommended `managed_directories` attribute on the `workspace` rule in `/WORKSPACE`,
> the Bazel-managed `node_modules` directory is placed in your workspace root in the standard location used by npm or yarn.
## Using Bazel-managed dependencies

To have Bazel manage its own copy of `node_modules`, which is useful to avoid
Expand Down Expand Up @@ -65,8 +62,28 @@ npm_install(
> If you don't need to pass any arguments to `node_repositories`,
you can skip calling that function. `yarn_install` and `npm_install` will do it by default.

You should now add the `@npm` workspace to the `managed_directories` option in the `workspace` rule at the top of the file. This tells Bazel that the `node_modules` directory is special and is managed by the package manager.
Add the `workspace` rule if it isn't already in your `/WORKSPACE` file.
### symlink_node_modules and managed_directories

Set `symlink_node_modules` to `True` to configure `npm_install` and/or
`yarn_install` to install `node_modules` inside the user workspace and have
Bazel use the `node_modules` folder in the user workspace for the build via a
symlink to the user's `node_modules` in the external repository it creates.

```python
load("@build_bazel_rules_nodejs//:index.bzl", "npm_install")

npm_install(
name = "npm",
package_json = "//:package.json",
package_lock_json = "//:package-lock.json",
symlink_node_modules = True,
)
```

You should now add the `@npm` workspace to the `managed_directories` option in
the `workspace` rule at the top of the file. This tells Bazel that the
`node_modules` directory is special and is managed by the package manager. Add
the `workspace` rule if it isn't already in your `/WORKSPACE` file.

```python
workspace(
Expand All @@ -75,7 +92,19 @@ workspace(
)
```

As of Bazel 0.26 this feature is still experimental, so also add this line to the `.bazelrc` to opt-in:
As of rules_nodejs 5.0, `symlink_node_modules` defaults to `False` and using
`managed_directories` is not recommended. We've found that the benefits of using
`symlink_node_modules`, which allows Bazel to use a `node_modules` directory
that is in the user workspace, do not outweigh the downsides of the repository
rule not defining all of their inputs and of having to re-run the repository rule
if the user's `node_modules` folder is deleted. On persistent CI machines, that
will delete the `node_modules` folder when cleaning the clone between jobs, the
repository rule will run for every job when `symlink_node_modules` is enabled.
With `symlink_node_modules` disabled, the repository rule will only re-run if
its inputs change between jobs.

NB: On older versions of Bazel you may have to add the following flag to your
`.bazelrc` to enable managed directories.

```
common --experimental_allow_incremental_repository_updates
Expand Down Expand Up @@ -131,13 +160,7 @@ and npm deps, `yarn_install` (or `npm_install`) can be called separately for
each.

```python
workspace(
name = "my_wksp",
managed_directories = {
"@app1_npm": ["app1/node_modules"],
"@app2_npm": ["app2/node_modules"],
},
)
workspace(name = "my_wksp")

yarn_install(
name = "app1_npm",
Expand Down
21 changes: 21 additions & 0 deletions e2e/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ e2e_integration_test(
npm_packages = {
"//packages/jasmine:npm_package": "@bazel/jasmine",
},
# TODO: figure out why this fails on Windows since setting
# symlink_node_modules to False in the test WORKSPACE
tags = ["no-bazelci-windows"],
)

e2e_integration_test(
Expand All @@ -43,21 +46,33 @@ e2e_integration_test(

e2e_integration_test(
name = "e2e_fine_grained_symlinks",
# TODO: figure out why this fails on Windows since setting
# symlink_node_modules to False in the test WORKSPACE
tags = ["no-bazelci-windows"],
)

e2e_integration_test(
name = "e2e_jasmine",
npm_packages = {
"//packages/jasmine:npm_package": "@bazel/jasmine",
},
# TODO: figure out why this fails on Windows since setting
# symlink_node_modules to False in the test WORKSPACE
tags = ["no-bazelci-windows"],
)

e2e_integration_test(
name = "e2e_node_loader_no_preserve_symlinks",
# TODO: figure out why this fails on Windows since setting
# symlink_node_modules to False in the test WORKSPACE
tags = ["no-bazelci-windows"],
)

e2e_integration_test(
name = "e2e_node_loader_preserve_symlinks",
# TODO: figure out why this fails on Windows since setting
# symlink_node_modules to False in the test WORKSPACE
tags = ["no-bazelci-windows"],
)

e2e_integration_test(
Expand Down Expand Up @@ -93,10 +108,16 @@ e2e_integration_test(

e2e_integration_test(
name = "e2e_symlinked_node_modules_npm",
# TODO: figure out why this fails on Windows since setting
# symlink_node_modules to False in the test WORKSPACE
tags = ["no-bazelci-windows"],
)

e2e_integration_test(
name = "e2e_symlinked_node_modules_yarn",
# TODO: figure out why this fails on Windows since setting
# symlink_node_modules to False in the test WORKSPACE
tags = ["no-bazelci-windows"],
)

# terser rules are tested in the e2e_webapp
Expand Down
5 changes: 1 addition & 4 deletions e2e/bazel_managed_deps/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

workspace(
name = "e2e_bazel_managed_deps",
managed_directories = {"@npm": ["node_modules"]},
)
workspace(name = "e2e_bazel_managed_deps")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

Expand Down
5 changes: 1 addition & 4 deletions e2e/concatjs_devserver/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

workspace(
name = "e2e_concatjs_devserver",
managed_directories = {"@npm": ["node_modules"]},
)
workspace(name = "e2e_concatjs_devserver")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

Expand Down
5 changes: 1 addition & 4 deletions e2e/coverage/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

workspace(
name = "e2e_coverage",
managed_directories = {"@npm": ["node_modules"]},
)
workspace(name = "e2e_coverage")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

Expand Down
5 changes: 1 addition & 4 deletions e2e/fine_grained_symlinks/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
workspace(
name = "e2e_fine_grained_symlinks",
managed_directories = {"@npm": ["node_modules"]},
)
workspace(name = "e2e_fine_grained_symlinks")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

Expand Down
5 changes: 1 addition & 4 deletions e2e/jasmine/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

workspace(
name = "e2e_jasmine",
managed_directories = {"@npm": ["node_modules"]},
)
workspace(name = "e2e_jasmine")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

Expand Down
2 changes: 2 additions & 0 deletions e2e/node_loader_no_preserve_symlinks/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")
yarn_install(
name = "npm",
package_json = "//:package.json",
# TODO: figure out why this test fails with symlink_node_modules disabled
symlink_node_modules = True,
yarn_lock = "//:yarn.lock",
)
7 changes: 1 addition & 6 deletions e2e/node_loader_preserve_symlinks/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
workspace(
name = "e2e_node_loader_preserve_symlinks",
managed_directories = {
"@npm": ["node_modules"],
},
)
workspace(name = "e2e_node_loader_preserve_symlinks")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

Expand Down
5 changes: 1 addition & 4 deletions e2e/nodejs_image/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

workspace(
name = "e2e_nodejs_image",
managed_directories = {"@npm": ["node_modules"]},
)
workspace(name = "e2e_nodejs_image")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

Expand Down
5 changes: 1 addition & 4 deletions e2e/nodejs_repository/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
workspace(
name = "nodejs_repository",
managed_directories = {"@npm": ["node_modules"]},
)
workspace(name = "nodejs_repository")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

Expand Down
4 changes: 0 additions & 4 deletions e2e/packages/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ npm_install(
data = ["//:postinstall.js"],
package_json = "//:npm1/package.json",
package_lock_json = "//:npm1/package-lock.json",
symlink_node_modules = False,
)

npm_install(
Expand All @@ -34,15 +33,13 @@ npm_install(
data = ["//:postinstall.js"],
package_json = "//:npm2/package.json",
package_lock_json = "//:npm2/package-lock.json",
symlink_node_modules = False,
)

yarn_install(
name = "e2e_packages_yarn_install",
args = ["--prod"],
data = ["//:postinstall.js"],
package_json = "//:yarn1/package.json",
symlink_node_modules = False,
yarn_lock = "//:yarn1/yarn.lock",
)

Expand All @@ -51,6 +48,5 @@ yarn_install(
args = ["--prod"],
data = ["//:postinstall.js"],
package_json = "//:yarn2/package.json",
symlink_node_modules = False,
yarn_lock = "//:yarn2/yarn.lock",
)
5 changes: 1 addition & 4 deletions e2e/symlinked_node_modules_npm/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
workspace(
name = "e2e_symlinked_node_modules_yarn",
managed_directories = {"@npm": ["node_modules"]},
)
workspace(name = "e2e_symlinked_node_modules_yarn")

# There are some assertions that can only by made by running tests from within
# this nested repository folder. These assertions are run in CI so we use
Expand Down
5 changes: 1 addition & 4 deletions e2e/symlinked_node_modules_yarn/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
workspace(
name = "e2e_symlinked_node_modules_yarn",
managed_directories = {"@npm": ["node_modules"]},
)
workspace(name = "e2e_symlinked_node_modules_yarn")

# There are some assertions that can only by made by running tests from within
# this nested repository folder. These assertions are run in CI so we use
Expand Down
5 changes: 1 addition & 4 deletions e2e/typescript/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

workspace(
name = "e2e_typescript",
managed_directories = {"@npm": ["node_modules"]},
)
workspace(name = "e2e_typescript")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

Expand Down
5 changes: 1 addition & 4 deletions e2e/webapp/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

workspace(
name = "e2e_webapp",
managed_directories = {"@npm": ["node_modules"]},
)
workspace(name = "e2e_webapp")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

Expand Down
Loading

0 comments on commit d35ce7b

Please sign in to comment.