Skip to content

Commit 7f2a2fc

Browse files
authored
When generating manifest the type should be before the targets (#8042)
Method [`generateManifestFileContents(packageDirectory:)`](https://github.com/swiftlang/swift-package-manager/blob/80b1e179bef19e6eb71b0d20b2cb471053ae1b2c/Sources/PackageModel/ManifestSourceGeneration.swift#L33) is generating invalid manifest if the manifest has a `ProductDescription` with a library with dynamic type. Specificaly the in the generated Manifest it places the type: `.dynamic`, after the targets. The problem in [here](https://github.com/swiftlang/swift-package-manager/blob/80b1e179bef19e6eb71b0d20b2cb471053ae1b2c/Sources/PackageModel/ManifestSourceGeneration.swift#L204), where the code that adds the dynamic type is after the code that adds the targets.
1 parent 04329d3 commit 7f2a2fc

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

Sources/PackageModel/ManifestSourceGeneration.swift

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,29 +201,32 @@ fileprivate extension SourceCodeFragment {
201201
else {
202202
var params: [SourceCodeFragment] = []
203203
params.append(SourceCodeFragment(key: "name", string: product.name))
204-
if !product.targets.isEmpty {
204+
if !product.targets.isEmpty && !product.type.isLibrary {
205205
params.append(SourceCodeFragment(key: "targets", strings: product.targets))
206206
}
207207
switch product.type {
208208
case .library(let type):
209209
if type != .automatic {
210210
params.append(SourceCodeFragment(key: "type", enum: type.rawValue))
211211
}
212-
self.init(enum: "library", subnodes: params, multiline: true)
213-
case .executable:
214-
self.init(enum: "executable", subnodes: params, multiline: true)
215-
case .snippet:
216-
self.init(enum: "sample", subnodes: params, multiline: true)
217-
case .plugin:
218-
self.init(enum: "plugin", subnodes: params, multiline: true)
219-
case .test:
220-
self.init(enum: "test", subnodes: params, multiline: true)
212+
if !product.targets.isEmpty {
213+
params.append(SourceCodeFragment(key: "targets", strings: product.targets))
214+
}
215+
self.init(enum: "library", subnodes: params, multiline: true)
216+
case .executable:
217+
self.init(enum: "executable", subnodes: params, multiline: true)
218+
case .snippet:
219+
self.init(enum: "sample", subnodes: params, multiline: true)
220+
case .plugin:
221+
self.init(enum: "plugin", subnodes: params, multiline: true)
222+
case .test:
223+
self.init(enum: "test", subnodes: params, multiline: true)
221224
case .macro:
222225
self.init(enum: "macro", subnodes: params, multiline: true)
223226
}
224227
}
225228
}
226-
229+
227230
/// Instantiates a SourceCodeFragment to represent a single target.
228231
init(from target: TargetDescription) {
229232
var params: [SourceCodeFragment] = []

Tests/WorkspaceTests/ManifestSourceGenerationTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,45 @@ final class ManifestSourceGenerationTests: XCTestCase {
156156
try await testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
157157
}
158158

159+
func testDynamicLibraryType() async throws {
160+
let manifestContents = """
161+
// swift-tools-version:5.3
162+
// The swift-tools-version declares the minimum version of Swift required to build this package.
163+
164+
import PackageDescription
165+
166+
let package = Package(
167+
name: "MyPackage",
168+
platforms: [
169+
.macOS(.v10_14),
170+
.iOS(.v13)
171+
],
172+
products: [
173+
// Products define the executables and libraries a package produces, and make them visible to other packages.
174+
.library(
175+
name: "MyPackage",
176+
type: .dynamic,
177+
targets: ["MyPackage"]),
178+
],
179+
dependencies: [
180+
// Dependencies declare other packages that this package depends on.
181+
// .package(url: /* package url */, from: "1.0.0"),
182+
],
183+
targets: [
184+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
185+
// Targets can depend on other targets in this package, and on products in packages this package depends on.
186+
.target(
187+
name: "MyPackage",
188+
dependencies: []),
189+
.testTarget(
190+
name: "MyPackageTests",
191+
dependencies: ["MyPackage"]),
192+
]
193+
)
194+
"""
195+
try await testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
196+
}
197+
159198
func testCustomPlatform() async throws {
160199
let manifestContents = """
161200
// swift-tools-version:5.6

0 commit comments

Comments
 (0)