Skip to content

TSCBasic: avoid validating the relative path on Windows #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Sources/TSCBasic/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ public struct RelativePath: Hashable {
_impl = impl
}

/// Private initializer for constructing a relative path without performing
/// normalization or canonicalization. This will construct a path without
/// an anchor and thus may be invalid.
fileprivate init(unsafeUncheckedPath string: String) {
self.init(PathImpl(string: string))
}

/// Initializes the RelativePath from `str`, which must be a relative path
/// (which means that it must not begin with a path separator or a tilde).
/// An empty input path is allowed, but will be normalized to a single `.`
Expand Down Expand Up @@ -915,7 +922,7 @@ extension AbsolutePath {
// might be an empty path (when self and the base are equal).
let relComps = pathComps.dropFirst(baseComps.count)
#if os(Windows)
result = RelativePath(relComps.joined(separator: "\\"))
result = RelativePath(unsafeUncheckedPath: relComps.joined(separator: "\\"))
#else
result = RelativePath(relComps.joined(separator: "/"))
#endif
Expand All @@ -934,11 +941,12 @@ extension AbsolutePath {
var relComps = Array(repeating: "..", count: newBaseComps.count)
relComps.append(contentsOf: newPathComps)
#if os(Windows)
result = RelativePath(relComps.joined(separator: "\\"))
result = RelativePath(unsafeUncheckedPath: relComps.joined(separator: "\\"))
#else
result = RelativePath(relComps.joined(separator: "/"))
#endif
}

assert(AbsolutePath(base, result) == self)
return result
}
Expand Down