Repository navigation
Fix crash on imports with attributes on their own line - #366
Merged
Merged
Conversation
`Import(line:)` parsed the import's source text and sliced it between the first and last newline. When an attribute sits on its own line (e.g. `@diagnose(...)` or `@preconcurrency` above `import X`, or a `#if`-wrapped attribute as emitted by swift-openapi-generator 1.14+), the end index lands before the `import ` keyword and the range construction traps. Build `Import` from `ImportDeclSyntax` instead: module path (plus import kind, e.g. `struct Foundation.URL`), `@testable` from the attributes, and the access level from the modifiers. Other attributes are dropped as before; the access level is now kept when an import also has an attribute (`@_spi(X) public import Y` -> `public import Y`). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sidepelican
approved these changes
Nov 14, 2026
sidepelican
left a comment
Collaborator
There was a problem hiding this comment.
Good way. Thank you.
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.
Problem
mockolo traps (SIGTRAP, exit 133,
Fatal error: Range requires lowerBound <= upperBound) when a source file has an import whose attribute is on its own line:The same happens for
@preconcurrencyon its own line, or an attribute wrapped in#if. The last case is now common: swift-openapi-generator 1.14+ starts every generated file withso running mockolo over any target with OpenAPI-generated code crashes (reproduced with 2.6.1, 2.7.0 and current
master).Root cause
Import(line:)parsednode.trimmedDescriptionas text and sliced it between the first and last newline. With a newline between the attribute andimport, the end index lands before theimportkeyword and themoduleNamerange is invalid. Single-line attributes didn't crash, but they made the modifier unparsable, so@_spi(X) public import Ylost its access level.Fix
Build
ImportfromImportDeclSyntaxinstead of from its text: module path (plus import kind, e.g.struct Foundation.URL),@testablefrom the attributes, access level from the modifiers. Other attributes are still dropped, as before. One small behavior change: the access level is now kept when an import also has an attribute (@_spi(X) public import Y→public import Y, previouslyimport Y), consistent with how plainpublic import Yis handled.Tests
Added to
ACLImportTests:testAttributedImports: single-line and own-line attributes (@_spi,@preconcurrency,@_implementationOnly,@testable,@diagnose), including inside an#ifimport block.testOpenAPIGeneratorImportHeader: the swift-openapi-generator header above, plus@preconcurrency public import struct Foundation.URLunder#if os(Linux).Both crash the test process without the fix. Full suite passes locally (Xcode 27.0 / Swift 6.4).
🤖 Generated with Claude Code
Fixes #367