navidrome/navidrome · error

reading go.mod template: %w

Error message

reading go.mod template: %w

What it means

GenerateGoMod reads templates/go.mod.tmpl from the embedded templates FS (templatesFS). If that embedded read fails, the error is wrapped as 'reading go.mod template: %w'. Since the file is embedded at build time, a failure means the template is missing from the embed directive or the embed.FS was not populated — a build/packaging problem, not a runtime filesystem issue.

Source

Thrown at plugins/cmd/ndpgen/internal/generator.go:322

		Services []Service
	}{
		Package:  pkgName,
		Services: services,
	}

	var buf bytes.Buffer
	if err := tmpl.Execute(&buf, data); err != nil {
		return nil, fmt.Errorf("executing template: %w", err)
	}

	return buf.Bytes(), nil
}

// GenerateGoMod generates the go.mod file for the Go client library.
func GenerateGoMod() ([]byte, error) {
	tmplContent, err := templatesFS.ReadFile("templates/go.mod.tmpl")
	if err != nil {
		return nil, fmt.Errorf("reading go.mod template: %w", err)
	}
	return tmplContent, nil
}

// capabilityTemplateData holds data for capability template execution.
type capabilityTemplateData struct {
	Package    string
	Capability Capability
}

// capabilityFuncMap returns template functions for capability code generation.
func capabilityFuncMap(cap Capability) template.FuncMap {
	return template.FuncMap{
		"formatDoc":         formatDoc,
		"indent":            indentText,
		"agentName":         capabilityAgentName,
		"providerInterface": func(e Export) string { return e.ProviderInterfaceName() },
		"implVar":           func(e Export) string { return e.ImplVarName() },

View on GitHub (pinned to 4ed7494a32)

Solutions

  1. Verify templates/go.mod.tmpl exists in plugins/cmd/ndpgen/internal/templates and is covered by the //go:embed pattern.
  2. Run go build ./... and regenerate; embed.FS is validated at compile time, so build errors point at the missing file.
  3. Check .gitignore / release packaging isn't excluding *.tmpl files from the source used to build.
  4. Restore the file from git history if it was renamed or removed: git checkout -- plugins/cmd/ndpgen/internal/templates/go.mod.tmpl

Example fix

// before: embed pattern misses tmpl files
//go:embed templates/*.go
var templatesFS embed.FS
// after
//go:embed templates
var templatesFS embed.FS
Defensive patterns

Strategy: fallback

Validate before calling

// compile-time guard next to the embed directive
//go:embed templates
var templatesFS embed.FS
// runtime pre-check
if _, err := templatesFS.ReadFile("templates/go.mod.tmpl"); err != nil {
    panic("go.mod.tmpl missing from embedded templates: " + err.Error())
}

Try / catch

gomod, err := generator.GenerateGoMod()
if err != nil {
    if strings.Contains(err.Error(), "reading go.mod template") {
        log.Fatalf("embedded go.mod.tmpl missing — rebuild ndpgen from a complete checkout: %v", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling GenerateGoMod (via generateGoModFile) when templates/go.mod.tmpl does not exist under the embedded //go:embed templates path, or the embed pattern excludes it (e.g. .tmpl files ignored by build constraints or embed globs).

Common situations: Template file deleted or renamed (go.mod.tmpl -> go.mod.template) without updating //go:embed; building from a partial checkout/trimmed source archive; packaging tools (upx, custom builders) stripping embedded assets; wrong working copy committed to CI.

Related errors


AI-assisted analysis of navidrome/navidrome@4ed7494a32 (2026-09-01). Data as JSON: /api/errors/0bb8285c84c89883. Report an issue: GitHub.