Skip to content

io/fs: fs.Sub does not cause error if directory is not present #77447

@Sebbito

Description

@Sebbito

Go version

1.25.7

Output of go env in your module/workspace:

AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/user/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/user/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/zr/wt9b1_rj0p371zpbgqfsczt40000gn/T/go-build2255838079=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/user/Code/website/website/go.mod'
GOMODCACHE='/Users/user/go/pkg/mod'
GONOPROXY='bitbucket.org/check24'
GONOSUMDB='bitbucket.org/check24'
GOOS='darwin'
GOPATH='/Users/user/go'
GOPRIVATE='bitbucket.org/check24'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/user/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.25.7.darwin-arm64'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/user/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/user/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.25.7.darwin-arm64/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.25.7'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Create a SubFS from an embed.FS for a directory that is not present. Then walk that directory and get an error:

package main

import (
	"embed"
	"io/fs"
	"log"
)

//go:embed content
var embedded embed.FS

func main() {
	s, err := fs.Sub(embedded, "test")
	if err != nil {
		log.Fatalf("error opening sub: %v", err)
	}
	err = fs.WalkDir(s, ".", func(path string, d fs.DirEntry, err error) error {
		if err != nil { return err }
		return nil
	})
	if err != nil {
		log.Fatalf("walkdir error: %v", err)
	}
}

What did you see happen?

Creating the SubFS worked which is misleading since the directory doesn't exist. Therefore the error will be thrown while walking the directory

$ tree
.
├── content
│   └── file
└── main.go

2 directories, 2 files

$ go run main.go 
2026/02/05 10:46:34 walkdir error: open .: file does not exist
exit status 1

What did you expect to see?

I would have expected fs.Sub to return an error and stop the execution before walking the directory:

$ go run main.go 
2026/02/05 10:46:34 error opening sub: 'test' does not exist
exit status 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions