Add FileExists and DirExists for checking what is at a path #16

Merged
sneak merged 1 commits from clawbot/util:proposal-file-exists into master 2026-09-05 05:43:52 +02:00
Contributor

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
clawbot added 1 commit 2026-09-05 05:29:40 +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 master 2026-09-05 05:43:52 +02:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sneak/util#16