-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gofumpt: Add lang-version option #2069
Conversation
Hey, thank you for opening your first Pull Request ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the code related to format.Options
must be moved outside the loop:
settings := lintCtx.Settings().Gofumpt
// Get the target Go language version to be used in gofumpt.
// Reuse logic from `https://github.com/mvdan/gofumpt/blob/ba6406b58f3630c5246474b20f6ee5c83d1cc6b9/gofmt.go#L127-L133`.
langVersion := settings.LangVersion
if langVersion == "" {
outGoVersion, errGoVersion := exec.Command("go", "list", "-m", "-f", "{{.GoVersion}}").Output()
outGoVersion = bytes.TrimSpace(outGoVersion)
if errGoVersion == nil && len(outGoVersion) > 0 {
langVersion = string(outGoVersion)
}
}
options := format.Options{
LangVersion: langVersion,
ExtraRules: settings.ExtraRules,
}
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}
var issues []goanalysis.Issue
for _, f := range fileNames {
input, err := ioutil.ReadFile(f)
if err != nil {
return nil, fmt.Errorf("unable to open file %s: %w", f, err)
}
output, err := format.Source(input, options)
if err != nil {
return nil, fmt.Errorf("error while running gofumpt: %w", err)
}
// ...
}
b167421
to
a9a08bb
Compare
Good catch! Updated, thanks! |
I think you can move it even further out, outside of |
@kailun-qin can you address changes as commit, because when you squash it's not possible to see changes.
@bombsimon it depends on the implementation to get the default Go version. |
a9a08bb
to
84991ec
Compare
Sorry that I didn't notice this before the latest commit. Will do for the followings.
Using a hardcoding default (to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@kailun-qin please don't squash, we will squash when we will merge. |
@ldez Sure, excuse me for not catching your comment before refreshing the last commit. I'll not squash for the following ones. |
Without the Go language version input, `gofumpt` cannot apply some rules which are version dependent. Defaulting `lang-version` to the Go version of the running environment if not specified. Signed-off-by: Kailun Qin <kailun.qin@intel.com>
Updated, thanks! |
Hey, @kailun-qin — we just merged your PR to
By joining the team, you’ll be able to label issues, review pull requests, and merge approved pull requests. Thanks again! |
Thanks for the hint! I'll use that way for the future suggested changes. |
Signed-off-by: Kailun Qin <kailun.qin@intel.com>
Without the Go language version input,
gofumpt
cannot apply some ruleswhich are version dependent.
Defaulting
lang-version
to the Go version of the running environmentif not specified.
Signed-off-by: Kailun Qin kailun.qin@intel.com