@@ -1253,11 +1253,8 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
12531253 // it cached. We can just return it.
12541254 return mainFile
12551255 }
1256- guard let mainFilesProvider else {
1257- return uri
1258- }
12591256
1260- let mainFiles = await mainFilesProvider . mainFilesContainingFile ( uri)
1257+ let mainFiles = await mainFiles ( containing : uri)
12611258 if mainFiles. contains ( uri) {
12621259 // If the main files contain the file itself, prefer to use that one
12631260 return uri
@@ -1270,6 +1267,37 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
12701267 }
12711268 }
12721269
1270+ /// Returns all main files that include the given document.
1271+ ///
1272+ /// On Darwin platforms, this also performs the following normalization: indexstore-db by itself returns realpaths
1273+ /// but the build system might be using standardized Darwin paths (eg. realpath is `/private/tmp` but the standardized
1274+ /// path is `/tmp`). If the realpath that indexstore-db returns could not be found in the build system's source files
1275+ /// but the standardized path is part of the source files, return the standardized path instead.
1276+ package func mainFiles( containing uri: DocumentURI ) async -> [ DocumentURI ] {
1277+ guard let mainFilesProvider else {
1278+ return [ uri]
1279+ }
1280+ let mainFiles = Array ( await mainFilesProvider. mainFiles ( containing: uri, crossLanguage: false ) )
1281+ #if canImport(Darwin)
1282+ if let buildableSourceFiles = try ? await self . buildableSourceFiles ( ) {
1283+ return mainFiles. map { mainFile in
1284+ if buildableSourceFiles. contains ( mainFile) {
1285+ return mainFile
1286+ }
1287+ guard let fileURL = mainFile. fileURL else {
1288+ return mainFile
1289+ }
1290+ let standardized = DocumentURI ( fileURL. standardizedFileURL)
1291+ if buildableSourceFiles. contains ( standardized) {
1292+ return standardized
1293+ }
1294+ return mainFile
1295+ }
1296+ }
1297+ #endif
1298+ return mainFiles
1299+ }
1300+
12731301 /// Returns the main file used for `uri`, if this is a registered file.
12741302 ///
12751303 /// For testing purposes only.
@@ -1317,11 +1345,9 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
13171345 filesWithUpdatedDependencies. formUnion ( sourceFiles. flatMap ( \. sources) . map ( \. uri) )
13181346 }
13191347
1320- if let mainFilesProvider {
1321- var mainFiles = await Set ( events. asyncFlatMap { await mainFilesProvider. mainFilesContainingFile ( $0. uri) } )
1322- mainFiles. subtract ( events. map ( \. uri) )
1323- filesWithUpdatedDependencies. formUnion ( mainFiles)
1324- }
1348+ var mainFiles = await Set ( events. asyncFlatMap { await self . mainFiles ( containing: $0. uri) } )
1349+ mainFiles. subtract ( events. map ( \. uri) )
1350+ filesWithUpdatedDependencies. formUnion ( mainFiles)
13251351
13261352 await self . filesDependenciesUpdatedDebouncer. scheduleCall ( filesWithUpdatedDependencies)
13271353 }
0 commit comments