Skip to content

Fix 'Package import was not used in package declarations' warning#135

Closed
gage-halverson-fetch wants to merge 1 commit intomainfrom
fix/package-import-warning
Closed

Fix 'Package import was not used in package declarations' warning#135
gage-halverson-fetch wants to merge 1 commit intomainfrom
fix/package-import-warning

Conversation

@gage-halverson-fetch
Copy link
Copy Markdown

Summary

This PR fixes the Swift 6 compiler warning "Package import of 'Mocking' was not used in package declarations" that occurs when using package import Mocking with the @Mocked macro.

The Problem

With Swift 6's InternalImportsByDefault feature, the compiler incorrectly flags package imports as unused when the imported module's types are only referenced in macro-generated code. This is because macro expansions happen after the import usage check.

Related Swift Forums discussion: https://forums.swift.org/t/internal-imports-by-default-is-very-problematic-for-generated-code/82722/4

The Solution

For protocols with package access level, the @Mocked macro now generates a top-level variable that explicitly references a type from the Mocking module:

package let _noOpProtocolName: _MockingModule.Type = _MockingModule.self

This ensures the compiler recognizes that the package import is being used in a package declaration.

Changes

  • Added _MockingModule marker enum to Sources/Mocking/Models/
  • Updated @Mocked macro to generate _noOp variable for package access level only
  • Added prefixed(_noOp) to the macro's peer names
  • Updated all affected tests

Test plan

  • All 204 tests pass
  • Verified warning is resolved when using package import Mocking in consuming package

🤖 Generated with Claude Code

For `package` protocols, generate a top-level variable that references
the `_MockingModule` type from Mocking to ensure the compiler recognizes
the package import as used.

This works around a Swift compiler limitation where `package import`
statements are incorrectly flagged as unused when the imported module's
types are only referenced in macro-generated code.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@graycampbell
Copy link
Copy Markdown
Collaborator

So here are my thoughts:

  • Given that this warning does not appear when using public instead of package, it seems to me that this is an issue with the compiler, not Swift Mocking.
  • It's just a warning, not a compiler error, so it's not preventing development.
  • I would prefer we submit a report to Apple and wait for them to fix the compiler than add this workaround because, let's face it, this is pretty gross, even if it is well documented.
  • Hiding this compiler warning just makes it less likely to be found and reported by other people, which will make it less likely to be resolved by Apple.
  • If this warning eventually turns into a compiler error, then we can add this workaround, but I would like this to be a last resort.

@gage-halverson-fetch
Copy link
Copy Markdown
Author

Thanks for the feedback! I'll be closing this in favor of getting swiftlang/swift#86376 merged into Swift

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.

2 participants