]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Fix regression when loading config -e is empty or HUGO_ENV or HUGO_ENVIRONMENT is set
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 24 May 2023 13:53:49 +0000 (15:53 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 24 May 2023 14:39:31 +0000 (16:39 +0200)
Fixes #11013

commands/hugobuilder.go
commands/server.go
testscripts/commands/hugo.txt
testscripts/commands/hugo_configdev_env.txt [new file with mode: 0644]
testscripts/commands/hugo_configdev_environment.txt [new file with mode: 0644]
testscripts/commands/hugo_configprod.txt [new file with mode: 0644]

index 20e800aca5d2aa47e64a4e98844b6cd3f3d2506e..fa194e00069fad42727c62a1714054f5cd91e85f 100644 (file)
@@ -990,9 +990,23 @@ func (c *hugoBuilder) loadConfig(cd *simplecobra.Commandeer, running bool) error
        cfg := config.New()
        cfg.Set("renderToDisk", (c.s == nil && !c.r.renderToMemory) || (c.s != nil && c.s.renderToDisk))
        watch := c.r.buildWatch || (c.s != nil && c.s.serverWatch)
-       if c.r.environment != "" {
-               cfg.Set("environment", c.r.environment)
+       if c.r.environment == "" {
+               // We need to set the environment as early as possible because we need it to load the correct config.
+               // Check if the user has set it in env.
+               if env := os.Getenv("HUGO_ENVIRONMENT"); env != "" {
+                       c.r.environment = env
+               } else if env := os.Getenv("HUGO_ENV"); env != "" {
+                       c.r.environment = env
+               } else {
+                       if c.s != nil {
+                               // The server defaults to development.
+                               c.r.environment = hugo.EnvironmentDevelopment
+                       } else {
+                               c.r.environment = hugo.EnvironmentProduction
+                       }
+               }
        }
+       cfg.Set("environment", c.r.environment)
 
        cfg.Set("internal", maps.Params{
                "running": running,
index a288bb2c80604717b9e1d29c0fa2c572a014eecc..ad432037cc74457ca045c66393b81edf88a5f7f3 100644 (file)
@@ -546,9 +546,6 @@ func (c *serverCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
        c.doLiveReload = !c.disableLiveReload
        c.fastRenderMode = !c.disableFastRender
        c.showErrorInBrowser = c.doLiveReload && !c.disableBrowserError
-       if c.r.environment == "" {
-               c.r.environment = hugo.EnvironmentDevelopment
-       }
 
        if c.fastRenderMode {
                // For now, fast render mode only. It should, however, be fast enough
index 9907f525856b74c6905b7edb9145b8bcfc6b0fb7..60f7ffe71a2ab0cae94a4c665b292e2546fe3c6d 100644 (file)
@@ -5,13 +5,13 @@ stdout 'Pages.*|1'
 stdout 'Total in'
 checkfile public/index.html
 checkfile public/p1/index.html
-grep 'IsServer: false' public/index.html
+grep 'IsServer: false;IsProduction: true'  public/index.html
 
 -- hugo.toml --
 baseURL = "http://example.org/"
 disableKinds = ["RSS", "sitemap", "robotsTXT", "404", "taxonomy", "term"]
 -- layouts/index.html --
-Home|IsServer: {{ .Site.IsServer }}|
+Home|IsServer: {{ .Site.IsServer }};IsProduction: {{ hugo.IsProduction }}|
 -- layouts/_default/single.html --
 Title: {{ .Title }}
 -- content/p1.md --
diff --git a/testscripts/commands/hugo_configdev_env.txt b/testscripts/commands/hugo_configdev_env.txt
new file mode 100644 (file)
index 0000000..758f4fc
--- /dev/null
@@ -0,0 +1,19 @@
+# Test the hugo command.
+env HUGO_ENV=development
+
+hugo
+grep 'myparam: dev§'  public/index.html
+
+-- hugo.toml --
+baseURL = "http://example.org/"
+disableKinds = ["RSS", "sitemap", "robotsTXT", "404", "taxonomy", "term"]
+-- layouts/index.html --
+myparam: {{ site.Params.myparam }}§
+-- layouts/_default/single.html --
+Title: {{ .Title }}
+-- config/development/params.toml --
+myparam = "dev"
+-- content/p1.md --
+---
+title: "P1"
+---
diff --git a/testscripts/commands/hugo_configdev_environment.txt b/testscripts/commands/hugo_configdev_environment.txt
new file mode 100644 (file)
index 0000000..0371481
--- /dev/null
@@ -0,0 +1,22 @@
+# Test the hugo command.
+env HUGO_ENVIRONMENT=development
+
+hugo
+grep 'myparam: dev§'  public/index.html
+
+hugo -e production
+grep 'myparam: §'  public/index.html
+
+-- hugo.toml --
+baseURL = "http://example.org/"
+disableKinds = ["RSS", "sitemap", "robotsTXT", "404", "taxonomy", "term"]
+-- layouts/index.html --
+myparam: {{ site.Params.myparam }}§
+-- layouts/_default/single.html --
+Title: {{ .Title }}
+-- config/development/params.toml --
+myparam = "dev"
+-- content/p1.md --
+---
+title: "P1"
+---
diff --git a/testscripts/commands/hugo_configprod.txt b/testscripts/commands/hugo_configprod.txt
new file mode 100644 (file)
index 0000000..ac046b2
--- /dev/null
@@ -0,0 +1,18 @@
+# Test the hugo command.
+
+hugo
+grep 'myparam: §'  public/index.html
+
+-- hugo.toml --
+baseURL = "http://example.org/"
+disableKinds = ["RSS", "sitemap", "robotsTXT", "404", "taxonomy", "term"]
+-- layouts/index.html --
+myparam: {{ site.Params.myparam }}§
+-- layouts/_default/single.html --
+Title: {{ .Title }}
+-- config/development/params.toml --
+myparam = "dev"
+-- content/p1.md --
+---
+title: "P1"
+---