golangci/golangci-lint · error
format source: %w
Error message
format source: %w
What it means
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 `_ ""`.
Source
Thrown at pkg/commands/internal/imports.go:65
if err != nil {
return nil, fmt.Errorf("parse template: %w", err)
}
var imps []string
for _, plugin := range cfg.Plugins {
imps = append(imps, plugin.Import)
}
buf := &bytes.Buffer{}
err = impTmpl.Execute(buf, map[string]any{"Imports": imps})
if err != nil {
return nil, fmt.Errorf("execute template: %w", err)
}
source, err := format.Source(buf.Bytes())
if err != nil {
return nil, fmt.Errorf("format source: %w", err)
}
return source, nil
}
View on GitHub (pinned to ed7a235d2d)
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
Example fix
// before (bad config)
plugins:
- module: golangci-lint-plugin-example
# import missing -> renders `_ ""`
// after
plugins:
- module: golangci-lint-plugin-example
import: github.com/org/golangci-lint-plugin-example/pkg/plugin Defensive patterns
Strategy: validation
Validate before calling
// validate plugin import paths before calling generateImports
for _, p := range cfg.Plugins {
if p.Import == "" {
return fmt.Errorf("plugin %q has empty import path", p.Module)
}
if !importPathRe.MatchString(p.Import) {
return fmt.Errorf("plugin import %q is not a valid module path", p.Import)
}
}
var importPathRe = regexp.MustCompile(`^[a-zA-Z0-9._~/-]+$`) Try / catch
source, err := generateImports(cfg)
if err != nil {
if strings.Contains(err.Error(), "format source") {
// log cfg.Plugins so the offending import path is visible
}
return err
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
AI-assisted analysis of golangci/golangci-lint@ed7a235d2d (2026-09-02).
Data as JSON: /api/errors/821d5cbcc3f61bc4.
Report an issue: GitHub.