Skip to content

Commit bb04073

Browse files
committed
fix(inspector): inherit stdio for inspect runner
1 parent 4b4f39c commit bb04073

3 files changed

Lines changed: 77 additions & 22 deletions

File tree

cargo-statum-graph/src/lib.rs

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fs;
55
use std::io;
66
use std::path::Component;
77
use std::path::{Path, PathBuf};
8-
use std::process::{Command, ExitStatus};
8+
use std::process::{Command, ExitStatus, Stdio};
99

1010
use cargo_metadata::{Metadata, MetadataCommand, Package, PackageId};
1111
use statum_graph::CodebaseDoc;
@@ -69,6 +69,7 @@ pub enum Error {
6969
manifest_path: PathBuf,
7070
status: ExitStatus,
7171
details: Option<String>,
72+
diagnostics_reported: bool,
7273
},
7374
}
7475

@@ -137,6 +138,7 @@ impl fmt::Display for Error {
137138
manifest_path,
138139
status,
139140
details,
141+
diagnostics_reported: _,
140142
} => match details {
141143
Some(details) => write!(
142144
formatter,
@@ -153,6 +155,18 @@ impl fmt::Display for Error {
153155
}
154156
}
155157

158+
impl Error {
159+
pub fn diagnostics_reported(&self) -> bool {
160+
matches!(
161+
self,
162+
Self::RunnerFailed {
163+
diagnostics_reported: true,
164+
..
165+
}
166+
)
167+
}
168+
}
169+
156170
impl std::error::Error for Error {
157171
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
158172
match self {
@@ -191,6 +205,7 @@ pub fn run(options: Options) -> Result<Vec<PathBuf>, Error> {
191205
temp_dir.path().join("Cargo.toml"),
192206
&prepared.input.manifest_path,
193207
"codebase export",
208+
RunnerStdio::Captured,
194209
)?;
195210

196211
Ok(bundle_paths(&out_dir, &options.stem))
@@ -216,6 +231,7 @@ pub fn inspect(options: InspectOptions) -> Result<(), Error> {
216231
temp_dir.path().join("Cargo.toml"),
217232
&prepared.input.manifest_path,
218233
"inspect session",
234+
RunnerStdio::Inherited,
219235
)
220236
}
221237

@@ -590,28 +606,59 @@ fn run_runner(
590606
runner_manifest_path: PathBuf,
591607
target_manifest_path: &Path,
592608
operation: &'static str,
609+
stdio: RunnerStdio,
593610
) -> Result<(), Error> {
594-
let output = Command::new("cargo")
611+
let mut command = Command::new("cargo");
612+
command
595613
.arg("run")
596614
.arg("--quiet")
597615
.arg("--manifest-path")
598-
.arg(&runner_manifest_path)
599-
.output()
600-
.map_err(|source| Error::Io {
601-
action: "run generated cargo runner",
602-
path: runner_manifest_path.clone(),
603-
source,
604-
})?;
605-
606-
if output.status.success() {
607-
Ok(())
608-
} else {
609-
Err(Error::RunnerFailed {
610-
operation,
611-
manifest_path: target_manifest_path.to_path_buf(),
612-
status: output.status,
613-
details: normalize_runner_failure_details(&output.stderr, &output.stdout),
614-
})
616+
.arg(&runner_manifest_path);
617+
618+
match stdio {
619+
RunnerStdio::Captured => {
620+
let output = command.output().map_err(|source| Error::Io {
621+
action: "run generated cargo runner",
622+
path: runner_manifest_path.clone(),
623+
source,
624+
})?;
625+
626+
if output.status.success() {
627+
Ok(())
628+
} else {
629+
Err(Error::RunnerFailed {
630+
operation,
631+
manifest_path: target_manifest_path.to_path_buf(),
632+
status: output.status,
633+
details: normalize_runner_failure_details(&output.stderr, &output.stdout),
634+
diagnostics_reported: false,
635+
})
636+
}
637+
}
638+
RunnerStdio::Inherited => {
639+
let status = command
640+
.stdin(Stdio::inherit())
641+
.stdout(Stdio::inherit())
642+
.stderr(Stdio::inherit())
643+
.status()
644+
.map_err(|source| Error::Io {
645+
action: "run generated cargo runner",
646+
path: runner_manifest_path.clone(),
647+
source,
648+
})?;
649+
650+
if status.success() {
651+
Ok(())
652+
} else {
653+
Err(Error::RunnerFailed {
654+
operation,
655+
manifest_path: target_manifest_path.to_path_buf(),
656+
status,
657+
details: None,
658+
diagnostics_reported: true,
659+
})
660+
}
661+
}
615662
}
616663
}
617664

@@ -784,6 +831,12 @@ struct PreparedRun {
784831
patch_root: Option<PathBuf>,
785832
}
786833

834+
#[derive(Clone, Copy)]
835+
enum RunnerStdio {
836+
Captured,
837+
Inherited,
838+
}
839+
787840
#[derive(Clone, Copy)]
788841
enum RunnerMode<'a> {
789842
Export { out_dir: &'a Path, stem: &'a str },

cargo-statum-graph/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ fn main() -> ExitCode {
5050
match run_main() {
5151
Ok(()) => ExitCode::SUCCESS,
5252
Err(error) => {
53-
eprintln!("{error}");
53+
if !error.diagnostics_reported() {
54+
eprintln!("{error}");
55+
}
5456
ExitCode::FAILURE
5557
}
5658
}

cargo-statum-graph/tests/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ fn inspect_command_fails_closed_without_an_interactive_terminal() {
150150

151151
let stderr = String::from_utf8_lossy(&output.stderr);
152152
assert!(
153-
stderr.contains("inspect session"),
154-
"stderr should identify the inspect path, got: {stderr}"
153+
!stderr.contains("inspect session"),
154+
"stderr should not be double-wrapped by the parent CLI, got: {stderr}"
155155
);
156156
assert!(
157157
stderr.contains("requires an interactive terminal on stdin and stdout"),

0 commit comments

Comments
 (0)