Skip to content

Commit 4181648

Browse files
committed
Error on relative file URLs
It turns out that relative file URLs actually do not exist, see [here](#3604 (comment)) for more discussion. Instead we will now error and guide users to using a local package reference.
1 parent a16a934 commit 4181648

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

Sources/PackageLoading/PackageDescription4Loader.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,11 @@ extension PackageDependencyDescription {
345345
} else if dependencyLocation.hasPrefix(filePrefix) {
346346
// FIXME: SwiftPM can't handle file locations with file:// scheme so we need to
347347
// strip that. We need to design a Location data structure for SwiftPM.
348-
return AbsolutePath(String(dependencyLocation.dropFirst(filePrefix.count)), relativeTo: AbsolutePath(packageLocation)).pathString
348+
let location = String(dependencyLocation.dropFirst(filePrefix.count))
349+
if location.first != "/" {
350+
throw ManifestParseError.invalidManifestFormat("file:// URLs cannot be relative, did you mean to use `.package(path:)`?", diagnosticFile: nil)
351+
}
352+
return AbsolutePath(location).pathString
349353
} else if URL.scheme(dependencyLocation) == nil {
350354
// If the dependency URL is not remote, try to "fix" it.
351355
// If the URL has no scheme, we treat it as a path (either absolute or relative to the base URL).

Tests/PackageLoadingTests/PD4_2LoadingTests.swift

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -516,17 +516,12 @@ class PackageDescription4_2LoadingTests: PackageDescriptionLoadingTests {
516516
)
517517
"""
518518

519-
try loadManifestThrowing(stream.bytes) { manifest in
520-
if let dep = manifest.dependencies.first {
521-
switch dep {
522-
case .scm(let scm):
523-
XCTAssertEqual(scm.location, "/best")
524-
default:
525-
XCTFail("dependency was expected to be remote")
526-
}
527-
} else {
528-
XCTFail("manifest had no dependencies")
529-
}
519+
do {
520+
try loadManifestThrowing(stream.bytes) { _ in }
521+
XCTFail("Unexpected success")
522+
} catch ManifestParseError.invalidManifestFormat(let message, let diagnosticFile) {
523+
XCTAssertNil(diagnosticFile)
524+
XCTAssertEqual(message, "file:// URLs cannot be relative, did you mean to use `.package(path:)`?")
530525
}
531526
}
532527

0 commit comments

Comments
 (0)