Skip to content

[Observation] Use absolute path in synthesized #sourceLocation#89202

Open
airspeedswift wants to merge 3 commits into
swiftlang:mainfrom
airspeedswift:fix-observable-macro-sourcelocation-filepath
Open

[Observation] Use absolute path in synthesized #sourceLocation#89202
airspeedswift wants to merge 3 commits into
swiftlang:mainfrom
airspeedswift:fix-observable-macro-sourcelocation-filepath

Conversation

@airspeedswift

Copy link
Copy Markdown
Member

The @Observable macro's locationAnnotated helper wraps relocated didSet/willSet bodies in a #sourceLocation directive so diagnostics point back to the user's spelled location. It was using the default location(of:) overload, which produces #fileID-style paths (Module/file.swift) — not a filesystem path.

When SourceKit or Xcode tries to resolve a diagnostic or index entry whose location was remapped through that directive, the relative identifier doesn't open as a file. The user-visible symptom is "the file couldn't be opened because there is no such file" in the editor, with diagnostics and error navigation breaking for the file.

Pass filePathMode: .filePath to use the absolute path.

fixes rdar://174093766

@airspeedswift airspeedswift requested a review from a team as a code owner May 17, 2026 22:49
@airspeedswift

Copy link
Copy Markdown
Member Author

@swift-ci please smoke test

Comment thread lib/Macros/Sources/ObservationMacros/ObservableMacro.swift

@hamishknight hamishknight left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! As mentioned above I think it would be nice to add a test to verify that #fileID does the correct thing in the expansion

The `@Observable` macro's `locationAnnotated` helper wraps relocated
`didSet`/`willSet` bodies in a `#sourceLocation` directive so diagnostics
point back to the user's spelled location. It was using the default
`location(of:)` overload, which produces `#fileID`-style paths
(`Module/file.swift`) — not a filesystem path.

When SourceKit or Xcode tries to resolve a diagnostic or index entry
whose location was remapped through that directive, the relative
identifier doesn't open as a file. The user-visible symptom is "the file
couldn't be opened because there is no such file" in the editor, with
diagnostics and error navigation breaking for the file.

Pass `filePathMode: .filePath` to use the absolute path.
…rceLocation

The @observable macro wraps didSet/willSet bodies in
#sourceLocation(file: "/abs/path.swift", ...). Confirm that #fileID
inside such a body still evaluates to the Module/file.swift form rather
than the absolute path.
@airspeedswift airspeedswift force-pushed the fix-observable-macro-sourcelocation-filepath branch from c66bda3 to 6123163 Compare May 24, 2026 03:10
@airspeedswift

Copy link
Copy Markdown
Member Author

@swift-ci please smoke test

@airspeedswift

Copy link
Copy Markdown
Member Author

with swiftlang/sourcekit-lsp#2666

@swift-ci Please smoke test Windows platform

@airspeedswift

Copy link
Copy Markdown
Member Author

@swift-ci please smoke test windows platform

1 similar comment
@airspeedswift

Copy link
Copy Markdown
Member Author

@swift-ci please smoke test windows platform

When the synthesized `#sourceLocation` path contains backslashes,
swift-syntax emits a raw string literal (`#"..."#`) instead of a plain
`"..."`. Make the FileCheck pattern accept either form.
@airspeedswift

Copy link
Copy Markdown
Member Author

@swift-ci please smoke test and merge

1 similar comment
@airspeedswift

Copy link
Copy Markdown
Member Author

@swift-ci please smoke test and merge

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.

3 participants