Skip to content

fix: http_archive flags excludes and strip_prefix didn't mix - #1512

Open
lf- wants to merge 1 commit into
meta:mainfrom
lf-:jade/push-nqtzotyxpsoz
Open

lf- wants to merge 1 commit into
meta:mainfrom
lf-:jade/push-nqtzotyxpsoz

Conversation

@lf-

@lf- lf- commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

strip_prefix is handed to tar as a member pattern, which is positional.
bsdtar -- macOS tar and Windows tar.exe -- stops parsing options at the
first positional, so the --exclude-from= that unarchive.bzl appended after
it was read as a second member pattern rather than a flag. GNU tar
tolerates these being in either order so nobody noticed.

Split the strip-prefix handling into the --strip-components=N flag and the
member pattern, and emit the member pattern at the very end of the
command line.

The regression test packs a fixture tarball with a genrule and extracts it with
both attributes set, then verifies the files exist when extracted.
The test in question is totally vibed but my claude claims it tried
running it.

Concretely: create the tar from the test here, then:

gnu tar:
$ tar xf foo.tar.gz wrapper/nested --strip-components=1
(success)

bsd tar:
$ tar xf foo.tar.gz wrapper/nested --strip-components=1
tar: --strip-components=1: Not found in archive
tar: Error exit delayed from previous errors.

Assisted-By: Claude Opus 5

`strip_prefix` is handed to tar as a member pattern, which is positional.
bsdtar -- macOS `tar` and Windows `tar.exe` -- stops parsing options at the
first positional, so the `--exclude-from=` that `unarchive.bzl` appended after
it was read as a second member pattern rather than a flag. GNU tar
tolerates these being in either order so nobody noticed.

Split the strip-prefix handling into the `--strip-components=N` flag and the
member pattern, and emit the member pattern at the very end of the
command line.

The regression test packs a fixture tarball with a genrule and extracts it with
both attributes set, then verifies the files exist when extracted.
The test in question is totally vibed but my claude claims it tried
running it.

Concretely: create the tar from the test here, then:

gnu tar:
$ tar xf foo.tar.gz wrapper/nested --strip-components=1
(success)

bsd tar:
$ tar xf foo.tar.gz wrapper/nested --strip-components=1
tar: --strip-components=1: Not found in archive
tar: Error exit delayed from previous errors.

Assisted-By: Claude Opus 5
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 19, 2026
@lf- lf- changed the title Order tar's member pattern after its flags in http_archive fix: http_archive flags excludes and strip_prefix didn't mix Sep 19, 2026
@meta-codesync

meta-codesync Bot commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D120789614. (Because this pull request was imported automatically, there will not be any future comments.)

meta-codesync Bot pushed a commit that referenced this pull request Nov 28, 2026
Summary:
`excludes` of `http_archive` and `extract_archive` matches its regexes against `tar --list`, then writes the matched member names to a file that tar reads with `--exclude-from`. tar reads each line of that file as a glob pattern, not as a name. A member whose name contains `[`, `*` or `?` is not excluded, and other members that the pattern happens to match are dropped instead. tar exits 0, so the build succeeds with the wrong set of files.

Names like that are common in web projects: Next.js, SvelteKit and Astro name route files `[id].js` or `[...slug].tsx`.

This PR escapes `\`, `*`, `?` and `[` in each name it writes to the list. GNU tar and bsdtar both read an escaped character literally.

## Affected version

origin/main 00c2ee4. The exclusion list has been written this way since 8af857a ("http_archive: reimplement exclusion lists using dynamic_output").

## Reproduction

```
mkdir -p pages && for f in '[id].js' d.js index.js; do echo x > "pages/$f"; done
tar -czf archive.tar.gz pages
```

```python
extract_archive(
    name = "extracted",
    src = "archive.tar.gz",
    excludes = ["^pages/\\[id\\]\\.js$"],
)
```

`buck2 build :extracted --show-full-simple-output`, then list the output directory.

## Expected

`pages/d.js` and `pages/index.js`.

## Actual

`pages/[id].js` and `pages/index.js`. The exclusion list holds `pages/[id].js`, and tar reads `[id]` as the character class "i or d". Same result with GNU tar 1.35 (Debian) and bsdtar 3.5.3 (macOS).

Pull Request resolved: #1666

Test Plan:
- New `tests/prelude/http_archive/excludes_with_glob_characters`: archives `pages/{[id].js,d.js,index.js}`, extracts with the exclude above, and asserts the result is exactly `pages/d.js` and `pages/index.js`.
- Checked with a locally built buck2 on macOS. origin/main extracts `[id].js` and `index.js`; this branch extracts `d.js` and `index.js`. A plain exclude (`"index"`) gives the same result as before.
- GNU tar 1.35 with the escaped list extracts exactly `d.js` and `index.js`.

This touches the same file as #1512 (`excludes` with `strip_prefix` on bsdtar) but different lines, and fixes a separate problem.

Reviewed By: dtolnay

Differential Revision: D134821702

fbshipit-source-id: 18c7d4ea2335fec790a18a7f1f43ff4d24762b82

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant