Skip to content

Commit ca5774a

Browse files
author
Jay Conrod
committed
embed: treat uninitialized FS as empty
As described in the FS documentation. This prevents http.FS and other clients from panicking when the go:embed directive is missing. For #43682 Related #43698 Change-Id: Iecf26d229a099e55d24670c3119cd6c6d17ecc6e Reviewed-on: https://go-review.googlesource.com/c/go/+/283852 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Jay Conrod <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent d047c91 commit ca5774a

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/embed/embed.go

+6
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ func (f FS) lookup(name string) *file {
244244
if name == "." {
245245
return dotFile
246246
}
247+
if f.files == nil {
248+
return nil
249+
}
247250

248251
// Binary search to find where name would be in the list,
249252
// and then check if name is at that position.
@@ -261,6 +264,9 @@ func (f FS) lookup(name string) *file {
261264

262265
// readDir returns the list of files corresponding to the directory dir.
263266
func (f FS) readDir(dir string) []file {
267+
if f.files == nil {
268+
return nil
269+
}
264270
// Binary search to find where dir starts and ends in the list
265271
// and then return that slice of the list.
266272
files := *f.files

src/embed/internal/embedtest/embed_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,20 @@ func TestHidden(t *testing.T) {
112112
testDir(t, star, "testdata/.hidden",
113113
"fortune.txt", "more/") // but not .more or _more
114114
}
115+
116+
func TestUninitialized(t *testing.T) {
117+
var uninitialized embed.FS
118+
testDir(t, uninitialized, ".")
119+
f, err := uninitialized.Open(".")
120+
if err != nil {
121+
t.Fatal(err)
122+
}
123+
defer f.Close()
124+
fi, err := f.Stat()
125+
if err != nil {
126+
t.Fatal(err)
127+
}
128+
if !fi.IsDir() {
129+
t.Errorf("in uninitialized embed.FS, . is not a directory")
130+
}
131+
}

0 commit comments

Comments
 (0)