From 2b01c85d14102353015cf6860d30be3d92964495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 8 Nov 2021 11:50:51 +0100 Subject: [PATCH] Fix path resolution in hugo new With theme and project with content directories and command on the form `hugo new posts/test.md`. Fixes #9129 --- create/content_test.go | 2 +- hugolib/content_factory_test.go | 18 ++++++++++++++++++ hugolib/filesystems/basefs.go | 11 +++++++---- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/create/content_test.go b/create/content_test.go index 6f0ed8c86..2d68f7610 100644 --- a/create/content_test.go +++ b/create/content_test.go @@ -35,7 +35,7 @@ import ( ) // TODO(bep) clean this up. Export the test site builder in Hugolib or something. -func TestNewContent(t *testing.T) { +func TestNewContentFromFile(t *testing.T) { cases := []struct { name string kind string diff --git a/hugolib/content_factory_test.go b/hugolib/content_factory_test.go index 50cc783f6..dc3b4fc91 100644 --- a/hugolib/content_factory_test.go +++ b/hugolib/content_factory_test.go @@ -57,4 +57,22 @@ Hello World. b.Assert(buf.String(), qt.Contains, `title: "Mypage"`) }) + // Issue #9129 + c.Run("Content in both project and theme", func(c *qt.C) { + b := newTestSitesBuilder(c) + b.WithConfigFile("toml", ` +theme = 'ipsum' +`) + + themeDir := filepath.Join("themes", "ipsum") + b.WithSourceFile("content/posts/foo.txt", `Hello.`) + b.WithSourceFile(filepath.Join(themeDir, "content/posts/foo.txt"), `Hello.`) + b.CreateSites() + cf := NewContentFactory(b.H) + abs, err := cf.CreateContentPlaceHolder(filepath.FromSlash("posts/test.md")) + b.Assert(err, qt.IsNil) + b.Assert(abs, qt.Equals, filepath.FromSlash("content/posts/test.md")) + + }) + } diff --git a/hugolib/filesystems/basefs.go b/hugolib/filesystems/basefs.go index cfbd295ba..939d88459 100644 --- a/hugolib/filesystems/basefs.go +++ b/hugolib/filesystems/basefs.go @@ -157,11 +157,14 @@ func (b *BaseFs) AbsProjectContentDir(filename string) (string, string) { if !isAbs { // A filename on the form "posts/mypage.md", put it inside // the first content folder, usually /content. - // The Dirs are ordered with the most important last, so pick that. + // Pick the last project dir (which is probably the most important one). contentDirs := b.SourceFilesystems.Content.Dirs - firstContentDir := contentDirs[len(contentDirs)-1].Meta().Filename - return filename, filepath.Join(firstContentDir, filename) - + for i := len(contentDirs) - 1; i >= 0; i-- { + meta := contentDirs[i].Meta() + if meta.Module == "project" { + return filename, filepath.Join(meta.Filename, filename) + } + } } return "", ""