@@ -371,7 +371,16 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
371
371
return nil
372
372
}
373
373
374
- if url. pathExtension == " h " , let substituteFile = buildTarget. sources. first {
374
+ if !buildTarget. sources. contains ( url) ,
375
+ let substituteFile = buildTarget. sources. sorted ( by: { $0. path < $1. path } ) . first
376
+ {
377
+ // If `url` is not part of the target's source, it's most likely a header file. Fake compiler arguments for it
378
+ // from a substitute file within the target.
379
+ // Even if the file is not a header, this should give reasonable results: Say, there was a new `.cpp` file in a
380
+ // target and for some reason the `SwiftPMBuildSystem` doesn’t know about it. Then we would infer the target based
381
+ // on the file's location on disk and generate compiler arguments for it by picking a source file in that target,
382
+ // getting its compiler arguments and then patching up the compiler arguments by replacing the substitute file
383
+ // with the `.cpp` file.
375
384
return FileBuildSettings (
376
385
compilerArguments: try buildTarget. compileArguments ( for: substituteFile) ,
377
386
workingDirectory: workspacePath. pathString
@@ -406,7 +415,7 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
406
415
return [ ConfiguredTarget ( targetID: " " , runDestinationID: " dummy " ) ]
407
416
}
408
417
409
- if url . pathExtension == " h " , let target = try ? target ( forHeader : path) {
418
+ if let target = try ? inferredTarget ( for : path) {
410
419
return [ target]
411
420
}
412
421
@@ -614,8 +623,10 @@ extension SwiftPMBuildSystem {
614
623
return canonicalPath == path ? nil : impl ( canonicalPath)
615
624
}
616
625
617
- /// This finds the target the header belongs to based on its location in the file system.
618
- private func target( forHeader path: AbsolutePath ) throws -> ConfiguredTarget ? {
626
+ /// This finds the target a file belongs to based on its location in the file system.
627
+ ///
628
+ /// This is primarily intended to find the target a header belongs to.
629
+ private func inferredTarget( for path: AbsolutePath ) throws -> ConfiguredTarget ? {
619
630
func impl( _ path: AbsolutePath ) throws -> ConfiguredTarget ? {
620
631
var dir = path. parentDirectory
621
632
while !dir. isRoot {
0 commit comments