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

  1. Check the module name configured for the module — it must be a valid Go module path
  2. Remove invalid characters (spaces, symbols) from ModuleName in dagger.json
  3. Ensure ModuleName is non-empty before running codegen
  4. 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

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


AI-assisted analysis of dagger/dagger@82ba2681db (2026-09-05). Data as JSON: /api/errors/d55720f2ab843238. Report an issue: GitHub.