Skip to content

Commit 915bb2b

Browse files
authored
[ty] Prefer existing @ paths over response files in Ruff and ty (#28877)
Ruff and ty interpret `@list.py` as a response-file reference even when that literal path exists. This can turn a filename supplied by an integration into command-line options. Give existing literal `@` paths precedence in both CLIs, including dangling symlinks, arguments inside nested response files, and arguments after `--`. When the literal path is absent, keep expanding the response file. Both CLIs share the implementation in `ruff_cli`.
1 parent 4710e1a commit 915bb2b

11 files changed

Lines changed: 160 additions & 8 deletions

File tree

‎Cargo.lock‎

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ char_str = { version = "0.0.4", default-features = false, features = ["std"] }
1717
ruff = { version = "0.16.8", path = "crates/ruff" }
1818
ruff_annotate_snippets = { version = "0.0.14", path = "crates/ruff_annotate_snippets" }
1919
ruff_cache = { version = "0.0.14", path = "crates/ruff_cache" }
20+
ruff_cli = { version = "0.0.14", path = "crates/ruff_cli" }
2021
ruff_db = { version = "0.0.14", path = "crates/ruff_db", default-features = false }
2122
ruff_diagnostics = { version = "0.0.14", path = "crates/ruff_diagnostics" }
2223
ruff_formatter = { version = "0.0.14", path = "crates/ruff_formatter" }

‎crates/ruff/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dist = true
2121

2222
[dependencies]
2323
ruff_cache = { workspace = true }
24+
ruff_cli = { workspace = true }
2425
ruff_db = { workspace = true, default-features = false, features = ["os"] }
2526
ruff_diagnostics = { workspace = true }
2627
ruff_graph = { workspace = true, features = ["serde", "clap"] }
@@ -39,7 +40,6 @@ ruff_text_size = { workspace = true }
3940
ruff_workspace = { workspace = true }
4041

4142
anyhow = { workspace = true }
42-
argfile = { workspace = true }
4343
rkyv = { workspace = true }
4444
bitflags = { workspace = true }
4545
cachedir = { workspace = true }

‎crates/ruff/src/main.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ fn main() -> ExitCode {
3333
assert!(colored::control::set_virtual_terminal(true).is_ok());
3434

3535
let args = wild::args_os();
36-
let args = match argfile::expand_args_from(args, argfile::parse_fromfile, argfile::PREFIX)
37-
.context("Failed to read CLI arguments from files")
36+
let args = match ruff_cli::expand_args(args).context("Failed to read CLI arguments from files")
3837
{
3938
Ok(args) => args,
4039
Err(err) => return report_error(&err),

‎crates/ruff/tests/integration_test.rs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,6 +1868,56 @@ fn check_input_from_argfile() -> Result<()> {
18681868
Ok(())
18691869
}
18701870

1871+
#[test]
1872+
fn check_existing_at_paths_are_literal() -> Result<()> {
1873+
// Preserve existing @-prefixed paths so filenames from integrations cannot inject options.
1874+
let tempdir = TempDir::new()?;
1875+
fs::write(tempdir.path().join("@list.py"), "import os\n")?;
1876+
fs::create_dir(tempdir.path().join("@package"))?;
1877+
fs::write(tempdir.path().join("@package/main.py"), "import sys\n")?;
1878+
fs::write(
1879+
tempdir.path().join("list.py"),
1880+
"--output-file\nsentinel.txt\nunrelated.py\n",
1881+
)?;
1882+
fs::write(tempdir.path().join("package"), "unrelated.py\n")?;
1883+
fs::write(tempdir.path().join("unrelated.py"), "")?;
1884+
fs::write(tempdir.path().join("sentinel.txt"), "unchanged\n")?;
1885+
1886+
let mut settings = insta::Settings::clone_current();
1887+
settings.add_filter(r"\\", "/");
1888+
let _scope = settings.bind_to_scope();
1889+
1890+
assert_cmd_snapshot!(ruff_cmd()
1891+
.current_dir(tempdir.path())
1892+
.args([
1893+
"check",
1894+
"--isolated",
1895+
"--no-cache",
1896+
"--select",
1897+
"F401",
1898+
"--output-format",
1899+
"concise",
1900+
"@list.py",
1901+
"--",
1902+
"@package",
1903+
]), @"
1904+
success: false
1905+
exit_code: 1
1906+
----- stdout -----
1907+
@list.py:1:8: F401 [*] `os` imported but unused
1908+
@package/main.py:1:8: F401 [*] `sys` imported but unused
1909+
Found 2 errors.
1910+
[*] 2 fixable with the `--fix` option.
1911+
1912+
----- stderr -----
1913+
");
1914+
assert_eq!(
1915+
fs::read_to_string(tempdir.path().join("sentinel.txt"))?,
1916+
"unchanged\n"
1917+
);
1918+
Ok(())
1919+
}
1920+
18711921
#[test]
18721922
// Regression test for https://github.com/astral-sh/ruff/issues/20655
18731923
fn missing_argfile_reports_error() {

‎crates/ruff_cli/Cargo.toml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "ruff_cli"
3+
version = "0.0.14"
4+
description = "This is an internal component crate of Ruff"
5+
authors = { workspace = true }
6+
edition = { workspace = true }
7+
rust-version = { workspace = true }
8+
homepage = { workspace = true }
9+
documentation = { workspace = true }
10+
repository = { workspace = true }
11+
license = { workspace = true }
12+
13+
[dependencies]
14+
argfile = { workspace = true }
15+
16+
[lints]
17+
workspace = true

‎crates/ruff_cli/src/lib.rs‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//! Response-file expansion shared by the Ruff and ty command-line interfaces.
2+
3+
use std::ffi::{OsStr, OsString};
4+
use std::io;
5+
use std::path::Path;
6+
7+
/// Expands `@path` arguments unless the literal path, including `@`, exists.
8+
///
9+
/// Response files contain one argument per line and may refer to other response files.
10+
/// Relative paths are resolved from the current working directory. Expansion happens before
11+
/// option parsing, so the same rules apply before and after `--`.
12+
pub fn expand_args(args: impl Iterator<Item = OsString>) -> io::Result<Vec<OsString>> {
13+
let mut expanded = Vec::with_capacity(args.size_hint().0);
14+
for arg in args {
15+
if is_response_file(&arg) {
16+
expanded.extend(argfile::expand_args_from(
17+
std::iter::once(arg),
18+
|content, prefix| {
19+
content
20+
.lines()
21+
.map(|line| {
22+
if is_response_file(OsStr::new(line)) {
23+
argfile::Argument::parse_ref(line, prefix)
24+
} else {
25+
argfile::Argument::PassThrough(line.into())
26+
}
27+
})
28+
.collect()
29+
},
30+
argfile::PREFIX,
31+
)?);
32+
} else {
33+
expanded.push(arg);
34+
}
35+
}
36+
Ok(expanded)
37+
}
38+
39+
fn is_response_file(arg: &OsStr) -> bool {
40+
// Check the directory entry itself so dangling symlinks remain literal paths.
41+
arg.as_encoded_bytes().starts_with(b"@") && Path::new(arg).symlink_metadata().is_err()
42+
}

‎crates/ty/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ license.workspace = true
1818
doctest = false
1919

2020
[dependencies]
21+
ruff_cli = { workspace = true }
2122
ruff_db = { workspace = true, features = ["os", "cache", "junit"] }
2223
ruff_diagnostics = { workspace = true }
2324
ruff_python_ast = { workspace = true }
@@ -30,7 +31,6 @@ ty_site_packages = { workspace = true }
3031
ty_static = { workspace = true }
3132

3233
anyhow = { workspace = true }
33-
argfile = { workspace = true }
3434
clap = { workspace = true, features = ["wrap_help", "string", "env"] }
3535
clap_complete_command = { workspace = true }
3636
clearscreen = { workspace = true }

‎crates/ty/src/lib.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ pub fn run() -> anyhow::Result<ExitStatus> {
4747
ruff_db::set_program_version(crate::version::version().to_string()).unwrap();
4848

4949
let args = wild::args_os();
50-
let args = argfile::expand_args_from(args, argfile::parse_fromfile, argfile::PREFIX)
51-
.context("Failed to read CLI arguments from file")?;
50+
let args = ruff_cli::expand_args(args).context("Failed to read CLI arguments from file")?;
5251
let args = Cli::parse_from(args);
5352

5453
match args.command {

‎crates/ty/tests/cli/file_selection.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,38 @@ fn invalid_exclude_pattern() -> anyhow::Result<()> {
12441244
Ok(())
12451245
}
12461246

1247+
#[test]
1248+
fn response_files_preserve_literal_at_paths() -> anyhow::Result<()> {
1249+
// Preserve existing @-prefixed paths so filenames from integrations cannot select unrelated files.
1250+
let case = CliTest::with_files([
1251+
("@list.py", "value: int = 'literal'"),
1252+
("list.py", "other.py\n"),
1253+
("@package/main.py", "value: int = 'directory'"),
1254+
("package", "other.py\n"),
1255+
("other.py", ""),
1256+
("outer.args", "@inner.args\n"),
1257+
("inner.args", "@package\n"),
1258+
])?;
1259+
1260+
assert_cmd_snapshot!(case.command().args([
1261+
"--output-format",
1262+
"concise",
1263+
"@list.py",
1264+
"--",
1265+
"@outer.args",
1266+
]), @r#"
1267+
success: false
1268+
exit_code: 1
1269+
----- stdout -----
1270+
@list.py:1:14: error[invalid-assignment] Object of type `Literal["literal"]` is not assignable to `int`
1271+
@package/main.py:1:14: error[invalid-assignment] Object of type `Literal["directory"]` is not assignable to `int`
1272+
Found 2 diagnostics
1273+
1274+
----- stderr -----
1275+
"#);
1276+
Ok(())
1277+
}
1278+
12471279
/// Test that ty works correctly with Bazel's symlinked file structure
12481280
#[test]
12491281
#[cfg(unix)]

0 commit comments

Comments
 (0)