Skip to content

Commit 34176e5

Browse files
gregmagolanalexeagle
authored andcommitted
feat(builtin): add support for Predefined variables and Custom variable to npm_package_bin
args: Command-line arguments to the tool. Subject to 'Make variable' substitution. See https://docs.bazel.build/versions/master/be/make-variables.html. 1. Predefined source/output path substitions is applied first: See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_label_variables. Use $(execpath) $(execpaths) to expand labels to the execroot (where Bazel runs build actions). Use $(rootpath) $(rootpaths) to expand labels to the runfiles path that a built binary can use to find its dependencies. Since npm_package_bin is used primarily for build actions, in most cases you'll want to use $(execpath) or $(execpaths) to expand locations. Using $(location) and $(locations) expansions is not recommended as these are a synonyms for either execpath or rootpath, depending on the attribute being expanded. 2. "Make" variables are expanded second: Predefined "Make" variables such as $(COMPILATION_MODE) and $(TARGET_CPU) are expanded. See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_variables. Like genrule, you may also use some syntax sugar for locations. - `$@`: if you have only one output file, the location of the output - `$(@d)`: The output directory. If output_dir=False and there is only one file name in outs, this expands to the directory containing that file. If there are multiple files, this instead expands to the package's root directory in the genfiles tree, even if all generated files belong to the same subdirectory! If output_dir=True then this corresponds to the output directory which is the $(RULEDIR)/{target_name}. - `$(RULEDIR)`: the root output directory of the rule, corresponding with its package (can be used with output_dir=True or False) See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_genrule_variables. Custom variables are also expanded including variables set through the Bazel CLI with --define=SOME_VAR=SOME_VALUE. See https://docs.bazel.build/versions/master/be/make-variables.html#custom_variables.
1 parent e488ac7 commit 34176e5

2 files changed

Lines changed: 99 additions & 37 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright 2017 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Helper functions to expand "make" variables of form $(VAR)
16+
"""
17+
18+
def expand_variables(ctx, s, outs = [], output_dir = False):
19+
"""This function is the same as ctx.expand_make_variables with the additional
20+
genrule-like substitutions of:
21+
22+
- $@: The output file if it is a single file. Else triggers a build error.
23+
- $(@D): The output directory. If there is only one file name in outs,
24+
this expands to the directory containing that file. If there are multiple files,
25+
this instead expands to the package's root directory in the bin tree,
26+
even if all generated files belong to the same subdirectory!
27+
- $(RULEDIR): The output directory of the rule, that is, the directory
28+
corresponding to the name of the package containing the rule under the bin tree.
29+
30+
See https://docs.bazel.build/versions/master/be/general.html#genrule.cmd and
31+
https://docs.bazel.build/versions/master/be/make-variables.html#predefined_genrule_variables
32+
for more information of how these special variables are expanded.
33+
"""
34+
rule_dir = [ctx.bin_dir.path, ctx.label.package]
35+
additional_substitutions = {}
36+
37+
if output_dir:
38+
if s.find("$@") != -1 or s.find("$(@)") != -1:
39+
fail("""$@ substitution may only be used with output_dir=False.
40+
Upgrading rules_nodejs? Maybe you need to switch from $@ to $(@D)
41+
See https://github.com/bazelbuild/rules_nodejs/releases/tag/0.42.0""")
42+
43+
# We'll write into a newly created directory named after the rule
44+
output_dir = [ctx.bin_dir.path, ctx.label.package, ctx.attr.name]
45+
else:
46+
if s.find("$@") != -1 or s.find("$(@)") != -1:
47+
if len(ctx.outputs.outs) > 1:
48+
fail("""$@ substitution may only be used with a single out
49+
Upgrading rules_nodejs? Maybe you need to switch from $@ to $(RULEDIR)
50+
See https://github.com/bazelbuild/rules_nodejs/releases/tag/0.42.0""")
51+
additional_substitutions["@"] = ctx.outputs.outs[0].path
52+
if len(ctx.outputs.outs) == 1:
53+
output_dir = ctx.outputs.outs[0].dirname.split("/")
54+
else:
55+
output_dir = rule_dir[:]
56+
57+
# The list comprehension removes empty segments like if we are in the root package
58+
additional_substitutions["@D"] = "/".join([o for o in output_dir if o])
59+
additional_substitutions["RULEDIR"] = "/".join([o for o in rule_dir if o])
60+
61+
return ctx.expand_make_variables("args", s, additional_substitutions)

internal/node/npm_package_bin.bzl

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"A generic rule to run a tool that appears in node_modules/.bin"
22

33
load("//:providers.bzl", "NpmPackageInfo", "node_modules_aspect", "run_node")
4+
load("//internal/common:expand_variables.bzl", "expand_variables")
45
load("//internal/linker:link_node_modules.bzl", "module_mappings_aspect")
56

67
# Note: this API is chosen to match nodejs_binary
@@ -17,36 +18,12 @@ _ATTRS = {
1718
),
1819
}
1920

20-
# Need a custom expand_location function
21-
# because the output_dir is a tree artifact
22-
# so we weren't able to give it a label
23-
def _expand_location(ctx, s):
24-
rule_dir = [ctx.bin_dir.path, ctx.label.package]
25-
26-
if ctx.attr.output_dir:
27-
if s.find("$@") != -1:
28-
fail("""$@ substitution may only be used with output_dir=False.
29-
Upgrading rules_nodejs? Maybe you need to switch from $@ to $(@D)
30-
See https://github.com/bazelbuild/rules_nodejs/releases/tag/0.42.0""")
31-
32-
# We'll write into a newly created directory named after the rule
33-
output_dir = [ctx.bin_dir.path, ctx.label.package, ctx.attr.name]
34-
else:
35-
if s.find("$@") != -1 and len(ctx.outputs.outs) > 1:
36-
fail("""$@ substitution may only be used with a single out
37-
Upgrading rules_nodejs? Maybe you need to switch from $@ to $(RULEDIR)
38-
See https://github.com/bazelbuild/rules_nodejs/releases/tag/0.42.0""")
39-
s = s.replace("$@", ctx.outputs.outs[0].path)
40-
if len(ctx.outputs.outs) == 1:
41-
output_dir = ctx.outputs.outs[0].dirname.split("/")
42-
else:
43-
output_dir = rule_dir[:]
44-
45-
# The list comprehension removes empty segments like if we are in the root package
46-
s = s.replace("$(@D)", "/".join([o for o in output_dir if o]))
47-
s = s.replace("$(RULEDIR)", "/".join([o for o in rule_dir if o]))
48-
49-
return ctx.expand_location(s, targets = ctx.attr.data)
21+
def _expand_locations(ctx, s):
22+
# `.split(" ")` is a work-around https://github.com/bazelbuild/bazel/issues/10309
23+
# _expand_locations returns an array of args to support $(execpaths) expansions.
24+
# TODO: If the string has intentional spaces or if one or more of the expanded file
25+
# locations has a space in the name, we will incorrectly split it into multiple arguments
26+
return ctx.expand_location(s, targets = ctx.attr.data).split(" ")
5027

5128
def _inputs(ctx):
5229
# Also include files from npm fine grained deps as inputs.
@@ -72,10 +49,8 @@ def _impl(ctx):
7249
outputs = ctx.outputs.outs
7350

7451
for a in ctx.attr.args:
75-
# Workaround bazelbuild/bazel#10309
76-
# If one of the files has a space in the name, we will
77-
# incorrectly split it into multiple argv
78-
args.add_all(_expand_location(ctx, a).split(" "))
52+
args.add_all([expand_variables(ctx, e, outs = ctx.attr.outs, output_dir = ctx.attr.output_dir) for e in _expand_locations(ctx, a)])
53+
7954
run_node(
8055
ctx,
8156
executable = "tool",
@@ -111,9 +86,30 @@ def npm_package_bin(tool = None, package = None, package_bin = None, data = [],
11186
11287
args: Command-line arguments to the tool.
11388
114-
Subject to 'Make variable' substitution.
115-
Can use $(location) expansion. See https://docs.bazel.build/versions/master/be/make-variables.html
116-
Like genrule, you may also use some syntax sugar for locations:
89+
Subject to 'Make variable' substitution. See https://docs.bazel.build/versions/master/be/make-variables.html.
90+
91+
1. Predefined source/output path substitions is applied first:
92+
93+
See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_label_variables.
94+
95+
Use $(execpath) $(execpaths) to expand labels to the execroot (where Bazel runs build actions).
96+
97+
Use $(rootpath) $(rootpaths) to expand labels to the runfiles path that a built binary can use
98+
to find its dependencies.
99+
100+
Since npm_package_bin is used primarily for build actions, in most cases you'll want to
101+
use $(execpath) or $(execpaths) to expand locations.
102+
103+
Using $(location) and $(locations) expansions is not recommended as these are a synonyms
104+
for either $(execpath) or $(rootpath) depending on the context.
105+
106+
2. "Make" variables are expanded second:
107+
108+
Predefined "Make" variables such as $(COMPILATION_MODE) and $(TARGET_CPU) are expanded.
109+
See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_variables.
110+
111+
Like genrule, you may also use some syntax sugar for locations.
112+
117113
- `$@`: if you have only one output file, the location of the output
118114
- `$(@D)`: The output directory. If output_dir=False and there is only one file name in outs, this expands to the directory
119115
containing that file. If there are multiple files, this instead expands to the package's root directory in the genfiles
@@ -122,6 +118,11 @@ def npm_package_bin(tool = None, package = None, package_bin = None, data = [],
122118
- `$(RULEDIR)`: the root output directory of the rule, corresponding with its package
123119
(can be used with output_dir=True or False)
124120
121+
See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_genrule_variables.
122+
123+
Custom variables are also expanded including variables set through the Bazel CLI with --define=SOME_VAR=SOME_VALUE.
124+
See https://docs.bazel.build/versions/master/be/make-variables.html#custom_variables.
125+
125126
package: an npm package whose binary to run, like "terser". Assumes your node_modules are installed in a workspace called "npm"
126127
package_bin: the "bin" entry from `package` that should be run. By default package_bin is the same string as `package`
127128
tool: a label for a binary to run, like `@npm//terser/bin:terser`. This is the longer form of package/package_bin.

0 commit comments

Comments
 (0)