Skip to content

[Windows] Standardize slashes before path processing #731

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 3 commits into from
Jul 15, 2024

Conversation

jmschonfeld
Copy link
Contributor

@jmschonfeld jmschonfeld commented Jul 12, 2024

All of our String-based path processing assumes that the paths are standardized to forward slashes, however there are many sources (such as FileManager) where these paths may come in on Windows with backslashes resulting in incorrect path manipulation. This updates all of these functions to standardize to forward slashes where needed when performing path-related functions. On windows, these slashes will already be automatically changed back to backslashes if necessary by the file system representation functions.

Note: this currently doesn't handle drive letters any differently than other path components. We can probably do better there, but this at least unblocks the toolchain work and gets us on-par with what SCL-F used to do

@jmschonfeld jmschonfeld force-pushed the windows/path-standardization branch from 6a0ae90 to d72791a Compare July 12, 2024 22:51
@jmschonfeld jmschonfeld changed the title [WIP] Windows Path Standardization [Windows] Standardize slashes before path processing Jul 12, 2024
@jmschonfeld jmschonfeld marked this pull request as ready for review July 12, 2024 22:56
@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@@ -282,10 +282,16 @@ final class FileManagerTests : XCTestCase {
try $0.createDirectory(atPath: "create_dir_test2/nested2", withIntermediateDirectories: true)
XCTAssertEqual(try $0.contentsOfDirectory(atPath: "create_dir_test2").sorted(), ["nested", "nested2"])
XCTAssertNoThrow(try $0.createDirectory(atPath: "create_dir_test2/nested2", withIntermediateDirectories: true))

#if os(Windows)
try $0.createDirectory(atPath: "create_dir_test3\\nested", withIntermediateDirectories: true)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example of a previous API that used to fail without this change. Previously, this API called deletingLastPathComponent on the provided path, which would return an empty string and therefore fail to create the parent create_dir_test3 directory. Now it correctly creates create_dir_test3 followed by create_dir_test3/nested

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like there are two small improvements that can bee made, but seems fine otherwise.

let beforeDot = pathExtension[..<lastDot].unicodeScalars
let afterDot = pathExtension[pathExtension.index(after: lastDot)...].unicodeScalars
let beforeDot = pathExtension[..<lastDot]._standardizingSlashes().unicodeScalars
let afterDot = pathExtension[pathExtension.index(after: lastDot)...]._standardizingSlashes().unicodeScalars
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do we expect an arc separator after the extension separator? That is, the extension should be on the final arc, and so there would be no slashes right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't expect it to occur, but this is to ensure that we validate that it doesn't. I can't come up with any scenarios where we would currently end up in this situation, but invalidExtensionScalars contains a / and so to ensure that POSIX and Windows behaviors are consistent, I added this standardization here so that if we do somehow end up with a \ in the path extension that we mark it as invalid on Windows

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld jmschonfeld merged commit 75a3f5a into swiftlang:main Jul 15, 2024
3 checks passed
@jmschonfeld jmschonfeld deleted the windows/path-standardization branch July 15, 2024 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants