Skip to content

Commit 6bf809e

Browse files
committed
The logic to generate manifest source from existing parsed manifest data structures misquoted some package version requirement ranges.
This fixes those cases, producing a semantically equivalent package manifests. Since the model does not capture how a particular range was specified, it is not possible to recreate the original shorthands. A future improvement can either extend the model to record the original spelling of the range requirements, or can apply heuristics to shorten the rules in the future (e.g. automatically derive `upToNextMajor()` etc. This also eliminates an extra whitespace before the minimum tools version declaration when the specified tools version allows it, so older parsers can parse the generated manifests. rdar://76559428
1 parent 1645684 commit 6bf809e

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

Sources/PackageModel/ManifestSourceGeneration.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ extension Manifest {
2525
public var generatedManifestFileContents: String {
2626
/// Only write out the major and minor (not patch) versions of the
2727
/// tools version, since the patch version doesn't change semantics.
28+
/// We leave out the spacer if the tools version doesn't support it.
2829
return """
29-
// swift-tools-version: \(toolsVersion.major).\(toolsVersion.minor)
30+
// swift-tools-version:\(toolsVersion < .v5_4 ? "" : " ")\(toolsVersion.major).\(toolsVersion.minor)
3031
import PackageDescription
3132
3233
let package = \(SourceCodeFragment(from: self).generateSourceCode())
@@ -129,9 +130,9 @@ fileprivate extension SourceCodeFragment {
129130
params.append(SourceCodeFragment(key: "url", string: data.location))
130131
switch data.requirement {
131132
case .exact(let version):
132-
params.append(SourceCodeFragment(enum: "exact", string: version.description))
133+
params.append(SourceCodeFragment(enum: "exact", string: "\(version)"))
133134
case .range(let range):
134-
params.append(SourceCodeFragment(enum: "range", string: range.description))
135+
params.append(SourceCodeFragment("\"\(range.lowerBound)\"..<\"\(range.upperBound)\""))
135136
case .revision(let revision):
136137
params.append(SourceCodeFragment(enum: "revision", string: revision))
137138
case .branch(let branch):

Tests/WorkspaceTests/ManifestSourceGenerationTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,35 @@ class ManifestSourceGenerationTests: XCTestCase {
159159
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
160160
}
161161

162+
func testPackageDependencyVariations() throws {
163+
let manifestContents = """
164+
// swift-tools-version:5.3
165+
import PackageDescription
166+
167+
let package = Package(
168+
name: "MyPackage",
169+
dependencies: [
170+
.package(url: "/foo1", from: "1.0.0"),
171+
.package(url: "/foo2", .revision("58e9de4e7b79e67c72a46e164158e3542e570ab6")),
172+
.package(path: "../foo3"),
173+
.package(path: "/path/to/foo4"),
174+
.package(url: "/foo5", .exact("1.2.3")),
175+
.package(url: "/foo6", "1.2.3"..<"2.0.0"),
176+
.package(url: "/foo7", .branch("master")),
177+
.package(url: "/foo8", .upToNextMinor(from: "1.3.4")),
178+
.package(url: "/foo9", .upToNextMajor(from: "1.3.4")),
179+
.package(path: "~/path/to/foo10"),
180+
.package(path: "~foo11"),
181+
.package(path: "~/path/to/~/foo12"),
182+
.package(path: "~"),
183+
.package(path: "file:///path/to/foo13"),
184+
]
185+
)
186+
"""
187+
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
188+
}
189+
190+
162191
func testResources() throws {
163192
let manifestContents = """
164193
// swift-tools-version:5.3

0 commit comments

Comments
 (0)