Skip to content

Add a few more arguments to allow errors during indexing #1384

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
47 changes: 33 additions & 14 deletions Sources/SemanticIndex/UpdateIndexStoreTaskDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,6 @@ private func adjustSwiftCompilerArgumentsForIndexStoreUpdate(
.option("output-file-map", [.singleDash], [.separatedBySpace, .separatedByEqualSign]),
]

let removeFrontendFlags = [
"-experimental-skip-non-inlinable-function-bodies",
"-experimental-skip-all-function-bodies",
]

Comment on lines -433 to -437
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want to remove these for indexing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They would presumably only be there if we added them, which we're currently. not. Probably doesn't hurt to keep anyway though.

var result: [String] = []
result.reserveCapacity(compilerArguments.count)
var iterator = compilerArguments.makeIterator()
Expand All @@ -448,18 +443,14 @@ private func adjustSwiftCompilerArgumentsForIndexStoreUpdate(
case nil:
break
}
if argument == "-Xfrontend" {
if let nextArgument = iterator.next() {
if removeFrontendFlags.contains(nextArgument) {
continue
}
result += [argument, nextArgument]
continue
}
}
result.append(argument)
}
result += supplementalClangIndexingArgs.flatMap { ["-Xcc", $0] }
result += [
// Preparation produces modules with errors. We should allow reading them.
"-Xfrontend", "-experimental-allow-module-with-compiler-errors",
// Avoid emitting the ABI descriptor, we don't need it
"-Xfrontend", "-empty-abi-descriptor",
"-index-file",
"-index-file-path", fileToIndex.pseudoPath,
// batch mode is not compatible with -index-file
Expand Down Expand Up @@ -520,12 +511,40 @@ private func adjustClangCompilerArgumentsForIndexStoreUpdate(
}
result.append(argument)
}
result += supplementalClangIndexingArgs
result.append(
"-fsyntax-only"
)
return result
}

#if compiler(>=6.1)
#warning(
"Remove -fmodules-validate-system-headers from supplementalClangIndexingArgs once all supported Swift compilers have https://github.com/apple/swift/pull/74063"
)
#endif

fileprivate let supplementalClangIndexingArgs: [String] = [
// Retain extra information for indexing
"-fretain-comments-from-system-headers",
// Pick up macro definitions during indexing
"-Xclang", "-detailed-preprocessing-record",

// libclang uses 'raw' module-format. Match it so we can reuse the module cache and PCHs that libclang uses.
"-Xclang", "-fmodule-format=raw",

// Be less strict - we want to continue and typecheck/index as much as possible
"-Xclang", "-fallow-pch-with-compiler-errors",
"-Xclang", "-fallow-pcm-with-compiler-errors",
"-Wno-non-modular-include-in-framework-module",
"-Wno-incomplete-umbrella",

// sourcekitd adds `-fno-modules-validate-system-headers` before https://github.com/apple/swift/pull/74063.
// This completely disables system module validation and never re-builds pcm for system modules. The intended behavior
// is to only re-build those PCMs once per sourcekitd session.
"-fmodules-validate-system-headers",
]

fileprivate extension Sequence {
/// Returns `true` if this sequence contains an element that is equal to an element in `otherSequence` when
/// considering two elements as equal if they satisfy `predicate`.
Expand Down
43 changes: 43 additions & 0 deletions Tests/SourceKitLSPTests/BackgroundIndexingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -887,4 +887,47 @@ final class BackgroundIndexingTests: XCTestCase {
_ = try project.openDocument("Lib.swift")
_ = try await project.testClient.send(BarrierRequest())
}

func testImportPreparedModuleWithFunctionBodiesSkipped() async throws {
// This test case was crashing the indexing compiler invocation for Client if Lib was built for index preparation
// (using `-enable-library-evolution -experimental-skip-all-function-bodies -experimental-lazy-typecheck`) but x
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x?

// Client was not indexed with `-experimental-allow-module-with-compiler-errors`. rdar://129071600
let project = try await SwiftPMTestProject(
files: [
"Lib/Lib.swift": """
public class TerminalController {
public var 1️⃣width: Int { 1 }
}
""",
"Client/Client.swift": """
import Lib

func test(terminal: TerminalController) {
let width = terminal.width
}
""",
],
manifest: """
// swift-tools-version: 5.7

import PackageDescription

let package = Package(
name: "MyLibrary",
targets: [
.target(name: "Lib"),
.target(name: "Client", dependencies: ["Lib"]),
]
)
""",
enableBackgroundIndexing: true
)
let (uri, positions) = try project.openDocument("Lib.swift")

// Check that we indexed `Client.swift` by checking that we return a rename location within it.
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")])
}
}