pytorch/scripts
Zain Rizvi 95210cc409 [BE] Isolate pre-push hook dependencies in dedicated virtual environment (#160048)
This adds two changes:
- Isolates pre-push hook dependencies into an isolated venv, no longer affect your system environment
- Lets you manually run the pre-push lintrunner (including with lintrunner -a) by invoking `python scripts/lintrunner.py [-a]` (it's ugly, but better than nothing...for now)

This is a follow up to:
- https://github.com/pytorch/pytorch/pull/158389

## Problem
The current pre-push hook setup installs lintrunner and related dependencies globally, which makes developers nervous about system pollution and can cause version conflicts with existing installations.

Also, if the pre-push lintrunner found errors, you had to hope your normal lintrunner could fix them (which wasn't always the case, e.g. if those errors only manifested in certain python versions)

##  Key Changes:
  - Isolated Environment: Creates .git/hooks/linter/.venv/ with Python 3.9 (the python used in CI) and an isolated lintrunner installation
  - User-Friendly CLI: New python scripts/lintrunner.py wrapper allows developers to run lintrunner (including -a auto-fix) from any environment
  - Simplified Architecture: Eliminates pre-commit dependency entirely - uses direct git hooks

  File Changes:
  - scripts/setup_hooks.py: Rewritten to create isolated uv-managed virtual environment
  - scripts/lintrunner.py: New wrapper script with shared hash management logic
  - scripts/run_lintrunner.py: Removed (functionality merged into lintrunner.py)
  - .pre-commit-config.yaml: Removed (no longer needed)

##  Usage:
```
  # Setup (run once)
  python scripts/setup_hooks.py

  # Manual linting (works from any environment)
  python scripts/lintrunner.py        # Check mode
  python scripts/lintrunner.py -a     # Auto-fix mode

  # Git hooks work automatically
  git push  # Runs lintrunner in isolated environment

  # Need to skip the pre-push hook?
  git push --no-verify
```

##  Benefits:
  -  Zero global dependency installation
  -  Per-repository isolation prevents version conflicts
  -  Full lintrunner functionality is now accessible

##  Implementation Notes:
  - Virtual env is kept in a dedicated dir in .git, to keep per-repo mechanics
  - lintrunner.py does not need to be invoked from a specific venv.  It'll invoke the right venv itself.

A minor bug: It tends to garble the lintrunner output a bit, like the screenshot below shows, but I haven't found a workaround so far and it remains understandable to users:
<img width="241" height="154" alt="image" src="https://github.com/user-attachments/assets/9496f925-8524-4434-8486-dc579442d688" />

## What's next?
Features that could be added:
- Check for lintrunner updates, auto-update if needed
- Depending on dev response, this could be enabled by default for all pytorch/pytorch environments
Pull Request resolved: https://github.com/pytorch/pytorch/pull/160048
Approved by: https://github.com/seemethere
2025-08-12 01:58:46 +00:00
..
analysis [BE][Easy][1/19] enforce style for empty lines in import segments (#129752) 2024-07-16 00:42:56 +00:00
compile_tests [dynamo][ci] update PYTORCH_TEST_WITH_DYNAMO xfail/skips script for 3.13 (#155570) 2025-06-11 19:09:29 +00:00
export [BE][CI] bump ruff to 0.9.2: multiline assert statements (#144546) 2025-02-27 20:46:16 +00:00
jit [BE] fix typos in functorch/ and scripts/ (#156081) 2025-06-21 02:47:40 +00:00
onnx [build] modernize build-frontend: python setup.py develop/install -> [uv ]pip install --no-build-isolation [-e ]. (#156027) 2025-07-09 11:24:27 +00:00
release [release] delete tag-docker-images.sh as not required anymore (#156737) 2025-06-25 15:17:06 +00:00
release_notes [BE][EZ] Minor doc fixes (#158574) 2025-07-18 10:34:55 -05:00
build_host_protoc.sh Add host protoc script back (#159157) 2025-07-25 18:44:20 +00:00
install_triton_wheel.sh [XPU] Simplify XPU make triton by install from PyTorch source (#155675) 2025-06-12 13:02:23 +00:00
lint_urls.sh Update lint_urls.sh (#154838) 2025-06-02 14:50:34 +00:00
lint_xrefs.sh Re-enable link linter (#153280) 2025-05-23 20:56:25 +00:00
lintrunner.py [BE] Isolate pre-push hook dependencies in dedicated virtual environment (#160048) 2025-08-12 01:58:46 +00:00
proto.ps1 Revert "Cleanup old caffe2 scripts (#158475)" 2025-07-17 20:58:34 +00:00
README.md Cleanup old caffe2 scripts (#158475) 2025-07-23 01:21:31 +00:00
setup_hooks.py [BE] Isolate pre-push hook dependencies in dedicated virtual environment (#160048) 2025-08-12 01:58:46 +00:00

This directory contains the useful tools.