Repository navigation
Conversation
maxches99
requested review from
allevato,
dylansturg,
hamishknight,
hborla and
rintaro
as code owners
August 2, 2026 13:09
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
force-pushed
the
ordered-imports-compat-1242
branch
from
November 9, 2026 10:30
9572c7e to
43d8406
Compare
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds an
orderedImports.onlyGroupTestableImportsconfiguration 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
@testableattribute — even when it isn't the first attribute, e.g.@_spi(Testing) @testable import Foo— is now moved into the@testablegroup, and@_implementationOnlyimports 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
orderedImports.onlyGroupTestableImports(defaultfalse, so the current 603 behavior is unchanged). Whentrue, the pre-603 classification is used: only an import whose first attribute is@testableis placed in the@testablegroup, no separate group is created for@_implementationOnlyimports, 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.OrderedImportsConfiguration. The struct relied on synthesizedCodable, which throws on missing keys — so adding any new option would have broken every existing configuration file containing anorderedImportsobject (and a partial object such as{"includeConditionalImports": true}already failed to decode today). It now has an explicitinit(from:)that defaults missing keys, consistent with how the top-levelConfigurationdecodes.RuleDocumentation.mdregenerated viagenerate-swift-format) andDocumentation/Configuration.md.@testableimports still being grouped, and decoding of partialorderedImportsconfiguration 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.