Skip to content

Commit 87b2a64

Browse files
authored
feat(builtin): add environment attribute to yarn_install & npm_install (#1596)
This allows you to specify a dict of environment variables for Bazel to set before calling yarn and npm in the yarn_install and npm_install repository rules respectively. Also set BAZEL_YARN_INSTALL to "1" and BAZEL_NPM_INSTALL to "1" respectively (unless they are set to another value by the environment attribute).
1 parent 8d77827 commit 87b2a64

6 files changed

Lines changed: 66 additions & 3 deletions

File tree

WORKSPACE

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ load("@build_bazel_rules_nodejs//:index.bzl", "npm_install", "yarn_install")
6868

6969
yarn_install(
7070
name = "npm",
71+
data = [
72+
"//internal/npm_install/test:postinstall.js",
73+
],
74+
environment = {
75+
"SOME_USER_ENV": "yarn is great!",
76+
},
7177
# The @npm//:node_modules_filegroup generated by manual_build_file_contents
7278
# is used in the //packages/typescript/test/reference_types_directive:tsconfig_types
7379
# test. For now we're still supporting node_modules as a filegroup tho this may
@@ -192,6 +198,12 @@ local_repository(
192198

193199
yarn_install(
194200
name = "fine_grained_deps_yarn",
201+
data = [
202+
"//internal/npm_install/test:postinstall.js",
203+
],
204+
environment = {
205+
"SOME_USER_ENV": "yarn is great!",
206+
},
195207
included_files = [
196208
"",
197209
".js",
@@ -206,6 +218,12 @@ yarn_install(
206218

207219
npm_install(
208220
name = "fine_grained_deps_npm",
221+
data = [
222+
"//internal/npm_install/test:postinstall.js",
223+
],
224+
environment = {
225+
"SOME_USER_ENV": "npm is cool!",
226+
},
209227
included_files = [
210228
"",
211229
".js",

internal/npm_install/npm_install.bzl

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ If symlink_node_modules is True, this attribute is ignored since
7070
the dependency manager will run in the package.json location.
7171
""",
7272
),
73+
"environment": attr.string_dict(
74+
doc = """Environment variables to set before calling the package manager.""",
75+
default = {},
76+
),
7377
"included_files": attr.string_list(
7478
doc = """List of file extensions to be included in the npm package targets.
7579
@@ -263,11 +267,17 @@ cd "{root}" && "{npm}" {npm_args}
263267
if result.return_code:
264268
fail("pre_process_package_json.js failed: \nSTDOUT:\n%s\nSTDERR:\n%s" % (result.stdout, result.stderr))
265269

270+
env = dict(repository_ctx.attr.environment)
271+
env_key = "BAZEL_NPM_INSTALL"
272+
if env_key not in env.keys():
273+
env[env_key] = "1"
274+
266275
repository_ctx.report_progress("Running npm install on %s" % repository_ctx.attr.package_json)
267276
result = repository_ctx.execute(
268277
[repository_ctx.path("_npm.cmd" if is_windows_host else "_npm.sh")],
269278
timeout = repository_ctx.attr.timeout,
270279
quiet = repository_ctx.attr.quiet,
280+
environment = env,
271281
)
272282

273283
if result.return_code:
@@ -303,7 +313,11 @@ See npm CLI docs https://docs.npmjs.com/cli/install.html for complete list of su
303313
allow_single_file = True,
304314
),
305315
}),
306-
doc = "Runs npm install during workspace setup.",
316+
doc = """Runs npm install during workspace setup.
317+
318+
This rule will set the environment variable `BAZEL_NPM_INSTALL` to '1' (unless it
319+
set to another value in the environment attribute). Scripts may use to this to
320+
check if yarn is being run by the `npm_install` repository rule.""",
307321
implementation = _npm_install_impl,
308322
)
309323

@@ -390,11 +404,17 @@ cd "{root}" && "{yarn}" {yarn_args}
390404
if result.return_code:
391405
fail("pre_process_package_json.js failed: \nSTDOUT:\n%s\nSTDERR:\n%s" % (result.stdout, result.stderr))
392406

407+
env = dict(repository_ctx.attr.environment)
408+
env_key = "BAZEL_YARN_INSTALL"
409+
if env_key not in env.keys():
410+
env[env_key] = "1"
411+
393412
repository_ctx.report_progress("Running yarn install on %s" % repository_ctx.attr.package_json)
394413
result = repository_ctx.execute(
395414
[repository_ctx.path("_yarn.cmd" if is_windows_host else "_yarn.sh")],
396415
timeout = repository_ctx.attr.timeout,
397416
quiet = repository_ctx.attr.quiet,
417+
environment = env,
398418
)
399419
if result.return_code:
400420
fail("yarn_install failed: %s (%s)" % (result.stdout, result.stderr))
@@ -435,6 +455,10 @@ to yarn so that the local cache is contained within the external repository.
435455
allow_single_file = True,
436456
),
437457
}),
438-
doc = "Runs yarn install during workspace setup.",
458+
doc = """Runs yarn install during workspace setup.
459+
460+
This rule will set the environment variable `BAZEL_YARN_INSTALL` to '1' (unless it
461+
set to another value in the environment attribute). Scripts may use to this to
462+
check if yarn is being run by the `yarn_install` repository rule.""",
439463
implementation = _yarn_install_impl,
440464
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This script is called by postinstall steps of yarn_install & npm_install rules in the root
2+
// WORKSPACE. It tests that the environment attribute sets environment variables as expected.
3+
4+
const expectedYarn = 'yarn is great!';
5+
if (process.env['BAZEL_YARN_INSTALL'] === '1' && process.env['SOME_USER_ENV'] !== expectedYarn) {
6+
throw `Expected SOME_USER_ENV environment variable to be set to '${
7+
expectedYarn}' by yarn_install but got '${process.env['SOME_USER_ENV']}'`;
8+
}
9+
10+
const expectedNpm = 'npm is cool!';
11+
if (process.env['BAZEL_NPM_INSTALL'] === '1' && process.env['SOME_USER_ENV'] !== expectedNpm) {
12+
throw `Expected SOME_USER_ENV environment variable to be set to '${
13+
expectedNpm}' by npm_install but got '${process.env['SOME_USER_ENV']}'`;
14+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@
107107
"format": "git-clang-format",
108108
"format-all": "clang-format --glob='{internal/**/,examples/**/}*.{js,ts}' -i",
109109
"stardoc": "bazel build //docs && cp -f dist/bin/docs/*.md docs",
110-
"version": "conventional-changelog -p angular -i CHANGELOG.md -s && node ./scripts/on-version.js && bazel build //:release && node ./scripts/on-release.js && git stage index.bzl docs/install.md packages/create/index.js README.md CHANGELOG.md e2e/*/WORKSPACE examples/*/WORKSPACE"
110+
"version": "conventional-changelog -p angular -i CHANGELOG.md -s && node ./scripts/on-version.js && bazel build //:release && node ./scripts/on-release.js && git stage index.bzl docs/install.md packages/create/index.js README.md CHANGELOG.md e2e/*/WORKSPACE examples/*/WORKSPACE",
111+
"postinstall": "node internal/npm_install/test/postinstall.js"
111112
},
112113
"husky": {
113114
"hooks": {

tools/fine_grained_deps_npm/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@
1212
"http-server": "github:alexeagle/http-server#97205e945b69091606ed83aa0c8489e9ce65d282",
1313
"klaw": "1.3.1",
1414
"rxjs": "6.5.0"
15+
},
16+
"scripts": {
17+
"postinstall": "node internal/npm_install/test/postinstall.js"
1518
}
1619
}

tools/fine_grained_deps_yarn/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@
1212
"http-server": "github:alexeagle/http-server#97205e945b69091606ed83aa0c8489e9ce65d282",
1313
"klaw": "1.3.1",
1414
"rxjs": "6.5.0"
15+
},
16+
"scripts": {
17+
"postinstall": "node internal/npm_install/test/postinstall.js"
1518
}
1619
}

0 commit comments

Comments
 (0)