From 33c474b9b3bd470670740f30c5131071ce906b22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 31 Oct 2019 11:13:14 +0100 Subject: [PATCH] hugofs: Fix crash in multilingual content fs Fixes #6463 --- hugofs/rootmapping_fs.go | 8 ++- hugofs/rootmapping_fs_test.go | 2 +- hugolib/language_content_dir_test.go | 87 ++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/hugofs/rootmapping_fs.go b/hugofs/rootmapping_fs.go index e5679e09b33..0df49cd093d 100644 --- a/hugofs/rootmapping_fs.go +++ b/hugofs/rootmapping_fs.go @@ -196,12 +196,12 @@ func (fs *RootMappingFs) doLstat(name string, allowMultiple bool) ([]FileMetaInf fis []FileMetaInfo dirs []FileMetaInfo b bool - fi os.FileInfo root RootMapping err error ) for _, root = range roots { + var fi os.FileInfo fi, b, err = fs.statRoot(root, name) if err != nil { if os.IsNotExist(err) { @@ -233,12 +233,16 @@ func (fs *RootMappingFs) doLstat(name string, allowMultiple bool) ([]FileMetaInf return fis, dirs, b, nil } + if len(fis) == 0 { + return nil, nil, false, os.ErrNotExist + } + // Open it in this composite filesystem. opener := func() (afero.File, error) { return fs.Open(name) } - return []FileMetaInfo{decorateFileInfo(fi, fs, opener, "", "", root.Meta)}, nil, b, nil + return []FileMetaInfo{decorateFileInfo(fis[0], fs, opener, "", "", root.Meta)}, nil, b, nil } diff --git a/hugofs/rootmapping_fs_test.go b/hugofs/rootmapping_fs_test.go index d2459167cd2..548224c12dd 100644 --- a/hugofs/rootmapping_fs_test.go +++ b/hugofs/rootmapping_fs_test.go @@ -209,7 +209,7 @@ func TestRootMappingFsMount(t *testing.T) { blog, err := rfs.Stat(filepath.FromSlash("content/blog")) c.Assert(err, qt.IsNil) blogm := blog.(FileMetaInfo).Meta() - c.Assert(blogm.Lang(), qt.Equals, "sv") // Last match + c.Assert(blogm.Lang(), qt.Equals, "no") // First match f, err := blogm.Open() c.Assert(err, qt.IsNil) diff --git a/hugolib/language_content_dir_test.go b/hugolib/language_content_dir_test.go index 5406422dd29..0d1033c1f8a 100644 --- a/hugolib/language_content_dir_test.go +++ b/hugolib/language_content_dir_test.go @@ -319,3 +319,90 @@ Content. c.Assert(nnHome.RelPermalink(), qt.Equals, "/nn/") } + +// https://github.com/gohugoio/hugo/issues/6463 +func TestLanguageRootSectionsMismatch(t *testing.T) { + t.Parallel() + + config := ` +baseURL: "https://example.org/" +languageCode: "en-us" +title: "My New Hugo Site" +theme: "mytheme" + +contentDir: "content/en" + +languages: + en: + weight: 1 + languageName: "English" + contentDir: content/en + es: + weight: 2 + languageName: "Español" + contentDir: content/es + fr: + weight: 4 + languageName: "Française" + contentDir: content/fr + + +` + createPage := func(title string) string { + return fmt.Sprintf(`--- +title: %q +--- + +`, title) + } + + b := newTestSitesBuilder(t) + b.WithConfigFile("yaml", config) + + b.WithSourceFile("themes/mytheme/layouts/index.html", `MYTHEME`) + b.WithTemplates("index.html", ` +Lang: {{ .Lang }} +{{ range .Site.RegularPages }} +Page: {{ .RelPermalink }}|{{ .Title -}} +{{ end }} + +`) + b.WithSourceFile("static/hello.txt", `hello`) + b.WithContent("en/_index.md", createPage("en home")) + b.WithContent("es/_index.md", createPage("es home")) + b.WithContent("fr/_index.md", createPage("fr home")) + + for i := 1; i < 3; i++ { + b.WithContent(fmt.Sprintf("en/event/page%d.md", i), createPage(fmt.Sprintf("ev-en%d", i))) + b.WithContent(fmt.Sprintf("es/event/page%d.md", i), createPage(fmt.Sprintf("ev-es%d", i))) + b.WithContent(fmt.Sprintf("fr/event/page%d.md", i), createPage(fmt.Sprintf("ev-fr%d", i))) + b.WithContent(fmt.Sprintf("en/blog/page%d.md", i), createPage(fmt.Sprintf("blog-en%d", i))) + b.WithContent(fmt.Sprintf("es/blog/page%d.md", i), createPage(fmt.Sprintf("blog-es%d", i))) + b.WithContent(fmt.Sprintf("fr/other/page%d.md", i), createPage(fmt.Sprintf("other-fr%d", i))) + } + + b.Build(BuildCfg{}) + + b.AssertFileContent("public/index.html", ` +Lang: en +Page: /blog/page1/|blog-en1 +Page: /blog/page2/|blog-en2 +Page: /event/page1/|ev-en1 +Page: /event/page2/|ev-en2 +`) + + b.AssertFileContent("public/es/index.html", ` +Lang: es +Page: /es/blog/page1/|blog-es1 +Page: /es/blog/page2/|blog-es2 +Page: /es/event/page1/|ev-es1 +Page: /es/event/page2/|ev-es2 +`) + b.AssertFileContent("public/fr/index.html", ` +Lang: fr +Page: /fr/event/page1/|ev-fr1 +Page: /fr/event/page2/|ev-fr2 +Page: /fr/other/page1/|other-fr1 +Page: /fr/other/page2/|other-fr2`) + +}