Skip to content

fix(run): preserve empty continuation lines and comments among flags - #82

Merged
reteps merged 1 commit into
mainfrom
fix/run-empty-continuations
Nov 7, 2026
Merged

reteps merged 1 commit into
mainfrom
fix/run-empty-continuations

Conversation

@reteps

@reteps reteps commented Nov 7, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes #59.

  • Empty continuation lines (\ alone) in the shell body are preserved in every position, including before && lines and right after the flags. This uses the approach suggested in the issue: each one becomes a marker comment placeholder for the shfmt pass and is turned back into \ afterwards, aligned with the following line. A \ line with no whitespace around it (good\ / \ / night) joins two words into one, so it is left alone. That case came from buildkit's own continueIndent test file.
  • Comments and \ lines before or between RUN flags are preserved. The original source is scanned from the keyword through n.Flags. When such lines exist, each item goes on its own line. Otherwise the existing flag layout is untouched.
  • Fixes a stray \ line that appeared when a comment followed the flags.

Behavior change

tests/out/run5.dockerfile now keeps its \ line before # cleanup, as #59 asks.

Test plan

  • New fixture tests/in/empty-continuation.dockerfile covers every case from the issue plus pipes/||, non-mount flags, trailing whitespace, CMD, and glued words. Its output is idempotent.
  • Unit tests for scanFlagsRegion and isEmptyContinuation
  • Ran old and new binaries over 142 Dockerfiles on disk, including buildkit's parser test files. The only output difference is run5.
  • go test ./...

🤖 Generated with Claude Code

Lines holding only a backslash, used as visual separators inside long RUN
instructions, were dropped wherever mvdan/sh's printer drops them: before
a line starting with "&&" and at the start of the content (right after
the flags). Comments placed before or between RUN flags were dropped too,
because flags are rebuilt from the parsed n.Flags.

- Shell content: turn each empty continuation line into a marker comment
  before shfmt (the comment placeholders already survive every position)
  and back into a "\\" line afterwards, aligned with the next line. A
  "\\" line with no surrounding whitespace joins two words, so it is
  left alone.
- Flags region: scan the original source for comment and empty
  continuation lines among the flags and re-emit them, one item per line.
  Without such lines the existing flag layout is unchanged.
- Drop the redundant continuation the comment handling added after flags
  that already end in " \\", which produced a stray "\\" line.

tests/out/run5.dockerfile now keeps its separator line.

Fixes #59

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@reteps
reteps merged commit ef5fa8f into main Nov 7, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preserve empty continuation lines (backslash-only separator lines) in RUN

1 participant