Skip to content
Open
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
28 changes: 21 additions & 7 deletions Sources/BuildServerIntegration/SwiftPMBuildServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,29 @@ package actor SwiftPMBuildServer: BuiltInBuildServer {
let hostSDK = try SwiftSDK.hostSwiftSDK(AbsolutePath(validating: destinationToolchainBinDir.filePath))
let hostSwiftPMToolchain = try UserToolchain(swiftSDK: hostSDK)

let triple: Triple? =
if let triple = options.swiftPMOrDefault.triple {
try Triple(triple)
} else {
nil
}
let swiftSDKsDirectory: AbsolutePath? =
if let swiftSDKsDirectory = options.swiftPMOrDefault.swiftSDKsDirectory {
try AbsolutePath(validating: swiftSDKsDirectory, relativeTo: absProjectRoot)
} else {
nil
}
let destinationSDK = try SwiftSDK.deriveTargetSwiftSDK(
hostSwiftSDK: hostSDK,
hostTriple: hostSwiftPMToolchain.targetTriple,
customToolsets: options.swiftPMOrDefault.toolsets?.map {
try AbsolutePath(validating: $0, relativeTo: absProjectRoot)
} ?? [],
customCompileTriple: options.swiftPMOrDefault.triple.map { try Triple($0) },
customCompileTriple: triple,
swiftSDKSelector: options.swiftPMOrDefault.swiftSDK,
store: SwiftSDKBundleStore(
swiftSDKsDirectory: localFileSystem.getSharedSwiftSDKsDirectory(
explicitDirectory: options.swiftPMOrDefault.swiftSDKsDirectory.map {
try AbsolutePath(validating: $0, relativeTo: absProjectRoot)
}
explicitDirectory: swiftSDKsDirectory
),
hostToolchainBinDir: hostSwiftPMToolchain.swiftCompilerPath.parentDirectory,
fileSystem: localFileSystem,
Expand Down Expand Up @@ -320,9 +330,13 @@ package actor SwiftPMBuildServer: BuiltInBuildServer {
disableSandbox: options.swiftPMOrDefault.disableSandbox ?? false
)

self.traitConfiguration = TraitConfiguration(
enabledTraits: options.swiftPMOrDefault.traits.flatMap(Set.init)
)
let enabledTraits: Set<String>? =
if let traits = options.swiftPMOrDefault.traits {
Set(traits)
} else {
nil
}
self.traitConfiguration = TraitConfiguration(enabledTraits: enabledTraits)

packageLoadingQueue.async {
await orLog("Initial package loading") {
Expand Down
7 changes: 6 additions & 1 deletion Sources/Diagnose/ReduceCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ package struct ReduceCommand: AsyncParsableCommand {
)
var predicate: String?

private var nsPredicate: NSPredicate? { predicate.map { NSPredicate(format: $0) } }
private var nsPredicate: NSPredicate? {
guard let predicate else {
return nil
}
return NSPredicate(format: predicate)
}
#else
private var nsPredicate: NSPredicate? { nil }
#endif
Expand Down
7 changes: 6 additions & 1 deletion Sources/Diagnose/ReduceFrontendCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ package struct ReduceFrontendCommand: AsyncParsableCommand {
)
var predicate: String?

private var nsPredicate: NSPredicate? { predicate.map { NSPredicate(format: $0) } }
private var nsPredicate: NSPredicate? {
guard let predicate else {
return nil
}
return NSPredicate(format: predicate)
}
#else
private var nsPredicate: NSPredicate? { nil }
#endif
Expand Down
17 changes: 14 additions & 3 deletions Sources/SemanticIndex/UpdateIndexStoreTaskDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
indexFiles: indexFiles,
buildSettings: buildSettings,
processArguments: args,
workingDirectory: buildSettings.workingDirectory.map(AbsolutePath.init(validating:))
workingDirectory: buildSettings.workingDirectoryPath
)
case .singleFile(let file, let buildSettings):
// We only end up in this case if the file's build settings didn't contain `-index-unit-output-path` and the build
Expand All @@ -532,7 +532,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
indexFiles: [file.mainFile],
buildSettings: buildSettings,
processArguments: args,
workingDirectory: buildSettings.workingDirectory.map(AbsolutePath.init(validating:))
workingDirectory: buildSettings.workingDirectoryPath
)
}
}
Expand All @@ -556,7 +556,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
indexFiles: [uri],
buildSettings: buildSettings,
processArguments: args,
workingDirectory: buildSettings.workingDirectory.map(AbsolutePath.init(validating:))
workingDirectory: buildSettings.workingDirectoryPath
)
}

Expand Down Expand Up @@ -780,3 +780,14 @@ fileprivate extension Process {
}
}
}

fileprivate extension FileBuildSettings {
var workingDirectoryPath: AbsolutePath? {
get throws {
guard let workingDirectory else {
return nil
}
return try AbsolutePath(validating: workingDirectory)
}
}
}
5 changes: 4 additions & 1 deletion Sources/SourceKitD/SKDResponseDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ package final class SKDResponseDictionary: Sendable {
}

package subscript(key: sourcekitd_api_uid_t) -> String? {
return sourcekitd.api.variant_dictionary_get_string(dict, key).map(String.init(cString:))
guard let cString = sourcekitd.api.variant_dictionary_get_string(dict, key) else {
return nil
}
return String(cString: cString)
}

package subscript(key: sourcekitd_api_uid_t) -> Int? {
Expand Down
9 changes: 7 additions & 2 deletions Sources/SwiftLanguageService/Diagnostic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,13 @@ extension DiagnosticStage {
case sourcekitd.values.semaDiagStage:
self = .sema
default:
let desc = sourcekitd.api.uid_get_string_ptr(uid).map { String(cString: $0) }
logger.fault("Unknown diagnostic stage \(desc ?? "nil", privacy: .public)")
let uidDescription =
if let cString = sourcekitd.api.uid_get_string_ptr(uid) {
String(cString: cString)
} else {
"<nil>"
}
logger.fault("Unknown diagnostic stage \(uidDescription, privacy: .public)")
return nil
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftLanguageService/SemanticTokens.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ extension SwiftLanguageService {
let semanticTokens = await orLog("Loading semantic tokens") { try await semanticHighlightingTokens(for: snapshot) }

let range =
if let range = range.flatMap({ snapshot.byteSourceRange(of: $0) }) {
range
if let range {
snapshot.byteSourceRange(of: range)
} else {
await tree.range
}
Expand Down
8 changes: 7 additions & 1 deletion Sources/SwiftLanguageService/SwiftLanguageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -651,11 +651,17 @@ extension SwiftLanguageService {
// Check that the document wasn't modified while we were getting diagnostics. This could happen because we are
// calling `publishDiagnosticsIfNeeded` outside of `messageHandlingQueue` and thus a concurrent edit is
// possible while we are waiting for the sourcekitd request to return a result.
let latestVersionString =
if let version = latestSnapshotID?.version {
String(version)
} else {
"<nil>"
}
logger.log(
"""
Document was modified while loading diagnostics. \
Loaded diagnostics for \(snapshot.id.version, privacy: .public), \
latest snapshot is \((latestSnapshotID?.version).map(String.init) ?? "<nil>", privacy: .public)
latest snapshot is \(latestVersionString, privacy: .public)
"""
)
throw CancellationError()
Expand Down
7 changes: 6 additions & 1 deletion Sources/SwiftLanguageService/SwiftTestingScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,12 @@ final class SyntacticSwiftTestingTestScanner: SyntaxVisitor {
let suiteAttribute = node.attributes
.compactMap { $0.as(AttributeSyntax.self) }
.first { $0.isNamed("Suite", inModuleNamed: "Testing") }
let attributeData = suiteAttribute.map(TestingAttributeData.init(attribute:))
let attributeData: TestingAttributeData? =
if let suiteAttribute {
TestingAttributeData(attribute: suiteAttribute)
} else {
nil
}

if attributeData?.isHidden ?? false {
return .skipChildren
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,13 @@ struct SyntaxHighlightingTokenParser {
values.stringInterpolation
]
if !ignoredKinds.contains(uid) {
let name = api.uid_get_string_ptr(uid).map(String.init(cString:))
logger.error("Unknown token kind: \(name ?? "?", privacy: .public)")
let name =
if let cString = api.uid_get_string_ptr(uid) {
String(cString: cString)
} else {
"<nil>"
}
logger.error("Unknown token kind: \(name, privacy: .public)")
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ final class SKDRequestDictionaryReader: Sendable, CustomStringConvertible {
}

subscript(key: sourcekitd_api_uid_t) -> String? {
return sourcekitd.servicePluginApi.request_dictionary_get_string(sourcekitd_api_object_t(dict), key).map(
String.init(cString:)
)
guard let cString = sourcekitd.servicePluginApi.request_dictionary_get_string(sourcekitd_api_object_t(dict), key)
else {
return nil
}
return String(cString: cString)
}

subscript(key: sourcekitd_api_uid_t) -> Int64? {
Expand Down
2 changes: 1 addition & 1 deletion Tests/SourceKitLSPTests/BackgroundIndexingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ final class BackgroundIndexingTests: SourceKitLSPTestCase {
let result = try await project.testClient.send(
RenameRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"], newName: "height")
)
XCTAssertEqual((result?.changes?.keys).map(Set.init), [uri, try project.uri(for: "Client.swift")])
XCTAssertEqual(Set(try XCTUnwrap(result?.changes?.keys)), [uri, try project.uri(for: "Client.swift")])
}

func testDontPreparePackageManifest() async throws {
Expand Down
12 changes: 4 additions & 8 deletions Tests/SourceKitLSPTests/SwiftInterfaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class SwiftInterfaceTests: SourceKitLSPTestCase {
)
let location = try XCTUnwrap(resp?.locations?.only)
XCTAssertTrue(location.uri.pseudoPath.hasSuffix("Foundation.swiftinterface"))
let fileContents = try XCTUnwrap(location.uri.fileURL.flatMap({ try String(contentsOf: $0, encoding: .utf8) }))
let fileContents = try XCTUnwrap(String(contentsOf: try XCTUnwrap(location.uri.fileURL), encoding: .utf8))
// Smoke test that the generated Swift Interface contains Swift code
XCTAssert(
fileContents.hasPrefix("import "),
Expand Down Expand Up @@ -173,7 +173,7 @@ final class SwiftInterfaceTests: SourceKitLSPTestCase {
)
let location = try XCTUnwrap(response?.locations?.only)
XCTAssertTrue(location.uri.pseudoPath.hasSuffix("MyLibrary.swiftinterface"))
let fileContents = try XCTUnwrap(location.uri.fileURL.flatMap({ try String(contentsOf: $0, encoding: .utf8) }))
let fileContents = try XCTUnwrap(String(contentsOf: try XCTUnwrap(location.uri.fileURL), encoding: .utf8))
XCTAssertTrue(
fileContents.contains(
"""
Expand Down Expand Up @@ -420,13 +420,9 @@ private func assertSystemSwiftInterface(
)
)
let location = try XCTUnwrap(definition?.locations?.only)
XCTAssert(
(location.uri.fileURL?.lastPathComponent).map(swiftInterfaceFiles.contains) ?? false,
"Path '\(location.uri.pseudoPath)' did not match any of \(String(reflecting: swiftInterfaceFiles))",
line: line
)
assertContains(swiftInterfaceFiles, try XCTUnwrap(location.uri.fileURL?.lastPathComponent), line: line)
// load contents of swiftinterface
let contents = try XCTUnwrap(location.uri.fileURL.flatMap({ try String(contentsOf: $0, encoding: .utf8) }))
let contents = try XCTUnwrap(String(contentsOf: try XCTUnwrap(location.uri.fileURL), encoding: .utf8))
let lineTable = LineTable(contents)
let destinationLine = try XCTUnwrap(lineTable.line(at: location.range.lowerBound.line))
.trimmingCharacters(in: .whitespaces)
Expand Down