Skip to content

Commit e022b19

Browse files
committed
[Traits] Encode all trait information for package dump
# Motivation We need to encode all trait related package manifest configuration so that it shows up in the `dump-package` output. # Modification This PR encodes the missing `traits` property of the manifest.
1 parent e858647 commit e022b19

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

Sources/PackageModel/Manifest/Manifest.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ extension Manifest: Encodable {
524524
private enum CodingKeys: CodingKey {
525525
case name, path, url, version, targetMap, toolsVersion,
526526
pkgConfig, providers, cLanguageStandard, cxxLanguageStandard, swiftLanguageVersions,
527-
dependencies, products, targets, platforms, packageKind, revision,
527+
dependencies, products, targets, traits, platforms, packageKind, revision,
528528
defaultLocalization
529529
}
530530

@@ -555,6 +555,7 @@ extension Manifest: Encodable {
555555
try container.encode(self.dependencies, forKey: .dependencies)
556556
try container.encode(self.products, forKey: .products)
557557
try container.encode(self.targets, forKey: .targets)
558+
try container.encode(self.traits, forKey: .traits)
558559
try container.encode(self.platforms, forKey: .platforms)
559560
try container.encode(self.packageKind, forKey: .packageKind)
560561
}

Sources/PackageModel/Manifest/TraitDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
public struct TraitDescription: Sendable, Hashable, ExpressibleByStringLiteral {
13+
public struct TraitDescription: Sendable, Hashable, Codable, ExpressibleByStringLiteral {
1414
/// The trait's canonical name.
1515
///
1616
/// This is used when enabling the trait or when referring to it from other modifiers in the manifest.

Tests/FunctionalTests/TraitTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import DriverSupport
1414
import SPMTestSupport
1515
import PackageModel
16+
import TSCBasic
1617
import XCTest
1718

1819
final class TraitTests: XCTestCase {
@@ -161,5 +162,17 @@ final class TraitTests: XCTestCase {
161162
""")
162163
}
163164
}
165+
166+
func testTraits_dumpPackage() async throws {
167+
try await fixture(name: "Traits") { fixturePath in
168+
let packageRoot = fixturePath.appending("Example")
169+
let (dumpOutput, _) = try await SwiftPM.Package.execute(["dump-package"], packagePath: packageRoot)
170+
let json = try JSON(bytes: ByteString(encodingAsUTF8: dumpOutput))
171+
guard case let .dictionary(contents) = json else { XCTFail("unexpected result"); return }
172+
guard case let .array(traits)? = contents["traits"] else { XCTFail("unexpected result"); return }
173+
XCTAssertEqual(traits.count, 11)
174+
}
175+
}
176+
164177
}
165178
#endif

Tests/PackageGraphTests/ModulesGraphTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2369,7 +2369,7 @@ final class ModulesGraphTests: XCTestCase {
23692369
),
23702370
])
23712371
// Make sure aliases are found properly and do not fall back to pre‐5.2 behavior, leaking across onto other dependencies.
2372-
let required = manifest.dependenciesRequired(for: .everything)
2372+
let required = manifest.dependenciesRequired(for: .everything, enabledTraits: [])
23732373
let unrelated = try XCTUnwrap(required.first(where: { $0.nameForModuleDependencyResolutionOnly == "Unrelated" }))
23742374
let requestedProducts = unrelated.productFilter
23752375
#if ENABLE_TARGET_BASED_DEPENDENCY_RESOLUTION

Tests/PackageModelTests/ManifestTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class ManifestTests: XCTestCase {
9292
targets: targets
9393
)
9494

95-
XCTAssertEqual(manifest.dependenciesRequired(for: .everything).map({ $0.identity.description }).sorted(), [
95+
XCTAssertEqual(manifest.dependenciesRequired(for: .everything, enabledTraits: []).map({ $0.identity.description }).sorted(), [
9696
"bar1",
9797
"bar2",
9898
"bar3",
@@ -109,7 +109,7 @@ class ManifestTests: XCTestCase {
109109
targets: targets
110110
)
111111

112-
XCTAssertEqual(manifest.dependenciesRequired(for: .specific(["Foo"])).map({ $0.identity.description }).sorted(), [
112+
XCTAssertEqual(manifest.dependenciesRequired(for: .specific(["Foo"]), enabledTraits: []).map({ $0.identity.description }).sorted(), [
113113
"bar1", // Foo → Foo1 → Bar1
114114
"bar2", // Foo → Foo1 → Foo2 → Bar2
115115
"bar3", // Foo → Foo1 → Bar1 → could be from any package due to pre‐5.2 tools version.
@@ -126,7 +126,7 @@ class ManifestTests: XCTestCase {
126126
targets: targets
127127
)
128128

129-
XCTAssertEqual(manifest.dependenciesRequired(for: .everything).map({ $0.identity.description }).sorted(), [
129+
XCTAssertEqual(manifest.dependenciesRequired(for: .everything, enabledTraits: []).map({ $0.identity.description }).sorted(), [
130130
"bar1",
131131
"bar2",
132132
"bar3",

0 commit comments

Comments
 (0)