]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
modules: Include JSON error info from go mod download in error messages
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 21 Feb 2026 15:04:32 +0000 (16:04 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 21 Feb 2026 16:21:08 +0000 (17:21 +0100)
Fixes #14543

hugolib/hugo_modules_test.go
modules/client.go

index cc0bde639f487e858cf962db9537613ac7d280f8..450e4cbce88bd41daf232aa47a612f056cbd7b48 100644 (file)
@@ -267,6 +267,29 @@ Home: {{ .Title }}|{{ .Content }}|
        b.AssertFileContent("public/index.html", "Home: |<h1 id=\"hello-world\">Hello World</h1>\n|")
 }
 
+// https://github.com/gohugoio/hugo/issues/14543
+func TestModulePrivateRepo(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+baseURL = "https://example.com/"
+[module]
+[[module.imports]]
+path = "github.com/bep/this-is-a-non-existing-repo"
+version = "main"
+[[module.imports.mounts]]
+source = "content"
+target = "content"
+-- layouts/all.html --
+Title: {{ .Title }}|
+List: {{ .Title }}
+`
+
+       b, err := TestE(t, files, TestOptOsFs())
+       b.Assert(err, qt.ErrorMatches, `(?s).*mod download.*invalid version.*repository.*`)
+}
+
 // https://github.com/gohugoio/hugo/issues/6299
 func TestSiteWithGoModButNoModules(t *testing.T) {
        t.Parallel()
index dbad4d35cb8bdcf04da11ceb7ba3ea688413be86..968d10500145a2cda3f0689037ccd14e0e845366 100644 (file)
@@ -458,6 +458,11 @@ func (c *Client) downloadModuleVersion(path, version string) (*goModule, error)
        b := &bytes.Buffer{}
        err := c.runGo(context.Background(), b, args...)
        if err != nil {
+               // The -json flag makes Go output error details as JSON to stdout
+               // even on failure. Try to extract the error message from the JSON output.
+               if jsonErr := extractGoModDownloadError(b.Bytes()); jsonErr != "" {
+                       return nil, fmt.Errorf("failed to download module %s@%s: %s: %s", path, version, err, jsonErr)
+               }
                return nil, fmt.Errorf("failed to download module %s@%s: %w", path, version, err)
        }
 
@@ -965,6 +970,18 @@ type goModuleError struct {
        Err string // the error itself
 }
 
+func extractGoModDownloadError(b []byte) string {
+       // go mod download -json outputs Error as a plain string,
+       // unlike go list -m -json which uses {"Err": "..."}.
+       var m struct {
+               Error string
+       }
+       if err := json.Unmarshal(b, &m); err == nil && m.Error != "" {
+               return m.Error
+       }
+       return ""
+}
+
 type goModules []*goModule
 
 type modSum struct {