Skip to content

Commit 07d2f63

Browse files
committed
Only add the /Modules variant if the folder exists
This fixes an issue where for some dependencies, if both folders exist we get compilation errors.
1 parent b244155 commit 07d2f63

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

Sources/swift-sh/Helpers/DepsPackage.swift

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,33 @@ struct DepsPackage {
161161
/* Now swift has given us the arguments it thinks are needed to start the script.
162162
* Spoiler: they are not enough!
163163
* - When the deps contain an xcframework dependency, we have to add the -I option for swift to find the headers of the frameworks.
164-
* - Starting w/ Swift 6, the arguments given by the REPL invocation give an incorrect include search path:
165-
* we must add `/Modules` to the include path.
166-
* Because we still want to be compatible w/ Xcode < 6, we add the fixed search path before the one given by the REPL invocation. */
164+
* - Starting w/ Swift 6, the arguments given by the REPL invocation give an incorrect include search path: we must add `/Modules` to the include path.
165+
* We check whether the `Modules` folder exists and add it to the command-line if it does.
166+
* Previously we added it the modified version unconditionally for each -I arguments,
167+
* but if both versions are present we get compilation errors for some dependencies (for ArgumentParser for instance). */
167168
/* Add `/Modules` variants import options for Swift 6. */
168-
for (idx, arg) in ret.enumerated() {
169-
if arg.hasPrefix("-I") {
170-
ret.insert(arg + "/Modules", at: idx)
169+
var idx = 0
170+
while idx < ret.count {
171+
defer {idx += 1}
172+
let (path, hasDashI): (String, Bool)
173+
if ret[idx] == "-I" {
174+
idx += 1
175+
guard idx < ret.count else {
176+
break
177+
}
178+
path = ret[idx]
179+
hasDashI = false
180+
} else if ret[idx].hasPrefix("-I") {
181+
path = String(ret[idx].dropFirst(2))
182+
hasDashI = true
183+
} else {
184+
continue
185+
}
186+
187+
var isDir = ObjCBool(false)
188+
let pathWithModules = String(path.reversed().drop(while: { $0 == "/" }).reversed()) + "/Modules"
189+
if fm.fileExists(atPath: pathWithModules, isDirectory: &isDir) && isDir.boolValue {
190+
ret[idx] = (hasDashI ? "-I" : "") + pathWithModules
171191
}
172192
}
173193
/* Add xcframework import options. */

0 commit comments

Comments
 (0)