Skip to content

prelude: Exclude archive members by name, not as tar globs - #1666

Closed
phr4nky wants to merge 2 commits into
meta:mainfrom
phr4nky:fix-http-archive-exclude-globs
Closed

phr4nky wants to merge 2 commits into
meta:mainfrom
phr4nky:fix-http-archive-exclude-globs

Conversation

@phr4nky

@phr4nky phr4nky commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

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
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).

Test plan

  • tests/e2e/test_prelude_rules.rs: test_excluded_names_are_read_by_tar_as_globs pinned the old behavior ([id].js kept, i.js and d.js dropped). It is flipped to test_excluded_names_are_matched_literally, which expects d.js, i.js and keep.js; that is what the linux-e2e-tests job reported for this change.
  • 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.

`excludes` of `http_archive` and `extract_archive` matches its regexes
against `tar --list` and hands the matched member names to tar through
`--exclude-from`. tar reads each line of that file as a glob, so a name
with `[`, `*` or `?` excluded other members instead of itself: excluding
`pages/[id].js` kept it and dropped `pages/d.js`, with tar exiting 0.

Escape the glob characters of each name, which GNU tar and bsdtar both
read as the literal character.
@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 Nov 27, 2026
@meta-codesync

meta-codesync Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

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

`test_excluded_names_are_read_by_tar_as_globs` pinned the old behavior:
excluding `pages/[id].js` kept it and dropped `i.js` and `d.js`. With the
names escaped, the excluded file is the one left out. The new
`tests/prelude` test covered the same case and goes.

@dtolnay dtolnay left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

meta-codesync Bot pushed a commit to meta/buck2-prelude 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 00c2ee4f74. The exclusion list has been written this way since 8af857afa1 ("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).

X-link: meta/buck2#1666

Reviewed By: dtolnay

Differential Revision: D134821702

fbshipit-source-id: 18c7d4ea2335fec790a18a7f1f43ff4d24762b82
@meta-codesync meta-codesync Bot closed this in 9ffc7d6 Nov 28, 2026
@meta-codesync meta-codesync Bot added the Merged label Nov 28, 2026
@meta-codesync

meta-codesync Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in 9ffc7d6.

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. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants