|
2 | 2 |
|
3 | 3 | ## Overview
|
4 | 4 |
|
5 |
| -The number of package dependencies often grows, with that, a name collision can occur among modules from different packages. Module names such as `Logging` or `Utils` are common examples. In order to resolve the collision, SwiftPM (in 5.7+) introduces a new parameter `moduleAliases`, which allows a user to define new unique names for the conflicting modules without requiring any source code changes. |
| 5 | +The number of package dependencies often grows, with that, a name collision can occur among modules from different packages. Module names such as `Logging` or `Utils` are common examples. In order to resolve the collision, SwiftPM (in 5.7+) introduces a new parameter `moduleAliases`, which allows a user to define new unique names for the conflicting modules without requiring any source code changes. |
6 | 6 |
|
7 | 7 | ## How to Use
|
8 | 8 |
|
9 |
| -Let's consider the following scenarios to go over how module aliasing can be used. |
| 9 | +Let's consider the following scenarios to go over how module aliasing can be used. |
10 | 10 |
|
11 | 11 | ### Example 1
|
12 | 12 |
|
@@ -56,16 +56,16 @@ Package manifest `App`
|
56 | 56 | package: "swift-draw"),
|
57 | 57 | .product(name: "Utils",
|
58 | 58 | package: "swift-game",
|
59 |
| - moduleAliases: ["Utils": "GameUtils"]), |
| 59 | + moduleAliases: ["Utils": "SwiftGameUtils"]), |
60 | 60 | ])
|
61 | 61 | ]
|
62 | 62 | ```
|
63 | 63 |
|
64 |
| -The value for the `moduleAliases` parameter is a dictionary where the key is the original module name in conflict and the value is a user-defined new unique name, in this case `GameUtils`. This will rename the `Utils` module in package `swift-game` as `GameUtils`; the name of the binary will be `GameUtils.swiftmodule`. No source or manifest changes are required by the `swift-game` package. |
| 64 | +The value for the `moduleAliases` parameter is a dictionary where the key is the original module name in conflict and the value is a user-defined new unique name, in this case `SwiftGameUtils` to qualify it with the package identifier. This will rename the `Utils` module in package `swift-game` as `SwiftGameUtils`; the name of the binary will be `SwiftGameUtils.swiftmodule`. No source or manifest changes are required by the `swift-game` package. |
65 | 65 |
|
66 |
| -To use the aliased module, `App` needs to reference the the new name, i.e. `import GameUtils`. Its existing `import Utils` statement will continue to reference the `Utils` module from package `swift-draw`, as expected. |
| 66 | +To use the aliased module, `App` needs to reference the the new name, i.e. `import SwiftGameUtils`. Its existing `import Utils` statement will continue to reference the `Utils` module from package `swift-draw`, as expected. |
67 | 67 |
|
68 |
| -Note that the dependency product names are duplicate, i.e. both have the same name `Utils`, which is by default not allowed. However, this is allowed when module aliasing is used as long as no multiple files with the same product name are created. This means they must all be automatic library types, or at most one of them can be a static library, dylib, an executable, or any other type that creates a file or a directory with the product name. |
| 68 | +Note that the dependency product names are duplicate, i.e. both have the same name `Utils`, which is by default not allowed. However, this is allowed when module aliasing is used as long as no files with the same product name are created. This means they must all be automatic library types, or at most one of them can be a static library, dylib, an executable, or any other type that creates a file or a directory with the product name. |
69 | 69 |
|
70 | 70 | ### Example 2
|
71 | 71 |
|
@@ -106,24 +106,24 @@ Package manifest `App`
|
106 | 106 | package: "swift-draw"),
|
107 | 107 | .product(name: "Game",
|
108 | 108 | package: "swift-game",
|
109 |
| - moduleAliases: ["Utils": "GameUtils"]), |
| 109 | + moduleAliases: ["Utils": "SwiftGameUtils"]), |
110 | 110 | ])
|
111 | 111 | ]
|
112 | 112 | ```
|
113 | 113 |
|
114 |
| -The `Utils` module from `swift-game` is renamed as `GameUtils`, and all the references to `Utils` in source files of `Game` are compiled as `GameUtils`. Similar to Example 1, no source or manifest changes are required by the `swift-game` package. |
| 114 | +The `Utils` module from `swift-game` is renamed as `SwiftGameUtils`, and all the references to `Utils` in source files of `Game` are compiled as `SwiftGameUtils`. Similar to Example 1, no source or manifest changes are required by the `swift-game` package. Since the package identifier is unique to the package, using it as the prefix for the new module alias should prevent collisions. |
115 | 115 |
|
116 |
| -If more aliases need to be defined, they can be added with a comma delimiter, per below. |
| 116 | +If more aliases need to be defined, they can be added with a comma delimiter, per below. |
117 | 117 |
|
118 | 118 | ```
|
119 |
| - moduleAliases: ["Utils": "GameUtils", "Logging": "GameLogging"]), |
| 119 | + moduleAliases: ["Utils": "SwiftGameUtils", "Logging": "SwiftGameLogging"]), |
120 | 120 | ```
|
121 | 121 |
|
122 | 122 | ## Override Module Aliases
|
123 | 123 |
|
124 |
| -If module alias values defined upstream are conflicting downstream, they can be overridden by chaining; add an entry to the `moduleAliases` parameter downstream using the conflicting alias value as a key and provide a unique value. |
| 124 | +If module alias values defined upstream are conflicting downstream, they can be overridden by chaining; add an entry to the `moduleAliases` parameter downstream using the conflicting alias value as a key and provide a unique value. Note that this should be a particularly rare occurrence since prefixing a module alias with its package identifier will usually give it a globally unique alias. However, if this does occur then chaining can be a solution. |
125 | 125 |
|
126 |
| -To illustrate, the `swift-draw` and `swift-game` packages are modified to have the following dependencies and module aliases. |
| 126 | +To illustrate, the `swift-draw` and `swift-game` packages are modified to have the following dependencies and module aliases. |
127 | 127 |
|
128 | 128 | Package manifest `swift-draw`
|
129 | 129 | ```
|
@@ -183,19 +183,19 @@ To override it, the `App` manifest can define its own module aliases per below.
|
183 | 183 | dependencies: [
|
184 | 184 | .product(name: "Draw",
|
185 | 185 | package: "swift-draw",
|
186 |
| - moduleAliases: ["FooUtils": "DrawUtils"]), |
| 186 | + moduleAliases: ["FooUtils": "SwiftDrawUtils"]), |
187 | 187 | .product(name: "Game",
|
188 | 188 | package: "swift-game",
|
189 |
| - moduleAliases: ["FooUtils": "GameUtils"]), |
| 189 | + moduleAliases: ["FooUtils": "SwiftGameUtils"]), |
190 | 190 | ])
|
191 | 191 | ]
|
192 | 192 | ```
|
193 |
| -The `Utils` module from package `a-utils` will be renamed as `DrawUtils`, and `Utils` from package `c-utils` will be renamed as `GameUtils`. Each overridden alias will be applied to all of the targets that depend on each module. |
| 193 | +The `Utils` module from package `a-utils` will be renamed as `SwiftDrawUtils`, and `Utils` from package `c-utils` will be renamed as `SwiftGameUtils`. Each overridden alias will be applied to all of the targets that depend on each module. |
194 | 194 |
|
195 | 195 | ## Requirements
|
196 | 196 |
|
197 | 197 | * A package needs to adopt the swift tools version 5.7 and above to use the `moduleAliases` parameter.
|
198 |
| -* A module being aliased needs to be a pure Swift module only: no ObjC/C/C++/Asm are supported due to a likely symbol collision. Similarly, use of `@objc(name)` should be avoided. |
| 198 | +* A module being aliased needs to be a pure Swift module only: no ObjC/C/C++/Asm are supported due to a likely symbol collision. Similarly, use of `@objc(name)` should be avoided. |
199 | 199 | * A module being aliased cannot be a prebuilt binary due to the impact on mangling and serialization, i.e. source-based only.
|
200 | 200 | * A module being aliased should not be passed to a runtime call such as `NSClassFromString(...)` that converts (directly or indirectly) String to a type in a module since such call will fail.
|
201 | 201 | * If a target mapped to a module being aliased contains resources, they should be asset catalogs, localized strings, or resources that do not require explicit module names.
|
|
0 commit comments