Compare commits

...
Author SHA1 Message Date
Patrick szymkowiak c8e5618e6a docs: fix Linux binary filename in README (gnu → musl)
README still referenced x86_64-unknown-linux-gnu.tar.gz which was
replaced by x86_64-unknown-linux-musl in PR #267 (v0.23.0+).

Fixes #323
2026-03-06 11:25:25 +01:00
Alex Tsanis 884e37edbf stream proxy output while command is running (#268) 2026-03-06 11:02:59 +01:00
patrick szymkowiak 27b35d84a3 fix: strip npx/bunx/pnpm prefixes in lint linter detection (#186) (#366)
When running `rtk lint npx eslint ...`, args[0] was "npx" instead of
"eslint", causing no JSON format flag injection and duplicated output.

Extract strip_pm_prefix() and detect_linter() helpers, skip package
manager prefixes before identifying the actual linter name.
2026-03-06 10:51:11 +01:00
Radu Ursache 0a1bcb05e5 fix: pass through -R/--repo flag in gh view commands (#328)
gh view/checks commands (pr view, issue view, pr checks, run view)
were ignoring extra flags like -R/--repo, causing failures outside
git repositories where gh needs -R to know which repo to query.

The root cause: these functions took args[0] as the identifier and
discarded everything else, while constructing hardcoded gh commands.

Changes:
- Add extract_identifier_and_extra_args() to separate the positional
  identifier from flags, handling both `view 123 -R owner/repo` and
  `view -R owner/repo 123` argument orders
- Pass extra args through to gh in view_pr, view_issue, pr_checks,
  and view_run
- Add has_json_flag() check at top of run() to passthrough when user
  explicitly requests --json output (related fix from #313)

Fixes #223
2026-03-06 10:31:10 +01:00
Sam Kvale 799f0856e4 fix: gh pr edit/comment pass correct subcommand to gh (#332)
* fix: gh pr edit/comment pass correct subcommand to gh

pr_action was receiving the past-tense display label ("edited",
"commented") and using it as both the gh subcommand and the
ok_confirmation message. This caused `gh pr edited` to be invoked
instead of `gh pr edit`, which fails with "unknown flag" errors.

Split the parameter into subcmd (for the gh invocation) and
display_label (for ok_confirmation output).

* smaller diff
2026-03-06 09:59:00 +01:00
4 changed files with 346 additions and 60 deletions

No files matched your search

+1 -1
View File
@@ -103,7 +103,7 @@ cargo install rtk
Download from [rtk-ai/releases](https://github.com/rtk-ai/rtk/releases):
- macOS: `rtk-x86_64-apple-darwin.tar.gz` / `rtk-aarch64-apple-darwin.tar.gz`
- Linux: `rtk-x86_64-unknown-linux-gnu.tar.gz` / `rtk-aarch64-unknown-linux-gnu.tar.gz`
- Linux: `rtk-x86_64-unknown-linux-musl.tar.gz` / `rtk-aarch64-unknown-linux-gnu.tar.gz`
- Windows: `rtk-x86_64-pc-windows-msvc.zip`
## Quick Start
+164 -37
View File
@@ -108,8 +108,58 @@ fn filter_markdown_segment(text: &str) -> String {
s
}
/// Check if args contain --json flag (user wants specific JSON fields, not RTK filtering)
fn has_json_flag(args: &[String]) -> bool {
args.iter().any(|a| a == "--json")
}
/// Extract a positional identifier (PR/issue number) from args, returning it
/// separately from the remaining extra flags (like -R, --repo, etc.).
/// Handles both `view 123 -R owner/repo` and `view -R owner/repo 123`.
fn extract_identifier_and_extra_args(args: &[String]) -> Option<(String, Vec<String>)> {
if args.is_empty() {
return None;
}
// Known gh flags that take a value — skip these and their values
let flags_with_value = ["-R", "--repo", "-q", "--jq", "-t", "--template"];
let mut identifier = None;
let mut extra = Vec::new();
let mut skip_next = false;
for arg in args {
if skip_next {
extra.push(arg.clone());
skip_next = false;
continue;
}
if flags_with_value.contains(&arg.as_str()) {
extra.push(arg.clone());
skip_next = true;
continue;
}
if arg.starts_with('-') {
extra.push(arg.clone());
continue;
}
// First non-flag arg is the identifier (number/URL)
if identifier.is_none() {
identifier = Some(arg.clone());
} else {
extra.push(arg.clone());
}
}
identifier.map(|id| (id, extra))
}
/// Run a gh command with token-optimized output
pub fn run(subcommand: &str, args: &[String], verbose: u8, ultra_compact: bool) -> Result<()> {
// When user explicitly passes --json, they want raw gh JSON output, not RTK filtering
if has_json_flag(args) {
return run_passthrough("gh", subcommand, args);
}
match subcommand {
"pr" => run_pr(args, verbose, ultra_compact),
"issue" => run_issue(args, verbose, ultra_compact),
@@ -136,8 +186,8 @@ fn run_pr(args: &[String], verbose: u8, ultra_compact: bool) -> Result<()> {
"create" => pr_create(&args[1..], verbose),
"merge" => pr_merge(&args[1..], verbose),
"diff" => pr_diff(&args[1..], verbose),
"comment" => pr_action("commented", &args[1..], verbose),
"edit" => pr_action("edited", &args[1..], verbose),
"comment" => pr_action("commented", &args, verbose),
"edit" => pr_action("edited", &args, verbose),
_ => run_passthrough("gh", "pr", args),
}
}
@@ -229,20 +279,22 @@ fn list_prs(args: &[String], _verbose: u8, ultra_compact: bool) -> Result<()> {
fn view_pr(args: &[String], _verbose: u8, ultra_compact: bool) -> Result<()> {
let timer = tracking::TimedExecution::start();
if args.is_empty() {
return Err(anyhow::anyhow!("PR number required"));
}
let pr_number = &args[0];
let (pr_number, extra_args) = match extract_identifier_and_extra_args(args) {
Some(result) => result,
None => return Err(anyhow::anyhow!("PR number required")),
};
let mut cmd = Command::new("gh");
cmd.args([
"pr",
"view",
pr_number,
&pr_number,
"--json",
"number,title,state,author,body,url,mergeable,reviews,statusCheckRollup",
]);
for arg in &extra_args {
cmd.arg(arg);
}
let output = cmd.output().context("Failed to run gh pr view")?;
let raw = String::from_utf8_lossy(&output.stdout).to_string();
@@ -398,14 +450,16 @@ fn view_pr(args: &[String], _verbose: u8, ultra_compact: bool) -> Result<()> {
fn pr_checks(args: &[String], _verbose: u8, _ultra_compact: bool) -> Result<()> {
let timer = tracking::TimedExecution::start();
if args.is_empty() {
return Err(anyhow::anyhow!("PR number required"));
}
let pr_number = &args[0];
let (pr_number, extra_args) = match extract_identifier_and_extra_args(args) {
Some(result) => result,
None => return Err(anyhow::anyhow!("PR number required")),
};
let mut cmd = Command::new("gh");
cmd.args(["pr", "checks", pr_number]);
cmd.args(["pr", "checks", &pr_number]);
for arg in &extra_args {
cmd.arg(arg);
}
let output = cmd.output().context("Failed to run gh pr checks")?;
let raw = String::from_utf8_lossy(&output.stdout).to_string();
@@ -607,20 +661,22 @@ fn list_issues(args: &[String], _verbose: u8, ultra_compact: bool) -> Result<()>
fn view_issue(args: &[String], _verbose: u8) -> Result<()> {
let timer = tracking::TimedExecution::start();
if args.is_empty() {
return Err(anyhow::anyhow!("Issue number required"));
}
let issue_number = &args[0];
let (issue_number, extra_args) = match extract_identifier_and_extra_args(args) {
Some(result) => result,
None => return Err(anyhow::anyhow!("Issue number required")),
};
let mut cmd = Command::new("gh");
cmd.args([
"issue",
"view",
issue_number,
&issue_number,
"--json",
"number,title,state,author,body,url",
]);
for arg in &extra_args {
cmd.arg(arg);
}
let output = cmd.output().context("Failed to run gh issue view")?;
let raw = String::from_utf8_lossy(&output.stdout).to_string();
@@ -796,22 +852,23 @@ fn should_passthrough_run_view(extra_args: &[String]) -> bool {
}
fn view_run(args: &[String], _verbose: u8) -> Result<()> {
if args.is_empty() {
return Err(anyhow::anyhow!("Run ID required"));
}
let run_id = &args[0];
let extra_args = &args[1..];
let (run_id, extra_args) = match extract_identifier_and_extra_args(args) {
Some(result) => result,
None => return Err(anyhow::anyhow!("Run ID required")),
};
// Pass through when user requests logs or JSON — the filter would strip them
if should_passthrough_run_view(extra_args) {
return run_passthrough_with_extra("gh", &["run", "view", run_id], extra_args);
if should_passthrough_run_view(&extra_args) {
return run_passthrough_with_extra("gh", &["run", "view", &run_id], &extra_args);
}
let timer = tracking::TimedExecution::start();
let mut cmd = Command::new("gh");
cmd.args(["run", "view", run_id]);
cmd.args(["run", "view", &run_id]);
for arg in &extra_args {
cmd.arg(arg);
}
let output = cmd.output().context("Failed to run gh run view")?;
let raw = String::from_utf8_lossy(&output.stdout).to_string();
@@ -1070,23 +1127,24 @@ fn pr_diff(args: &[String], _verbose: u8) -> Result<()> {
/// Generic PR action handler for comment/edit
fn pr_action(action: &str, args: &[String], _verbose: u8) -> Result<()> {
let timer = tracking::TimedExecution::start();
let subcmd = &args[0];
let mut cmd = Command::new("gh");
cmd.args(["pr", action]);
cmd.arg("pr");
for arg in args {
cmd.arg(arg);
}
let output = cmd
.output()
.context(format!("Failed to run gh pr {}", action))?;
.context(format!("Failed to run gh pr {}", subcmd))?;
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
timer.track(
&format!("gh pr {}", action),
&format!("rtk gh pr {}", action),
&format!("gh pr {}", subcmd),
&format!("rtk gh pr {}", subcmd),
&stderr,
&stderr,
);
@@ -1094,8 +1152,8 @@ fn pr_action(action: &str, args: &[String], _verbose: u8) -> Result<()> {
std::process::exit(output.status.code().unwrap_or(1));
}
// Extract PR number from args
let pr_num = args
// Extract PR number from args (skip args[0] which is the subcommand)
let pr_num = args[1..]
.iter()
.find(|a| !a.starts_with('-'))
.map(|s| format!("#{}", s))
@@ -1112,8 +1170,8 @@ fn pr_action(action: &str, args: &[String], _verbose: u8) -> Result<()> {
};
timer.track(
&format!("gh pr {}", action),
&format!("rtk gh pr {}", action),
&format!("gh pr {}", subcmd),
&format!("rtk gh pr {}", subcmd),
&raw,
&filtered,
);
@@ -1277,6 +1335,75 @@ mod tests {
assert_eq!(result, "ok edited #42");
}
#[test]
fn test_has_json_flag_present() {
assert!(has_json_flag(&[
"view".into(),
"--json".into(),
"number,url".into()
]));
}
#[test]
fn test_has_json_flag_absent() {
assert!(!has_json_flag(&["view".into(), "42".into()]));
}
#[test]
fn test_extract_identifier_simple() {
let args: Vec<String> = vec!["123".into()];
let (id, extra) = extract_identifier_and_extra_args(&args).unwrap();
assert_eq!(id, "123");
assert!(extra.is_empty());
}
#[test]
fn test_extract_identifier_with_repo_flag_after() {
// gh issue view 185 -R rtk-ai/rtk
let args: Vec<String> = vec!["185".into(), "-R".into(), "rtk-ai/rtk".into()];
let (id, extra) = extract_identifier_and_extra_args(&args).unwrap();
assert_eq!(id, "185");
assert_eq!(extra, vec!["-R", "rtk-ai/rtk"]);
}
#[test]
fn test_extract_identifier_with_repo_flag_before() {
// gh issue view -R rtk-ai/rtk 185
let args: Vec<String> = vec!["-R".into(), "rtk-ai/rtk".into(), "185".into()];
let (id, extra) = extract_identifier_and_extra_args(&args).unwrap();
assert_eq!(id, "185");
assert_eq!(extra, vec!["-R", "rtk-ai/rtk"]);
}
#[test]
fn test_extract_identifier_with_long_repo_flag() {
let args: Vec<String> = vec!["42".into(), "--repo".into(), "owner/repo".into()];
let (id, extra) = extract_identifier_and_extra_args(&args).unwrap();
assert_eq!(id, "42");
assert_eq!(extra, vec!["--repo", "owner/repo"]);
}
#[test]
fn test_extract_identifier_empty() {
let args: Vec<String> = vec![];
assert!(extract_identifier_and_extra_args(&args).is_none());
}
#[test]
fn test_extract_identifier_only_flags() {
// No positional identifier, only flags
let args: Vec<String> = vec!["-R".into(), "rtk-ai/rtk".into()];
assert!(extract_identifier_and_extra_args(&args).is_none());
}
#[test]
fn test_extract_identifier_with_web_flag() {
let args: Vec<String> = vec!["123".into(), "--web".into()];
let (id, extra) = extract_identifier_and_extra_args(&args).unwrap();
assert_eq!(id, "123");
assert_eq!(extra, vec!["--web"]);
}
#[test]
fn test_run_view_passthrough_log_failed() {
assert!(should_passthrough_run_view(&["--log-failed".into()]));
+112 -11
View File
@@ -52,16 +52,43 @@ fn is_python_linter(linter: &str) -> bool {
matches!(linter, "ruff" | "pylint" | "mypy" | "flake8")
}
pub fn run(args: &[String], verbose: u8) -> Result<()> {
let timer = tracking::TimedExecution::start();
/// Strip package manager prefixes (npx, bunx, pnpm, pnpm exec, yarn) from args.
/// Returns the number of args to skip.
fn strip_pm_prefix(args: &[String]) -> usize {
let pm_names = ["npx", "bunx", "pnpm", "yarn"];
let mut skip = 0;
for arg in args {
if pm_names.contains(&arg.as_str()) || arg == "exec" {
skip += 1;
} else {
break;
}
}
skip
}
// Detect linter name (first arg if not a path/flag, else default to eslint)
/// Detect the linter name from args (after stripping PM prefixes).
/// Returns the linter name and whether it was explicitly specified.
fn detect_linter(args: &[String]) -> (&str, bool) {
let is_path_or_flag = args.is_empty()
|| args[0].starts_with('-')
|| args[0].contains('/')
|| args[0].contains('.');
let linter = if is_path_or_flag { "eslint" } else { &args[0] };
if is_path_or_flag {
("eslint", false)
} else {
(&args[0], true)
}
}
pub fn run(args: &[String], verbose: u8) -> Result<()> {
let timer = tracking::TimedExecution::start();
let skip = strip_pm_prefix(args);
let effective_args = &args[skip..];
let (linter, explicit) = detect_linter(effective_args);
// Python linters use Command::new() directly (they're on PATH via pip/pipx)
// JS linters use package_manager_exec (npx/pnpm exec)
@@ -78,13 +105,13 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
}
"ruff" => {
// Force JSON output for ruff check
if !args.contains(&"--output-format".to_string()) {
if !effective_args.contains(&"--output-format".to_string()) {
cmd.arg("check").arg("--output-format=json");
}
}
"pylint" => {
// Force JSON2 output for pylint
if !args.contains(&"--output-format".to_string()) {
if !effective_args.contains(&"--output-format".to_string()) {
cmd.arg("--output-format=json2");
}
}
@@ -97,11 +124,11 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
}
// Add user arguments (skip first if it was the linter name, and skip "check" for ruff if we added it)
let start_idx = if is_path_or_flag {
let start_idx = if !explicit {
0
} else if linter == "ruff" && !args.is_empty() && args[0] == "ruff" {
} else if linter == "ruff" && !effective_args.is_empty() && effective_args[0] == "ruff" {
// Skip "ruff" and "check" if we already added "check"
if args.len() > 1 && args[1] == "check" {
if effective_args.len() > 1 && effective_args[1] == "check" {
2
} else {
1
@@ -110,7 +137,7 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
1
};
for arg in &args[start_idx..] {
for arg in &effective_args[start_idx..] {
// Skip --output-format if we already added it
if linter == "ruff" && arg.starts_with("--output-format") {
continue;
@@ -123,7 +150,7 @@ pub fn run(args: &[String], verbose: u8) -> Result<()> {
// Default to current directory if no path specified (for ruff/pylint/mypy/eslint)
if matches!(linter, "ruff" | "pylint" | "mypy" | "eslint") {
let has_path = args
let has_path = effective_args
.iter()
.skip(start_idx)
.any(|a| !a.starts_with('-') && !a.contains('='));
@@ -580,6 +607,80 @@ mod tests {
assert!(result.contains("utils.py"));
}
#[test]
fn test_strip_pm_prefix_npx() {
let args: Vec<String> = vec!["npx".into(), "eslint".into(), "src/".into()];
assert_eq!(strip_pm_prefix(&args), 1);
}
#[test]
fn test_strip_pm_prefix_bunx() {
let args: Vec<String> = vec!["bunx".into(), "eslint".into(), ".".into()];
assert_eq!(strip_pm_prefix(&args), 1);
}
#[test]
fn test_strip_pm_prefix_pnpm_exec() {
let args: Vec<String> = vec!["pnpm".into(), "exec".into(), "eslint".into()];
assert_eq!(strip_pm_prefix(&args), 2);
}
#[test]
fn test_strip_pm_prefix_none() {
let args: Vec<String> = vec!["eslint".into(), "src/".into()];
assert_eq!(strip_pm_prefix(&args), 0);
}
#[test]
fn test_strip_pm_prefix_empty() {
let args: Vec<String> = vec![];
assert_eq!(strip_pm_prefix(&args), 0);
}
#[test]
fn test_detect_linter_eslint() {
let args: Vec<String> = vec!["eslint".into(), "src/".into()];
let (linter, explicit) = detect_linter(&args);
assert_eq!(linter, "eslint");
assert!(explicit);
}
#[test]
fn test_detect_linter_default_on_path() {
let args: Vec<String> = vec!["src/".into()];
let (linter, explicit) = detect_linter(&args);
assert_eq!(linter, "eslint");
assert!(!explicit);
}
#[test]
fn test_detect_linter_default_on_flag() {
let args: Vec<String> = vec!["--max-warnings=0".into()];
let (linter, explicit) = detect_linter(&args);
assert_eq!(linter, "eslint");
assert!(!explicit);
}
#[test]
fn test_detect_linter_after_npx_strip() {
// Simulates: rtk lint npx eslint src/ → after strip_pm_prefix, args = ["eslint", "src/"]
let full_args: Vec<String> = vec!["npx".into(), "eslint".into(), "src/".into()];
let skip = strip_pm_prefix(&full_args);
let effective = &full_args[skip..];
let (linter, _) = detect_linter(effective);
assert_eq!(linter, "eslint");
}
#[test]
fn test_detect_linter_after_pnpm_exec_strip() {
let full_args: Vec<String> =
vec!["pnpm".into(), "exec".into(), "biome".into(), "check".into()];
let skip = strip_pm_prefix(&full_args);
let effective = &full_args[skip..];
let (linter, _) = detect_linter(effective);
assert_eq!(linter, "biome");
}
#[test]
fn test_is_python_linter() {
assert!(is_python_linter("ruff"));
+69 -11
View File
@@ -1698,7 +1698,9 @@ fn main() -> Result<()> {
}
Commands::Proxy { args } => {
use std::process::Command;
use std::io::{Read, Write};
use std::process::{Command, Stdio};
use std::thread;
if args.is_empty() {
anyhow::bail!(
@@ -1718,18 +1720,74 @@ fn main() -> Result<()> {
eprintln!("Proxy mode: {} {}", cmd_name, cmd_args.join(" "));
}
let output = Command::new(cmd_name.as_ref())
let mut child = Command::new(cmd_name.as_ref())
.args(&cmd_args)
.output()
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.context(format!("Failed to execute command: {}", cmd_name))?;
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
let full_output = format!("{}{}", stdout, stderr);
let stdout_pipe = child
.stdout
.take()
.context("Failed to capture child stdout")?;
let stderr_pipe = child
.stderr
.take()
.context("Failed to capture child stderr")?;
// Print output
print!("{}", stdout);
eprint!("{}", stderr);
let stdout_handle = thread::spawn(move || -> std::io::Result<Vec<u8>> {
let mut reader = stdout_pipe;
let mut captured = Vec::new();
let mut buf = [0u8; 8192];
loop {
let count = reader.read(&mut buf)?;
if count == 0 {
break;
}
captured.extend_from_slice(&buf[..count]);
let mut out = std::io::stdout().lock();
out.write_all(&buf[..count])?;
out.flush()?;
}
Ok(captured)
});
let stderr_handle = thread::spawn(move || -> std::io::Result<Vec<u8>> {
let mut reader = stderr_pipe;
let mut captured = Vec::new();
let mut buf = [0u8; 8192];
loop {
let count = reader.read(&mut buf)?;
if count == 0 {
break;
}
captured.extend_from_slice(&buf[..count]);
let mut err = std::io::stderr().lock();
err.write_all(&buf[..count])?;
err.flush()?;
}
Ok(captured)
});
let status = child
.wait()
.context(format!("Failed waiting for command: {}", cmd_name))?;
let stdout_bytes = stdout_handle
.join()
.map_err(|_| anyhow::anyhow!("stdout streaming thread panicked"))??;
let stderr_bytes = stderr_handle
.join()
.map_err(|_| anyhow::anyhow!("stderr streaming thread panicked"))??;
let stdout = String::from_utf8_lossy(&stdout_bytes);
let stderr = String::from_utf8_lossy(&stderr_bytes);
let full_output = format!("{}{}", stdout, stderr);
// Track usage (input = output since no filtering)
timer.track(
@@ -1740,8 +1798,8 @@ fn main() -> Result<()> {
);
// Exit with same code as child process
if !output.status.success() {
std::process::exit(output.status.code().unwrap_or(1));
if !status.success() {
std::process::exit(status.code().unwrap_or(1));
}
}