@@ -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: \n STDOUT:\n %s\n STDERR:\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