Skip to content

Preserve shebang line when source file contains no imports#1207

Open
itsybitsybootsy wants to merge 1 commit into
swiftlang:mainfrom
itsybitsybootsy:fix/shebang-keep-on-own-line
Open

Preserve shebang line when source file contains no imports#1207
itsybitsybootsy wants to merge 1 commit into
swiftlang:mainfrom
itsybitsybootsy:fix/shebang-keep-on-own-line

Conversation

@itsybitsybootsy
Copy link
Copy Markdown
Contributor

Problem

OrderedImports rewrites every source file, including ones with no imports at all. For an input like

#!/usr/bin/env swift
// (c) Acme Inc.

print("Hello, World!")

the rule's convertToCodeBlockItems pass rebuilds the AST and ends up gluing the comment onto the shebang line:

#!/usr/bin/env swift  // (c) Acme Inc.

print("Hello, World!")

which is no longer a valid shebang. The variant with a blank line between the shebang and the comment is also broken, and the output is non-idempotent (the first format pass deletes the blank line, the second pass produces the collapsed version above).

The root cause is that orderImports(in:) is called unconditionally, even on files where the rule has nothing to do. The rebuild then loses the trivia attached to the shebang because the shebang has no syntax node of its own.

Resolves #1194.

Fix

Short-circuit orderImports(in:) at the top: if none of the Lines the generator produced are of any import type (.regularImport, .declImport, .implementationOnlyImport, .testableImport), return the original CodeBlockItemListSyntax unchanged. The rule has nothing to reorder, so its output should equal its input down to the trivia.

After the fix, both reproducers in the issue format to themselves:

#!/usr/bin/env swift
// (c) Acme Inc.

print("Hello, World!")

and

#!/usr/bin/env swift

// (c) Acme Inc.

print("Hello, World!")

Scope note

This PR fixes the no-imports cases from the issue. The mixed case where a file has both a shebang and imports is a separate, broader bug in how OrderedImports rebuilds trivia around the file header and is intentionally not addressed here.

Tests

Added two tests to GarbageTextTests.swift:

  • testHashBangFollowedByLineComment — covers the first reproducer; runs both through the pretty-printer and through the full SwiftFormatter pipeline (the bug only surfaced via the full pipeline, not the pretty-printer alone).
  • testHashBangFollowedByBlankLineAndComment — covers the second reproducer.

swift test locally: 915 tests, 1 skipped, 0 failures.


This contribution was developed with the help of Claude Code. The fix, regression tests, and the full local test suite were reviewed manually before submitting.

The rule walks the source file's top-level statements, reorders any
imports, and rebuilds the surrounding trivia. When there are no imports
to reorder it still runs the rebuild step, which can collapse the
trailing newline that separates a `#!` shebang line from the first
statement's leading comment. The CLI output then renders the shebang
fused with that comment on a single line.

When the rule finds no import statements the result is the unchanged
input. Returning the original `CodeBlockItemListSyntax` in that case
preserves shebang and file-header trivia exactly and avoids the fusion.

Adds GarbageText tests for shebang followed by a leading comment and
for shebang separated from a leading comment by a blank line, exercised
through both the pretty-print path and the full SwiftFormatter
pipeline so the regression doesn't slip back through one of them.
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.

Shebang syntax is broken by swift-format 6.3

1 participant