{"record":{"id":"d55720f2ab843238","repo":"dagger/dagger","slug":"failed-to-format-go-mod-w","errorCode":null,"errorMessage":"failed to format go.mod: %w","messagePattern":"failed to format go\\.mod: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/codegen/generator/go/generate_client.go","lineNumber":42,"sourceCode":"\tmfs := memfs.New()\n\tlayers := []fs.FS{mfs}\n\n\tgoModFile, exist, err := g.readGoMod()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to read go.mod: %w\", err)\n\t}\n\n\tif !exist {\n\t\t// Only create a root go.mod for legacy/standalone client mode (ClientDir == \".\")\n\t\t// For separate client modules, we don't create a parent go.mod - just the client's\n\t\tif g.Config.ClientConfig.ClientDir == \".\" {\n\t\t\tgoMod := new(modfile.File)\n\t\t\tgoMod.AddModuleStmt(strings.ToLower(g.Config.ClientConfig.ModuleName))\n\t\t\tgoMod.AddGoStmt(goVersion)\n\n\t\t\tmodBody, err := goMod.Format()\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"failed to format go.mod: %w\", err)\n\t\t\t}\n\n\t\t\tif err := mfs.WriteFile(\"go.mod\", modBody, 0600); err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"failed to write go.mod: %w\", err)\n\t\t\t}\n\n\t\t\treturn &generator.GeneratedState{\n\t\t\t\tOverlay:        layerfs.New(mfs),\n\t\t\t\tNeedRegenerate: true,\n\t\t\t}, nil\n\t\t}\n\t\t// For separate client modules without a parent go.mod, continue with nil goModFile\n\t}\n\n\tpackageImport := filepath.Join(\n\t\tstrings.ToLower(g.Config.ClientConfig.ModuleName),\n\t\tg.Config.ClientConfig.ClientDir,\n\t)","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/dagger/dagger/blob/82ba2681dbe30d3547a1dc50ea495900ab5b6047/cmd/codegen/generator/go/generate_client.go#L24-L60","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)"],"exampleFix":"// before (dagger.json)\n{\"name\": \"My Module\"}\n// after\n{\"name\": \"mymodule\"}","handlingStrategy":"validation","validationCode":"name := cfg.Name\nif name == \"\" || strings.ContainsAny(name, \" \\t\") {\n    return fmt.Errorf(\"invalid module name %q\", name)\n}","typeGuard":null,"tryCatchPattern":"state, err := gen.GenerateClient(ctx, schema)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to format go.mod\") {\n        // fix module name in dagger.json then retry\n    }\n    return err\n}","preventionTips":["Use lowercase, hyphen/slash-free valid Go module paths for ModuleName","Never leave module name empty in dagger.json"],"tags":["go","codegen","go-mod","module-name"],"backgroundTag":"go-mod-format-failed","analyzedSha":"82ba2681dbe30d3547a1dc50ea495900ab5b6047","analyzedAt":"2026-09-05T07:21:37.930Z","contentChangedAt":"2026-09-05T07:21:37.930Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}