fix: Redirect the logs from PMD to STDERR when writing to file CF-2115#186
fix: Redirect the logs from PMD to STDERR when writing to file CF-2115#186
Conversation
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",
```
Coverage summary from CodacySee diff coverage on Codacy
Coverage variation details
Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: Diff coverage details
Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: See your quality gate settings Change summary preferences |
There was a problem hiding this comment.
Pull request overview
This PR fixes an issue where PMD outputs progress logs to STDOUT when writing analysis results to a file, which pollutes the output stream. The fix redirects STDOUT to STDERR when an output file is specified, ensuring clean separation of analysis results from progress logs.
Key Changes
- Added conditional logic to redirect
cmd.Stdouttoos.Stderrwhen PMD writes to an output file - Maintained
cmd.Stdout = os.Stdoutbehavior when no output file is specified - Minor reordering of command setup (swapped order of Stderr and Dir assignment, no functional impact)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codacy's Analysis Summary0 new issue (≤ 1 medium issue) Review Pull Request in Codacy →
|
1 similar comment
Codacy's Analysis Summary0 new issue (≤ 1 medium issue) Review Pull Request in Codacy →
|
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