Skip to content

Fix build_with_debinfo.py broken by CONFIGURE_DEPENDS globbing - #188192

Merged
atalman merged 1 commit into
release/2.13from
cherry-pick-186780-by-pytorch_bot_bot_
Jun 25, 2026
Merged

atalman merged 1 commit into
release/2.13from
cherry-pick-186780-by-pytorch_bot_bot_

Conversation

@pytorchbot

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

## 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

Co-authored-by: Benjamin Glass <[email protected]>
(cherry picked from commit 8f9a6c8)
@pytorch-bot

pytorch-bot Bot commented Jun 25, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⏳ No Failures, 56 Pending

As of commit 2fd87b3 with merge base 636bc91 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@pytorch-bot

pytorch-bot Bot commented Jun 25, 2026

Copy link
Copy Markdown

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.

@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

@atalman
atalman merged commit 9330813 into release/2.13 Jun 25, 2026
78 of 129 checks passed
@github-actions
github-actions Bot deleted the cherry-pick-186780-by-pytorch_bot_bot_ branch July 26, 2026 02:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants