Skip to content

Use the package access level instead of @testable imports #1215

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

Closed
wants to merge 2 commits into from

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Mar 17, 2025

@testable imports have a number of build-time drawbacks, including:

  • Building the package in release configuration and then running tests requires the entire package to be rebuilt because SwiftPM needs to pass --enable-testing to the compiler invocations.
  • The usage of @testable means that swift-foundation cannot be included in the unified (aka. multiroot) build of all the other packages in Swift CI, which means that it needs to re-build swift-syntax, unnecessarily increasing CI time.

To fix this, make all @testable imports normal imports and mark the declarations that were previously accessed through @testable as package.

@ahoppen ahoppen requested a review from jmschonfeld March 17, 2025 23:51
@itingliu
Copy link
Contributor

@swift-ci please test

Copy link
Contributor

@jmschonfeld jmschonfeld left a comment

Choose a reason for hiding this comment

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

One overall comment: I just want to make sure that in all scenarios (building as a package, being built as part of the toolchain etc.) that making these package will not allow any client to start using them (unless they're doing some @_silgen_name shenanigans) since we don't actually install the package swift interface file anywhere, right?

@@ -30,7 +30,7 @@ extension AttributedString {
typealias _AttributeStorage = AttributedString._AttributeStorage

var version: Version
var string: BigString
package var string: BigString
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this is only used from one place: NumberFormatStyleTests which could easily do String(self.characters) instead of String(self._guts.string) which would save us from needing to mark this and the imports above as package.

For cases like this, would you rather we update them in a separate change and keep this as just a @testable --> package change, or would you want to incorporate that into this to reduce the number of things that we import as package like the collections modules above?

Copy link
Member Author

@ahoppen ahoppen Mar 21, 2025

Choose a reason for hiding this comment

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

I’d prefer if this is just a mechanical @testable -> package change. It’s fairly big already and I don’t want to hide any other functionality change in it.

So, if you could make those changes up-front, that would be great.

@@ -38,7 +38,7 @@ extension Date {
timeIntervalSinceReferenceDate / 86400 + Self.julianDayAtDateReference
}

func julianDay() throws (GregorianCalendarError) -> Int {
package func julianDay() throws (GregorianCalendarError) -> Int {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see uses of this, the initializer below, or the error type from our tests, was this an accidental addition?

Copy link
Member Author

@ahoppen ahoppen Mar 21, 2025

Choose a reason for hiding this comment

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

It’s used here:

That being said, the test should probably call julianDay instead of just getting a function reference to it 😉

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yeah that's probably why I missed it in my search since it didn't actually call the function - ok thanks for the explanation

@@ -35,7 +35,7 @@ public protocol FileManagerDelegate : AnyObject, Sendable {
}

// Default implementations for String-based requirements
extension FileManagerDelegate {
package extension FileManagerDelegate {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should actually be public instead of just package visibility, since we want clients to get these default implementations too (I guess this goes to show how uncommon it is for people to use this delegate on Linux/Windows)

Copy link
Member Author

Choose a reason for hiding this comment

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

Similar here, do you want to make a separate PR for that so we don’t hide functionality change in here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah we can put this in another PR

@@ -15,9 +15,9 @@ import TestSupport
#endif

#if FOUNDATION_FRAMEWORK
@testable import Foundation
Copy link
Contributor

Choose a reason for hiding this comment

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

Since TestSupport has an @_exported import Foundation, we can just remove these few lines entirely. The only reason they're spelled out here is because @_exported doesn't "re-export" the @testable-ness of the import. Same goes for any files that have changes like this

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed all imports to Foundation(Essentials|Internationalization|) when TestSupport is imported here: a23e682

#endif

final class ICUPatternGeneratorTests: XCTestCase {

typealias DateFieldCollection = Date.FormatStyle.DateFieldCollection
package typealias DateFieldCollection = Date.FormatStyle.DateFieldCollection
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this needed? The test module isn't a dependency of any other module, so it shouldn't need anything more permissive than internal

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I changed this by mistake

ahoppen added 2 commits March 21, 2025 16:18
`@testable` imports have a number of build-time drawbacks, including:
- Building the package in `release` configuration and then running tests requires the entire package to be rebuilt because SwiftPM needs to pass `--enable-testing` to the compiler invocations.
- The usage of `@testable` means that swift-foundation cannot be included in the unified (aka. multiroot) build of all the other packages in Swift CI, which means that it needs to re-build swift-syntax, unnecessarily increasing CI time.

To fix this, make all `@testable` imports normal imports and mark the declarations that were previously accessed through `@testable` as `package`.

a
…TestSupport` is imported

TestSupport has `@_exported` imports for those modules.
@ahoppen
Copy link
Member Author

ahoppen commented Mar 21, 2025

One overall comment: I just want to make sure that in all scenarios (building as a package, being built as part of the toolchain etc.) that making these package will not allow any client to start using them (unless they're doing some @_silgen_name shenanigans) since we don't actually install the package swift interface file anywhere, right?

I am pretty sure that we don’t install .package.swiftinterface but to double-check I just kicked off toolchain builds in swiftlang/swift#37710 and will check them.

@ahoppen
Copy link
Member Author

ahoppen commented Mar 24, 2025

Looks like the package symbols are being successfully stripped.

I checked the Linux build:

ls -lah usr/lib/swift/linux/Foundation.swiftmodule 
total 9912
drwxr-xr-x@  4 alex  staff   128B Mar 21 17:23 ./
drwxr-xr-x@ 67 alex  staff   2.1K Mar 24 13:43 ../
-rw-r--r--@  1 alex  staff   278K Mar 21 17:18 x86_64-unknown-linux-gnu.swiftdoc
-rw-r--r--@  1 alex  staff   4.6M Mar 21 17:18 x86_64-unknown-linux-gnu.swiftmodule

Grepping both .swiftmodule and .swiftdoc for numericStringRepresentationForBinaryInteger did not return any results

Windows also only contains .swiftmodule files (no .swiftinterface). macOS toolchains don’t contain Foundation, so there’s nothing to check here.

@jmschonfeld
Copy link
Contributor

@swift-ci please test

Copy link
Contributor

@parkera parkera left a comment

Choose a reason for hiding this comment

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

I think it is extremely unfortunate to lose the meaning of what internal vs. package means, even for the benefit of improved CI times. This will also make all of these exported symbols on Darwin. Let's look for another solution.

@ahoppen ahoppen closed this Mar 25, 2025
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.

4 participants