dagger/dagger · error
failed to format go.mod: %w
Error message
failed to format go.mod: %w
What it means
GenerateClient (cmd/codegen/generator/go/generate_client.go:42) fails with 'failed to format go.mod' when modfile.File.Format errors while generating a fresh go.mod for a standalone legacy client (ClientDir == '.' with no existing go.mod). The generated statement set is invalid for the modfile parser.
Source
Thrown at cmd/codegen/generator/go/generate_client.go:42
mfs := memfs.New()
layers := []fs.FS{mfs}
goModFile, exist, err := g.readGoMod()
if err != nil {
return nil, fmt.Errorf("failed to read go.mod: %w", err)
}
if !exist {
// Only create a root go.mod for legacy/standalone client mode (ClientDir == ".")
// For separate client modules, we don't create a parent go.mod - just the client's
if g.Config.ClientConfig.ClientDir == "." {
goMod := new(modfile.File)
goMod.AddModuleStmt(strings.ToLower(g.Config.ClientConfig.ModuleName))
goMod.AddGoStmt(goVersion)
modBody, err := goMod.Format()
if err != nil {
return nil, fmt.Errorf("failed to format go.mod: %w", err)
}
if err := mfs.WriteFile("go.mod", modBody, 0600); err != nil {
return nil, fmt.Errorf("failed to write go.mod: %w", err)
}
return &generator.GeneratedState{
Overlay: layerfs.New(mfs),
NeedRegenerate: true,
}, nil
}
// For separate client modules without a parent go.mod, continue with nil goModFile
}
packageImport := filepath.Join(
strings.ToLower(g.Config.ClientConfig.ModuleName),
g.Config.ClientConfig.ClientDir,
)View on GitHub (pinned to 82ba2681db)
Solutions
- Check the module name configured for the module — it must be a valid Go module path
- Remove invalid characters (spaces, symbols) from ModuleName in dagger.json
- Ensure ModuleName is non-empty before running codegen
- Update Dagger if you believe a valid module path triggers this (possible modfile library quirk)
Example fix
// before (dagger.json)
{"name": "My Module"}
// after
{"name": "mymodule"} Defensive patterns
Strategy: validation
Validate before calling
name := cfg.Name
if name == "" || strings.ContainsAny(name, " \t") {
return fmt.Errorf("invalid module name %q", name)
} Try / catch
state, err := gen.GenerateClient(ctx, schema)
if err != nil {
if strings.Contains(err.Error(), "failed to format go.mod") {
// fix module name in dagger.json then retry
}
return err
} Prevention
- Use lowercase, hyphen/slash-free valid Go module paths for ModuleName
- Never leave module name empty in dagger.json
When it happens
Trigger: g.readGoMod reports no existing go.mod, so a new modfile.File is built with AddModuleStmt(strings.ToLower(moduleName)) and AddGoStmt(goVersion); Format fails on these synthetic statements.
Common situations: ModuleName containing characters invalid in a Go module path (spaces, slashes misuse, uppercase kept in non-conforming form) that makes the synthetic modfile unformattable; an unexpected empty module name.
Related errors
- failed to read go.mod: %w
- failed to write go.mod: %w
- failed to find parent go.mod: %w
- failed to format client go.mod: %w
- failed to write client go.mod: %w
AI-assisted analysis of dagger/dagger@82ba2681db (2026-09-05).
Data as JSON: /api/errors/d55720f2ab843238.
Report an issue: GitHub.