Skip to content

Commit ee269d3

Browse files
authored
Merge pull request #768 from ahoppen/ahoppen/test-system-module-interface
Re-enable `SwiftInterfaceTests.testSystemModuleInterface`
2 parents 499cacc + a7cf7aa commit ee269d3

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

Sources/SKTestSupport/TestServer.swift

+27-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import SKCore
1515
import LanguageServerProtocol
1616
import LanguageServerProtocolJSONRPC
1717
import SourceKitLSP
18-
import class Foundation.Pipe
18+
import Foundation
1919
import LSPTestSupport
2020

2121
public final class TestSourceKitServer {
@@ -36,22 +36,40 @@ public final class TestSourceKitServer {
3636

3737
public static let serverOptions: SourceKitServer.Options = SourceKitServer.Options()
3838

39+
/// If the server is not using the global module cache, the path of the local
40+
/// module cache.
41+
///
42+
/// This module cache will be deleted when the test server is destroyed.
43+
private let moduleCache: URL?
44+
3945
public let client: TestClient
4046
let connImpl: ConnectionImpl
4147

4248
public var hasShutdown: Bool = false
4349

4450
/// The server, if it is in the same process.
4551
public let server: SourceKitServer?
46-
47-
public init(connectionKind: ConnectionKind = .local) {
52+
53+
/// - Parameters:
54+
/// - useGlobalModuleCache: If `false`, the server will use its own module
55+
/// cache in an empty temporary directory instead of the global module cache.
56+
public init(connectionKind: ConnectionKind = .local, useGlobalModuleCache: Bool = true) {
57+
if !useGlobalModuleCache {
58+
moduleCache = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString)
59+
} else {
60+
moduleCache = nil
61+
}
62+
var serverOptions = Self.serverOptions
63+
if let moduleCache {
64+
serverOptions.buildSetup.flags.swiftCompilerFlags += ["-module-cache-path", moduleCache.path]
65+
}
4866

4967
switch connectionKind {
5068
case .local:
5169
let clientConnection = LocalConnection()
5270
let serverConnection = LocalConnection()
5371
client = TestClient(server: serverConnection)
54-
server = SourceKitServer(client: clientConnection, options: Self.serverOptions, onExit: {
72+
server = SourceKitServer(client: clientConnection, options: serverOptions, onExit: {
5573
clientConnection.close()
5674
})
5775

@@ -76,7 +94,7 @@ public final class TestSourceKitServer {
7694
)
7795

7896
client = TestClient(server: clientConnection)
79-
server = SourceKitServer(client: serverConnection, options: Self.serverOptions, onExit: {
97+
server = SourceKitServer(client: serverConnection, options: serverOptions, onExit: {
8098
serverConnection.close()
8199
})
82100

@@ -101,6 +119,10 @@ public final class TestSourceKitServer {
101119

102120
deinit {
103121
close()
122+
123+
if let moduleCache {
124+
try? FileManager.default.removeItem(at: moduleCache)
125+
}
104126
}
105127

106128
func close() {

Tests/SourceKitLSPTests/SwiftInterfaceTests.swift

+7-14
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ final class SwiftInterfaceTests: XCTestCase {
3333
}
3434

3535
override func setUp() {
36-
connection = TestSourceKitServer()
36+
// This is the only test that references modules from the SDK (Foundation).
37+
// `testSystemModuleInterface` has been flaky for a long while and a
38+
// hypothesis is that it was failing because of a malformed global module
39+
// cache that might still be present from previous CI runs. If we use a
40+
// local module cache, we define away that source of bugs.
41+
connection = TestSourceKitServer(useGlobalModuleCache: false)
3742
sk = connection.client
3843
_ = try! sk.sendSync(InitializeRequest(
3944
processId: nil,
@@ -56,20 +61,8 @@ final class SwiftInterfaceTests: XCTestCase {
5661
sk = nil
5762
connection = nil
5863
}
59-
60-
func testSystemModuleInterface() throws {
61-
try XCTSkipIf(true, "Test is flaky - rdar://108256204")
62-
// This test is failing non-deterministically in CI becaue the file contents
63-
// of the generated interface just contain a newline.
64-
// I cannot reproduce the failure locally. Add some logging to determine
65-
// whether the issue is sourcekitd not returning an empty generated
66-
// interface or something around how the file is handled.
67-
// Remove this lowering of the log level once we have determined what the
68-
// issue is (rdar://104871745).
69-
let previousLogLevel = Logger.shared.currentLevel
70-
defer { Logger.shared.setLogLevel(previousLogLevel.description) }
71-
Logger.shared.setLogLevel("debug")
7264

65+
func testSystemModuleInterface() throws {
7366
let url = URL(fileURLWithPath: "/\(UUID())/a.swift")
7467
let uri = DocumentURI(url)
7568

0 commit comments

Comments
 (0)