Skip to content

Commit af36a11

Browse files
authored
Bifurcate SwiftPM library locations (#7212)
When using SwiftPM `PackageDescription` or `PackagePlugin` libraries in an inferior, the corresponding modules are now found in a "Modules" subdirectory, but I didn't adjust the include paths being used for this. It appears that prior versions of the Swift compiler would search for modules recursively but recent versions of 5.11 have stopped doing so, unmasking this existing issue.
1 parent 5f85bff commit af36a11

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,14 +1081,15 @@ public final class ManifestLoader: ManifestLoaderProtocol {
10811081
for toolsVersion: ToolsVersion
10821082
) -> [String] {
10831083
var cmd = [String]()
1084-
let runtimePath = self.toolchain.swiftPMLibrariesLocation.manifestLibraryPath
1084+
let libraryPath = self.toolchain.swiftPMLibrariesLocation.manifestLibraryPath
1085+
let modulesPath = self.toolchain.swiftPMLibrariesLocation.manifestModulesPath
10851086
cmd += ["-swift-version", toolsVersion.swiftLanguageVersion.rawValue]
10861087
// if runtimePath is set to "PackageFrameworks" that means we could be developing SwiftPM in Xcode
10871088
// which produces a framework for dynamic package products.
1088-
if runtimePath.extension == "framework" {
1089-
cmd += ["-I", runtimePath.parentDirectory.parentDirectory.pathString]
1089+
if modulesPath.extension == "framework" {
1090+
cmd += ["-I", modulesPath.parentDirectory.parentDirectory.pathString]
10901091
} else {
1091-
cmd += ["-I", runtimePath.pathString]
1092+
cmd += ["-I", modulesPath.pathString]
10921093
}
10931094
#if os(macOS)
10941095
if let sdkRoot = self.toolchain.sdkRootPath ?? self.sdkRoot() {

Sources/PackageModel/ToolchainConfiguration.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,18 @@ public struct ToolchainConfiguration {
7878
extension ToolchainConfiguration {
7979
public struct SwiftPMLibrariesLocation {
8080
public var manifestLibraryPath: AbsolutePath
81+
public var manifestModulesPath: AbsolutePath
8182
public var pluginLibraryPath: AbsolutePath
82-
83-
public init(manifestLibraryPath: AbsolutePath, manifestLibraryMinimumDeploymentTarget: PlatformVersion? = nil, pluginLibraryPath: AbsolutePath, pluginLibraryMinimumDeploymentTarget: PlatformVersion? = nil) {
83+
public var pluginModulesPath: AbsolutePath
84+
85+
public init(
86+
manifestLibraryPath: AbsolutePath,
87+
manifestModulesPath: AbsolutePath? = nil,
88+
manifestLibraryMinimumDeploymentTarget: PlatformVersion? = nil,
89+
pluginLibraryPath: AbsolutePath,
90+
pluginModulesPath: AbsolutePath? = nil,
91+
pluginLibraryMinimumDeploymentTarget: PlatformVersion? = nil
92+
) {
8493
#if os(macOS)
8594
if let manifestLibraryMinimumDeploymentTarget {
8695
self.manifestLibraryMinimumDeploymentTarget = manifestLibraryMinimumDeploymentTarget
@@ -102,7 +111,18 @@ extension ToolchainConfiguration {
102111
#endif
103112

104113
self.manifestLibraryPath = manifestLibraryPath
114+
if let manifestModulesPath {
115+
self.manifestModulesPath = manifestModulesPath
116+
} else {
117+
self.manifestModulesPath = manifestLibraryPath
118+
}
119+
105120
self.pluginLibraryPath = pluginLibraryPath
121+
if let pluginModulesPath {
122+
self.pluginModulesPath = pluginModulesPath
123+
} else {
124+
self.pluginModulesPath = pluginLibraryPath
125+
}
106126
}
107127

108128
public init(root: AbsolutePath, manifestLibraryMinimumDeploymentTarget: PlatformVersion? = nil, pluginLibraryMinimumDeploymentTarget: PlatformVersion? = nil) {

Sources/PackageModel/UserToolchain.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,9 +699,19 @@ public final class UserToolchain: Toolchain {
699699

700700
// this tests if we are debugging / testing SwiftPM with SwiftPM
701701
if localFileSystem.exists(applicationPath.appending("swift-package")) {
702+
// Newer versions of SwiftPM will emit modules to a "Modules" subdirectory, but we're also staying compatible with older versions for development.
703+
let modulesPath: AbsolutePath
704+
if localFileSystem.exists(applicationPath.appending("Modules")) {
705+
modulesPath = applicationPath.appending("Modules")
706+
} else {
707+
modulesPath = applicationPath
708+
}
709+
702710
return .init(
703711
manifestLibraryPath: applicationPath,
704-
pluginLibraryPath: applicationPath
712+
manifestModulesPath: modulesPath,
713+
pluginLibraryPath: applicationPath,
714+
pluginModulesPath: modulesPath
705715
)
706716
}
707717
}

Sources/Workspace/DefaultPluginScriptRunner.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public struct DefaultPluginScriptRunner: PluginScriptRunner, Cancellable {
141141

142142
// Get access to the path containing the PackagePlugin module and library.
143143
let pluginLibraryPath = self.toolchain.swiftPMLibrariesLocation.pluginLibraryPath
144+
let pluginModulesPath = self.toolchain.swiftPMLibrariesLocation.pluginModulesPath
144145

145146
// if runtimePath is set to "PackageFrameworks" that means we could be developing SwiftPM in Xcode
146147
// which produces a framework for dynamic package products.
@@ -196,10 +197,10 @@ public struct DefaultPluginScriptRunner: PluginScriptRunner, Cancellable {
196197

197198
// if runtimePath is set to "PackageFrameworks" that means we could be developing SwiftPM in Xcode
198199
// which produces a framework for dynamic package products.
199-
if pluginLibraryPath.extension == "framework" {
200-
commandLine += ["-I", pluginLibraryPath.parentDirectory.parentDirectory.pathString]
200+
if pluginModulesPath.extension == "framework" {
201+
commandLine += ["-I", pluginModulesPath.parentDirectory.parentDirectory.pathString]
201202
} else {
202-
commandLine += ["-I", pluginLibraryPath.pathString]
203+
commandLine += ["-I", pluginModulesPath.pathString]
203204
}
204205
#if os(macOS)
205206
if let sdkRoot = self.toolchain.sdkRootPath ?? self.sdkRoot() {

0 commit comments

Comments
 (0)