Skip to content

Add option to restore pre-603 import grouping in OrderedImports - #1255

Open
maxches99 wants to merge 1 commit into
swiftlang:mainfrom
maxches99:ordered-imports-compat-1242
Open

maxches99 wants to merge 1 commit into
swiftlang:mainfrom
maxches99:ordered-imports-compat-1242

Conversation

@maxches99

Copy link
Copy Markdown

Adds an orderedImports.onlyGroupTestableImports configuration option that restores the import grouping behavior of swift-format 602.0.0 and earlier, as suggested by @allevato in #1242.

Fixes #1242.

Motivation

#1013 had an unintended side effect on existing code: any import with a @testable attribute — even when it isn't the first attribute, e.g. @_spi(Testing) @testable import Foo — is now moved into the @testable group, and @_implementationOnly imports are moved into a new group of their own. This changed formatting output between 602 and 603 with no configuration escape hatch, which breaks projects that enforce formatting in CI.

Changes

  • New option orderedImports.onlyGroupTestableImports (default false, so the current 603 behavior is unchanged). When true, the pre-603 classification is used: only an import whose first attribute is @testable is placed in the @testable group, no separate group is created for @_implementationOnly imports, and imports with any other attributes are grouped and sorted together with regular imports. The legacy classification is taken verbatim from the pre-OrderedImports should create separate group for @_implementationOnly … #1013 implementation.
  • Robust decoding of OrderedImportsConfiguration. The struct relied on synthesized Codable, which throws on missing keys — so adding any new option would have broken every existing configuration file containing an orderedImports object (and a partial object such as {"includeConditionalImports": true} already failed to decode today). It now has an explicit init(from:) that defaults missing keys, consistent with how the top-level Configuration decodes.
  • Documentation: rule doc comment (with RuleDocumentation.md regenerated via generate-swift-format) and Documentation/Configuration.md.
  • Tests: the exact scenario from 602.0.0 and 603.0.0 produce different output with the same configuration #1242 (file is left untouched with the option enabled), sorting of attributed imports within the regular group, leading-@testable imports still being grouped, and decoding of partial orderedImports configuration objects.

Verification

On the reproduction from #1242, default behavior is unchanged, and with {"orderedImports": {"onlyGroupTestableImports": true}} the output matches 602.0.0 byte-for-byte. Full test suite passes.

The OrderedImports changes in swiftlang#1013 had an unintended side effect: any
import with a @testable attribute (even when it is not the first
attribute) is now placed in the @testable group, and @_implementationOnly
imports are placed in a new group of their own. This changed the output
for existing code compared to swift-format 602.0.0 and broke formatting
stability for projects that check formatting in CI.

Add an orderedImports.onlyGroupTestableImports configuration option
which restores the previous behavior: only imports whose first attribute
is @testable are placed in a separate group, and imports with any other
attributes are grouped and sorted together with regular imports. The
default behavior is unchanged.

Also decode OrderedImportsConfiguration with explicit defaults for
missing keys so that configuration files which omit some of the options
(including all files written before this change) continue to decode.

Fixes swiftlang#1242.
@maxches99
maxches99 force-pushed the ordered-imports-compat-1242 branch from 9572c7e to 43d8406 Compare November 9, 2026 10:30
@maxches99

Copy link
Copy Markdown
Author

Gentle ping — rebased onto current main. This adds an option to restore the pre-603 import grouping in OrderedImports (#1242). @allevato, would you have a chance to review it? The workflow run will need approval.

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.

602.0.0 and 603.0.0 produce different output with the same configuration

1 participant