Skip to content

Commit 689d162

Browse files
authored
Consider snippets in ResolvedProduct.executableTarget (#5931)
When computing the underlying executable target for a resolved product, consider snippet targets as well. Otherwise, this results in a fatal error when building a package that contains snippets. rdar://102784273
1 parent 16c7cb6 commit 689d162

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

Sources/PackageModel/Target.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ public extension Sequence where Iterator.Element == Target {
812812
switch $0.type {
813813
case .binary:
814814
return ($0 as? BinaryTarget)?.containsExecutable == true
815-
case .executable:
815+
case .executable, .snippet:
816816
return true
817817
default:
818818
return false

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4251,6 +4251,57 @@ final class BuildPlanTests: XCTestCase {
42514251
try sanitizerTest(.scudo, expectedName: "scudo")
42524252
}
42534253

4254+
func testSnippets() throws {
4255+
let fs = InMemoryFileSystem(emptyFiles:
4256+
"/Pkg/Sources/Lib/Lib.swift",
4257+
"/Pkg/Snippets/ASnippet.swift",
4258+
"/Pkg/.build/release.yaml"
4259+
)
4260+
let buildPath = AbsolutePath(path: "/Pkg/.build")
4261+
let observability = ObservabilitySystem.makeForTesting()
4262+
let graph = try loadPackageGraph(
4263+
fileSystem: fs,
4264+
manifests: [
4265+
Manifest.createRootManifest(
4266+
name: "Lib",
4267+
path: .init(path: "/Pkg"),
4268+
toolsVersion: .vNext,
4269+
dependencies: [],
4270+
products: [
4271+
ProductDescription(name: "Lib", type: .library(.automatic), targets: ["Lib"])
4272+
],
4273+
targets: [
4274+
TargetDescription(name: "Lib", dependencies: [], type: .regular),
4275+
]),
4276+
],
4277+
observabilityScope: observability.topScope
4278+
)
4279+
XCTAssertNoDiagnostics(observability.diagnostics)
4280+
let plan = try BuildPlan(
4281+
buildParameters: mockBuildParameters(buildPath: buildPath),
4282+
graph: graph,
4283+
fileSystem: fs,
4284+
observabilityScope: observability.topScope
4285+
)
4286+
4287+
let result = try BuildPlanResult(plan: plan)
4288+
result.checkProductsCount(1)
4289+
result.checkTargetsCount(2)
4290+
XCTAssertTrue(result.targetMap.values.contains { $0.target.name == "ASnippet" && $0.target.type == .snippet })
4291+
XCTAssertTrue(result.targetMap.values.contains { $0.target.name == "Lib" })
4292+
4293+
let yaml = buildPath.appending(component: "release.yaml")
4294+
let llbuild = LLBuildManifestBuilder(plan, fileSystem: fs, observabilityScope: observability.topScope)
4295+
try llbuild.generateManifest(at: yaml)
4296+
4297+
let yamlContents: String = try fs.readFileContents(yaml)
4298+
print(yamlContents)
4299+
XCTAssertMatch(yamlContents, .contains("""
4300+
inputs: ["/Pkg/Snippets/ASnippet.swift","/Pkg/.build/debug/Lib.swiftmodule"
4301+
"""))
4302+
4303+
}
4304+
42544305
private func sanitizerTest(_ sanitizer: PackageModel.Sanitizer, expectedName: String) throws {
42554306
let fs = InMemoryFileSystem(emptyFiles:
42564307
"/Pkg/Sources/exe/main.swift",

0 commit comments

Comments
 (0)