Repository navigation
Sanitize non-identifier characters in preview replacement module name - #45
Merged
Merged
Conversation
ReplacementModuleName embeds the source file's base name (minus the .swift extension) in the module name. File names such as FooView.ui.swift or Foo+Bar.swift therefore produce an invalid identifier and swiftc rejects the thunk before compiling anything: module name "App_PreviewReplacement_FooView.ui_0" is not a valid identifier Replace every character outside [A-Za-z0-9_] with '_'.
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.
Summary
ReplacementModuleNameembeds the source file's base name in the thunk module name ({Module}_PreviewReplacement_{FileName}_{N}). A file name containing characters that are not valid in a Swift identifier (.,+,-, spaces, …) makes that an invalid module name, andswiftcrejects the thunk before compiling anything.This PR replaces every character outside
[A-Za-z0-9_]with_, so previews work regardless of what characters a project uses in its file names.Problem
A module name has to be a valid Swift identifier, but axe users are free to put other characters in their file names. Whatever the convention, the thunk then fails before anything is compiled:
Two examples we hit:
Foo+Bar.swift— the common Swift name for an extension file (String+Validation.swift, …).FooView.ui.swift— a project-specific convention in our codebase. We suffix UI component files with.uiso that AI coding tools can tell a UI component from other Swift files by name alone (e.g. to pick capture targets). It is not a general Swift convention, but it is a legal file name, and.is exactly the kind of character this code should tolerate.The source itself is never parsed; the failure is purely in the
-module-nameargument.Fix
Notes on the choice:
Foo+Bar.swiftandFoo_Bar.swiftmap to the same base, but the trailing counter is the per-session reload counter (also used forthunk_<N>.dylib), so two files never share a module name within one host process. This is the same property the existing code already relied on for files with identical base names in different directories.swiftcdoes accept Unicode module names, so[^\p{L}\p{N}_]would preserve them, but I kept the conservative ASCII set since Go's\p{L}and Swift's identifier grammar are not guaranteed to agree. Happy to switch if you prefer readable non-ASCII names.Verification
TestReplacementModuleNamenow coversFooView.ui.swift → FooView_uiandFoo+Bar.swift → Foo_Bar.*.ui.swiftfile that previously failed with the error above now captures.