Skip to content

Commit f6e55a7

Browse files
authored
TSCBasic: avoid validating the relative path on Windows (#315)
The generated relative path is un-anchored and thus cannot be validated or canonicalized. Introduce a constructor for the unsafe constructor of a relative path and use that to create the relative path. This enables the construction of a non-canonical relative path for cousins.
1 parent d507dbb commit f6e55a7

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Sources/TSCBasic/Path.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ public struct RelativePath: Hashable {
247247
_impl = impl
248248
}
249249

250+
/// Private initializer for constructing a relative path without performing
251+
/// normalization or canonicalization. This will construct a path without
252+
/// an anchor and thus may be invalid.
253+
fileprivate init(unsafeUncheckedPath string: String) {
254+
self.init(PathImpl(string: string))
255+
}
256+
250257
/// Initializes the RelativePath from `str`, which must be a relative path
251258
/// (which means that it must not begin with a path separator or a tilde).
252259
/// An empty input path is allowed, but will be normalized to a single `.`
@@ -915,7 +922,7 @@ extension AbsolutePath {
915922
// might be an empty path (when self and the base are equal).
916923
let relComps = pathComps.dropFirst(baseComps.count)
917924
#if os(Windows)
918-
result = RelativePath(relComps.joined(separator: "\\"))
925+
result = RelativePath(unsafeUncheckedPath: relComps.joined(separator: "\\"))
919926
#else
920927
result = RelativePath(relComps.joined(separator: "/"))
921928
#endif
@@ -934,11 +941,12 @@ extension AbsolutePath {
934941
var relComps = Array(repeating: "..", count: newBaseComps.count)
935942
relComps.append(contentsOf: newPathComps)
936943
#if os(Windows)
937-
result = RelativePath(relComps.joined(separator: "\\"))
944+
result = RelativePath(unsafeUncheckedPath: relComps.joined(separator: "\\"))
938945
#else
939946
result = RelativePath(relComps.joined(separator: "/"))
940947
#endif
941948
}
949+
942950
assert(AbsolutePath(base, result) == self)
943951
return result
944952
}

0 commit comments

Comments
 (0)