8000 fix: quotes within js_image_layer regexes by jbedard · Pull Request #2145 · aspect-build/rules_js · GitHub
[go: up one dir, main page]

Skip to content

fix: quotes within js_image_layer regexes #2145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/js_image_layer.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 14 additions & 13 deletions js/private/js_image_layer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ load("@aspect_bazel_lib//lib:tar.bzl", "tar_lib")
load("@bazel_skylib//lib:paths.bzl", "paths")

_DEFAULT_LAYER_GROUPS = {
"node": "\\/js\\/private\\/node-patches\\/|\\/bin\\/nodejs\\/",
"package_store_3p": "\\/\\.aspect_rules_js\\/(?!.*@0\\.0\\.0).*\\/node_modules",
"package_store_1p": "\\.aspect_rules_js\\/.*@0\\.0\\.0\\/node_modules",
"node_modules": "\\/node_modules\\/",
"node": "/js/private/node-patches/|/bin/nodejs/",
"package_store_3p": "/\\.aspect_rules_js/(?!.*@0\\.0\\.0).*/node_modules",
"package_store_1p": "\\.aspect_rules_js/.*@0\\.0\\.0/node_modules",
"node_modules": "/node_modules/",
"app": "", # empty means just match anything.
}

Expand Down Expand Up @@ -278,10 +278,10 @@ The default layer groups are as follows and always created.

```
{
"node": "\\/js\\/private\\/node-patches\\/|\\/bin\\/nodejs\\/",
"package_store_1p": "\\.aspect_rules_js\\/.*@0\\.0\\.0\\/node_modules",
"package_store_3p": "\\.aspect_rules_js\\/.*\\/node_modules",
"node_modules": "\\/node_modules\\/",
"node": "/js/private/node-patches/|/bin/nodejs/",
"package_store_1p": "\\.aspect_rules_js/.*@0\\.0\\.0/node_modules",
"package_store_3p": "\\.aspect_rules_js/.*/node_modules",
"node_modules": "/node_modules/",
"app": "", # empty means just match anything.
}
```
Expand Down Expand Up @@ -335,13 +335,14 @@ def _run_splitter(ctx, runfiles_dir, files, entries_json, layer_groups):
unused_inputs = ctx.actions.declare_file("{}_{}_unused_inputs.txt".format(ctx.label.name, name))
splitter_outputs.extend([mtree, unused_inputs])
VARIABLES += """
const {name}_re = new RegExp({});
const {name}mtree = new Set(["#mtree"]);
const {name}unusedinputs = createWriteStream("{}");
""".format(unused_inputs.path, name = name)
const {name}unusedinputs = createWriteStream({});
""".format(json.encode(match), json.encode(unused_inputs.path), name = name)

STMT = "else if" if PICK_STATEMENTS != "" else "if"

IF_STMT = "%s (/%s/.test(key))" % (STMT, match)
IF_STMT = "%s (%s_re.test(key))" % (STMT, name)

# Empty match means, match anything, same as .* but faster.
if match == "":
Expand Down Expand Up @@ -380,7 +381,7 @@ def _run_splitter(ctx, runfiles_dir, files, entries_json, layer_groups):
"{{RUNFILES_DIR}}": runfiles_dir,
"{{REPO_NAME}}": ctx.workspace_name,
"{{ENTRIES}}": entries_json.path,
"{{PRESERVE_SYMLINKS}}": ctx.attr.preserve_symlinks,
"'{{PRESERVE_SYMLINKS}}'": json.encode(ctx.attr.preserve_symlinks),
"{{UNUSED_INPUTS}}": unused_inputs.path,
"/*{{VARIABLES}}*/": VARIABLES,
"/*{{PICK_STATEMENTS}}*/": PICK_STATEMENTS,
Expand Down Expand Up @@ -616,7 +617,7 @@ js_image_layer_lib = struct(
doc = """Preserve symlinks for entries matching the pattern.
By default symlinks within the `node_modules` is preserved.
""",
default = ".*\\/node_modules\\/.*",
default = ".*/node_modules/.*",
),
"layer_groups": attr.string_dict(
doc = """Layer groups to create.
Expand Down
4 changes: 3 additions & 1 deletion js/private/js_image_layer.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions js/private/test/image/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,23 @@ assert_js_image_layer_listings(
name = "custom_owner_test",
js_image_layer = ":custom_owner",
)

# Case 3: Change owner
# bazel run :custom_owner_test_update_all
make_js_image_layer(
name = "regex_edge_cases",
binary = ":bin",
layer_groups = {
# Odd characters to insert into js string/regex/backtick expressions
"odd_characters": "spaces quotes ' \" ` slashes / \\ | symbols \\{",
},
platform = ":linux_amd64",
preserve_symlinks = "node_modules|/foobar/\"'",
root = "/app",
)

assert_js_image_layer_listings(
name = "regex_edge_cases_test",
additional_layers = ["odd_characters"],
js_image_layer = ":regex_edge_cases",
)
23 changes: 23 additions & 0 deletions js/private/test/image/regex_edge_cases_test_app.bzlmod.listing
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/
-r-xr-xr-x 0 0 0 23981 Jan 1 1970 ./app/js/private/test/image/bin
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/examples/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/examples/npm_package/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d/
-r-xr-xr-x 0 0 0 387 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d/index.js
-r-xr-xr-x 0 0 0 164 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d/package.json
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_/
-r-xr-xr-x 0 0 0 23981 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_/bin
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_node_bin/
-r-xr-xr-x 0 0 0 133 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_node_bin/node
-r-xr-xr-x 0 0 0 20 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/main.js
17 changes: 17 additions & 0 deletions js/private/test/image/regex_edge_cases_test_node.bzlmod.listing
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/
-r-xr-xr-x 0 0 0 32555 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
-r-xr-xr-x 0 0 0 1702 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/nodejs/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/nodejs/bin/
-r-xr-xr-x 0 0 0 80316256 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/nodejs/bin/node
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/examples/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/examples/npm_package/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d/node_modules/
lrwxrwxr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d/node_modules/acorn -> ../../../../../node_modules/.aspect_rules_js/acorn@8.7.1/node_modules/acorn
lrwxrwxr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d/node_modules/uuid -> ../../../../../node_modules/.aspect_rules_js/uuid@8.3.2/node_modules/uuid
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/node_modules/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/node_modules/@mycorp/
lrwxrwxr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/node_modules/@mycorp/pkg-d -> ../../../../../../node_modules/.aspect_rules_js/@mycorp+pkg-d@0.0.0/node_modules/@mycorp/pkg-d
lrwxrwxr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/node_modules/acorn -> ../../../../../node_modules/.aspect_rules_js/acorn@8.12.1/node_modules/acorn
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/node_modules/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/node_modules/.aspect_rules_js/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/node_modules/.aspect_rules_js/@mycorp+pkg-d@0.0.0/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/node_modules/.aspect_rules_js/@mycorp+pkg-d@0.0.0/node_modules/
drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/node_modules/.aspect_rules_js/@mycorp+pkg-d@0.0.0/node_modules/@mycorp/
lrwxrwxr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/node_modules/.aspect_rules_js/@mycorp+pkg-d@0.0.0/node_modules/@mycorp/pkg-d -> ../../../../../examples/npm_package/packages/pkg_d
Loading
0