Skip to content

Commit 6930c35

Browse files
committed
fix: trim command whitespace in Docker script to avoid trailing newline issues
YAML literal blocks (|) include trailing newlines, causing commands like 'grep --version\n' to be passed to the shell. When quoted with %q, this becomes 'grep --version\n' which the shell interprets as '--versionn'. Now trimming whitespace from commands before embedding in the script.
1 parent 0616cf6 commit 6930c35

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

pkg/runner/docker.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,13 @@ func (r *Docker) createScriptFile(shell string, cmd string, env []string) (strin
423423
r.logger.Debug("Added preparation command to script: %s", r.opts.PrepareCommand)
424424
}
425425

426-
// Add the main command
426+
// Add the main command (trim whitespace to avoid issues with trailing newlines from YAML literal blocks)
427427
content.WriteString("# Main command to execute\n")
428+
trimmedCmd := strings.TrimSpace(cmd)
428429
if shell != "" {
429-
fmt.Fprintf(&content, "exec %s -c %q\n", shell, cmd)
430+
fmt.Fprintf(&content, "exec %s -c %q\n", shell, trimmedCmd)
430431
} else {
431-
fmt.Fprintf(&content, "exec sh -c %q\n", cmd)
432+
fmt.Fprintf(&content, "exec sh -c %q\n", trimmedCmd)
432433
}
433434

434435
// Write the content to the file

0 commit comments

Comments
 (0)