refactor: improve RunCommand error semantics to distinguish execution failures from non-zero exits#610
Merged
harp-intel merged 2 commits intomainfrom Jan 9, 2026
Merged
Conversation
… failures from non-zero exits Previously, RunCommand and RunCommandEx returned a non-nil error both when a command failed to execute (e.g., binary not found) and when it executed successfully but returned a non-zero exit code. This was confusing because many commands use non-zero exit codes for normal conditions (e.g., grep returns 1 when no matches found). This change updates the error return semantics: - err is now only non-nil for actual execution failures - exitCode indicates the command's exit status (0 for success, non-zero otherwise) All call sites have been updated to check both err (for execution failures) and exitCode (for command results) separately, providing clearer error messages for each case.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the RunCommand and RunCommandEx functions to return nil error when a command executes successfully but returns a non-zero exit code, reserving the error return for actual execution failures (e.g., binary not found, permission denied). This change improves error semantics by distinguishing between execution failures and non-zero exits.
Changes:
- Modified core
RunCommand/RunCommandExfunctions to seterr = nilwhen a command executes but returns a non-zero exit code - Updated all call sites to check
errandexitCodeseparately with distinct error handling - Improved error messages to differentiate between execution failures and non-zero exit codes
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/target/helpers.go | Core change: sets err = nil for non-zero exit codes, only returning errors for actual execution failures |
| internal/workflow/targets.go | Updates call sites to handle execution errors and non-zero exit codes separately |
| internal/workflow/signals.go | Refactors signal handling to distinguish between execution failures and non-zero exits |
| internal/target/remote_target.go | Updates remote target operations to check both error and exit code |
| internal/target/local_target.go | Updates local target privilege checks to verify both error and exit code |
| internal/script/script.go | Separates execution failure logging from non-zero exit code handling |
| cmd/metrics/process.go | Improves error messages and separates execution vs exit code checks |
| cmd/metrics/nmi_watchdog.go | Updates sysctl command calls to check exit codes separately |
| cmd/metrics/metadata.go | Separates error handling for command execution failures vs non-zero exits |
…nctions Signed-off-by: Harper, Jason M <jason.m.harper@intel.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
RunCommandandRunCommandExto returnnilerror when a command executes successfully but returns a non-zero exit codeerrandexitCodeseparately with distinct error handlingMotivation
Previously,
RunCommandreturned a non-nil error both when:This was confusing because many commands use non-zero exit codes for normal conditions:
grepreturns 1 when no matches foundps -preturns 1 when process doesn't existwhichreturns 1 when command not foundChanges
Core change in
internal/target/helpers.go:Updated call sites pattern: