Repository: gohugoio/hugo (language: go)

Issue:
The development config directory is read when running `hugo`
Problem exists in v0.112.0, v0.112.1, v0.112.2.

```text
config/
├── _default/
│   └── config.toml
└── development/
    └── module.toml  <-- BUG: this is read when running `hugo`
```
Minimal example:

```text
git clone --single-branch -b hugo-forum-topic-44506 https://github.com/jmooring/hugo-testing hugo-forum-topic-44506
cd hugo-forum-topic-44506
hugo
```

> Error: failed to load modules: module "foo" not found

Then run:

```text
hugo -e production
```

No problems.

Reference: <https://discourse.gohugo.io/t/44506/3>

---

This could expose sensitive/internal content.

config/development/module.toml

```text
[[mounts]]
source = 'content'
target = 'content'

[[mounts]]
source = 'hidden'
target = 'content'
```


Discussion / hints:

Fix patch:
diff --git a/commands/hugobuilder.go b/commands/hugobuilder.go
index 20e800aca5d..fa194e00069 100644
--- a/commands/hugobuilder.go
+++ b/commands/hugobuilder.go
@@ -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,
diff --git a/commands/server.go b/commands/server.go
index a288bb2c806..ad432037cc7 100644
--- a/commands/server.go
+++ b/commands/server.go
@@ -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


Test patch:
diff --git a/testscripts/commands/hugo.txt b/testscripts/commands/hugo.txt
index 9907f525856..60f7ffe71a2 100644
--- a/testscripts/commands/hugo.txt
+++ b/testscripts/commands/hugo.txt
@@ -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
index 00000000000..758f4fc9643
--- /dev/null
+++ b/testscripts/commands/hugo_configdev_env.txt
@@ -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
index 00000000000..037148178c9
--- /dev/null
+++ b/testscripts/commands/hugo_configdev_environment.txt
@@ -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
index 00000000000..ac046b20570
--- /dev/null
+++ b/testscripts/commands/hugo_configprod.txt
@@ -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"
+---