aboutsummaryrefslogtreecommitdiff
path: root/src/embed
diff options
context:
space:
mode:
authorJay Conrod <jayconrod@google.com>2021-01-14 13:04:17 -0500
committerJay Conrod <jayconrod@google.com>2021-01-19 16:31:09 +0000
commitca5774a5a533ce26ed64010fcc98f258e5bb0cc1 (patch)
treefa9cfa43a421d801a7424504bdd83b2a8c9cf8fb /src/embed
parentd047c91a6c0f22af00d1c1e770a9d85201392656 (diff)
downloadgo-ca5774a5a533ce26ed64010fcc98f258e5bb0cc1.tar.gz
go-ca5774a5a533ce26ed64010fcc98f258e5bb0cc1.zip
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 <jayconrod@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Jay Conrod <jayconrod@google.com> Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src/embed')
-rw-r--r--src/embed/embed.go6
-rw-r--r--src/embed/internal/embedtest/embed_test.go17
2 files changed, 23 insertions, 0 deletions
diff --git a/src/embed/embed.go b/src/embed/embed.go
index 29e0adf1a6..cc6855e6a5 100644
--- a/src/embed/embed.go
+++ b/src/embed/embed.go
@@ -244,6 +244,9 @@ func (f FS) lookup(name string) *file {
if name == "." {
return dotFile
}
+ if f.files == nil {
+ return nil
+ }
// Binary search to find where name would be in the list,
// and then check if name is at that position.
@@ -261,6 +264,9 @@ func (f FS) lookup(name string) *file {
// readDir returns the list of files corresponding to the directory dir.
func (f FS) readDir(dir string) []file {
+ if f.files == nil {
+ return nil
+ }
// Binary search to find where dir starts and ends in the list
// and then return that slice of the list.
files := *f.files
diff --git a/src/embed/internal/embedtest/embed_test.go b/src/embed/internal/embedtest/embed_test.go
index 40f65ffc3f..43ae5c7e05 100644
--- a/src/embed/internal/embedtest/embed_test.go
+++ b/src/embed/internal/embedtest/embed_test.go
@@ -112,3 +112,20 @@ func TestHidden(t *testing.T) {
testDir(t, star, "testdata/.hidden",
"fortune.txt", "more/") // but not .more or _more
}
+
+func TestUninitialized(t *testing.T) {
+ var uninitialized embed.FS
+ testDir(t, uninitialized, ".")
+ f, err := uninitialized.Open(".")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer f.Close()
+ fi, err := f.Stat()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !fi.IsDir() {
+ t.Errorf("in uninitialized embed.FS, . is not a directory")
+ }
+}