{"record":{"id":"0aacfcedf2dbaecb","repo":"golangci/golangci-lint","slug":"formatting-s-w","errorCode":null,"errorMessage":"formatting %s: %w","messagePattern":"formatting (.+?): %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/commands/internal/migrate/cloner/cloner.go","lineNumber":185,"sourceCode":"}\n\nfunc writeNewFile(fset *token.FileSet, file *ast.File, srcPath, dstDir string) error {\n\tvar buf bytes.Buffer\n\n\tbuf.WriteString(\"// Code generated by pkg/commands/internal/migrate/cloner/cloner.go. DO NOT EDIT.\\n\\n\")\n\n\terr := printer.Fprint(&buf, fset, file)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"printing %s: %w\", srcPath, err)\n\t}\n\n\tdstPath := filepath.Join(dstDir, filepath.Base(srcPath))\n\n\t_ = os.MkdirAll(filepath.Dir(dstPath), os.ModePerm)\n\n\tformatted, err := imports.Process(dstPath, buf.Bytes(), nil)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"formatting %s: %w\", dstPath, err)\n\t}\n\n\t//nolint:gosec,mnd // The permission is right.\n\terr = os.WriteFile(dstPath, formatted, 0o644)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"writing file %s: %w\", dstPath, err)\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":167,"sourceCodeEnd":196,"githubUrl":"https://github.com/golangci/golangci-lint/blob/ed7a235d2d771152056fdc142a9855567c5796a9/pkg/commands/internal/migrate/cloner/cloner.go#L167-L196","documentation":"After printing the transformed AST, writeNewFile runs the result through golang.org/x/tools/imports.Process, which parses, formats, and fixes up import statements. The 'formatting %s: %w' wrap means the printed AST text is not valid Go or the imports fix-up failed. The most common cause: after processFile drops const/var declarations and rewrites the package name, referenced imports or identifiers disappear, leaving invalid or unused-import state that Process cannot resolve into compilable source.","triggerScenarios":"writeNewFile is called and imports.Process(dstPath, buf.Bytes(), nil) fails because the generated versiontwo package source does not parse — e.g. struct fields reference types that were removed with the const/var declarations, or the rewritten code contains references to deleted symbols.","commonSituations":"Adding new struct types to pkg/config whose fields use types defined in removed const/var blocks; renaming the package to versiontwo while code still references the old package-qualified identifiers; unresolvable imports in the copied file.","solutions":["Read the wrapped imports.Process error — it reports the parse error location in the generated file","Inspect the corresponding generated file under pkg/commands/internal/migrate/versiontwo to see the invalid source","Check whether the source struct fields reference types/values that processFile strips (const/var declarations) and adjust processStructFields/convertType to keep or convert them","Re-run the cloner after fixing pkg/config or the transformation rules"],"exampleFix":"// before (field type defined in a stripped const/var block)\ntype Config struct {\n    Mode config.Mode // type dropped by processFile -> unresolvable\n}\n// after: keep type declarations or convert the field\ntype Config struct {\n    Mode *string\n}","handlingStrategy":"validation","validationCode":"// pre-check: attempt imports.Process on a copy before writing to disk\nout, err := imports.Process(dstPath, buf.Bytes(), nil)\nif err != nil {\n    log.Printf(\"generated source for %s is invalid: %v\\n--- source ---\\n%s\", dstPath, err, buf.String())\n    return err\n}","typeGuard":null,"tryCatchPattern":"if err := writeNewFile(fset, file, srcPath, dstDir); err != nil {\n    if strings.Contains(err.Error(), \"formatting \") {\n        log.Printf(\"imports.Process failed for %s — inspect generated source under %s\", srcPath, dstDir)\n    }\n    return err\n}","preventionTips":["Ensure field types referenced in structs are not stripped by the const/var removal in processFile","Run the cloner end-to-end and build the generated versiontwo package (go build ./pkg/commands/internal/migrate/versiontwo) as a smoke test","Keep pkg/config imports resolvable under the new package name","Fix the underlying config source first; the cloner copies real code, not a fixer"],"tags":["go","goimports","formatting","code-generation"],"backgroundTag":"goimports-process-error","analyzedSha":"ed7a235d2d771152056fdc142a9855567c5796a9","analyzedAt":"2026-09-02T18:47:33.865Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}