Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Seems odd for Zig to change the semantics of fchdir() in this case. The underlying syscall returns EBADF, because it is a bad file descriptor. AT_FDCWD is defined for the openat() call, and probably shouldn't be a special case anywhere else?
Also, if you want Zig's fchdir() to understand AT_FDCWD, then it seems like it should check the directory to see if any of the error cases (e.g. Access Denied, or NotDir) apply.
Uh oh!
There was an error while loading. Please reload this page.
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.
Some context:
std.fs.cwd()returns aDirwith fdAT_FDCWD, which is not a real handle (it's the constant-100)std.ChildProcess.execwith.cwd_dir = std.fs.cwd()So, either the strategy for
std.fs.cwd()needs to change (would need to make it return a real fd) or we need some special handling ofAT_FDCWDthroughout the standard library. I'm not totally sure what the best way to go in the long run will be, but this particular change doesn't seem like it has many/any downsides, as I'm unsure why it would make sense forfchdirwithAT_FDCWDto return an error--it's almost by definition a no-op.More discussion in #9688
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.
Ah, the use of
AT_FDCWDbystd.fs.cwd()seems a bit funky to me. AFAICT, in Linux the -100 is only magical to theopenatcall (its the source of the "AT_" prefix on the symbol, I believe).If Zig wants to support a fake file descriptor number to represent the "current working directory" without actually opening it, then it should probably be a constant not defined by
openat? This does seem likely to lead to some awkward special casing, like shouldgetdentsandfstatsupport this special constant too? (Isstd.fs.stat(std.fs.cwd())is a thing too?)Also, I'm not sure where the code involved here falls on the stick-to-Posix versus Zig-universal-library discussion (
fchdiris strictly Posix-land?, andChildProcessis Zig?)Actually .. is a bigger problem that the lazily resolved
-100fromstat.fs.cwd()represents the wrong directory if its followed by achdir()before being used? E.g., if I create a ChildProcess with.cwd_dir = std.fs.cwd(), then do achdir()to somewhere else, then start the child process, which "current" directory should the child get?Uh oh!
There was an error while loading. Please reload this page.
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 has yet to be figured out, but if Zig wants to stick with
std.fs.cwd()returningAT_FDCWD, then it'll need some handling at some level of abstraction in all the places it can cause problems (you're right thatstd.fs.cwd().stat()will hit the same problem infstat). This is why I'm unsure if this is a good/viable long term plan, but right nowstd.fs.cwd()really relies on being essentially free (i.e. it can't fail and it can be called all over the place without opening a new fd each time).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 believe that it might be a good idea to handle
AT_FDCWDin other functions instd.os. However, this PR has been implemented as requested by Andrew in the PR I've linked in this one's description.