Skip to content
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

Merged
merged 2 commits into from
Jun 23, 2021
Merged

Conversation

kailun-qin
Copy link
Member

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

@boring-cyborg
Copy link

boring-cyborg bot commented Jun 21, 2021

Hey, thank you for opening your first Pull Request !

@CLAassistant
Copy link

CLAassistant commented Jun 21, 2021

CLA assistant check
All committers have signed the CLA.

@ldez ldez self-requested a review June 21, 2021 12:13
@ldez ldez added the enhancement New feature or improvement label Jun 21, 2021
@ldez ldez changed the title gofumpt: Add lang-version option + update linter gofumpt: Add lang-version option Jun 21, 2021
Copy link
Member

@ldez ldez left a 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)
				}
				// ...
			}

pkg/golinters/gofumpt.go Outdated Show resolved Hide resolved
@kailun-qin
Copy link
Member Author

kailun-qin commented Jun 22, 2021

Good catch! Updated, thanks!

@kailun-qin kailun-qin requested a review from ldez June 22, 2021 02:23
pkg/golinters/gofumpt.go Outdated Show resolved Hide resolved
@bombsimon
Copy link
Member

Good catch! Updated, thanks!

I think you can move it even further out, outside of analyzer.Run, no? I don't see why we need to figure out version and set options for every pass so doing it once should be enough, right @ldez?

@ldez
Copy link
Member

ldez commented Jun 22, 2021

@kailun-qin can you address changes as commit, because when you squash it's not possible to see changes.

I think you can move it even further out, outside of analyzer.Run, no?

@bombsimon it depends on the implementation to get the default Go version.

@kailun-qin
Copy link
Member Author

kailun-qin commented Jun 22, 2021

@kailun-qin can you address changes as commit, because when you squash it's not possible to see changes.

Sorry that I didn't notice this before the latest commit. Will do for the followings.

I think you can move it even further out, outside of analyzer.Run, no?

@bombsimon it depends on the implementation to get the default Go version.

Using a hardcoding default (to 1.15) to remove the dependency, and IMO this can be moved outside of analyzer.Run. PTAL @bombsimon @ldez, thanks!

Copy link
Member

@bombsimon bombsimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@ldez
Copy link
Member

ldez commented Jun 22, 2021

@kailun-qin please don't squash, we will squash when we will merge.

@kailun-qin
Copy link
Member Author

@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.
Please kindly take a look at the latest version and see if any comment. Many thanks!

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>
pkg/golinters/gofumpt.go Outdated Show resolved Hide resolved
pkg/golinters/gofumpt.go Outdated Show resolved Hide resolved
.golangci.example.yml Outdated Show resolved Hide resolved
@kailun-qin
Copy link
Member Author

Updated, thanks!

@kailun-qin kailun-qin requested a review from ldez June 23, 2021 01:59
@ldez ldez merged commit 678ae9f into golangci:master Jun 23, 2021
@golangci-automator
Copy link

Hey, @kailun-qin — we just merged your PR to golangci-lint! 🔥🚀

golangci-lint is built by awesome people like you. Let us say “thanks”: we just invited you to join the GolangCI organization on GitHub.
This will add you to our team of maintainers. Accept the invite by visiting this link.

By joining the team, you’ll be able to label issues, review pull requests, and merge approved pull requests.
More information about contributing is here.

Thanks again!

@kailun-qin
Copy link
Member Author

@kailun-qin kailun-qin deleted the add-lang-version branch June 23, 2021 04:07
Signed-off-by: Kailun Qin <kailun.qin@intel.com>
SeigeC pushed a commit to SeigeC/golangci-lint that referenced this pull request Apr 4, 2023
@ldez ldez added this to the v1.42 milestone Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants