Skip to content

Commit a655185

Browse files
committed
Add cross-compilation regression test
1 parent b5a9769 commit a655185

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

Sources/SKTestSupport/SkipUnless.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,56 @@ package actor SkipUnless {
477477
}
478478
}
479479

480+
package static func canSwiftPMCompileForIOS(
481+
file: StaticString = #filePath,
482+
line: UInt = #line
483+
) async throws {
484+
struct NoSwiftInToolchain: Error {}
485+
486+
return try await shared.skipUnlessSupported(file: file, line: line) {
487+
#if os(macOS)
488+
guard let swift = await ToolchainRegistry.forTesting.default?.swift else {
489+
throw NoSwiftInToolchain()
490+
}
491+
492+
let project = try await SwiftPMTestProject(
493+
files: [
494+
"Lib/MyFile.swift": """
495+
public func foo() {}
496+
""",
497+
],
498+
manifest: """
499+
let package = Package(
500+
name: "MyLibrary",
501+
targets: [
502+
.target(name: "Lib"),
503+
]
504+
)
505+
"""
506+
)
507+
do {
508+
var arguments = [
509+
try swift.filePath, "build", "--package-path", try project.scratchDirectory.filePath, "--target", "Lib",
510+
"--swift-sdk", "arm64-apple-ios",
511+
]
512+
if let globalModuleCache = try globalModuleCache {
513+
arguments += ["-Xswiftc", "-module-cache-path", "-Xswiftc", try globalModuleCache.filePath]
514+
}
515+
let status = try await Process.run(arguments: arguments, workingDirectory: nil)
516+
guard case .terminated(code: 0) = status.exitStatus else {
517+
let error = (try? String(decoding: status.stderrOutput.get(), as: UTF8.self)) ?? "unknown error"
518+
return .featureUnsupported(skipMessage: "Cannot build for iOS: \(error)")
519+
}
520+
return .featureSupported
521+
} catch {
522+
return .featureUnsupported(skipMessage: "Cannot build for iOS: \(error)")
523+
}
524+
#else
525+
return .featureUnsupported(skipMessage: "Cannot build for iOS outside macOS by default")
526+
#endif
527+
}
528+
}
529+
480530
package static func canCompileForWasm(
481531
file: StaticString = #filePath,
482532
line: UInt = #line

Tests/SourceKitLSPTests/BackgroundIndexingTests.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,53 @@ final class BackgroundIndexingTests: XCTestCase {
10181018
)
10191019
}
10201020

1021+
func testUseSwiftSDKFlagsDuringPreparation() async throws {
1022+
try await SkipUnless.canSwiftPMCompileForIOS()
1023+
1024+
var options = SourceKitLSPOptions.testDefault()
1025+
options.swiftPMOrDefault.swiftSDK = "arm64-apple-ios"
1026+
let project = try await SwiftPMTestProject(
1027+
files: [
1028+
"Lib/Lib.swift": """
1029+
#if os(iOS)
1030+
public func foo() -> Int { 1 }
1031+
#endif
1032+
""",
1033+
"Client/Client.swift": """
1034+
import Lib
1035+
1036+
func test() -> String {
1037+
return foo()
1038+
}
1039+
""",
1040+
],
1041+
manifest: """
1042+
let package = Package(
1043+
name: "MyLibrary",
1044+
targets: [
1045+
.target(name: "Lib"),
1046+
.target(name: "Client", dependencies: ["Lib"]),
1047+
]
1048+
)
1049+
""",
1050+
options: options,
1051+
enableBackgroundIndexing: true
1052+
)
1053+
1054+
// Check that we get an error about the return type of `foo` (`Int`) not being convertible to the return type of
1055+
// `test` (`String`), which indicates that `Lib` had `foo` and was thus compiled for iOS
1056+
let (uri, _) = try project.openDocument("Client.swift")
1057+
let diagnostics = try await project.testClient.send(
1058+
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
1059+
)
1060+
XCTAssert(
1061+
(diagnostics.fullReport?.items ?? []).contains(where: {
1062+
$0.message == "Cannot convert return expression of type 'Int' to return type 'String'"
1063+
}),
1064+
"Did not get expected diagnostic: \(diagnostics)"
1065+
)
1066+
}
1067+
10211068
func testLibraryUsedByExecutableTargetAndPackagePlugin() async throws {
10221069
try await SkipUnless.swiftPMStoresModulesForTargetAndHostInSeparateFolders()
10231070
let project = try await SwiftPMTestProject(

0 commit comments

Comments
 (0)