Skip to content

Commit 87c8b09

Browse files
inazarenkocopybara-github
authored andcommitted
Allow adding prefix path when unpacking HTTP repos.
Motivation: #16136 Closes #16137. PiperOrigin-RevId: 469410562 Change-Id: Iab57783de006604bab9dde268cd2225310f2dab6
1 parent 2cfc4bd commit 87c8b09

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/test/shell/bazel/external_integration_test.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,69 @@ EOF
752752
assert_contains "abc" bazel-genfiles/external/x/catter.out
753753
}
754754

755+
function test_adding_prefix_zip() {
756+
mkdir -p z
757+
echo "abc" > z/w
758+
zip -r z z
759+
local sha256=$(sha256sum z.zip | cut -f 1 -d ' ')
760+
serve_file z.zip
761+
762+
cat >> $(create_workspace_with_default_repos WORKSPACE) <<EOF
763+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
764+
http_archive(
765+
name = "ws",
766+
url = "http://127.0.0.1:$nc_port/z.zip",
767+
sha256 = "$sha256",
768+
add_prefix = "x/y",
769+
build_file = "@//:ws.BUILD",
770+
)
771+
EOF
772+
cat > ws.BUILD <<EOF
773+
genrule(
774+
name = "catter",
775+
cmd = "cat \$< > \$@",
776+
outs = ["catter.out"],
777+
srcs = ["x/y/z/w"],
778+
)
779+
EOF
780+
touch BUILD
781+
782+
bazel build @ws//:catter &> $TEST_log || fail "Build failed"
783+
assert_contains "abc" bazel-genfiles/external/ws/catter.out
784+
}
785+
786+
function test_adding_and_stripping_prefix_zip() {
787+
mkdir -p z
788+
echo "abc" > z/w
789+
zip -r z z
790+
local sha256=$(sha256sum z.zip | cut -f 1 -d ' ')
791+
serve_file z.zip
792+
793+
cat >> $(create_workspace_with_default_repos WORKSPACE) <<EOF
794+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
795+
http_archive(
796+
name = "ws",
797+
url = "http://127.0.0.1:$nc_port/z.zip",
798+
sha256 = "$sha256",
799+
strip_prefix = "z",
800+
add_prefix = "x",
801+
build_file = "@//:ws.BUILD",
802+
)
803+
EOF
804+
cat > ws.BUILD <<EOF
805+
genrule(
806+
name = "catter",
807+
cmd = "cat \$< > \$@",
808+
outs = ["catter.out"],
809+
srcs = ["x/w"],
810+
)
811+
EOF
812+
touch BUILD
813+
814+
bazel build @ws//:catter &> $TEST_log || fail "Build failed"
815+
assert_contains "abc" bazel-genfiles/external/ws/catter.out
816+
}
817+
755818
function test_moving_build_file() {
756819
echo "abc" > w
757820
tar czf x.tar.gz w

tools/build_defs/repo/http.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def _http_archive_impl(ctx):
129129

130130
download_info = ctx.download_and_extract(
131131
all_urls,
132-
"",
132+
ctx.attr.add_prefix,
133133
ctx.attr.sha256,
134134
ctx.attr.type,
135135
ctx.attr.strip_prefix,
@@ -270,6 +270,15 @@ discarded and inaccessible (e.g., a top-level license file). This includes
270270
files/directories that start with the prefix but are not in the directory
271271
(e.g., `foo-lib-1.2.3.release-notes`). If the specified prefix does not
272272
match a directory in the archive, Bazel will return an error.""",
273+
),
274+
"add_prefix": attr.string(
275+
default = "",
276+
doc = """Destination directory relative to the repository directory.
277+
278+
The archive will be unpacked into this directory, after applying `strip_prefix`
279+
(if any) to the file paths within the archive. For example, file
280+
`foo-1.2.3/src/foo.h` will be unpacked to `bar/src/foo.h` if `add_prefix = "bar"`
281+
and `strip_prefix = "foo-1.2.3"`.""",
273282
),
274283
"type": attr.string(
275284
doc = """The archive type of the downloaded file.

0 commit comments

Comments
 (0)