Skip to content

Commit 5fe8328

Browse files
committed
fix: Redirect the logs from PMD to STDERR when writing to file CF-2115
It seems that when PMD is ran with an output file it logs the info messages to the STDOUT. But when returning results to the STDOUT does not do those logs (as most tools) On the scope of our CLI, we usually propagate a temporary file location even if we output to the STDOUT, so when such file is propagated to PMD, we need to make sure to redirect the STDOUT to STDERR Example ``` Processing files 0% [ ] 0/2 (0:00:00) Violations:0, Errors:0^MProcessing files 50% [========= ] 1/2 (0:00:00) Violations:0, Errors:0^MProcessing files 100% [==================] 2/2 (0:00:00) Violations:0, Errors:1^MProcessing files 100% [==================] 2/2 (0:00:00) Violations:0, Errors:1^MProcessing files 100% [==================] 2/2 (0:00:00) Violations:0, Errors:1 Processing files 100% [==================] 2/2 (0:00:00) Violations:0, Errors:1^MProcessing files 100% [==================] 2/2 (0:00:00) Violations:0, Errors:1 Tool is not installed, installing... Installing revive using go runtime... { "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json", ```
1 parent f0a5bd7 commit 5fe8328

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

tools/pmdRunner.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,15 @@ func RunPmd(repositoryToAnalyseDirectory string, pmdBinary string, pathsToCheck
8686
// Output file
8787
if outputFile != "" {
8888
cmd.Args = append(cmd.Args, "-r", outputFile)
89+
// When storing results in a file, all the logs output should go to stderr
90+
// Note that for formats like SARIF, tools output their results to a temporary file
91+
cmd.Stdout = os.Stderr
92+
} else {
93+
cmd.Stdout = os.Stdout
8994
}
9095

91-
cmd.Dir = repositoryToAnalyseDirectory
9296
cmd.Stderr = os.Stderr
93-
cmd.Stdout = os.Stdout
97+
cmd.Dir = repositoryToAnalyseDirectory
9498

9599
// Get Java runtime info
96100
javaRuntime := config.Runtimes()["java"]

0 commit comments

Comments
 (0)