Skip to content

Commit 72c14d8

Browse files
authored
fix(builtin): preserve lone $ in templated_args for legacy support (#1772)
This preservation may be removed in a future version so that templated_args behaves the same as the built-in *_binary and *_test args
1 parent 5c9878f commit 72c14d8

6 files changed

Lines changed: 60 additions & 39 deletions

File tree

internal/common/preserve_legacy_rlocation.bzl renamed to internal/common/preserve_legacy_templated_args.bzl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""Helper functions to preserve legacy `$(rlocation ` usage
15+
"""Helper functions to preserve legacy `$` usage in templated_args
1616
"""
1717

18-
def preserve_legacy_rlocation(input):
19-
"""Converts legacy `$(rlocation ` to `$(rlocation ` which is preserved when expanding make
20-
variables with ctx.expand_make_variables.
18+
def preserve_legacy_templated_args(input):
19+
"""Converts legacy uses of `$` to `$$` so that the new call to ctx.expand_make_variables
20+
21+
Converts any lone `$` that is not proceeded by a `(` to `$$. Also converts legacy `$(rlocation `
22+
to `$$(rlocation ` as that is a commonly used bash function so we don't want to break this
23+
legacy behavior.
2124
2225
Args:
2326
input: String to be modified
@@ -32,5 +35,9 @@ def preserve_legacy_rlocation(input):
3235
if i == 0 or input[i - 1] != "$":
3336
# insert an additional "$"
3437
result += "$"
38+
elif input[i] == "$" and (i + 1 == length or (i + 1 < length and input[i + 1] != "(" and input[i + 1] != "$")):
39+
if i == 0 or input[i - 1] != "$":
40+
# insert an additional "$"
41+
result += "$"
3542
result += input[i]
3643
return result

internal/common/test/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ load("@npm_bazel_jasmine//:index.from_src.bzl", "jasmine_node_test")
33
load("//internal/common:copy_to_bin.bzl", "copy_to_bin")
44
load("//internal/common:params_file.bzl", "params_file")
55
load(":expand_into_runfiles_test.bzl", "expand_into_runfiles_test_suite")
6-
load(":preserve_legacy_rlocation_test.bzl", "preserve_legacy_rlocation_test_suite")
6+
load(":preserve_legacy_templated_args_test.bzl", "preserve_legacy_templated_args_test_suite")
77

88
licenses(["notice"])
99

@@ -78,4 +78,4 @@ sh_test(
7878

7979
expand_into_runfiles_test_suite()
8080

81-
preserve_legacy_rlocation_test_suite()
81+
preserve_legacy_templated_args_test_suite()

internal/common/test/preserve_legacy_rlocation_test.bzl

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"Unit tests for //internal/common:preserve_legacy_templated_args.bzl"
2+
3+
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
4+
load("//internal/common:preserve_legacy_templated_args.bzl", "preserve_legacy_templated_args")
5+
6+
def _impl(ctx):
7+
env = unittest.begin(ctx)
8+
9+
conversions = {
10+
"$": "$$",
11+
"$$": "$$",
12+
"$$$$(BAR)": "$$$$(BAR)",
13+
"$$(": "$$(",
14+
"$$(BAR)": "$$(BAR)",
15+
"$$(rlocation foobar)": "$$(rlocation foobar)",
16+
"$$(rlocation foobar)$$(rlocation foobar)": "$$(rlocation foobar)$$(rlocation foobar)",
17+
"$(": "$(",
18+
"$(BAR)": "$(BAR)",
19+
"$(rlocation foobar)": "$$(rlocation foobar)",
20+
"$(rlocation foobar)$(rlocation foobar)": "$$(rlocation foobar)$$(rlocation foobar)",
21+
"$(rlocation! foobar)": "$(rlocation! foobar)",
22+
"$(rlocation! foobar)$(rlocation! foobar)": "$(rlocation! foobar)$(rlocation! foobar)",
23+
}
24+
25+
for key in conversions:
26+
asserts.equals(env, "%s" % conversions[key], preserve_legacy_templated_args("%s" % key))
27+
asserts.equals(env, " %s " % conversions[key], preserve_legacy_templated_args(" %s " % key))
28+
asserts.equals(env, "%s %s" % (conversions[key], conversions[key]), preserve_legacy_templated_args("%s %s" % (key, key)))
29+
asserts.equals(env, " %s %s " % (conversions[key], conversions[key]), preserve_legacy_templated_args(" %s %s " % (key, key)))
30+
asserts.equals(env, "a%sb%sc" % (conversions[key], conversions[key]), preserve_legacy_templated_args("a%sb%sc" % (key, key)))
31+
32+
return unittest.end(env)
33+
34+
preserve_legacy_templated_args_test = unittest.make(
35+
impl = _impl,
36+
attrs = {},
37+
)
38+
39+
def preserve_legacy_templated_args_test_suite():
40+
unittest.suite("preserve_legacy_templated_args_tests", preserve_legacy_templated_args_test)

internal/node/node.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ load("//:providers.bzl", "JSNamedModuleInfo", "NodeRuntimeDepsInfo", "NpmPackage
2424
load("//internal/common:expand_into_runfiles.bzl", "expand_location_into_runfiles")
2525
load("//internal/common:module_mappings.bzl", "module_mappings_runtime_aspect")
2626
load("//internal/common:path_utils.bzl", "strip_external")
27-
load("//internal/common:preserve_legacy_rlocation.bzl", "preserve_legacy_rlocation")
27+
load("//internal/common:preserve_legacy_templated_args.bzl", "preserve_legacy_templated_args")
2828
load("//internal/common:windows_utils.bzl", "create_windows_native_launcher_script", "is_windows")
2929
load("//internal/linker:link_node_modules.bzl", "module_mappings_aspect", "write_node_modules_manifest")
3030
load("//internal/node:node_repositories.bzl", "BUILT_IN_NODE_PLATFORMS")
@@ -214,7 +214,7 @@ def _nodejs_binary_impl(ctx):
214214

215215
# First replace any instances of "$(rlocation " with "$$(rlocation " to preserve
216216
# legacy uses of "$(rlocation"
217-
expanded_args = [preserve_legacy_rlocation(a) for a in ctx.attr.templated_args]
217+
expanded_args = [preserve_legacy_templated_args(a) for a in ctx.attr.templated_args]
218218

219219
# First expand predefined source/output path variables:
220220
# $(execpath), $(rootpath) & legacy $(location)

internal/node/test/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ npm_package_bin(
282282
"$(TARGET_CPU)",
283283
"$(BINDIR)",
284284
"$(SOME_TEST_ENV)",
285+
"somearg$$",
286+
"some0arg",
285287
],
286288
tool = ":expand_variables",
287289
)
@@ -296,6 +298,9 @@ jasmine_node_test(
296298
"$(TARGET_CPU)",
297299
"$(BINDIR)",
298300
"$(SOME_TEST_ENV)",
301+
# Should preserve lone $ in templated_args
302+
"somearg$",
303+
"some$#arg",
299304
],
300305
)
301306

0 commit comments

Comments
 (0)