Skip to content

Commit d7083ac

Browse files
authored
fix(builtin): set cwd before running yarn for yarn_install (#1569)
Also fix the same for @nodejs//:yarn & @nodejs//:yarn_node_repositories targets. This allows yarn to pickup the `yarn-path` attribute of `.yarnrc` which is only checked inside `${process.cwd()}/.yarnrc` when yarn runs. Previously we running yarn with `--cwd` which works for all other `.yarnrc` options except `yarn-path`.
1 parent c2061e3 commit d7083ac

2 files changed

Lines changed: 50 additions & 26 deletions

File tree

internal/node/node_repositories.bzl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -505,12 +505,12 @@ SET SCRIPT_DIR=%~dp0
505505

506506
# Npm entry point for node_repositories
507507
repository_ctx.file("bin/npm_node_repositories.cmd", content = """@echo off
508+
SET SCRIPT_DIR=%~dp0
508509
""" + "".join([
509510
"""
510-
SET SCRIPT_DIR=%~dp0
511511
echo Running npm %* in {root}
512512
cd "{root}"
513-
call "%SCRIPT_DIR%\\{node}" "%SCRIPT_DIR%\\{script}" --scripts-prepend-node-path=false %*
513+
CALL "%SCRIPT_DIR%\\{node}" "%SCRIPT_DIR%\\{script}" --scripts-prepend-node-path=false %*
514514
if %errorlevel% neq 0 exit /b %errorlevel%
515515
""".format(
516516
root = repository_ctx.path(package_json).dirname,
@@ -557,8 +557,8 @@ set -e
557557
# Executes the given yarn command over each of the package.json folders provided in node_repositories.
558558
""" + GET_SCRIPT_DIR + "".join([
559559
"""
560-
echo Running yarn --cwd "{root}" "$@"
561-
"$SCRIPT_DIR/{node}" "$SCRIPT_DIR/{script}" --cwd "{root}" "$@"
560+
echo Running yarn "$@" in {root}
561+
(cd "{root}"; "$SCRIPT_DIR/{node}" "$SCRIPT_DIR/{script}" "$@")
562562
""".format(
563563
root = repository_ctx.path(package_json).dirname,
564564
node = paths.relativize(node_entry, "bin"),
@@ -585,8 +585,9 @@ SET SCRIPT_DIR=%~dp0
585585
SET SCRIPT_DIR=%~dp0
586586
""" + "".join([
587587
"""
588-
echo Running yarn --cwd "{root}" %*
589-
CALL "%SCRIPT_DIR%\\{node}" "%SCRIPT_DIR%\\{script}" --cwd "{root}" %*
588+
echo Running yarn %* in {root}
589+
cd "{root}"
590+
CALL "%SCRIPT_DIR%\\{node}" "%SCRIPT_DIR%\\{script}" %*
590591
if %errorlevel% neq 0 exit /b %errorlevel%
591592
""".format(
592593
root = repository_ctx.path(package_json).dirname,

internal/npm_install/npm_install.bzl

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,23 @@ def _yarn_install_impl(repository_ctx):
311311

312312
_check_min_bazel_version("yarn_install", repository_ctx)
313313

314+
is_windows_host = is_windows_os(repository_ctx)
314315
node = repository_ctx.path(get_node_label(repository_ctx))
315316
yarn = get_yarn_label(repository_ctx)
316317

318+
yarn_args = []
319+
if not repository_ctx.attr.use_global_yarn_cache:
320+
yarn_args.extend(["--cache-folder", repository_ctx.path("_yarn_cache")])
321+
else:
322+
# Multiple yarn rules cannot run simultaneously using a shared cache.
323+
# See https://github.com/yarnpkg/yarn/issues/683
324+
# The --mutex option ensures only one yarn runs at a time, see
325+
# https://yarnpkg.com/en/docs/cli#toc-concurrency-and-mutex
326+
# The shared cache is not necessarily hermetic, but we need to cache downloaded
327+
# artifacts somewhere, so we rely on yarn to be correct.
328+
yarn_args.extend(["--mutex", "network"])
329+
yarn_args.extend(repository_ctx.attr.args)
330+
317331
# If symlink_node_modules is true then run the package manager
318332
# in the package.json folder; otherwise, run it in the root of
319333
# the external repository
@@ -322,6 +336,34 @@ def _yarn_install_impl(repository_ctx):
322336
else:
323337
root = repository_ctx.path("")
324338

339+
# The entry points for npm install for osx/linux and windows
340+
if not is_windows_host:
341+
repository_ctx.file(
342+
"yarn",
343+
content = """#!/usr/bin/env bash
344+
# Immediately exit if any command fails.
345+
set -e
346+
(cd "{root}"; "{yarn}" {yarn_args})
347+
""".format(
348+
root = root,
349+
yarn = repository_ctx.path(yarn),
350+
yarn_args = " ".join(yarn_args),
351+
),
352+
executable = True,
353+
)
354+
else:
355+
repository_ctx.file(
356+
"yarn.cmd",
357+
content = """@echo off
358+
cd "{root}" && "{yarn}" {yarn_args}
359+
""".format(
360+
root = root,
361+
yarn = repository_ctx.path(yarn),
362+
yarn_args = " ".join(yarn_args),
363+
),
364+
executable = True,
365+
)
366+
325367
if not repository_ctx.attr.symlink_node_modules:
326368
repository_ctx.symlink(
327369
repository_ctx.attr.yarn_lock,
@@ -339,28 +381,9 @@ def _yarn_install_impl(repository_ctx):
339381
if result.return_code:
340382
fail("pre_process_package_json.js failed: \nSTDOUT:\n%s\nSTDERR:\n%s" % (result.stdout, result.stderr))
341383

342-
args = [
343-
repository_ctx.path(yarn),
344-
"--cwd",
345-
root,
346-
]
347-
348-
if not repository_ctx.attr.use_global_yarn_cache:
349-
args.extend(["--cache-folder", repository_ctx.path("_yarn_cache")])
350-
else:
351-
# Multiple yarn rules cannot run simultaneously using a shared cache.
352-
# See https://github.com/yarnpkg/yarn/issues/683
353-
# The --mutex option ensures only one yarn runs at a time, see
354-
# https://yarnpkg.com/en/docs/cli#toc-concurrency-and-mutex
355-
# The shared cache is not necessarily hermetic, but we need to cache downloaded
356-
# artifacts somewhere, so we rely on yarn to be correct.
357-
args.extend(["--mutex", "network"])
358-
359-
args.extend(repository_ctx.attr.args)
360-
361384
repository_ctx.report_progress("Running yarn install on %s" % repository_ctx.attr.package_json)
362385
result = repository_ctx.execute(
363-
args,
386+
[repository_ctx.path("yarn.cmd" if is_windows_host else "yarn")],
364387
timeout = repository_ctx.attr.timeout,
365388
quiet = repository_ctx.attr.quiet,
366389
)

0 commit comments

Comments
 (0)