Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions data_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1434,4 +1434,38 @@ var suites = []FixtureSuite{
},
},
},
// exec with non-existent command should fail gracefully
{
{
Name: "exec non-existent command should not panic",
Config: FixtureConfig{
CliArgs: []string{"exec", "--endpoint", "{{endpoint}}", "command-that-does-not-exist-anywhere"},
},
Expect: Results{
SpanCount: 1,
ExitCode: 127, // exit 127 like shell does for "command not found"
Config: otelcli.DefaultConfig().WithEndpoint("{{endpoint}}"),
},
CheckFuncs: []CheckFunc{
func(t *testing.T, f Fixture, r Results) {
// should send a span with error status
if r.Span == nil {
t.Errorf("expected a span to be sent even when command fails to start")
return
}
if r.Span.Status == nil {
t.Errorf("expected span status to be set")
return
}
if r.Span.Status.Code != 2 { // STATUS_CODE_ERROR
t.Errorf("expected span status code ERROR (2), got %d", r.Span.Status.Code)
}
// error message should mention the command failure
if !strings.Contains(r.Span.Status.Message, "exec command failed") {
t.Errorf("expected error message to contain 'exec command failed', got: %q", r.Span.Status.Message)
}
},
},
},
},
}
16 changes: 13 additions & 3 deletions otelcli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,11 @@ func doExec(cmd *cobra.Command, args []string) {

// append process attributes
span.Attributes = append(span.Attributes, processAttrs...)
pidAttrs := processPidAttrs(config, int64(child.Process.Pid), int64(os.Getpid()))
span.Attributes = append(span.Attributes, pidAttrs...)
// child.Process is nil if the command failed to start (e.g., command not found)
if child.Process != nil {
pidAttrs := processPidAttrs(config, int64(child.Process.Pid), int64(os.Getpid()))
span.Attributes = append(span.Attributes, pidAttrs...)
}

cancelCtxDeadline()
close(signals)
Expand All @@ -169,7 +172,14 @@ func doExec(cmd *cobra.Command, args []string) {
}

// set the global exit code so main() can grab it and os.Exit() properly
Diag.ExecExitCode = child.ProcessState.ExitCode()
// ProcessState is nil if the command failed to start
if child.ProcessState != nil {
Diag.ExecExitCode = child.ProcessState.ExitCode()
} else {
// command failed to start (e.g., command not found)
// use exit code 127 to match shell behavior for "command not found"
Diag.ExecExitCode = 127
}

config.PropagateTraceparent(span, os.Stdout)
}
Expand Down