Skip to content

fix(ruff): preserve explicit check targets - #4787

Open
Aschenbath wants to merge 1 commit into
rtk-ai:developfrom
Aschenbath:fix/ruff-preserve-explicit-targets
Open

Aschenbath wants to merge 1 commit into
rtk-ai:developfrom
Aschenbath:fix/ruff-preserve-explicit-targets

Conversation

@Aschenbath

@Aschenbath Aschenbath commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #4752.

rtk ruff check a=b.py used to append . and lint unrelated files in the working directory, potentially turning a successful check into an error. The same happened for targets after -- that contain = or begin with -.

Remove the extra path inference and let Ruff provide its own current-directory default when no target is supplied. Explicit targets remain scoped to the requested files. Ruff routing and output-format handling keep their existing behavior.

Test plan

  • CLI argv suite: the two added regressions fail before the fix (3 pass, 2 fail), then all 5 tests pass.
  • Official Ruff 0.16.6: all 9 isolated scenarios pass, including five explicit-target cases and four default-directory/config controls. Before the fix, all five explicit-target cases incorrectly lint a deliberately failing neighbor.
  • cargo fmt --all --check, the separate proxy formatting check, and cargo clippy --workspace --all-targets pass.
  • Complete cargo test --all --no-fail-fast: 5,213 pass, zero fail, eight existing ignored tests, and no name filtering.
  • Original CI workflow on the same commit in the fork: all 14 jobs pass, including Linux, macOS, Windows MSVC/GNU, OSS edition, proxy, security checks, and benchmarks.

Local builds disabled dev/test debuginfo to bound the RAM build cache; optimization, debug assertions, and test timeouts were unchanged.
CI used the repository's normal build profiles. Upstream Actions is awaiting maintainer approval.

Implementation, tests, and this description were prepared with Codex.

@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟢 Risk low

Summary

Fixes a bug in rtk ruff check where explicit targets containing = (e.g. a=b.py) or appearing after -- were misidentified as option assignments, causing RTK to append . and lint the entire working directory instead of just the requested file. The fix removes the faulty path-inference heuristic and lets Ruff fall back to its own current-directory default only when no target is given.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #4752


Analyzed automatically by wshm · This is an automated analysis, not a human review.

@Aschenbath
Aschenbath marked this pull request as ready for review October 25, 2026 04:17

@Protocol-zero-0 Protocol-zero-0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Independently reviewed exact head 16db8f0f1bfe9bded3fdf4e20444c4a5505d802a against its immediate parent 32b2ec43.

With real Ruff 0.17.0 on Linux, I ran 25 isolated scenarios / 75 native-parent-head processes. The explicit-target cases (a=b.py, implicit check, and -- followed by equals/dash/another-separator names) no longer lint a deliberately failing neighbor. Config/output-format variants, multiple targets, and a missing-path warning were also checked. Ten cases that previously returned an unrelated failure now match native Ruff's exit 0; a genuinely failing equals-name target retains F401 and exit 1. No-target/config defaults and invalid-option/format/separator exit-2 controls retain their behavior.

The PR's five CLI argv tests pass at head; applying that same test file to the parent gives 3 passes / 2 failures. All 18 Ruff unit tests, root and proxy formatting checks, and warnings-denied all-target clippy pass.

One separate inherited issue surfaced in the stdin control: piping import sys into ruff check --stdin-filename a=b.py - under an F401-only config gives native Ruff exit 1, but both parent and head RTK return exit 0 and no F401. The shared runner defaults to null stdin and this call site does not request inherit_stdin(). This PR removes the parent's extra Ignoring file . warning, but does not fix stdin forwarding. I would track that independently rather than broaden this target-inference fix.

No full-suite, other-platform or pending-PR integration validation in this review. No new regression found in this patch's scope; approved.

@Protocol-zero-0

Copy link
Copy Markdown
Contributor

Follow-up to the stdin case in my review: I reproduced it independently on current develop (e05c7bdfacfc) and prepared a narrow patch. It is separate from this PR's explicit-target change.

printf 'import sys\n' | rtk ruff check --stdin-filename sample.py - reads EOF rather than the supplied source: native Ruff reports F401/exit 1; the base returns exit 0 without F401. ruff format --check ... - similarly accepts unformatted or invalid input. The Ruff call site uses the shared runner's default null stdin.

The patch enables the runner's existing inherit_stdin() option. Ruff itself decides whether the invocation reads stdin; no argument parsing or routing change is added. Four subprocess regressions verify exact input bytes from a pipe and a redirected file, a 128 KiB binary payload, EOF, and the child exit code. The same tests on the base give 3 failures and 1 empty-input control pass; patched, all 4 pass.

Validation on Linux with Ruff 0.17.0: 31 scenarios / 93 native–base–patched executions, all patched exit codes equal native, fixing 12 baseline exit-code mismatches. Includes F401/clean/empty/syntax stdin, output-format/exit-zero/quiet controls, format check, 200,011-byte source, usage errors and ordinary file/directory controls. The small formatter-stdout case also matches native byte for byte. Two argv forms retain the base's extra-dot Ignoring file . warning; your explicit-target fix remains separate.

18 Ruff unit tests, full workspace 5253 passed / 8 ignored, root/proxy fmt and warnings-denied all-targets clippy pass. No pending-PR integration or non-Linux execution. This does not make ruff server live: #4106's passthrough work is still needed. No general formatter-output preservation claim beyond the tested case.

Apply-ready stdin-only patch for develop e05c7bd
diff --git a/src/cmds/python/ruff_cmd.rs b/src/cmds/python/ruff_cmd.rs
index 1830dac1..5f6a231d 100644
--- a/src/cmds/python/ruff_cmd.rs
+++ b/src/cmds/python/ruff_cmd.rs
@@ -121,7 +121,10 @@ pub fn run(args: &[String], verbose: u8) -> Result<i32> {
                 filter_passthrough_output(stdout, is_analyze_graph)
             }
         },
-        runner::RunOptions::stdout_only().clean_outputs(clean_outputs),
+        // Ruff decides whether to read stdin (for example, `check -` or `format -`).
+        runner::RunOptions::stdout_only()
+            .inherit_stdin()
+            .clean_outputs(clean_outputs),
     )
 }
 
diff --git a/tests/ruff_stdin_test.rs b/tests/ruff_stdin_test.rs
new file mode 100644
index 00000000..4295651b
--- /dev/null
+++ b/tests/ruff_stdin_test.rs
@@ -0,0 +1,91 @@
+#![cfg(unix)]
+
+use std::io::Write;
+use std::os::unix::fs::PermissionsExt;
+use std::process::Stdio;
+
+mod common;
+
+fn check_stdin(args: &[&str], input: &[u8], redirected: bool) {
+    let dir = tempfile::tempdir().expect("temporary Ruff directory");
+    let capture = dir.path().join("received");
+    let stub = dir.path().join("ruff");
+    std::fs::write(
+        &stub,
+        "#!/bin/sh\ncat > \"$RUFF_STDIN_CAPTURE\"\nprintf '[]\\n'\nexit 7\n",
+    )
+    .expect("write Ruff stand-in");
+    std::fs::set_permissions(&stub, std::fs::Permissions::from_mode(0o755))
+        .expect("executable Ruff stand-in");
+    let mut cmd = common::rtk_command();
+    let path = format!(
+        "{}:{}",
+        dir.path().display(),
+        std::env::var("PATH").unwrap_or_default()
+    );
+    cmd.arg("ruff")
+        .args(args)
+        .env("PATH", path)
+        .env("RUFF_STDIN_CAPTURE", &capture)
+        .stdout(Stdio::piped())
+        .stderr(Stdio::piped());
+    let output = if redirected {
+        let source = dir.path().join("source");
+        std::fs::write(&source, input).expect("write redirected stdin");
+        cmd.stdin(std::fs::File::open(source).expect("open redirected stdin"))
+            .output()
+            .expect("run rtk ruff")
+    } else {
+        let mut child = cmd.stdin(Stdio::piped()).spawn().expect("spawn rtk ruff");
+        let mut stdin = child.stdin.take().expect("piped stdin");
+        // A writer thread also handles payloads larger than the pipe buffer.
+        let bytes = input.to_vec();
+        let writer = std::thread::spawn(move || stdin.write_all(&bytes));
+        let output = child.wait_with_output().expect("wait for rtk ruff");
+        let _ = writer.join().expect("stdin writer");
+        output
+    };
+    assert_eq!(
+        output.status.code(),
+        Some(7),
+        "native exit code: {output:?}"
+    );
+    assert_eq!(
+        std::fs::read(capture).expect("captured stdin"),
+        input,
+        "Ruff must receive the original stdin bytes"
+    );
+}
+
+#[test]
+fn check_receives_piped_source() {
+    check_stdin(
+        &["check", "--stdin-filename", "sample.py", "-"],
+        b"import sys\n",
+        false,
+    );
+}
+
+#[test]
+fn format_receives_redirected_source() {
+    check_stdin(
+        &["format", "--stdin-filename", "sample.py", "-"],
+        b"x=1\n",
+        true,
+    );
+}
+
+#[test]
+fn check_receives_large_binary_payload_unchanged() {
+    let bytes: Vec<u8> = (0..131072).map(|i| (i % 256) as u8).collect();
+    check_stdin(
+        &["check", "--stdin-filename", "sample.py", "-"],
+        &bytes,
+        false,
+    );
+}
+
+#[test]
+fn empty_stdin_reaches_eof() {
+    check_stdin(&["check", "--stdin-filename", "sample.py", "-"], b"", false);
+}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ruff rust

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rtk ruff check appends . when every path is dash-prefixed or contains =, so rtk ruff check a=b.py lints the whole tree

2 participants