Skip to content

Commit 552178e

Browse files
authored
fix(builtin): pkg_npm shouldn't assume the name of the nodejs toolchain (#3129)
Instead it should just ask the resolved toolchain for the info it needs
1 parent 90c7fe0 commit 552178e

File tree

8 files changed

+68
-31
lines changed

8 files changed

+68
-31
lines changed

examples/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ example_integration_test(
225225
example_integration_test(
226226
name = "examples_vue",
227227
npm_packages = {},
228+
repositories = {
229+
"//:release": "build_bazel_rules_nodejs",
230+
"//:release-core": "rules_nodejs",
231+
},
228232
)
229233

230234
example_integration_test(
@@ -249,6 +253,10 @@ example_integration_test(
249253
"@alan-agius4",
250254
"@jbedard",
251255
],
256+
repositories = {
257+
"//:release": "build_bazel_rules_nodejs",
258+
"//:release-core": "rules_nodejs",
259+
},
252260
tags = [
253261
# TODO(alexeagle): re-enable when it stops timing out on 4.x branch
254262
"manual",

examples/angular_bazel_architect/WORKSPACE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ http_archive(
1616
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/4.4.0/rules_nodejs-4.4.0.tar.gz"],
1717
)
1818

19+
# Temporary state: this example needs to have both build_bazel_rules_nodejs and rules_nodejs.
20+
# Once we have cut over the toolchains to rules_nodejs only, we shouldn't need this.
21+
http_archive(
22+
name = "rules_nodejs",
23+
sha256 = "a2b1b60c51b0193ed1646accf77a28cfd4f4ce1f6c86f32ce11455101be3a9c4",
24+
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/4.4.3/rules_nodejs-core-4.4.3.tar.gz"],
25+
)
26+
1927
load("@build_bazel_rules_nodejs//nodejs:repositories.bzl", "rules_nodejs_dependencies")
2028

2129
rules_nodejs_dependencies()
@@ -36,3 +44,10 @@ yarn_install(
3644
symlink_node_modules = False,
3745
yarn_lock = "//:yarn.lock",
3846
)
47+
48+
load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")
49+
50+
nodejs_register_toolchains(
51+
name = "node16",
52+
node_version = "16.9.0",
53+
)

examples/vue/WORKSPACE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,25 @@ http_archive(
1111
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/4.4.0/rules_nodejs-4.4.0.tar.gz"],
1212
)
1313

14+
# Temporary state: this example needs to have both build_bazel_rules_nodejs and rules_nodejs.
15+
# Once we have cut over the toolchains to rules_nodejs only, we shouldn't need this.
16+
http_archive(
17+
name = "rules_nodejs",
18+
sha256 = "a2b1b60c51b0193ed1646accf77a28cfd4f4ce1f6c86f32ce11455101be3a9c4",
19+
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/4.4.3/rules_nodejs-core-4.4.3.tar.gz"],
20+
)
21+
1422
load("@build_bazel_rules_nodejs//nodejs:repositories.bzl", "rules_nodejs_dependencies")
1523

1624
rules_nodejs_dependencies()
1725

26+
load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")
27+
28+
nodejs_register_toolchains(
29+
name = "node16",
30+
node_version = "16.9.0",
31+
)
32+
1833
load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "npm_install")
1934

2035
node_repositories(

internal/pkg_npm/npm_script_generator.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ const fs = require('fs');
2424

2525
function main(args) {
2626
const
27-
[outDir, packPath, publishPath, runNpmTemplatePath, packBatPath, publishBatPath,
28-
runNpmBatTemplatePath] = args;
27+
[outDir, packPath, publishPath, runNpmTemplatePath, packBatPath, publishBatPath] = args;
2928
const cwd = process.cwd();
3029
if (/[\//]sandbox[\//]/.test(cwd)) {
3130
console.error('Error: npm_script_generator must be run with no sandbox');
@@ -36,10 +35,8 @@ function main(args) {
3635
fs.writeFileSync(packPath, npmTemplate.replace('TMPL_args', `pack "${cwd}/${outDir}"`));
3736
fs.writeFileSync(publishPath, npmTemplate.replace('TMPL_args', `publish "${cwd}/${outDir}"`));
3837

39-
const npmBatTemplate = fs.readFileSync(runNpmBatTemplatePath, {encoding: 'utf-8'});
40-
fs.writeFileSync(packBatPath, npmBatTemplate.replace('TMPL_args', `pack "${cwd}/${outDir}"`));
41-
fs.writeFileSync(
42-
publishBatPath, npmBatTemplate.replace('TMPL_args', `publish "${cwd}/${outDir}"`));
38+
fs.writeFileSync(packBatPath, npmTemplate.replace('TMPL_args', `pack "${cwd}/${outDir}"`));
39+
fs.writeFileSync(publishBatPath, npmTemplate.replace('TMPL_args', `publish "${cwd}/${outDir}"`));
4340
}
4441

4542
if (require.main === module) {

internal/pkg_npm/pkg_npm.bzl

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,14 @@ See the section on stamping in the [README](stamping)
143143
),
144144
"_npm_script_generator": attr.label(
145145
default = Label("//internal/pkg_npm:npm_script_generator"),
146-
cfg = "host",
146+
cfg = "exec",
147147
executable = True,
148148
),
149149
"_packager": attr.label(
150150
default = Label("//internal/pkg_npm:packager"),
151-
cfg = "host",
151+
cfg = "exec",
152152
executable = True,
153153
),
154-
"_run_npm_bat_template": attr.label(
155-
default = Label("@nodejs//:run_npm.bat.template"),
156-
allow_single_file = True,
157-
),
158-
"_run_npm_template": attr.label(
159-
default = Label("@nodejs//:run_npm.sh.template"),
160-
allow_single_file = True,
161-
),
162154
})
163155

164156
# Used in angular/angular /packages/bazel/src/ng_package/ng_package.bzl
@@ -283,22 +275,22 @@ def create_package(ctx, deps_files, nested_packages):
283275

284276
def _create_npm_scripts(ctx, package_dir):
285277
args = ctx.actions.args()
278+
toolchain = ctx.toolchains["@rules_nodejs//nodejs:toolchain_type"].nodeinfo
286279

287280
args.add_all([
288281
package_dir.path,
289282
ctx.outputs.pack_sh.path,
290283
ctx.outputs.publish_sh.path,
291-
ctx.file._run_npm_template.path,
284+
toolchain.run_npm.path,
292285
ctx.outputs.pack_bat.path,
293286
ctx.outputs.publish_bat.path,
294-
ctx.file._run_npm_bat_template.path,
295287
])
296288

297289
ctx.actions.run(
298290
progress_message = "Generating npm pack & publish scripts",
299291
mnemonic = "GenerateNpmScripts",
300292
executable = ctx.executable._npm_script_generator,
301-
inputs = [ctx.file._run_npm_template, ctx.file._run_npm_bat_template, package_dir],
293+
inputs = [toolchain.run_npm, package_dir],
302294
outputs = [ctx.outputs.pack_sh, ctx.outputs.publish_sh, ctx.outputs.pack_bat, ctx.outputs.publish_bat],
303295
arguments = [args],
304296
# Must be run local (no sandbox) so that the pwd is the actual execroot
@@ -362,6 +354,7 @@ pkg_npm = rule(
362354
attrs = PKG_NPM_ATTRS,
363355
doc = _DOC,
364356
outputs = PKG_NPM_OUTPUTS,
357+
toolchains = ["@rules_nodejs//nodejs:toolchain_type"],
365358
)
366359

367360
def pkg_npm_macro(name, tgz = None, **kwargs):

nodejs/private/toolchains_repo.bzl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ resolved_toolchain(name = "resolved_toolchain", visibility = ["//visibility:publ
105105
toolchain(
106106
name = "{platform}_toolchain",
107107
exec_compatible_with = {compatible_with},
108-
target_compatible_with = {compatible_with},
109108
toolchain = "@{user_node_repository_name}_{platform}//:node_toolchain",
110109
toolchain_type = "@rules_nodejs//nodejs:toolchain_type",
111110
)

nodejs/repositories.bzl

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -521,16 +521,16 @@ if %errorlevel% neq 0 exit /b %errorlevel%
521521

522522
# This template file is used by the packager tool and the pkg_npm rule.
523523
# `yarn publish` is not ready for use under Bazel, see https://github.com/yarnpkg/yarn/issues/610
524-
repository_ctx.file("run_npm.sh.template", content = """
524+
if repository_ctx.attr.platform.startswith("windows"):
525+
run_npm = """
526+
"{node}" "{script}" TMPL_args %*
527+
"""
528+
else:
529+
run_npm = """
525530
"{node}" "{script}" TMPL_args "$@"
526-
""".format(
527-
node = repository_ctx.path(node_entry),
528-
script = repository_ctx.path(npm_script),
529-
))
531+
"""
530532

531-
repository_ctx.file("run_npm.bat.template", content = """
532-
"{node}" "{script}" TMPL_args %*
533-
""".format(
533+
repository_ctx.file("run_npm.template", content = run_npm.format(
534534
node = repository_ctx.path(node_entry),
535535
script = repository_ctx.path(npm_script),
536536
))
@@ -616,8 +616,7 @@ if %errorlevel% neq 0 exit /b %errorlevel%
616616
build_content = """# Generated by node_repositories.bzl
617617
package(default_visibility = ["//visibility:public"])
618618
exports_files([
619-
"run_npm.sh.template",
620-
"run_npm.bat.template",
619+
"run_npm.template",
621620
"{node_entry}",
622621
"{npm_entry}",
623622
"{yarn_entry}",
@@ -667,7 +666,11 @@ filegroup(
667666
if repository_ctx.attr.platform:
668667
build_content += """
669668
load("@rules_nodejs//nodejs:toolchain.bzl", "node_toolchain")
670-
node_toolchain(name = "node_toolchain", target_tool = ":node_bin")
669+
node_toolchain(
670+
name = "node_toolchain",
671+
target_tool = ":node_bin",
672+
run_npm = ":run_npm.template",
673+
)
671674
"""
672675
repository_ctx.file("BUILD.bazel", content = build_content)
673676

nodejs/toolchain.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ NodeInfo = provider(
2222
"tool_files": """Files required in runfiles to make the nodejs executable available.
2323
2424
May be empty if the target_tool_path points to a locally installed node binary.""",
25+
"run_npm": """A template for a script that wraps npm.
26+
On Windows, this is a Batch script, otherwise it uses Bash.""",
2527
},
2628
)
2729

@@ -57,6 +59,7 @@ def _node_toolchain_impl(ctx):
5759
nodeinfo = NodeInfo(
5860
target_tool_path = target_tool_path,
5961
tool_files = tool_files,
62+
run_npm = ctx.file.run_npm,
6063
)
6164

6265
# Export all the providers inside our ToolchainInfo
@@ -84,6 +87,10 @@ node_toolchain = rule(
8487
doc = "Path to an existing nodejs executable for the target platform.",
8588
mandatory = False,
8689
),
90+
"run_npm": attr.label(
91+
doc = "A template file that allows us to execute npm",
92+
allow_single_file = True,
93+
),
8794
},
8895
doc = """Defines a node toolchain.
8996

0 commit comments

Comments
 (0)