Skip to content

Commit e8e0850

Browse files
authored
[Traits] Enable all traits when building the symbol graph (#8177)
# Motivation When building the symbol graph for documentation when want to make sure all trait guarded code is build. # Modification This PR changes the trait configuration when extracting the symbol graph by enabling all traits. # Result This PR will make sure all traits are enabled. In the future, we might want to pass a trait configuration through the DocC plugin to SwiftPM that allows us to build documentation with specific traits enabled but for now this should be fine since a package should support enabling all traits at once.
1 parent a19fd17 commit e8e0850

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

Fixtures/Traits/Package10/Package.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@ let package = Package(
1818
.target(
1919
name: "Package10Library1"
2020
),
21+
.plugin(
22+
name: "SymbolGraphExtract",
23+
capability: .command(
24+
intent: .custom(verb: "extract", description: "")
25+
)
26+
),
2127
]
2228
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import PackagePlugin
2+
3+
@main struct SymbolGraphExtractPlugin: CommandPlugin {
4+
func performCommand(
5+
context: PluginContext,
6+
arguments: [String]
7+
) throws {
8+
let result = try self.packageManager.getSymbolGraph(for: context.package.targets.first!, options: .init())
9+
print(result.directoryPath)
10+
}
11+
}

Fixtures/Traits/Package10/Sources/Package10Library1/Library.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,11 @@ public func hello() {
1010
print("Package10Library1 trait2 disabled")
1111
#endif
1212
}
13+
14+
#if Package10Trait1
15+
public struct TypeGatedByPackage10Trait1 {}
16+
#endif
17+
18+
#if Package10Trait2
19+
public struct TypeGatedByPackage10Trait2 {}
20+
#endif

Sources/Commands/Utilities/PluginDelegate.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import CoreCommands
1616
import Foundation
1717
import PackageModel
1818
import SPMBuildCore
19+
import PackageGraph
1920

2021
import protocol TSCBasic.OutputByteStream
2122
import class TSCBasic.BufferedOutputByteStream
@@ -398,7 +399,7 @@ final class PluginDelegate: PluginInvocationDelegate {
398399
// Create a build system for building the target., skipping the the cache because we need the build plan.
399400
let buildSystem = try await swiftCommandState.createBuildSystem(
400401
explicitBuildSystem: .native,
401-
traitConfiguration: .init(),
402+
traitConfiguration: TraitConfiguration(enableAllTraits: true),
402403
cacheBuildManifest: false
403404
)
404405

Tests/FunctionalTests/TraitTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,5 +238,31 @@ final class TraitTests: XCTestCase {
238238
XCTAssertTrue(stdout.contains(expectedOut))
239239
}
240240
}
241+
242+
func testPackageDumpSymbolGraph_enablesAllTraits() async throws {
243+
try await fixture(name: "Traits") { fixturePath in
244+
let (stdout, _) = try await executeSwiftPackage(fixturePath.appending("Package10"), extraArgs: ["dump-symbol-graph"])
245+
let optionalPath = stdout
246+
.lazy
247+
.split(whereSeparator: \.isNewline)
248+
.first { String($0).hasPrefix("Files written to ") }?
249+
.dropFirst(17)
250+
251+
let path = String(try XCTUnwrap(optionalPath))
252+
let symbolGraph = try String(contentsOfFile: "\(path)/Package10Library1.symbols.json", encoding: .utf8)
253+
XCTAssertTrue(symbolGraph.contains("TypeGatedByPackage10Trait1"))
254+
XCTAssertTrue(symbolGraph.contains("TypeGatedByPackage10Trait2"))
255+
}
256+
}
257+
258+
func testPackagePluginGetSymbolGraph_enablesAllTraits() async throws {
259+
try await fixture(name: "Traits") { fixturePath in
260+
let (stdout, _) = try await executeSwiftPackage(fixturePath.appending("Package10"), extraArgs: ["plugin", "extract"])
261+
let path = String(stdout.split(whereSeparator: \.isNewline).first!)
262+
let symbolGraph = try String(contentsOfFile: "\(path)/Package10Library1.symbols.json", encoding: .utf8)
263+
XCTAssertTrue(symbolGraph.contains("TypeGatedByPackage10Trait1"))
264+
XCTAssertTrue(symbolGraph.contains("TypeGatedByPackage10Trait2"))
265+
}
266+
}
241267
}
242268
#endif

0 commit comments

Comments
 (0)