-
Notifications
You must be signed in to change notification settings - Fork 109
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
Implement FilePath.getCurrentWorkingDirectory() #71
Conversation
Calling
Glibc has It may also be worth emphasising the point about this being process-wide mutable state. For example, Foundation's documentation has a big, red warning box about it. Personally, I think the concept of a process-wide, mutable "current working directory" is a serious mistake; a dinosaur we should allow to go extinct. One thread can start working on some files, assuming one working directory, and then another thread can change the working directory and affect the way paths are resolved by the other thread. There's a good argument for leaving this out of |
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.
I like it! @lorentey your take?
The man page says:
These routines have traditionally been used by programs to save the name of a working directory for the purpose of returning to it. A much faster and less error-prone method of accomplishing this is to open the current directory (`.') and use the fchdir(2) function to return.
I think it makes sense to also land changeWorkingDirectory in the same release as this, but where would it live? (This doesn't have to block this PR, of course).
defer { system_free(cwd) } | ||
return FilePath(platformString: cwd) | ||
} | ||
|
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.
Way back when I was thinking this would be hosted on some kind of current process type, but I think this approach makes more sense. It's also available for typename elision IIUC:
func foo(_: FilePath)
let x = foo(.getCurrentWorkingDirectory())
I feel like making this a function rather than a throwing var makes sense (ignoring tools version concerns) as it will make a syscall.
I'm amenable to this idea, but perhaps the piece that should "go extinct" is the mutable bit, meaning we don't provide API to set the working directory (or implement it as
|
Swift System's job is to faithfully expose the underlying system's APIs, not to reimagine them. We can be selective about which syscalls we expose, but this is not the right place (nor the right forum) to discuss changing the underlying OS functionality. I believe there are legitimate reasons Swift programs might want to get (or even set) the working directory, so it seems reasonable for System to expose related syscalls. |
Hmm, however NIO handles it. @Lukasa , do you know? |
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.
This is very much desirable, and I agree it's good to have this as a static FilePath member.
Comments below.
/// Returns the current working directory of this process. | ||
/// | ||
/// - Warning: This value is global and care should be taken to make sure it does not unexpectedly change. | ||
public static func getCurrentWorkingDirectoryForProcess() throws -> FilePath { |
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 should lose the get
prefix -- per the Swift API design guidelines, functions without side effects should read as noun phrases.
I know you specifically added this in response to discussions, but I would like to also remove the ForProcess
suffix. There are two reasons for this:
-
First, I don't think it makes sense to try documenting the precise semantics of these functions through their naming -- it seems futile to me, and it leads to an unwieldy naming scheme.
-
Far, far more importantly, while the default semantics of these syscalls have been pretty stable, there is no reason an OS cannot switch to different behavior in certain circumstances. For instance, on Linux and macOS at least we have non-portable ways to set up a thread-local working directory.
public static func getCurrentWorkingDirectoryForProcess() throws -> FilePath { | |
public static func currentWorkingDirectory() throws -> FilePath { |
/// | ||
/// - Warning: This value is global and care should be taken to make sure it does not unexpectedly change. | ||
public static func getCurrentWorkingDirectoryForProcess() throws -> FilePath { | ||
guard let cwd = system_getcwd(nil, 0) else { |
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.
This will allocate twice -- wouldn't it be better if we supplied this with an appropriately sized FilePath buffer and only resize it if we guessed wrong?
If desired, we can also eliminate allocations altogether by adding a variant that takes an inout FilePath. (I don't think we need that.)
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.
This might be better but given all the discussion about how this shouldn't be used often, I preferred the simpler variant rather than adding an infrequently taken second codepath.
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.
FilePath
used to have a with-style internal init that used max-path-length, but it turns out there are multiple notions of such a thing and it's probably too complicated as-is (and wastes space). I agree that it doesn't matter for now; in the future we'd like to do it with something like an init on FilePath
, which can make the space/speed tradeoffs.
I suggest simply adding them as a methods on extension FilePath {
func setCurrentWorkingDirectory() throws
}
extension FileDescriptor {
func setCurrentWorkingDirectory() throws
}
(We can also call these |
Right, the argument I was making is that it may not be a good idea to expose it. I'm aware that Github comments are not an effective way to change the world's operating systems 😄. This is an ancient API that is deeply flawed when used on modern systems, and almost every OS warns against using them or has introduced better APIs as an alternative:
Microsoft documentation: So there is clearly widespread recognition that this is a problem. Whilst it is a very familiar API, it should not be a given that it deserves a place in swift-system, or that it deserves an easy and convenient name IMO. We should be taking the hint that every major system discourages using it. If we really want to do this, might I suggest that it be named |
I agree with pretty much every word, but I don't think it would be practical for System not to expose Providing a wrapper for There are also a myriad valid reasons why people might want to change the current working directory. (I view System as mostly targeting use cases that are below Foundation -- I would go as far as to suggest it is usually a mistake to import System from a regular app.)
We normally use the Also, by this reasoning, pretty much every single API exposed by System ought to be marked |
We definitely need a wrapper for (That said, I don't think that |
Sure - I just like to make sure that we're considering all the options, you know? |
I (weakly-held) liked the We could argue that making it a |
Because we don't have the luxury of using the spelling (making no judgement on the word |
Closing due to lack of activity... feel free to reopen and merge if you get to it |
I'm not sure how to mock
system_getcwd
, but otherwise this works.We may eventually want to have a stack-allocated buffer of PATH_MAX size so we don't do two allocations, but I don't think we can achieve this in present-day Swift.