Skip to content

Fix visibility on import triggering a compile warning #2151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Wilfred
Copy link
Contributor

@Wilfred Wilfred commented May 13, 2025

Previously, building sourcekit-lsp would produce the following warning:

$ swift build
Building for debugging...
/Users/wilfred/src/sourcekit-lsp/Sources/SKLogging/CustomLogStringConvertible.swift:13:9: warning: package import of 'Foundation' was not used in package declarations
 11 | //===----------------------------------------------------------------------===//
 12 |
 13 | package import Foundation
    |         `- warning: package import of 'Foundation' was not used in package declarations

Remove the visibility modifier, so no warning is produced.

@Wilfred Wilfred requested a review from ahoppen as a code owner May 13, 2025 12:32
@Wilfred Wilfred force-pushed the fix_foundation_import_warning branch from 40a62e6 to e888c87 Compare May 13, 2025 13:01
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thanks for opening the PR to fix this. Looks good to me.

@ahoppen
Copy link
Member

ahoppen commented May 13, 2025

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge May 13, 2025 13:44
auto-merge was automatically disabled May 13, 2025 15:51

Head branch was pushed to by a user without write access

@Wilfred Wilfred force-pushed the fix_foundation_import_warning branch from e888c87 to f44489c Compare May 13, 2025 15:51
@Wilfred
Copy link
Contributor Author

Wilfred commented May 13, 2025

Argh, the package import is needed when you're using the non-darwin logger. I've updated the PR.

@ahoppen
Copy link
Member

ahoppen commented May 13, 2025

Thanks ❤️

@ahoppen
Copy link
Member

ahoppen commented May 13, 2025

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge May 13, 2025 16:07
@ahoppen
Copy link
Member

ahoppen commented May 13, 2025

@swift-ci Please test Windows

@ahoppen
Copy link
Member

ahoppen commented May 14, 2025

Looks like there are some more import-related errors. Could you take a look at them? If you can’t figure them out, let me know and I can help investigate

Previously, building sourcekit-lsp would produce the following
warning when building on macOS:

    $ swift build
    Building for debugging...
    /Users/wilfred/src/sourcekit-lsp/Sources/SKLogging/CustomLogStringConvertible.swift:13:9: warning: package import of 'Foundation' was not used in package declarations
     11 | //===----------------------------------------------------------------------===//
     12 |
     13 | package import Foundation
        |         `- warning: package import of 'Foundation' was not used in package declarations

Remove the visibility modifier, so no warning is produced.
auto-merge was automatically disabled May 16, 2025 15:26

Head branch was pushed to by a user without write access

@Wilfred Wilfred force-pushed the fix_foundation_import_warning branch from f44489c to 6b4b4b2 Compare May 16, 2025 15:26
@Wilfred
Copy link
Contributor Author

Wilfred commented May 16, 2025

OK, I've tweaked the conditional to check whether we're on macOS, which seems to fix the build.

I don't understand why the warning only triggered on macOS though. I see InternalImportsByDefault is set in Package.swift.

/home/wilfred/src/sourcekit-lsp/Sources/SKLogging/CustomLogStringConvertible.swift:40:21: error: class cannot be declared package because its superclass is internal
 14 | import Foundation
 15 | #else
 16 | import Foundation
    | `- note: class 'NSObject' imported as 'internal' from 'Foundation' here
 17 | #endif
 18 | 
    :
 38 | /// thus pass this object to OSLog, which just forwards to `description` or
 39 | /// `redactedDescription` of an object that implements `CustomLogStringConvertible`.
 40 | package final class CustomLogStringConvertibleWrapper: NSObject, Sendable {
    |                     |- error: class cannot be declared package because its superclass is internal
    |                     `- note: class 'NSObject' is imported by this file as 'internal' from 'Foundation'

This is the build failure I see on my Linux box, which suggests that the Linux build is treating import Foundation as private import Foundation, whereas the macOS build isn't for some reason. CustomLogStringConvertibleWrapper is still defined on both platforms unless I'm missing something.

TLDR: This fixes the warning and I think should make CI green, although there's some detail I don't understand.

@bnbarham
Copy link
Contributor

I believe the difference here is where NSObject is defined (seems to be Foundation on non-Darwin), though I'm not positive how we end up finding a package-visible-NSObject on macOS 🤔

We could maybe make this:

#if canImport(ObjectiveC)
import Foundation
package import ObjectiveC
#else
package import Foundation
#endif

To make it super obvious, or just:

#if canImport(ObjectiveC)
import Foundation
#else
package import Foundation
#endif

?

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