ruff
a243d414 - Fix caching for PEP 723 scripts with inline configurations (#30608)

Commit
1 day ago
Fix caching for PEP 723 scripts with inline configurations (#30608) ## Summary Today, a cached script can retain stale diagnostics or formatting results when an extended configuration changes or a CLI override changes only the script’s settings. For example, repeating a project setting on the command line can change which Python version Ruff targets for an unchanged script. **`ruff.toml`** ```toml preview = true target-version = "py310" per-file-target-version = { "script.py" = "py310" } lint.select = ["UP045"] ``` **`script.py`** ```python # /// script # requires-python = ">=3.9" # /// from typing import Optional x: Optional[int] ``` | Command | Project target for `script.py` | Script target | Expected result | | --- | --- | --- | --- | | `ruff check script.py` | 3.10 | 3.9 | Passes | | `ruff check --target-version py310 script.py` | 3.10 | 3.10 | Suggests `int \| None` | | `ruff check --config 'per-file-target-version = { "script.py" = "py310" }' script.py` | 3.10 | 3.10 | Suggests `int \| None` | In the first run, Ruff infers Python 3.9 from `requires-python`. That takes precedence over both the project’s target and its matching per-file target. Without `requires-python`, this script would inherit the matching per-file target of 3.10. The other two commands repeat a project setting as an explicit override. Ruff applies that override after resolving the script’s requirement. The project settings and the file’s modification time are unchanged. Before this PR, Ruff incorrectly used the cached result and missed the diagnostic. This PR keeps the first result cacheable and includes the script cache mode in the file key so neither override can reuse it. In preview, Ruff skips cache writes for an individual script when either of these conditions holds. - The script declares `[tool.ruff]`, even if the table is empty. An extended file or a CLI override can change the script’s settings independently of the project. - The script declares `requires-python` and the command uses `--target-version`, a `--config` override for `target-version` or `per-file-target-version`, `--isolated`, or `--config <file>`. Any CLI per-file override counts, even if its pattern doesn’t match. Other files remain cacheable, though switching modes can cause an extra cache miss. In preview, the formatter also skips the cache write if it rewrites a file with a script header in either the input or the output. Formatting can turn `#/// script` into `# /// script`, so the next run needs to read the newly active settings. Ran the Ruff CLI, workspace, and server tests and verified the three commands above with caching enabled.
Author
Parents
Loading