Skip to content

Commit a1696ea

Browse files
authored
Detect conflicting index store units. Closes #462 (#544)
1 parent 034f0f3 commit a1696ea

File tree

5 files changed

+19
-5
lines changed

5 files changed

+19
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- Workaround Swift shorthand if-let syntax bug (https://github.com/apple/swift/issues/61509). Global properties are not handled by this workaround.
1616
- Fix retaining inferred associated types.
1717
- Fix redundant public accessibility analysis for types used in closure signatures.
18+
- Conflicting index store units are now detected and result in an error.
1819

1920
## 2.10.0 (2022-10-10)
2021

Package.resolved

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var dependencies: [Package.Dependency] = [
66
.package(url: "https://github.com/jpsim/Yams", from: "5.0.0"),
77
.package(url: "https://github.com/tadija/AEXML", from: "4.0.0"),
88
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
9-
.package(name: "SwiftIndexStore", url: "https://github.com/kateinoigakukun/swift-indexstore", from: "0.0.0"),
9+
.package(name: "SwiftIndexStore", url: "https://github.com/ileitch/swift-indexstore", .revision("f4e55301ca7d6d25057c514bea0e7407a1620f5f")),
1010
.package(name: "SwiftSyntax", url: "https://github.com/apple/swift-syntax.git", .revision("a82041008d2c678a97407fbd0ce420d3ab047538"))
1111
]
1212

Sources/PeripheryKit/Indexer/SwiftIndexer.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ public final class SwiftIndexer {
7575
let jobs = try unitsByFile.map { (file, units) -> Job in
7676
let modules = try units.reduce(into: Set<String>()) { (set, unit) in
7777
if let name = try indexStore.moduleName(for: unit) {
78-
set.insert(name)
78+
let (didInsert, _) = set.insert(name)
79+
if !didInsert {
80+
let targets = try Set(units.compactMap { try indexStore.target(for: $0) })
81+
throw PeripheryError.conflictingIndexUnitsError(file: file, module: name, unitTargets: targets)
82+
}
7983
}
8084
}
8185
let sourceFile = SourceFile(path: file, modules: modules)

Sources/Shared/PeripheryError.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import SystemPackage
23

34
public enum PeripheryError: Error, LocalizedError, CustomStringConvertible {
45
case shellCommandFailed(cmd: String, args: [String], status: Int32, output: String)
@@ -20,6 +21,7 @@ public enum PeripheryError: Error, LocalizedError, CustomStringConvertible {
2021
case unindexedTargetsError(targets: Set<String>, indexStorePath: String)
2122
case jsonDeserializationError(error: Error, json: String)
2223
case indexStoreNotFound(derivedDataPath: String)
24+
case conflictingIndexUnitsError(file: FilePath, module: String, unitTargets: Set<String>)
2325

2426
public var errorDescription: String? {
2527
switch self {
@@ -68,6 +70,13 @@ public enum PeripheryError: Error, LocalizedError, CustomStringConvertible {
6870
return "JSON deserialization failed: \(describe(error))\nJSON:\n\(json)"
6971
case let .indexStoreNotFound(derivedDataPath):
7072
return "Failed to find index datastore at path: \(derivedDataPath)"
73+
case let .conflictingIndexUnitsError(file, module, targets):
74+
var parts = ["Found conflicting index store units for '\(file)' in module '\(module)'."]
75+
if targets.count > 1 {
76+
parts.append("The units have conflicting build targets: \(targets.sorted().joined(separator: ", ")).")
77+
}
78+
parts.append("If you passed the '--index-store-path' option, ensure that Xcode is not open with a project that may write to this index store while Periphery is running.")
79+
return parts.joined(separator: " ")
7180
}
7281
}
7382

0 commit comments

Comments
 (0)