Skip to content

Commit 574a059

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 574a059

File tree

3 files changed

+7
-14
lines changed

3 files changed

+7
-14
lines changed

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import TSCUtility
1515
import Foundation
1616
public typealias FileSystem = TSCBasic.FileSystem
1717

18-
public enum ManifestParseError: Swift.Error {
18+
public enum ManifestParseError: Swift.Error, Equatable {
1919
/// The manifest contains invalid format.
2020
case invalidManifestFormat(String, diagnosticFile: AbsolutePath?)
2121

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: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -516,18 +516,7 @@ 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-
}
530-
}
519+
XCTAssertManifestLoadThrows(ManifestParseError.invalidManifestFormat("file:// URLs cannot be relative, did you mean to use `.package(path:)`?", diagnosticFile: nil), stream.bytes)
531520
}
532521

533522
func testCacheInvalidationOnEnv() throws {

0 commit comments

Comments
 (0)