As the lead of a Swift codebase split into Core and Feature modules in one package plus an app, what do you make `public` versus `package`, and what import access policy do you set?
answer
- who sits outside the package
- a package name the build passes
- imports carry access levels too
- plain imports are public until a future mode
basics
~20 sMake public only what modules outside the package must use, give cross-module plumbing package access, and leave the rest internal. Then decide whether imports stay implicitly public or become explicit, with InternalImportsByDefault and public import for real API dependencies.
solid answer
~50 s`package` (Swift 5.9) makes a declaration visible to every module compiled with the same `-package-name` and to no module outside it, so Core can share helpers with Feature without the app seeing them. Anything the app or another package calls must be `public`, and no public signature may mention a `package` type. Access-level imports (Swift 6.0) apply the same idea to dependencies: `internal import Storage` stops Core's public signatures from exposing Storage's types. A plain `import` is still `public` in Swift 5 and Swift 6 modes; the upcoming feature `InternalImportsByDefault` makes it `internal`. There is no single right policy: explicit imports catch dependency leaks and, for resilient modules, let clients skip loading hidden dependencies, but they cost migration churn, and a plain import of a module beside an explicit `internal` or `package` import of it in another file is an error. Pick one policy per module and enforce it.
code
swift · 16 lines// Core module, built with -package-name NewsReader
internal import Storage // an implementation detail of Core
public struct Article { // the API the app outside the package uses
public let title: String
public init(title: String) { self.title = title }
}
package struct ArticleCache { // shared with Feature, hidden from the app
package init() {}
package private(set) var count = 0
package mutating func insert(_ article: Article) { count += 1 }
}
func load() -> Record { Record() } // internal: may use the internal import
// public func load() -> Record // error: its result uses an internal typego deeper
Recall that package sits between public and internal and that imports can carry an access level.
Explain how the package name is supplied, and that plain imports stay public in Swift 5 and Swift 6 modes.
Diagnose the errors a migration produces, from internal types in public signatures to the ambiguous mixed-import error, and what hiding buys only for resilient modules.
Weigh package-scoped surface and strict imports against churn, choose per module, and enforce it consistently instead of file by file.
## What `package` adds `package` arrived in Swift 5.9 (SE-0386) and sits between `public` and `internal`. A `package` declaration is visible to **every module compiled with the same package name** and to no other module. The name is not written in Swift source: the build passes it with `-package-name`, SwiftPM passes the package's identity automatically, and Xcode exposes it as the Package Access Identifier build setting. Without a package name, `package` is an error: `the package access level used on 'ArticleCache' requires a package name; set it with the compiler flag -package-name`. Rules that shape the decision: - a `public` declaration may not use a `package` type in its signature (`function cannot be declared public because its result uses a package type`); - a `package class` cannot be subclassed, and its members cannot be overridden, from another module, even inside the package; Swift has no spelling for "open within the package"; - `package(set)` lets the package's modules write a property that outside clients only read; - an app module compiled with a different package name, or with none, sees only `public` and `open`. ## Access levels on imports Since Swift 6.0 (SE-0409) an `import` can carry `public`, `package`, `internal`, `fileprivate` or `private`; `open` is rejected. The level is an **upper bound** on how the imported declarations may be used in that file. With `internal import Storage`, a `public func load() -> Record` fails with `function cannot be declared public because its result uses an internal type`, and `@inlinable` bodies cannot mention Storage either. Defaults depend on the mode: | Setting | A plain `import Storage` is | |---|---| | Swift 5 or Swift 6 language mode | `public` | | `-enable-upcoming-feature InternalImportsByDefault` | `internal` | `InternalImportsByDefault` belongs to a future language mode, so Swift 6 mode does **not** turn it on. Two diagnostics matter for a policy. If one file writes `internal import Storage` and another writes a plain `import Storage`, the build fails: `ambiguous implicit access level for import of 'Storage'; it is imported as 'internal' elsewhere`. And an explicit `public import` that no public declaration uses earns a warning that it was not used in public declarations or inlinable code. A related upcoming feature, `MemberImportVisibility` (Swift 6.1, also off until enabled), stops extension members of a module you only import transitively from resolving: the compiler then asks for a direct import of the defining module. ## What hiding a dependency buys - **Compile-time honesty**: Core's public API cannot quietly start exposing a dependency's types. - **Fewer loaded modules for clients**, but only in some builds: a client can skip a hidden dependency when Core is resilient (built with library evolution). A non-resilient module's dependencies are still loaded by its clients, `package` imports are loaded by modules in the same package, and a client with a `@testable import` loads everything. ## Policies a lead chooses between | Policy | Surface | Imports | Cost | |---|---|---|---| | Minimal | `public` for whatever crosses a module | plain imports | Package helpers leak to the app; dependency types leak silently | | Package-scoped | `public` only for the app's API, `package` for Core-to-Feature plumbing | plain imports | Low churn; dependency leaks remain possible | | Strict | as above | `InternalImportsByDefault` on, `public import` only for API dependencies | Migration churn, every file explicit, best enforcement | ## Making the call 1. List what crosses the package boundary: that alone is `public`, and anything a module outside the package must subclass is `open`. Everything Feature needs from Core but the app does not is `package`. 2. Where Feature needs to customize Core across modules, prefer a protocol over a class hierarchy, since `package` cannot express "subclassable within the package". 3. Pick one import policy per module and apply it to every file: writing `internal` or `package` on some imports of a module while other files keep a plain import of it hits the ambiguous-access error. 4. Enable `InternalImportsByDefault` (and, if desired, `MemberImportVisibility`) through the build settings for the modules you migrate; the manifest mechanics belong with Swift Package Manager. 5. Revisit when a module becomes a binary framework shipped with library evolution, where hidden dependencies start paying off in client builds.
- Feature needs to subclass a Core class, but the app must not. Can `package` express that in Swift?No. A `package class` cannot be subclassed from another module, even in the same package, and making it `open` also exposes it to the app. The usual answer is a `package` or `public` protocol that Feature conforms to, keeping the class `final` or `package`.
- Why can enabling `InternalImportsByDefault` in a Swift module break the build of files that did not change?Every plain `import` in that module becomes `internal`, so any public declaration whose signature mentions a type from that module now fails until its file says `public import`. If only some files were edited to explicit levels while the flag is off, a plain import in one file beside an explicit `internal` or `package` import of the same module in another is itself an error.
- package declarations are visible to every module that imports them
- Swift 6 language mode makes plain imports internal by default
- A package class can be subclassed by any module in the same package
- internal import always stops clients from loading the dependency
- Some files can write internal import Storage while others keep a plain import Storage
Assumes Swift 6.4: package access since Swift 5.9, access-level imports since 6.0 with plain imports public in Swift 5 and 6 modes; InternalImportsByDefault and MemberImportVisibility (6.1) are upcoming features off until enabled.