Skip to content

Commit 4c1401e

Browse files
authored
fix(builtin): detect yarn 2+ berry and adjust CLI args (#3195)
Fixes #3071 Fixes #1599
1 parent 89fbec3 commit 4c1401e

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

docs/Built-ins.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,9 @@ check if yarn is being run by the `yarn_install` repository rule.
14301430

14311431
(*List of strings*): Arguments passed to yarn install.
14321432

1433-
See yarn CLI docs https://yarnpkg.com/en/docs/cli/install for complete list of supported arguments.
1433+
See yarn CLI docs for complete list of supported arguments.
1434+
Yarn 1: https://yarnpkg.com/en/docs/cli/install
1435+
Yarn 2+ (Berry): https://yarnpkg.com/cli/install
14341436

14351437
Defaults to `[]`
14361438

@@ -1517,7 +1519,9 @@ Defaults to `False`
15171519

15181520
<h4 id="yarn_install-frozen_lockfile">frozen_lockfile</h4>
15191521

1520-
(*Boolean*): Use the `--frozen-lockfile` flag for yarn.
1522+
(*Boolean*): Use the `--frozen-lockfile` flag for yarn 1
1523+
1524+
Users of Yarn 2+ (Berry) should just pass `--immutable` to the `args` attribute.
15211525

15221526
Don't generate a `yarn.lock` lockfile and fail if an update is needed.
15231527

@@ -1841,9 +1845,11 @@ have bugs.
18411845
Disabling this attribute causes every run of yarn to have a unique
18421846
cache_directory.
18431847

1844-
If True, this rule will pass `--mutex network` to yarn to ensure that
1848+
If True and using Yarn 1, this rule will pass `--mutex network` to yarn to ensure that
18451849
the global cache can be shared by parallelized yarn_install rules.
18461850

1851+
The True value has no effect on Yarn 2+ (Berry).
1852+
18471853
If False, this rule will pass `--cache-folder /path/to/external/repository/__yarn_cache`
18481854
to yarn so that the local cache is contained within the external repository.
18491855

internal/npm_install/npm_install.bzl

+30-5
Original file line numberDiff line numberDiff line change
@@ -813,25 +813,43 @@ def _yarn_install_impl(repository_ctx):
813813
is_windows_host = is_windows_os(repository_ctx)
814814
node = repository_ctx.path(get_node_label(repository_ctx))
815815
yarn = get_yarn_label(repository_ctx)
816+
result = repository_ctx.execute(
817+
[repository_ctx.path(yarn), "--version"],
818+
timeout = repository_ctx.attr.timeout,
819+
quiet = repository_ctx.attr.quiet,
820+
)
821+
if result.return_code:
822+
fail("yarn --version failed: %s (%s)" % (result.stdout, result.stderr))
823+
if result.stdout.startswith("1."):
824+
yarn_version = "classic"
825+
else:
826+
yarn_version = "berry"
816827

817828
yarn_args = []
818829

819830
# Set frozen lockfile as default install to install the exact version from the yarn.lock
820831
# file. To perform an yarn install use the vendord yarn binary with:
821832
# `bazel run @nodejs//:yarn install` or `bazel run @nodejs//:yarn install -- -D <dep-name>`
822833
if repository_ctx.attr.frozen_lockfile:
823-
yarn_args.append("--frozen-lockfile")
834+
if yarn_version == "classic":
835+
yarn_args.append("--frozen-lockfile")
836+
else:
837+
fail("--frozen-lockfile should not be used with yarn 2+. Just pass arguments like --immutable.")
824838

825839
if not repository_ctx.attr.use_global_yarn_cache:
826840
yarn_args.extend(["--cache-folder", str(repository_ctx.path("_yarn_cache"))])
827-
else:
841+
elif yarn_version == "classic":
828842
# Multiple yarn rules cannot run simultaneously using a shared cache.
829843
# See https://github.com/yarnpkg/yarn/issues/683
830844
# The --mutex option ensures only one yarn runs at a time, see
831845
# https://yarnpkg.com/en/docs/cli#toc-concurrency-and-mutex
832846
# The shared cache is not necessarily hermetic, but we need to cache downloaded
833847
# artifacts somewhere, so we rely on yarn to be correct.
834848
yarn_args.extend(["--mutex", "network"])
849+
else:
850+
# Can't tell from documentation if Yarn Berry has any replacement for the --mutex
851+
# flag. We'll have to assume it's safe to run concurrently.
852+
pass
835853
yarn_args.extend(repository_ctx.attr.args)
836854

837855
# Run the package manager in the package.json folder
@@ -936,12 +954,17 @@ yarn_install = repository_rule(
936954
"args": attr.string_list(
937955
doc = """Arguments passed to yarn install.
938956
939-
See yarn CLI docs https://yarnpkg.com/en/docs/cli/install for complete list of supported arguments.""",
957+
See yarn CLI docs for complete list of supported arguments.
958+
Yarn 1: https://yarnpkg.com/en/docs/cli/install
959+
Yarn 2+ (Berry): https://yarnpkg.com/cli/install
960+
""",
940961
default = [],
941962
),
942963
"frozen_lockfile": attr.bool(
943964
default = True,
944-
doc = """Use the `--frozen-lockfile` flag for yarn.
965+
doc = """Use the `--frozen-lockfile` flag for yarn 1
966+
967+
Users of Yarn 2+ (Berry) should just pass `--immutable` to the `args` attribute.
945968
946969
Don't generate a `yarn.lock` lockfile and fail if an update is needed.
947970
@@ -964,9 +987,11 @@ have bugs.
964987
Disabling this attribute causes every run of yarn to have a unique
965988
cache_directory.
966989
967-
If True, this rule will pass `--mutex network` to yarn to ensure that
990+
If True and using Yarn 1, this rule will pass `--mutex network` to yarn to ensure that
968991
the global cache can be shared by parallelized yarn_install rules.
969992
993+
The True value has no effect on Yarn 2+ (Berry).
994+
970995
If False, this rule will pass `--cache-folder /path/to/external/repository/__yarn_cache`
971996
to yarn so that the local cache is contained within the external repository.
972997
""",

0 commit comments

Comments
 (0)