Skip to content

Plugin import scanner isn't passing in the library directory, making canImport not work #6323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Workspace/Workspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ extension Workspace {
packageGraph: PackageGraph,
completion: @escaping(Result<[PackageIdentity: [String: [String]]], Error>) -> Void) {
let pluginTargets = packageGraph.allTargets.filter{$0.type == .plugin}
let scanner = SwiftcImportScanner(swiftCompilerEnvironment: hostToolchain.swiftCompilerEnvironment, swiftCompilerFlags: hostToolchain.swiftCompilerFlags, swiftCompilerPath: hostToolchain.swiftCompilerPath)
let scanner = SwiftcImportScanner(swiftCompilerEnvironment: hostToolchain.swiftCompilerEnvironment, swiftCompilerFlags: hostToolchain.swiftCompilerFlags + ["-I", hostToolchain.swiftPMLibrariesLocation.pluginLibraryPath.pathString], swiftCompilerPath: hostToolchain.swiftCompilerPath)
var importList = [PackageIdentity: [String: [String]]]()

for pluginTarget in pluginTargets {
Expand Down
17 changes: 14 additions & 3 deletions Tests/SPMBuildCoreTests/PluginInvocationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1011,19 +1011,30 @@ class PluginInvocationTests: XCTestCase {
try localFileSystem.writeFileContents(qPluginTargetDir.appending("plugin.swift"), string: """
import PackagePlugin
import XcodeProjectPlugin
#if canImport(ModuleFoundViaExtraSearchPaths)
import ModuleFoundViaExtraSearchPaths
#endif
@main struct QBuildToolPlugin: BuildToolPlugin {
func createBuildCommands(
context: PluginContext,
target: Target
) throws -> [Command] { }
}
""")

// Create something that looks like another module that can be detected via `canImport()`.
let fakeExtraModulesDir = tmpPath.appending("ExtraModules")
try localFileSystem.createDirectory(fakeExtraModulesDir, recursive: true)
let fakeExtraModuleFile = fakeExtraModulesDir.appending("ModuleFoundViaExtraSearchPaths.swiftmodule")
try localFileSystem.writeFileContents(fakeExtraModuleFile, string: "")

/////////
// Load a workspace from the package.
let observability = ObservabilitySystem.makeForTesting()
let workspace = try Workspace(
fileSystem: localFileSystem,
forRootPackage: packageDir,
location: try Workspace.Location(forRootPackage: packageDir, fileSystem: localFileSystem),
customHostToolchain: UserToolchain(destination: .hostDestination(), customLibrariesLocation: .init(manifestLibraryPath: fakeExtraModulesDir, pluginLibraryPath: fakeExtraModulesDir)),
customManifestLoader: ManifestLoader(toolchain: UserToolchain.default),
delegate: MockWorkspaceDelegate()
)
Expand Down Expand Up @@ -1060,8 +1071,8 @@ class PluginInvocationTests: XCTestCase {
} else if pkg.description == "otherpackage" {
XCTAssertNotNil(dict[pkg]?["QPlugin"])

let possibleImports1 = ["PackagePlugin", "XcodeProjectPlugin"]
let possibleImports2 = ["PackagePlugin", "XcodeProjectPlugin", "_SwiftConcurrencyShims"]
let possibleImports1 = ["PackagePlugin", "XcodeProjectPlugin", "ModuleFoundViaExtraSearchPaths"]
let possibleImports2 = ["PackagePlugin", "XcodeProjectPlugin", "ModuleFoundViaExtraSearchPaths", "_SwiftConcurrencyShims"]
XCTAssertTrue(entry["QPlugin"] == possibleImports1 ||
entry["QPlugin"] == possibleImports2)
count += 1
Expand Down