Add strip_components to extract/download_and_extract `http_arch…#29281
Add strip_components to extract/download_and_extract `http_arch…#29281willstranton wants to merge 3 commits intobazelbuild:masterfrom
strip_components to extract/download_and_extract `http_arch…#29281Conversation
da36a7a to
613bd88
Compare
If the source archive URL is deterministic, the exact prefix should be known? |
Yes, that's true, but it's inconvenient to have to examine an archive to determine that exact prefix. This pull request is a "quality of life" improvement. As you point out, it's not a "must have". Summarizing from the community:
I remember having to update dependencies manually before BCR. You had to update the tar archive AND the prefix that was stripped.
|
|
OK, thanks for the context! If we do this, we should also backport this to Bazel 8 & 9, so that modules can keep the compatibility with multiple LTS releases when using this feature. |
There was a problem hiding this comment.
Pull request overview
Adds a new strip_components integer attribute/parameter (similar to tar --strip-components) to http_archive, repository_ctx.download_and_extract, and repository_ctx.extract, enabling prefix stripping without knowing an exact directory name.
Changes:
- Introduces
strip_componentsplumbing from Starlark (http_archive,download_and_extract,extract) down to the Java decompressor layer. - Implements component stripping during extraction for
.zip,.7z, and tar-based archives. - Adds/updates integration + unit tests covering component stripping and rename-ordering behavior.
Reviewed changes
Copilot reviewed 14 out of 15 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/build_defs/repo/http.bzl | Adds strip_components attr, enforces mutual exclusivity with strip_prefix, passes through to download_and_extract. |
| src/main/java/com/google/devtools/build/lib/vfs/PathFragment.java | Adds PathFragment.stripComponents(int) utility used by decompressors. |
| src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkBaseExternalContext.java | Adds strip_components params to download_and_extract/extract and wires into decompression. |
| src/main/java/com/google/devtools/build/lib/bazel/repository/decompressor/DecompressorDescriptor.java | Adds stripComponents field + builder validation for mutual exclusivity with prefix. |
| src/main/java/com/google/devtools/build/lib/bazel/repository/decompressor/ZipDecompressor.java | Applies component stripping to zip entry paths before extraction. |
| src/main/java/com/google/devtools/build/lib/bazel/repository/decompressor/SevenZDecompressor.java | Applies component stripping to 7z entry paths before extraction. |
| src/main/java/com/google/devtools/build/lib/bazel/repository/decompressor/CompressedTarFunction.java | Applies component stripping to tar entry paths before extraction. |
| src/main/java/com/google/devtools/build/lib/bazel/repository/decompressor/CompressedFunction.java | Updates docs to note stripComponents is ignored for single-file compressor formats. |
| src/test/shell/bazel/external_integration_test.sh | Adds http_archive integration coverage for strip_components (tar/zip + add_prefix). |
| src/test/java/com/google/devtools/build/lib/vfs/PathFragmentTest.java | Adds unit tests for PathFragment.stripComponents. |
| src/test/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkBaseExternalContextTest.java | Updates test calls for new downloadAndExtract signature. |
| src/test/java/com/google/devtools/build/lib/bazel/repository/decompressor/ZipDecompressorTest.java | Adds zip decompression tests for strip_components (+ rename ordering). |
| src/test/java/com/google/devtools/build/lib/bazel/repository/decompressor/SevenZDecompressorTest.java | Adds 7z decompression tests for strip_components (+ rename ordering + strip-all). |
| src/test/java/com/google/devtools/build/lib/bazel/repository/decompressor/CompressedTarFunctionTest.java | Adds tar.gz decompression tests for strip_components (+ rename ordering). |
| src/test/tools/bzlmod/MODULE.bazel.lock | Updates lockfile digests due to test/module changes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Thanks, please run |
|
Let me know this is fixed, I will add the import label |
…ive` The `strip_components` attribute functions similar to tar --strip-components: > Strip NUMBER leading components from file names on extraction. This is an alternative to the existing `strip_prefix` attribute, which required knowing the exact prefix to be stripped. Only one of the two attributes (`strip_prefix`, `strip_components`) can be set at one time. Fixes bazelbuild#28879 RELNOTES[NEW]: Adds the `strip_components` attribute to `extract`/`download_and_extract`/`http_archive` to allow stripping of path components when extracting files.
CI now passing. |
Add
strip_componentstoextract/download_and_extracthttp_archiveDescription
The
strip_componentsattribute functions similar totar --strip-components:This is an alternative to the existing
strip_prefixattribute, which required knowing the exact prefix to be stripped. Only one of the two attributes (strip_prefix,strip_components) can be set at one time.Motivation
See #28879
Build API Changes
See #28879
Yes
N/A - this is not a breaking change.
Checklist
Release Notes
RELNOTES[NEW]: Adds the
strip_componentsattribute toextract/download_and_extract/http_archiveto allow stripping of path components when extracting files.