bazel-contrib / bazel-lib

Common useful functions for writing BUILD files and Starlark macros/rules
Apache License 2.0
141 stars 91 forks source link

[Bug] `mtree_mutate` incorrectly strips root directory prefixes #978

Open j-eid opened 1 month ago

j-eid commented 1 month ago

It looks like #852 causes issues with strip_prefix in mtree_mutate when stripping the root directory.

https://github.com/bazel-contrib/bazel-lib/blob/250c835b5f39f239de3ae2d97b3273d4c7390c5b/lib/private/modify_mtree.awk#L17-L20

This causes a / to be added to the first keyword in the mtree specification.

Steps to reproduce

# BUILD

genrule(
    name = "test.mf",
    outs = ["test.mf.spec"],
    cmd = "printf 'root_dir/ uid=0 gid=0 time=0 mode=0755 type=dir\nroot_dir/child_dir/ uid=0 gid=0 time=0 mode=0755 type=dir' >$@",
)

mtree_mutate(
    name = "test_mutate.mf",
    mtree = ":test.mf",
    strip_prefix = "root_dir",
)

Result:

# test_mutate.mf.spec

uid=0/ gid=0 time=0 mode=0755 type=dir
child_dir/ uid=0 gid=0 time=0 mode=0755 type=dir

The entry should probably be deleted unless package_dir is provided. I'm not very familiar with awk syntax, but another check for whether package_dir is provided and a more complex check on L18 would probably fix this.