Skip to content

Commit

Permalink
Fix regression when loading config -e is empty or HUGO_ENV or HUGO_EN…
Browse files Browse the repository at this point in the history
…VIRONMENT is set

Fixes #11013
  • Loading branch information
bep committed May 24, 2023
1 parent 5adc837 commit 231374a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 7 deletions.
18 changes: 16 additions & 2 deletions commands/hugobuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 0 additions & 3 deletions commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions testscripts/commands/hugo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
Expand Down
19 changes: 19 additions & 0 deletions testscripts/commands/hugo_configdev_env.txt
Original file line number Diff line number Diff line change
@@ -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"
---
22 changes: 22 additions & 0 deletions testscripts/commands/hugo_configdev_environment.txt
Original file line number Diff line number Diff line change
@@ -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"
---
18 changes: 18 additions & 0 deletions testscripts/commands/hugo_configprod.txt
Original file line number Diff line number Diff line change
@@ -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"
---

0 comments on commit 231374a

Please sign in to comment.