Skip to content

io/fs: should fs.Sub(fsys, "no such directory") return a file system that fails fstest.TestFS? #45087

@dchapes

Description

@dchapes

What version of Go are you using (go version)?

$ go version
go version go1.16.2 freebsd/amd64

Does this issue reproduce with the latest release?

If you mean latest released Go, then yes.
If you mean tip, I'm not sure, but I expect so.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
[output trimmed]
GO111MODULE=""
GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="freebsd"
GOOS="freebsd"
GOVERSION="go1.16.2"
CGO_ENABLED="1"

What did you do?

    fs, err := fs.Sub(someFS, "noSuchDir")
    err = fstest.TestFS(fs)

https://play.golang.org/p/eXsqSUYHWQs

What did you expect to see?

No error returned.

What did you see instead?

It fails due to lack of an empty root directory:

    prog.go:18: TestFS found errors:
        .: Open: open .: file does not exist

This may very well be a "works as intended" issue, and if so I apologise for the noise.

FYI: I ran into this while experimenting with implementing the opposite of fs.Sub, a Prefix(fs.FS, string) (fs.FS, error).
I provided a prefixFS.Sub as an optimisation where it returns another prefixFS with the prefix adjusted or calls fs.Sub on it's underlying file system to avoid chaining multiple wrappers. I ended up also implementing an EmptyFS that I return when the sub-dir isn't within my prefix in order to keep fstest.TestFS happy.

I imagine others implementing fs.SubFS and trying to test their code would appreciate not having to make their own EmptyFS just to satisfy fstest.TestFS. Perhaps an empty file system implementation could be provided or fstest could be tweaked with respect to what it accepts as a completely emtpy file system.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions