Skip to content

Commit 83ed97f

Browse files
committed
fix: capture stderr output for detailed cargo metadata error reporting
1 parent f19d403 commit 83ed97f

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

src/main/java/io/github/guacsec/trustifyda/providers/CargoProvider.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@ private CargoMetadata executeCargoMetadata() throws IOException, InterruptedExce
329329
Process process =
330330
new ProcessBuilder(cargoExecutable, "metadata", "--format-version", "1")
331331
.directory(workingDir.toFile())
332-
.redirectError(ProcessBuilder.Redirect.DISCARD)
333332
.start();
334333

335334
String output;
@@ -339,16 +338,21 @@ private CargoMetadata executeCargoMetadata() throws IOException, InterruptedExce
339338

340339
boolean finished = process.waitFor(TIMEOUT, TimeUnit.SECONDS);
341340

342-
if (!finished) {
343-
process.destroyForcibly();
344-
throw new InterruptedException("cargo metadata timed out after " + TIMEOUT + " seconds");
345-
}
346-
347341
int exitCode = process.exitValue();
342+
348343
if (exitCode != 0) {
349-
if (debugLoggingIsNeeded()) {
350-
log.warning("cargo metadata failed with exit code: " + exitCode);
344+
String errorOutput = "";
345+
try (InputStream errorStream = process.getErrorStream()) {
346+
errorOutput = new String(errorStream.readAllBytes(), StandardCharsets.UTF_8);
347+
} catch (IOException e) {
348+
log.warning("Failed to read error stream: " + e.getMessage());
351349
}
350+
351+
String errorMessage = "cargo metadata failed with exit code: " + exitCode;
352+
if (!errorOutput.isEmpty()) {
353+
errorMessage += ". Error: " + errorOutput.trim();
354+
}
355+
log.warning(errorMessage);
352356
return null;
353357
}
354358

@@ -359,6 +363,11 @@ private CargoMetadata executeCargoMetadata() throws IOException, InterruptedExce
359363
return null;
360364
}
361365

366+
if (!finished) {
367+
process.destroyForcibly();
368+
throw new InterruptedException("cargo metadata timed out after " + TIMEOUT + " seconds");
369+
}
370+
362371
try {
363372
CargoMetadata metadata = MAPPER.readValue(output, CargoMetadata.class);
364373
if (debugLoggingIsNeeded()) {

0 commit comments

Comments
 (0)