Repository: gohugoio/hugo (language: go)

Issue:
Hugo can not find resource cache when setting a path in baseURL
<!-- Please answer these questions before submitting your issue. Thanks! -->

### What version of Hugo are you using (`hugo version`)?

<pre>
$ hugo version
hugo v0.97.0+extended darwin/arm64 BuildDate=unknown
</pre>

### Does this issue reproduce with the latest release?

This is an issue for any theme which has a resource cache, especially those generated by PostCSS. 

To reproduce it, you can use https://github.com/wangchucheng/hugo-eureka-starters as an example site and set `baseURL = example.com/test`. To force Hugo to look for cache instead of regenerating it, I set `useResourceCacheWhen = always`. The theme works well without `baseURL` or without resource cache.

There will be an error message like: `Error: Error building site: POSTCSS: failed to transform "css/eureka.css" (text/css): resource "css/test/css/eureka.css_5423b02c25971c3328ec4f49ccfb7444" not found in file cache` when running `hugo` or `hugo server`.

Hugo tends to find resource cache in `css/<base-path>/...`. However, the resource cache was in `css/...`. It is not possible for theme creators to know what path a user will use, so it looks like this may be a bug.


Discussion / hints:
I am having this problem all of a sudden too.

I can build locally, but I get a failed file cache error when I push to github for the action.

Fix patch:
diff --git a/resources/resource.go b/resources/resource.go
index e38090dbe73..5773977a26b 100644
--- a/resources/resource.go
+++ b/resources/resource.go
@@ -20,6 +20,7 @@ import (
 	"os"
 	"path"
 	"path/filepath"
+	"strings"
 	"sync"
 
 	"github.com/gohugoio/hugo/resources/internal"
@@ -267,7 +268,10 @@ func (l *genericResource) Data() any {
 }
 
 func (l *genericResource) Key() string {
-	return l.RelPermalink()
+	if l.spec.BasePath == "" {
+		return l.RelPermalink()
+	}
+	return strings.TrimPrefix(l.RelPermalink(), l.spec.BasePath)
 }
 
 func (l *genericResource) MediaType() media.Type {


Test patch:
diff --git a/hugolib/integrationtest_builder.go b/hugolib/integrationtest_builder.go
index 0ec202f89b4..df51b37c32d 100644
--- a/hugolib/integrationtest_builder.go
+++ b/hugolib/integrationtest_builder.go
@@ -41,13 +41,15 @@ func NewIntegrationTestBuilder(conf IntegrationTestConfig) *IntegrationTestBuild
 	}
 
 	if conf.NeedsOsFS {
-		tempDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test")
-		c.Assert(err, qt.IsNil)
-		conf.WorkingDir = filepath.Join(tempDir, conf.WorkingDir)
-		if !conf.PrintAndKeepTempDir {
-			c.Cleanup(clean)
-		} else {
-			fmt.Println("\nUsing WorkingDir dir:", conf.WorkingDir)
+		if !filepath.IsAbs(conf.WorkingDir) {
+			tempDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test")
+			c.Assert(err, qt.IsNil)
+			conf.WorkingDir = filepath.Join(tempDir, conf.WorkingDir)
+			if !conf.PrintAndKeepTempDir {
+				c.Cleanup(clean)
+			} else {
+				fmt.Println("\nUsing WorkingDir dir:", conf.WorkingDir)
+			}
 		}
 	} else if conf.WorkingDir == "" {
 		conf.WorkingDir = helpers.FilePathSeparator
diff --git a/resources/resource_transformers/postcss/integration_test.go b/resources/resource_transformers/postcss/integration_test.go
index fdebcc52c37..ab48297e4da 100644
--- a/resources/resource_transformers/postcss/integration_test.go
+++ b/resources/resource_transformers/postcss/integration_test.go
@@ -23,6 +23,7 @@ import (
 
 	qt "github.com/frankban/quicktest"
 	"github.com/gohugoio/hugo/htesting"
+	"github.com/gohugoio/hugo/hugofs"
 	"github.com/gohugoio/hugo/hugolib"
 )
 
@@ -55,6 +56,9 @@ h1 {
 
 -- config.toml --
 disablekinds = ['taxonomy', 'term', 'page']
+baseURL = "https://example.com"
+[build]
+useResourceCacheWhen = 'never'
 -- content/p1.md --
 -- data/hugo.toml --
 slogan = "Hugo Rocks!"
@@ -99,25 +103,40 @@ func TestTransformPostCSS(t *testing.T) {
 	}
 
 	c := qt.New(t)
-
-	b := hugolib.NewIntegrationTestBuilder(
-		hugolib.IntegrationTestConfig{
-			T:               c,
-			NeedsOsFS:       true,
-			NeedsNpmInstall: true,
-			LogLevel:        jww.LevelInfo,
-			TxtarString:     postCSSIntegrationTestFiles,
-		}).Build()
-
-	b.AssertLogContains("Hugo Environment: production")
-	b.AssertLogContains(filepath.FromSlash(fmt.Sprintf("PostCSS Config File: %s/postcss.config.js", b.Cfg.WorkingDir)))
-	b.AssertLogContains(filepath.FromSlash(fmt.Sprintf("package.json: %s/package.json", b.Cfg.WorkingDir)))
-
-	b.AssertFileContent("public/index.html", `
-Styles RelPermalink: /css/styles.css
+	tempDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test")
+	c.Assert(err, qt.IsNil)
+	c.Cleanup(clean)
+
+	for _, s := range []string{"never", "always"} {
+
+		repl := strings.NewReplacer(
+			"https://example.com",
+			"https://example.com/foo",
+			"useResourceCacheWhen = 'never'",
+			fmt.Sprintf("useResourceCacheWhen = '%s'", s),
+		)
+
+		files := repl.Replace(postCSSIntegrationTestFiles)
+
+		fmt.Println("===>", s, files)
+
+		b := hugolib.NewIntegrationTestBuilder(
+			hugolib.IntegrationTestConfig{
+				T:               c,
+				NeedsOsFS:       true,
+				NeedsNpmInstall: true,
+				LogLevel:        jww.LevelInfo,
+				WorkingDir:      tempDir,
+				TxtarString:     files,
+			}).Build()
+
+		b.AssertFileContent("public/index.html", `
+Styles RelPermalink: /foo/css/styles.css
 Styles Content: Len: 770917|
 `)
 
+	}
+
 }
 
 // 9880
@@ -186,3 +205,40 @@ func TestTransformPostCSSImporSkipInlineImportsNotFound(t *testing.T) {
 	s.AssertFileContent("public/css/styles.css", `@import "components/doesnotexist.css";`)
 
 }
+
+// Issue 9787
+func TestTransformPostCSSResourceCacheWithPathInBaseURL(t *testing.T) {
+	if !htesting.IsCI() {
+		t.Skip("Skip long running test when running locally")
+	}
+
+	c := qt.New(t)
+	tempDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test")
+	c.Assert(err, qt.IsNil)
+	c.Cleanup(clean)
+
+	for i := 0; i < 2; i++ {
+		files := postCSSIntegrationTestFiles
+
+		if i == 1 {
+			files = strings.ReplaceAll(files, "https://example.com", "https://example.com/foo")
+			files = strings.ReplaceAll(files, "useResourceCacheWhen = 'never'", "	useResourceCacheWhen = 'always'")
+		}
+
+		b := hugolib.NewIntegrationTestBuilder(
+			hugolib.IntegrationTestConfig{
+				T:               c,
+				NeedsOsFS:       true,
+				NeedsNpmInstall: true,
+				LogLevel:        jww.LevelInfo,
+				TxtarString:     files,
+				WorkingDir:      tempDir,
+			}).Build()
+
+		b.AssertFileContent("public/index.html", `
+Styles Content: Len: 770917
+`)
+
+	}
+
+}