Recently, while working on a PR for a project, I encountered an issue that caused our GitHub CI/CD checks to fail.
During the development, after submitting the PR, the CI/CD pipeline flagged an error, hindering the progress of my PR. The message displayed was:
“File is not goimports-ed with -local xxx.”
I ran make lint locally and faced the following error message:
“internal/http/handler/v1beta2/stream_test.go:3: File is not goimports-ed with -local neutron (goimports)”
This indicated an issue with the order of imports in my Go file, which did not meet the standards set by our project’s static code analysis tool, golangci-lint.
Understanding the Configuration
Upon investigating the .golangci.yml configuration file, I noticed that goimports linter was enabled, which was responsible for enforcing import order. Here’s the crucial part of the configuration:
run:
go: '1.20'
linters:
enable:
- gofmt
- goimports
disable:
- typecheck
linters-settings:
goimports:
local-prefixes: neutron
The local-prefixes directive was particularly important. It specifies that any imports starting with “neutron” are considered local and must be placed after third-party packages, adhering to the goimports rules.
The Fix To resolve the issue, I had two main options:
1.Manually Running goimports:
I executed the following command in the terminal:
goimports -w -local neutron $(find . -type f -name '*.go')
This command adjusts the import order in all Go files across the project, ensuring compliance with our golangci-lint settings.
2.Configuring IDE to Automatically Run goimports:
Alternatively, configuring the IDE to automatically apply goimports upon saving a file can prevent such issues from reoccurring. This ensures that every file is formatted correctly, reducing the chances of CI/CD failures related to code style.
