You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: proposals/0339-module-aliasing-for-disambiguation.md
+17-17Lines changed: 17 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,29 +37,29 @@ App
37
37
38
38
The modules from each package have the following code:
39
39
40
-
```
40
+
```swift
41
41
[Module Game] // swift-game
42
42
43
43
importUtils// swift-game
44
44
publicfuncstart(level: Utils.Level) { ... }
45
45
```
46
46
47
-
```
47
+
```swift
48
48
[Module Utils] // swift-game
49
49
50
50
publicstructLevel { ... }
51
51
publicvar currentLevel: Utils.Level { ... }
52
52
```
53
53
54
-
```
54
+
```swift
55
55
[Module Utils] // swift-draw
56
56
57
57
publicprotocolDrawable { ... }
58
58
publicclassCanvas: Utils.Drawable { ... }
59
59
```
60
60
61
61
Since `App` depends on these two `Utils` modules, we have a conflict, thus we need to rename one. We will introduce a new setting in SwiftPM called `moduleAliases` that will allow setting unique names for dependencies, like so:
62
-
```
62
+
```swift
63
63
targets: [
64
64
.executableTarget(
65
65
name: "App",
@@ -71,7 +71,7 @@ Since `App` depends on these two `Utils` modules, we have a conflict, thus we ne
71
71
```
72
72
73
73
The setting `moduleAliases` will rename `Utils` from the `swift-game` package as `GameUtils` and alias all its references in the source code to be compiled as `GameUtils`. Since renaming one of the `Utils` modules will resolve the conflict, it is not necessary to rename the other `Utils` module. The references to `Utils` in the `Game` module will be built as `GameUtils` without requiring any source changes. If `App` needs to reference both `Utils` modules in its source code, it can do so by directly including the aliased name:
74
-
```
74
+
```swift
75
75
[App]
76
76
77
77
importGameUtils
@@ -130,8 +130,8 @@ App
130
130
131
131
Here are the manifest examples for `swift-game` and `swift-draw`.
132
132
133
-
```
134
-
{
133
+
```swift
134
+
letpackage=Package(
135
135
name: "swift-game",
136
136
dependencies: [],
137
137
products: [
@@ -142,11 +142,11 @@ Here are the manifest examples for `swift-game` and `swift-draw`.
142
142
.target(name: "Game", dependencies: ["Utils"]),
143
143
.target(name: "Utils", dependencies: [])
144
144
]
145
-
}
145
+
)
146
146
```
147
147
148
-
```
149
-
{
148
+
```swift
149
+
letpackage=Package(
150
150
name: "swift-draw",
151
151
dependencies: [],
152
152
products: [
@@ -155,12 +155,12 @@ Here are the manifest examples for `swift-game` and `swift-draw`.
155
155
targets: [
156
156
.target(name: "Utils", dependencies: [])
157
157
]
158
-
}
158
+
)
159
159
```
160
160
161
161
The `App` manifest needs to explicitly define unique names for the conflicting modules via a new parameter called `moduleAliases`.
162
-
```
163
-
{
162
+
```swift
163
+
letpackage=Package(
164
164
name: "App",
165
165
dependencies: [
166
166
.package(url: https://.../swift-game.git),
@@ -177,15 +177,15 @@ The `App` manifest needs to explicitly define unique names for the conflicting m
177
177
.product(name: "Utils", package: "swift-draw"),
178
178
])
179
179
]
180
-
}
180
+
)
181
181
```
182
182
183
183
SwiftPM will perform validations when it parses `moduleAliases`; foreach entry, it will check whether the given alias is a unique name, whether there is a conflict among aliases, whether the specified module is built from source (pre-compiled modules cannot be rebuilt to respect the rename), and whether the module is a pure Swift module (see **Requirements/Limitations** section for more details).
184
184
185
185
It will also check ifany aliases are defined in upstream packages and override them if necessary. For example, if the `swift-game` package were modified per below and defined its own alias `SwiftUtils` for module `Utils` from a dependency package, the alias defined in `App` will override it, thus the `Utils` module from `swift-utils` will be built as `GameUtils`.
186
186
187
-
```
188
-
{
187
+
```swift
188
+
letpackage=Package(
189
189
name: "swift-game",
190
190
dependencies: [
191
191
.package(url: https://.../swift-utils.git),
@@ -201,7 +201,7 @@ It will also check if any aliases are defined in upstream packages and override
201
201
moduleAliases: ["Utils":"SwiftUtils"]),
202
202
])
203
203
]
204
-
}
204
+
)
205
205
```
206
206
207
207
Once the validation and alias overriding steps pass, dependency resolution will take place using the new module names, and the `-module-alias [name]=[new_name]` flag will be passed to the build execution.
0 commit comments