Skip to content

prelude: Unescape # and $ in makefile dep files - #1657

Closed
phr4nky wants to merge 1 commit into
meta:mainfrom
phr4nky:fix-makefile-dep-escapes
Closed

phr4nky wants to merge 1 commit into
meta:mainfrom
phr4nky:fix-makefile-dep-escapes

Conversation

@phr4nky

@phr4nky phr4nky commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Summary

In the makefile dep tracking mode (the default cpp_dep_tracking_mode, and the mode for gcc), makefile_to_dep_file.py only unescapes \ . clang and gcc also write # as \# and $ as $$, so a header named "ha#sh.h" ends up in the dep file as "ha/#sh.h" and "dol$lar.h" as "dol$$lar.h". Neither names an input of the action, so Buck2 drops the header, and edits to it do not invalidate the cached compile.

Affected version

origin/main 4b47e14 (checked) and release 2026-11-15 (5b3b13d, same code).

Reproduction

printf 'int a;\n' > 'ha#sh.h'
printf 'int b;\n' > 'dol$lar.h'
printf '#include "ha#sh.h"\n#include "dol$lar.h"\nint main(){return 0;}\n' > m.c
python3 prelude/cxx/tools/dep_file_processor.py makefile m.d dep.txt clang -c m.c -o m.o -MD -MF m.d
cat m.d dep.txt

Expected

dep.txt lists m.c, ha#sh.h and dol$lar.h: make reads \# as # and $$ as $, and the show_headers mode reports the same compile with these names.

Actual

m.o: m.c ha\#sh.h dol$$lar.h
m.c
ha/#sh.h
dol$$lar.h

(m.d shortened to the project headers.)

Analysis (optional)

rewrite_dep_file in prelude/cxx/tools/makefile_to_dep_file.py handles a backslash only in front of a space; normalize_and_write_deps in prelude/cxx/tools/dep_file_utils.py then turns the backslash left in ha\#sh.h into /. $$ is passed through unchanged.

Patch (optional)

Parse the rule as make reads it: 2N+1 backslashes before a space are N backslashes and a space, \# is #, $$ is $, and any other backslash stays part of the path. Test Plan: new MakefileTest cases in prelude/cxx/tools/tests/test_dep_file_processor.py, using what clang 21 writes for these names (both fail before the change); the reproduction above now writes m.c, ha#sh.h, dol$lar.h; names with escaped spaces (sp ace.h, sub dir/n#s.h) still come out unchanged. gcc was not available here; its -MD output uses the same escapes.

@meta-cla

meta-cla Bot commented Nov 25, 2026

Copy link
Copy Markdown

Hi @phr4nky!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@phr4nky
phr4nky force-pushed the fix-makefile-dep-escapes branch from 9def4e6 to ae19669 Compare November 25, 2026 21:48
@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 25, 2026
@meta-codesync

meta-codesync Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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

@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!

@dtolnay

dtolnay commented Nov 28, 2026

Copy link
Copy Markdown

Needs rebase to resolve conflict in prelude/cxx/tools/tests/test_dep_file_processor.py.

clang and gcc write `#` as `\#` and `$` as `$$` in the make rules of
`-MD`, besides `\ ` for a space. makefile_to_dep_file.py only removed
the backslash in front of a space, and dep_file_utils.py then turned the
remaining backslash into `/`, so a header such as `ha#sh.h` or
`dol$lar.h` was listed as `ha/#sh.h` or `dol$$lar.h`. Buck2 keeps only
the tagged inputs a dep file names, so edits to such a header did not
invalidate the cached compile.

Parse the rule the way make reads it: 2N+1 backslashes before a space are
N backslashes and a space, `\#` is `#` and `$$` is `$`. Any other
backslash stays part of the path.
@phr4nky
phr4nky force-pushed the fix-makefile-dep-escapes branch from ae19669 to 2ce0951 Compare November 28, 2026 07:44
@phr4nky

phr4nky commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

@dtolnay many thanks - it's rebased and merge conflicted solved.

meta-codesync Bot pushed a commit to meta/buck2-prelude that referenced this pull request Nov 28, 2026
Summary:
`system_cxx_toolchain` (and `cxx_tools_info_toolchain`) with `compiler_type = "clang"` tracks C/C++ dependencies with the `show_headers` mode, `clang -H`. clang does not print the headers it reads through `-include` or anything they include, and it does not print the files of `#embed`. The rules pass `prefix_header`, and a `precompiled_header` that is not compiled, as `-include <header>` tagged with the headers tag. The dep file leaves these headers out, so Buck2 treats them as unused, and an edit to one of them is a dep file cache hit that keeps the old object.

This PR switches `clang` to the `makefile` mode, as gcc already uses: `clang -MD` lists all of them.

## Affected version

origin/main 00c2ee4f74. The mapping in `prelude/toolchains/cxx.bzl` has been in place since b179908fbd (meta/buck2#1005).

## Reproduction

```
mkdir inc
printf '#pragma once\nint inner;\n' > inc/inner.h
printf '#pragma once\n#include "inner.h"\nint p;\n' > inc/prefix.h
printf '#pragma once\nint q;\n' > inc/normal.h
printf '#include "normal.h"\nint main(){return q+p+inner;}\n' > b.c
python3 prelude/cxx/tools/dep_file_processor.py show_headers b.c dep.txt clang -Iinc -include inc/prefix.h -H -fno-color-diagnostics -c b.c -o b.o
cat dep.txt
```

Through Buck2, with the e2e fixture of this PR and the prelude of origin/main: build `root//:lib[objects][r.c.o]`, edit `pi.h` (included by the prefix header `p.h`), and build again.

## Expected

The dep file lists `inc/prefix.h` and `inc/inner.h`, as `dep_file_processor.py makefile` does for the same compile with `-MD`. The second build compiles again.

## Actual

```
inc/normal.h
b.c
```

`buck2 audit dep-files root//:lib c_compile r.c` lists neither `p.h` nor `pi.h`, and `buck2 log what-ran` shows the second build as `LocalDepFileCache`. With this PR, both headers are listed and the compile runs again (`Local`).

## Notes

- `clang_cl` and `clang_windows` keep their modes. meta/buck2#1656 changes the `clang_cl` line of the same block, so whichever lands second needs a trivial rebase.
- With `makefile` mode, clang compiles also pick up the `\#` / `$$` unescaping of meta/buck2#1657.

X-link: meta/buck2#1665

Reviewed By: dtolnay

Differential Revision: D134817891

fbshipit-source-id: 7a5415dc4ca4385c3291fb61d0d7e48921610f9d
meta-codesync Bot pushed a commit that referenced this pull request Nov 28, 2026
Summary:
`system_cxx_toolchain` (and `cxx_tools_info_toolchain`) with `compiler_type = "clang"` tracks C/C++ dependencies with the `show_headers` mode, `clang -H`. clang does not print the headers it reads through `-include` or anything they include, and it does not print the files of `#embed`. The rules pass `prefix_header`, and a `precompiled_header` that is not compiled, as `-include <header>` tagged with the headers tag. The dep file leaves these headers out, so Buck2 treats them as unused, and an edit to one of them is a dep file cache hit that keeps the old object.

This PR switches `clang` to the `makefile` mode, as gcc already uses: `clang -MD` lists all of them.

## Affected version

origin/main 00c2ee4. The mapping in `prelude/toolchains/cxx.bzl` has been in place since b179908 (#1005).

## Reproduction

```
mkdir inc
printf '#pragma once\nint inner;\n' > inc/inner.h
printf '#pragma once\n#include "inner.h"\nint p;\n' > inc/prefix.h
printf '#pragma once\nint q;\n' > inc/normal.h
printf '#include "normal.h"\nint main(){return q+p+inner;}\n' > b.c
python3 prelude/cxx/tools/dep_file_processor.py show_headers b.c dep.txt clang -Iinc -include inc/prefix.h -H -fno-color-diagnostics -c b.c -o b.o
cat dep.txt
```

Through Buck2, with the e2e fixture of this PR and the prelude of origin/main: build `root//:lib[objects][r.c.o]`, edit `pi.h` (included by the prefix header `p.h`), and build again.

## Expected

The dep file lists `inc/prefix.h` and `inc/inner.h`, as `dep_file_processor.py makefile` does for the same compile with `-MD`. The second build compiles again.

## Actual

```
inc/normal.h
b.c
```

`buck2 audit dep-files root//:lib c_compile r.c` lists neither `p.h` nor `pi.h`, and `buck2 log what-ran` shows the second build as `LocalDepFileCache`. With this PR, both headers are listed and the compile runs again (`Local`).

## Notes

- `clang_cl` and `clang_windows` keep their modes. #1656 changes the `clang_cl` line of the same block, so whichever lands second needs a trivial rebase.
- With `makefile` mode, clang compiles also pick up the `\#` / `$$` unescaping of #1657.

Pull Request resolved: #1665

Test Plan:
- `tests/e2e/test_prelude_tools.rs`: the fixture library gets `prefix_header = "p.h"`, which includes `pi.h`. `test_dep_file_keeps_the_headers_found_next_to_the_source` expects both in the dep file. The new `test_edit_to_a_header_of_the_prefix_header_rebuilds_the_object` edits only `pi.h` and expects a `Local` compile.
- Checked manually on macOS (the e2e test is skipped there) with a locally built buck2 against the fixture: origin/main gives `LocalDepFileCache`, this branch gives `Local`.

Reviewed By: dtolnay

Differential Revision: D134817891

fbshipit-source-id: 7a5415dc4ca4385c3291fb61d0d7e48921610f9d
@meta-codesync meta-codesync Bot closed this in 6be3099 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 6be3099.

meta-codesync Bot pushed a commit to meta/buck2-prelude that referenced this pull request Nov 28, 2026
Summary:
In the `makefile` dep tracking mode (the default `cpp_dep_tracking_mode`, and the mode for gcc), `makefile_to_dep_file.py` only unescapes `\ `. clang and gcc also write `#` as `\#` and `$` as `$$`, so a header named "ha#sh.h" ends up in the dep file as "ha/#sh.h" and "dol$lar.h" as "dol$$lar.h". Neither names an input of the action, so Buck2 drops the header, and edits to it do not invalidate the cached compile.

## Affected version

origin/main 4b47e1430e (checked) and release 2026-11-15 (5b3b13d3ea, same code).

## Reproduction

```
printf 'int a;\n' > 'ha#sh.h'
printf 'int b;\n' > 'dol$lar.h'
printf '#include "ha#sh.h"\n#include "dol$lar.h"\nint main(){return 0;}\n' > m.c
python3 prelude/cxx/tools/dep_file_processor.py makefile m.d dep.txt clang -c m.c -o m.o -MD -MF m.d
cat m.d dep.txt
```

## Expected

`dep.txt` lists `m.c`, `ha#sh.h` and `dol$lar.h`: make reads `\#` as `#` and `$$` as `$`, and the `show_headers` mode reports the same compile with these names.

## Actual

```
m.o: m.c ha\#sh.h dol$$lar.h
m.c
ha/#sh.h
dol$$lar.h
```

(`m.d` shortened to the project headers.)

## Analysis (optional)

`rewrite_dep_file` in `prelude/cxx/tools/makefile_to_dep_file.py` handles a backslash only in front of a space; `normalize_and_write_deps` in `prelude/cxx/tools/dep_file_utils.py` then turns the backslash left in `ha\#sh.h` into `/`. `$$` is passed through unchanged.

## Patch (optional)

Parse the rule as make reads it: 2N+1 backslashes before a space are N backslashes and a space, `\#` is `#`, `$$` is `$`, and any other backslash stays part of the path. Test Plan: new `MakefileTest` cases in `prelude/cxx/tools/tests/test_dep_file_processor.py`, using what clang 21 writes for these names (both fail before the change); the reproduction above now writes `m.c`, `ha#sh.h`, `dol$lar.h`; names with escaped spaces (`sp ace.h`, `sub dir/n#s.h`) still come out unchanged. gcc was not available here; its `-MD` output uses the same escapes.

X-link: meta/buck2#1657

Reviewed By: dtolnay

Differential Revision: D134594337

fbshipit-source-id: 165b2d91cf79ab825cf66cf5272524fba10afdd8
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