Skip to content

Fix build_with_debinfo.py broken by CONFIGURE_DEPENDS globbing - #186780

Closed
zklaus wants to merge 3 commits into
pytorch:mainfrom
zklaus:fix-build-with-debinfo
Closed

zklaus wants to merge 3 commits into
pytorch:mainfrom
zklaus:fix-build-with-debinfo

Conversation

@zklaus

@zklaus zklaus commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

Summary

tools/build_with_debinfo.py recompiles named source files with -g so you
can get debug info for a targeted file without a full (slow, ABI-incompatible)
debug build. It is referenced in CONTRIBUTING.md, and it broke on current
main: running it just prints Nothing to do and rebuilds nothing.

Root cause

The tool derived its compile/link commands from ninja -j1 -v -n torch_python
(a dry run) and rewrote -O2/-O3 to -g. The build now uses
file(GLOB ... CONFIGURE_DEPENDS), which wires a glob-verification step into
build.ninja's own regeneration (CMakeFiles/VerifyGlobs.cmake +
regenerate-during-build). In dry-run (-n) mode ninja cannot run that check
or reload the regenerated graph, so ninja -n <target> only ever reports the
regeneration step and never the real compile/link commands. The tool parsed an
empty plan and no-op'd.

Fix

Source the commands from places the glob-check does not gate:

  • the per-file compile command from build/compile_commands.json (rewrite
    -O2/-O3 -> -g);
  • the libtorch_python link command from ninja -t commands torch_python
    (a graph walk, not a dry run; strip ninja's : && ... && : wrapper).

Then symlink build/lib into torch/lib as before. The tool now rebuilds
exactly the files you name plus the relink, rather than touching sources and
relying on ninja's staleness detection. The old >100 stale items guard was a
dry-run artifact and is removed; the tool now requires compile_commands.json
and an already-built tree.

Tests

  • Unit tests (tools/test/test_build_with_debinfo.py) for the pure
    plan-derivation helpers: the -O -> -g rewrite, compile_commands.json
    indexing, the command/arguments entry forms, and the ninja -t commands
    link extraction.
  • Build-tree smoke check: a new --dry-run mode plus a step in
    .ci/pytorch/build.sh that, after the Linux build, asserts the emitted plan
    contains a -g compile and the libtorch_python relink. This exercises the
    real compile_commands.json and ninja -t commands and fails loudly if a
    future build-system change breaks those data sources -- the class of bug
    here, which unit tests alone would not catch. --dry-run only reads the
    tree, so it does not dirty the checkout.

Test plan

Validated on a real editable CUDA build of viable/strict:

python tools/build_with_debinfo.py --verbose torch/csrc/Module.cpp

recompiles Module.cpp.o with .debug_info, relinks libtorch_python.so
(which then carries .debug_info), symlinks it into torch/lib, and
import torch still works.

python tools/build_with_debinfo.py --dry-run torch/csrc/Module.cpp

exits 0, prints the plan, and leaves the git tree clean; the build.sh -g and
libtorch_python plan assertions both pass against that output.

python -m pytest tools/test/test_build_with_debinfo.py

-> 9 passed.

🤖 Generated with Claude Code

zklaus added 2 commits June 9, 2026 13:33
tools/build_with_debinfo.py recompiles named source files with `-g` to give a
targeted file debug info without a full (slow, ABI-incompatible) debug build.
It derived the compile/link commands from `ninja -j1 -v -n torch_python`
(a dry run) and rewrote `-O2`/`-O3` to `-g`.

That stopped working once the build adopted `file(GLOB ... CONFIGURE_DEPENDS)`.
CONFIGURE_DEPENDS wires a glob-verification step into build.ninja's own
regeneration (CMakeFiles/VerifyGlobs.cmake + regenerate-during-build). In
dry-run mode ninja cannot run that check or reload the regenerated graph, so
`ninja -n <target>` only ever reports the regeneration step and never the real
compile/link commands. The tool parsed an empty plan and printed
"Nothing to do", silently rebuilding nothing.

Source the commands from places the glob-check does not gate:
- the per-file compile command from build/compile_commands.json (rewrite
  `-O2`/`-O3` -> `-g`, run in the entry's directory);
- the libtorch_python link command from `ninja -t commands torch_python`
  (a graph walk, not a dry run; strip ninja's `: && ... && :` wrapper).
Then symlink build/lib into torch/lib as before.

The tool now rebuilds exactly the files you name plus the relink, instead of
touching sources and relying on ninja's staleness detection. The old
">100 stale items" guard was an artifact of the dry-run approach and is
removed; the tool now requires compile_commands.json and an already-built tree.

Test Plan:
Reproduced on a fresh editable CUDA build of viable/strict: the old tool
no-ops ("Nothing to do"); the new one works.

```
python tools/build_with_debinfo.py --verbose torch/csrc/Module.cpp
```

- torch_python.dir/csrc/Module.cpp.o is recompiled with .debug_info
  (was -O2, none before).
- lib/libtorch_python.so is relinked and carries .debug_info.
- torch/lib/libtorch_python.so becomes a symlink into build/lib.
- `import torch` still works (2.13.0a0, cuda True).

Authored with assistance from Claude.
Add a regression guard for the tool in two tiers:

- Unit tests (tools/test/test_build_with_debinfo.py) cover the pure
  plan-derivation helpers: the -O2/-O3 -> -g rewrite, compile_commands.json
  indexing, the command/arguments entry forms, and the `ninja -t commands`
  link extraction (including the `: && ... && :` unwrap). They need no build
  and run on the existing tools test job.

- A build-tree smoke check. build_with_debinfo.py gains a --dry-run mode that
  prints the rebuild plan without compiling/relinking (and skips the
  is_devel_setup check, since CI build jobs install from a wheel rather than
  editable). .ci/pytorch/build.sh runs `--dry-run torch/csrc/Module.cpp`
  after the Linux build, guarded to configs that actually built Module.cpp
  and have ninja, and asserts the emitted plan contains a -g debug compile
  and the libtorch_python relink -- not just a zero exit. This exercises the
  real compile_commands.json and `ninja -t commands` and fails loudly if a
  future build-system change breaks those data sources or the -g rewrite --
  the class of bug (CONFIGURE_DEPENDS) that broke the tool originally and that
  unit tests alone would not catch. --dry-run only reads the tree, so it does
  not dirty the checkout (assert_git_not_dirty).

Test Plan:

```
python -m pytest tools/test/test_build_with_debinfo.py
```
-> 9 passed.

On a real editable CUDA build of viable/strict:

```
python tools/build_with_debinfo.py --dry-run torch/csrc/Module.cpp
```
prints a compile command (with -g) plus the libtorch_python link command and
exits 0, leaving the git tree clean; the build.sh `-g` and libtorch_python
plan assertions both pass against that output.

Authored with assistance from Claude.
@pytorch-bot

pytorch-bot Bot commented Jun 9, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/186780

Note: Links to docs will display an error until the docs builds have been completed.

❌ 5 New Failures

As of commit 03c9084 with merge base 1d81f67 (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@zklaus
zklaus requested a review from benjaminglass1 June 9, 2026 11:40
@zklaus
zklaus marked this pull request as ready for review June 9, 2026 12:29
@zklaus
zklaus requested a review from a team as a code owner June 9, 2026 12:29
@atalman atalman added this to the 2.13.0 milestone Jun 9, 2026
@malfet

malfet commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

@claude review

@claude

claude Bot commented Jun 9, 2026 •

Copy link
Copy Markdown

Claude finished @malfet's task in 2m 30s —— View job


Code Review: Fix build_with_debinfo.py

  • Read changed files and understand the implementation
  • Review tools/build_with_debinfo.py changes
  • Review tools/test/test_build_with_debinfo.py
  • Review .ci/pytorch/build.sh changes
  • Post review feedback

Overall: This is a well-written fix. The root cause analysis is clear, the new approach is sound, and the testing strategy covers both unit logic and integration against real build trees. A few observations below:

Strengths

  1. Correct root cause and fix — Sourcing commands from compile_commands.json + ninja -t commands instead of ninja -n is the right move. Both are unaffected by the CONFIGURE_DEPENDS glob-verification step.

  2. Good separation of testable logic — debugify, index_compile_commands, entry_command, and extract_link_command are all pure functions that are easy to unit test. The test file exercises the important cases cleanly.

  3. Defensive CI smoke test — The guard condition ([[ -f build/compile_commands.json ]] && command -v ninja > /dev/null && grep -q ...) ensures the check only runs when the prerequisite data exists, which avoids false failures in non-ninja CI environments.

  4. --dry-run is a valuable addition — Allows the CI integration without dirtying the build, and is useful for developers debugging the tool itself.

Minor Issues / Suggestions

  1. debugify uses naive string replacement (tools/build_with_debinfo.py:90): cmd.replace("-O2", "-g").replace("-O3", "-g") could match substrings like -O2 inside a path (e.g. -I/path/to/O2-build/). In practice this is extremely unlikely in a PyTorch build, but a more precise approach would be to operate on word boundaries or split on whitespace. Not a blocker — just noting it.

  2. extract_link_command takes the last match (tools/build_with_debinfo.py:126): The docstring explains this is intentional (the link is always last in ninja -t commands output for a target). This is correct for the current use case, but could silently pick the wrong line if ninja's output format changes. Consider adding a heuristic (e.g. checking for -shared or -dynamiclib) as an extra assertion in the future.

  3. No __init__.py in tools/test/: The test file imports from tools.build_with_debinfo import ... which requires tools to be importable as a package (it has __init__.py) but tools/test/ does not. This should work with pytest (which adds the file's directory to sys.path) but running via python -m pytest tools/test/test_build_with_debinfo.py from the repo root should also work since tools/__init__.py exists. Just noting this is consistent with other test files in the same directory (e.g., test_cmake.py).

  4. CI smoke test regex (.ci/pytorch/build.sh:292): grep -qE ' -g( |$)' is a good pattern — it won't false-match -gline-tables-only since that doesn't have the trailing space/EOL after just -g. Looks correct.

  5. Exit codes (tools/build_with_debinfo.py:152-163): The tool uses negative exit codes (-95, -1). On most Unix systems these wrap to 161 and 255 respectively. Consider using positive exit codes (1 or 2) for consistency, but this is pre-existing behavior, not introduced by this PR.

Potential concern

The tool now always calls ninja -t commands torch_python even in --dry-run mode, which requires a valid build.ninja with the torch_python target. The CI guard (command -v ninja + presence of compile_commands.json) covers this, but if someone has a build tree with compile_commands.json but a stale/missing build.ninja, they'll get a confusing subprocess error rather than a clear message. A minor robustness improvement would be to catch subprocess.CalledProcessError from torch_python_link_command() and emit a friendlier diagnostic, but this isn't critical.

Verdict

The PR is clean, well-tested, and solves a real breakage. The approach is well-justified in the commit messages and header comment. I'd approve this. The suggestions above are all minor/optional improvements that could be follow-ups.


@benjaminglass1 benjaminglass1 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@zklaus Only one minor bit of feedback, and also a quick question: does this handle builds with USE_RELATIVE_PATHS enabled?

Comment thread .ci/pytorch/build.sh Resolved

@atalman atalman left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

lgtm

Per benjaminglass1's review on pytorchgh-186780: drop the unnecessary `echo` pipe in
the .ci/pytorch/build.sh smoke check in favor of bash here-strings.

Authored with assistance from Claude.

Co-authored-by: Benjamin Glass <[email protected]>
@zklaus

zklaus commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator Author

@benjaminglass1, yes — USE_RELATIVE_PATHS only rewrites the #include inside the generated CPU-capability kernel copies (cmake/Codegen.cmake) to be relative; it doesn't touch compile_commands.json's directory/file fields or the per-file compile/link commands the tool replays, so the lookup-and-replay is unaffected. (The smoke-test target, Module.cpp, isn't a generated file anyway.)

@benjaminglass1

Copy link
Copy Markdown
Collaborator

@zklaus LGTM!

@zklaus

zklaus commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jun 10, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: This PR needs a release notes: label
If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Details for Dev Infra team Raised by workflow job

@zklaus

zklaus commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

jemitche1 pushed a commit to jemitche1/pytorch that referenced this pull request Jun 13, 2026
…ch#186780)

## Summary

`tools/build_with_debinfo.py` recompiles named source files with `-g` so you
can get debug info for a targeted file without a full (slow, ABI-incompatible)
debug build. It is referenced in `CONTRIBUTING.md`, and it broke on current
`main`: running it just prints `Nothing to do` and rebuilds nothing.

## Root cause

The tool derived its compile/link commands from `ninja -j1 -v -n torch_python`
(a dry run) and rewrote `-O2`/`-O3` to `-g`. The build now uses
`file(GLOB ... CONFIGURE_DEPENDS)`, which wires a glob-verification step into
`build.ninja`'s own regeneration (`CMakeFiles/VerifyGlobs.cmake` +
`regenerate-during-build`). In dry-run (`-n`) mode ninja cannot run that check
or reload the regenerated graph, so `ninja -n <target>` only ever reports the
regeneration step and never the real compile/link commands. The tool parsed an
empty plan and no-op'd.

## Fix

Source the commands from places the glob-check does not gate:

- the per-file compile command from `build/compile_commands.json` (rewrite
  `-O2`/`-O3` -> `-g`);
- the `libtorch_python` link command from `ninja -t commands torch_python`
  (a graph walk, not a dry run; strip ninja's `: && ... && :` wrapper).

Then symlink `build/lib` into `torch/lib` as before. The tool now rebuilds
exactly the files you name plus the relink, rather than touching sources and
relying on ninja's staleness detection. The old `>100 stale items` guard was a
dry-run artifact and is removed; the tool now requires `compile_commands.json`
and an already-built tree.

## Tests

- **Unit tests** (`tools/test/test_build_with_debinfo.py`) for the pure
  plan-derivation helpers: the `-O` -> `-g` rewrite, `compile_commands.json`
  indexing, the command/arguments entry forms, and the `ninja -t commands`
  link extraction.
- **Build-tree smoke check**: a new `--dry-run` mode plus a step in
  `.ci/pytorch/build.sh` that, after the Linux build, asserts the emitted plan
  contains a `-g` compile and the `libtorch_python` relink. This exercises the
  real `compile_commands.json` and `ninja -t commands` and fails loudly if a
  future build-system change breaks those data sources -- the class of bug
  here, which unit tests alone would not catch. `--dry-run` only reads the
  tree, so it does not dirty the checkout.

## Test plan

Validated on a real editable CUDA build of `viable/strict`:

```
python tools/build_with_debinfo.py --verbose torch/csrc/Module.cpp
```
recompiles `Module.cpp.o` with `.debug_info`, relinks `libtorch_python.so`
(which then carries `.debug_info`), symlinks it into `torch/lib`, and
`import torch` still works.

```
python tools/build_with_debinfo.py --dry-run torch/csrc/Module.cpp
```
exits 0, prints the plan, and leaves the git tree clean; the build.sh `-g` and
`libtorch_python` plan assertions both pass against that output.

```
python -m pytest tools/test/test_build_with_debinfo.py
```
-> 9 passed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Pull Request resolved: pytorch#186780
Approved by: https://github.com/benjaminglass1, https://github.com/atalman

Co-authored-by: Benjamin Glass <[email protected]>
@atalman

atalman commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot cherry-pick --onto release/2.13 -c critical

@pytorchbot

Copy link
Copy Markdown
Collaborator

Cherry picking #186780

The cherry pick PR is at #188192 and it is recommended to link a critical cherry pick PR with an issue. The following tracker issues are updated:

Details for Dev Infra team Raised by workflow job

atalman pushed a commit that referenced this pull request Jun 25, 2026
Fix build_with_debinfo.py broken by CONFIGURE_DEPENDS globbing (#186780)

## Summary

`tools/build_with_debinfo.py` recompiles named source files with `-g` so you
can get debug info for a targeted file without a full (slow, ABI-incompatible)
debug build. It is referenced in `CONTRIBUTING.md`, and it broke on current
`main`: running it just prints `Nothing to do` and rebuilds nothing.

## Root cause

The tool derived its compile/link commands from `ninja -j1 -v -n torch_python`
(a dry run) and rewrote `-O2`/`-O3` to `-g`. The build now uses
`file(GLOB ... CONFIGURE_DEPENDS)`, which wires a glob-verification step into
`build.ninja`'s own regeneration (`CMakeFiles/VerifyGlobs.cmake` +
`regenerate-during-build`). In dry-run (`-n`) mode ninja cannot run that check
or reload the regenerated graph, so `ninja -n <target>` only ever reports the
regeneration step and never the real compile/link commands. The tool parsed an
empty plan and no-op'd.

## Fix

Source the commands from places the glob-check does not gate:

- the per-file compile command from `build/compile_commands.json` (rewrite
  `-O2`/`-O3` -> `-g`);
- the `libtorch_python` link command from `ninja -t commands torch_python`
  (a graph walk, not a dry run; strip ninja's `: && ... && :` wrapper).

Then symlink `build/lib` into `torch/lib` as before. The tool now rebuilds
exactly the files you name plus the relink, rather than touching sources and
relying on ninja's staleness detection. The old `>100 stale items` guard was a
dry-run artifact and is removed; the tool now requires `compile_commands.json`
and an already-built tree.

## Tests

- **Unit tests** (`tools/test/test_build_with_debinfo.py`) for the pure
  plan-derivation helpers: the `-O` -> `-g` rewrite, `compile_commands.json`
  indexing, the command/arguments entry forms, and the `ninja -t commands`
  link extraction.
- **Build-tree smoke check**: a new `--dry-run` mode plus a step in
  `.ci/pytorch/build.sh` that, after the Linux build, asserts the emitted plan
  contains a `-g` compile and the `libtorch_python` relink. This exercises the
  real `compile_commands.json` and `ninja -t commands` and fails loudly if a
  future build-system change breaks those data sources -- the class of bug
  here, which unit tests alone would not catch. `--dry-run` only reads the
  tree, so it does not dirty the checkout.

## Test plan

Validated on a real editable CUDA build of `viable/strict`:

```
python tools/build_with_debinfo.py --verbose torch/csrc/Module.cpp
```
recompiles `Module.cpp.o` with `.debug_info`, relinks `libtorch_python.so`
(which then carries `.debug_info`), symlinks it into `torch/lib`, and
`import torch` still works.

```
python tools/build_with_debinfo.py --dry-run torch/csrc/Module.cpp
```
exits 0, prints the plan, and leaves the git tree clean; the build.sh `-g` and
`libtorch_python` plan assertions both pass against that output.

```
python -m pytest tools/test/test_build_with_debinfo.py
```
-> 9 passed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Pull Request resolved: #186780
Approved by: https://github.com/benjaminglass1, https://github.com/atalman


(cherry picked from commit 8f9a6c8)

Co-authored-by: Klaus Zimmermann <[email protected]>
Co-authored-by: Benjamin Glass <[email protected]>
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…ch#186780)

## Summary

`tools/build_with_debinfo.py` recompiles named source files with `-g` so you
can get debug info for a targeted file without a full (slow, ABI-incompatible)
debug build. It is referenced in `CONTRIBUTING.md`, and it broke on current
`main`: running it just prints `Nothing to do` and rebuilds nothing.

## Root cause

The tool derived its compile/link commands from `ninja -j1 -v -n torch_python`
(a dry run) and rewrote `-O2`/`-O3` to `-g`. The build now uses
`file(GLOB ... CONFIGURE_DEPENDS)`, which wires a glob-verification step into
`build.ninja`'s own regeneration (`CMakeFiles/VerifyGlobs.cmake` +
`regenerate-during-build`). In dry-run (`-n`) mode ninja cannot run that check
or reload the regenerated graph, so `ninja -n <target>` only ever reports the
regeneration step and never the real compile/link commands. The tool parsed an
empty plan and no-op'd.

## Fix

Source the commands from places the glob-check does not gate:

- the per-file compile command from `build/compile_commands.json` (rewrite
  `-O2`/`-O3` -> `-g`);
- the `libtorch_python` link command from `ninja -t commands torch_python`
  (a graph walk, not a dry run; strip ninja's `: && ... && :` wrapper).

Then symlink `build/lib` into `torch/lib` as before. The tool now rebuilds
exactly the files you name plus the relink, rather than touching sources and
relying on ninja's staleness detection. The old `>100 stale items` guard was a
dry-run artifact and is removed; the tool now requires `compile_commands.json`
and an already-built tree.

## Tests

- **Unit tests** (`tools/test/test_build_with_debinfo.py`) for the pure
  plan-derivation helpers: the `-O` -> `-g` rewrite, `compile_commands.json`
  indexing, the command/arguments entry forms, and the `ninja -t commands`
  link extraction.
- **Build-tree smoke check**: a new `--dry-run` mode plus a step in
  `.ci/pytorch/build.sh` that, after the Linux build, asserts the emitted plan
  contains a `-g` compile and the `libtorch_python` relink. This exercises the
  real `compile_commands.json` and `ninja -t commands` and fails loudly if a
  future build-system change breaks those data sources -- the class of bug
  here, which unit tests alone would not catch. `--dry-run` only reads the
  tree, so it does not dirty the checkout.

## Test plan

Validated on a real editable CUDA build of `viable/strict`:

```
python tools/build_with_debinfo.py --verbose torch/csrc/Module.cpp
```
recompiles `Module.cpp.o` with `.debug_info`, relinks `libtorch_python.so`
(which then carries `.debug_info`), symlinks it into `torch/lib`, and
`import torch` still works.

```
python tools/build_with_debinfo.py --dry-run torch/csrc/Module.cpp
```
exits 0, prints the plan, and leaves the git tree clean; the build.sh `-g` and
`libtorch_python` plan assertions both pass against that output.

```
python -m pytest tools/test/test_build_with_debinfo.py
```
-> 9 passed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Pull Request resolved: pytorch#186780
Approved by: https://github.com/benjaminglass1, https://github.com/atalman

Co-authored-by: Benjamin Glass <[email protected]>
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…ch#186780)

## Summary

`tools/build_with_debinfo.py` recompiles named source files with `-g` so you
can get debug info for a targeted file without a full (slow, ABI-incompatible)
debug build. It is referenced in `CONTRIBUTING.md`, and it broke on current
`main`: running it just prints `Nothing to do` and rebuilds nothing.

## Root cause

The tool derived its compile/link commands from `ninja -j1 -v -n torch_python`
(a dry run) and rewrote `-O2`/`-O3` to `-g`. The build now uses
`file(GLOB ... CONFIGURE_DEPENDS)`, which wires a glob-verification step into
`build.ninja`'s own regeneration (`CMakeFiles/VerifyGlobs.cmake` +
`regenerate-during-build`). In dry-run (`-n`) mode ninja cannot run that check
or reload the regenerated graph, so `ninja -n <target>` only ever reports the
regeneration step and never the real compile/link commands. The tool parsed an
empty plan and no-op'd.

## Fix

Source the commands from places the glob-check does not gate:

- the per-file compile command from `build/compile_commands.json` (rewrite
  `-O2`/`-O3` -> `-g`);
- the `libtorch_python` link command from `ninja -t commands torch_python`
  (a graph walk, not a dry run; strip ninja's `: && ... && :` wrapper).

Then symlink `build/lib` into `torch/lib` as before. The tool now rebuilds
exactly the files you name plus the relink, rather than touching sources and
relying on ninja's staleness detection. The old `>100 stale items` guard was a
dry-run artifact and is removed; the tool now requires `compile_commands.json`
and an already-built tree.

## Tests

- **Unit tests** (`tools/test/test_build_with_debinfo.py`) for the pure
  plan-derivation helpers: the `-O` -> `-g` rewrite, `compile_commands.json`
  indexing, the command/arguments entry forms, and the `ninja -t commands`
  link extraction.
- **Build-tree smoke check**: a new `--dry-run` mode plus a step in
  `.ci/pytorch/build.sh` that, after the Linux build, asserts the emitted plan
  contains a `-g` compile and the `libtorch_python` relink. This exercises the
  real `compile_commands.json` and `ninja -t commands` and fails loudly if a
  future build-system change breaks those data sources -- the class of bug
  here, which unit tests alone would not catch. `--dry-run` only reads the
  tree, so it does not dirty the checkout.

## Test plan

Validated on a real editable CUDA build of `viable/strict`:

```
python tools/build_with_debinfo.py --verbose torch/csrc/Module.cpp
```
recompiles `Module.cpp.o` with `.debug_info`, relinks `libtorch_python.so`
(which then carries `.debug_info`), symlinks it into `torch/lib`, and
`import torch` still works.

```
python tools/build_with_debinfo.py --dry-run torch/csrc/Module.cpp
```
exits 0, prints the plan, and leaves the git tree clean; the build.sh `-g` and
`libtorch_python` plan assertions both pass against that output.

```
python -m pytest tools/test/test_build_with_debinfo.py
```
-> 9 passed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Pull Request resolved: pytorch#186780
Approved by: https://github.com/benjaminglass1, https://github.com/atalman

Co-authored-by: Benjamin Glass <[email protected]>
pytorchmergebot pushed a commit that referenced this pull request Aug 10, 2026
#186780 introduced number of regressions to the logic, namely it only rebuilded one file and assumed its part of torch_python, rendering the tool useless for ATen changes (torch_cpu, torch_cuda) and also compile .metal shaders with line information.

Build the plan from Ninja's command graph and transitively run every command that consumes the requested source's outputs. This restores rebuilding through intermediate libraries and custom steps, including Metal commands, while limiting debug flags to the requested source compilation.

Extend test accordingly

Authored with the assistance of Codex (OpenAI).
Pull Request resolved: #192729
Approved by: https://github.com/atalman, https://github.com/albanD
ghstack dependencies: #192349
ankitbhrdwj pushed a commit to ankitbhrdwj/pytorch that referenced this pull request Aug 12, 2026
pytorch#186780 introduced number of regressions to the logic, namely it only rebuilded one file and assumed its part of torch_python, rendering the tool useless for ATen changes (torch_cpu, torch_cuda) and also compile .metal shaders with line information.

Build the plan from Ninja's command graph and transitively run every command that consumes the requested source's outputs. This restores rebuilding through intermediate libraries and custom steps, including Metal commands, while limiting debug flags to the requested source compilation.

Extend test accordingly

Authored with the assistance of Codex (OpenAI).
Pull Request resolved: pytorch#192729
Approved by: https://github.com/atalman, https://github.com/albanD
ghstack dependencies: pytorch#192349
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants