Adds FileExists(path string) bool and DirExists(path string) bool, which
report whether a path holds a regular file or a directory.
These belong here because command-line programs ask the question constantly and
the standard library only offers os.Stat plus an error check, which is
regularly written wrongly: a directory passes an "it exists" test when the
caller wanted a file, and an error that is not "not found" gets treated as
absence anyway. They also sit next to the existing Mkdirp and CopyFile.
Things to know:
Both return false for a path that cannot be looked at, not just for one that
is missing, so a permission problem is indistinguishable from absence. That
is deliberate, since neither case gives the caller a usable file, but a caller
who needs to tell the two apart should use os.Stat directly.
Symbolic links are followed, because os.Stat follows them. A link pointing
at nothing is false. There are tests for both.
Neither answer is a promise about the next line of code: the file can be
removed between the check and the open, so code that then opens the file
should still handle the error rather than trust the check.
The test uses t.TempDir, which needs Go 1.15, while go.mod still says go 1.14. Building with a toolchain that old is not something anyone is
doing in practice, but raising that line would make the file honest, and it
is left out of here so the ten proposal branches do not conflict over it.
The code is in a new file, exists.go, for the same reason.
make test on this branch reports one failure, TestNowUnixMicro. That test
already fails on master and is unrelated to this change.
Model: opus-5
Adds `FileExists(path string) bool` and `DirExists(path string) bool`, which
report whether a path holds a regular file or a directory.
These belong here because command-line programs ask the question constantly and
the standard library only offers `os.Stat` plus an error check, which is
regularly written wrongly: a directory passes an "it exists" test when the
caller wanted a file, and an error that is not "not found" gets treated as
absence anyway. They also sit next to the existing `Mkdirp` and `CopyFile`.
Things to know:
- Both return false for a path that cannot be looked at, not just for one that
is missing, so a permission problem is indistinguishable from absence. That
is deliberate, since neither case gives the caller a usable file, but a caller
who needs to tell the two apart should use `os.Stat` directly.
- Symbolic links are followed, because `os.Stat` follows them. A link pointing
at nothing is false. There are tests for both.
- Neither answer is a promise about the next line of code: the file can be
removed between the check and the open, so code that then opens the file
should still handle the error rather than trust the check.
- The test uses `t.TempDir`, which needs Go 1.15, while `go.mod` still says
`go 1.14`. Building with a toolchain that old is not something anyone is
doing in practice, but raising that line would make the file honest, and it
is left out of here so the ten proposal branches do not conflict over it.
- The code is in a new file, `exists.go`, for the same reason.
- `make test` on this branch reports one failure, `TestNowUnixMicro`. That test
already fails on `master` and is unrelated to this change.
Model: opus-5
clawbot
self-assigned this 2026-09-05 05:29:39 +02:00
FileExists reports whether a path holds a regular file and DirExists whether it
holds a directory, both answering false for anything they cannot look at.
Comes with doc comments and table-driven tests. (closes#15)
Model: opus-5
sneak
merged commit d70ad443e3 into master2026-09-05 05:43:52 +02:00
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Adds
FileExists(path string) boolandDirExists(path string) bool, whichreport whether a path holds a regular file or a directory.
These belong here because command-line programs ask the question constantly and
the standard library only offers
os.Statplus an error check, which isregularly written wrongly: a directory passes an "it exists" test when the
caller wanted a file, and an error that is not "not found" gets treated as
absence anyway. They also sit next to the existing
MkdirpandCopyFile.Things to know:
is missing, so a permission problem is indistinguishable from absence. That
is deliberate, since neither case gives the caller a usable file, but a caller
who needs to tell the two apart should use
os.Statdirectly.os.Statfollows them. A link pointingat nothing is false. There are tests for both.
removed between the check and the open, so code that then opens the file
should still handle the error rather than trust the check.
t.TempDir, which needs Go 1.15, whilego.modstill saysgo 1.14. Building with a toolchain that old is not something anyone isdoing in practice, but raising that line would make the file honest, and it
is left out of here so the ten proposal branches do not conflict over it.
exists.go, for the same reason.make teston this branch reports one failure,TestNowUnixMicro. That testalready fails on
masterand is unrelated to this change.Model: opus-5