{"record":{"id":"821d5cbcc3f61bc4","repo":"golangci/golangci-lint","slug":"format-source-w","errorCode":null,"errorMessage":"format source: %w","messagePattern":"format source: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/commands/internal/imports.go","lineNumber":65,"sourceCode":"\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"parse template: %w\", err)\n\t}\n\n\tvar imps []string\n\tfor _, plugin := range cfg.Plugins {\n\t\timps = append(imps, plugin.Import)\n\t}\n\n\tbuf := &bytes.Buffer{}\n\n\terr = impTmpl.Execute(buf, map[string]any{\"Imports\": imps})\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"execute template: %w\", err)\n\t}\n\n\tsource, err := format.Source(buf.Bytes())\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"format source: %w\", err)\n\t}\n\n\treturn source, nil\n}\n","sourceCodeStart":47,"sourceCodeEnd":70,"githubUrl":"https://github.com/golangci/golangci-lint/blob/ed7a235d2d771152056fdc142a9855567c5796a9/pkg/commands/internal/imports.go#L47-L70","documentation":"generateImports renders a synthetic Go source file (package main with blank imports of each plugin) and runs it through go/format.Source. The 'format source: %w' wrap means the rendered bytes are not valid Go syntax. This happens when a plugin's Import string is empty or malformed, producing an invalid import line like `_ \"\"`.","triggerScenarios":"updatePluginsFile is called with a Configuration whose Plugins list contains an entry with an empty or syntactically invalid Import path, so the generated `import ( _ \"...\" )` block fails gofmt parsing.","commonSituations":"A misconfigured plugin entry in the builder configuration (missing import path, typo like a space or invalid characters in the module path), or an empty plugin struct appended to cfg.Plugins.","solutions":["Read the wrapped go/format error: it reports the line/column offset in the generated source; run the builder with debug logging to see the generated source (updatePluginsFile logs it after successful generation)","Check the configuration's plugins entries for empty or invalid Import values and correct them","Run gofmt on the failing snippet locally to confirm the syntax problem","Add validation that skips/errors on plugins with empty Import before rendering"],"exampleFix":"// before (bad config)\nplugins:\n  - module: golangci-lint-plugin-example\n    # import missing -> renders `_ \"\"`\n// after\nplugins:\n  - module: golangci-lint-plugin-example\n    import: github.com/org/golangci-lint-plugin-example/pkg/plugin","handlingStrategy":"validation","validationCode":"// validate plugin import paths before calling generateImports\nfor _, p := range cfg.Plugins {\n    if p.Import == \"\" {\n        return fmt.Errorf(\"plugin %q has empty import path\", p.Module)\n    }\n    if !importPathRe.MatchString(p.Import) {\n        return fmt.Errorf(\"plugin import %q is not a valid module path\", p.Import)\n    }\n}\nvar importPathRe = regexp.MustCompile(`^[a-zA-Z0-9._~/-]+$`)","typeGuard":null,"tryCatchPattern":"source, err := generateImports(cfg)\nif err != nil {\n    if strings.Contains(err.Error(), \"format source\") {\n        // log cfg.Plugins so the offending import path is visible\n    }\n    return err\n}","preventionTips":["Always set the Import field for every plugin in the builder configuration","Run gofmt mentally: every rendered import line must be `_ \"<valid-path>\"`","Add a unit test with an empty plugin entry asserting a clear validation error","Keep module paths free of spaces/invalid characters"],"tags":["go","gofmt","code-generation","configuration"],"backgroundTag":"invalid-go-source-generated","analyzedSha":"ed7a235d2d771152056fdc142a9855567c5796a9","analyzedAt":"2026-09-02T18:47:33.865Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}