Skip to content

Lexical Context behaves differently in Unit Tests compared to App/Package usage #79282

Open
@Jeehut

Description

@Jeehut

Description

Similar to swiftlang/swift-syntax#2559 (which has been resolved in the meantime), I'm experiencing a difference between my unit tests and actually running a Swift macro in an app project or even within another target in the same package. You can see it all in action in my new TranslateKit Swift package.

The freestanding expression macro #tk was defined here:
https://github.com/FlineDev/TranslateKit/blob/main/Sources/TranslateKit/Macros.swift

And the full macro implementation can be found here:
https://github.com/FlineDev/TranslateKit/blob/main/Sources/TranslateKitMacros/TranslationKey.swift

Steps to Reproduce

When I write this code using my #tk macro linked above:

import SwiftUI

extension MyNamespace {
   struct MyView: View {
      var body: some View {
         Button(#tk("Save Changes")) { 
            self.handleSave()
         }
      }
   }
}

I would expect the #tk("Save Changes") macro to get expanded to this in both unit tests and application code:

String(localized: "MyNamespace.MyView.Body.saveChanges", defaultValue: "Save Changes")

But I only get the expected expansion in unit tests, but not in actual code. There, I get this instead:

String(localized: "MyNamespace.MyView.saveChanges", defaultValue: "Save Changes")

It seems that in unit tests I get more entries in the lexical context than in actual code.

I'm using Xcode 15.2 final version. I do not have any other Swift versions installed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    swift macroFeature → declarations: Swift `macro` declarations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions