-
Notifications
You must be signed in to change notification settings - Fork 2.3k
correct incorrect handling of file paths #6355
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
base: main
Are you sure you want to change the base?
Conversation
Here's an example of your CHANGELOG entry: * correct incorrect handling of file paths.
[compnerd](https://github.com/compnerd)
[#issue_number](https://github.com/realm/SwiftLint/issues/issue_number)note: There are two invisible spaces after the entry's text. Generated by 🚫 Danger |
38c399c to
8e2e62c
Compare
`URL.path` is not a usable rendering of the path. `URL.path` represents the URI path. If the path is meant to be consumable as a file path, the file system representation (aka FSR) must be retrieved. This improves the file path handling and makes additional tests now pass.
|
@SimplyDanny I think that I will need a little bit of help here. The current failure seems to be unrelated to my change and might be a general configuration issue. It does seem that this change is passing the CI and does improve the test coverage on Windows. I'm not sure that brace expansion is required - the musl path does not support that either. Please let me know if there is anything else that this needs. |
|
I think this was just a hiccup in the pipeline. Works now after retriggering it. I'll have a look at the changes later. |
SimplyDanny
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from a few nits, this looks reasonable to me, even though is a bit strange that one needs to rely on an "unsafe" and unwieldy API with withUnsafeFileSystemRepresentation.
| public extension URL { | ||
| var filepath: String { | ||
| self.withUnsafeFileSystemRepresentation { String(cString: $0!) } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We normally use 4 spaces for indentation.
| // if path is a file, it won't be returned in `enumerator(atPath:)` | ||
| if absolutePath.bridge().isSwiftFile(), absolutePath.isFile { | ||
| return [absolutePath] | ||
| if FileManager.default.isFile(atPath: root.path), root.pathExtension == "swift" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For FileManager.default.isFile(atPath:), we don't need to use root.filepath?
| @@ -0,0 +1,7 @@ | |||
| import Foundation | |||
|
|
|||
| public extension URL { | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add isSwiftFile that encapsulates FileManager.default.isFile(atPath: path) and pathExtension == "swift" to this extension.
URL.pathis not a usable rendering of the path.URL.pathrepresents the URI path. If the path is meant to be consumable as a file path, the file system representation (aka FSR) must be retrieved.Unfortunately, the FSR access is a bit more complicated as the API for that returns a +0 borrowed object with the lifetime bound to
URL. Because this relies on the toll-free bridged storage ofURL, Swift does not have this. Instead, we are provided awithUnsafeFileSystemRepresentationwhich provides a heap allocated buffer that is scoped, which we can copy into aString. To abstract away this wart, we introduce an extension onURLto access the FSR through the newfilepathproperty.This improves the file path handling and makes additional tests now pass.