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

x/tools/internal/imports: trailing comment with double-quote breaks imports grouping/sorting #51671

Closed
jhump opened this issue Mar 14, 2022 · 4 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@jhump
Copy link

jhump commented Mar 14, 2022

A regexp is used to extract the import path from a line of code when inserting newlines to split a sequence of imports into groups. The regexp uses greedy matching, which means if there is an errant quote after the import path (such as when a trailing comment includes a quote), the regexp fails to find the line. By failing to find the line, it never inserts any newlines and thus fails to actually group and sort imports.

What version of Go are you using (go version)?

I'm using Go 1.16 and Go 1.18rc1. The issue is in the golang.org/x/tools module and happens with any version of Go.

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

I've tried this using amd64 and arm64 on windows, darwin, and linux. The issue is not platform-specific.

What did you do?

Here's an example input that tickles the problem:

// test.go
package boo

import (
	"context"
	baz "foo.bar/baz"
	buzz "foo.bar/buzz"
	fizz "foo.bar/fizz"
	bar "[github.com/foo/bar](http://github.com/foo/bar)" // this is a "typical" import
)

var (
	_ = context.Background()
	_ = fizz.F{}
	_ = buzz.B{}
	_ = bar.B{}
	_ = baz.B{}
)

We invoke goimports with the above input:

goimports -local foo.bar/ test.go

What did you expect to see?

Expected imports:

	"context"

	bar "github.com/foo/bar" // this is a "typical" import

	baz "foo.bar/baz"
	buzz "foo.bar/buzz"
	fizz "foo.bar/fizz"

What did you see instead?

Because the newlines are not inserted, to break up the imports into groups, the imports get re-sorted by the final "format" step, so
the actual imports end up like this instead:

	"context"
	baz "foo.bar/baz"
	buzz "foo.bar/buzz"
	fizz "foo.bar/fizz"
	bar "github.com/foo/bar" // this is a "typical" import

Upon stepping through the code, it is observed that at this line, the regexp match looks like github.com/foo/bar" // this is a "typical, instead of just github.com/foo/bar.

This is easily fixed by changing the relevant regexp to use a non-greedy match, so its matched text ends at the first observed double-quote instead of the very last one. See CL 386914 for the fix.

@ianlancetaylor
Copy link
Member

You don't have to open an issue if you've already sent a fix.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Mar 14, 2022
@gopherbot gopherbot added this to the Unreleased milestone Mar 14, 2022
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/386914 mentions this issue: internal/imports: use first quote when matching import path

@dmitshur dmitshur added the NeedsFix The path to resolution is known, but the work has not been done. label Mar 14, 2022
@jhump
Copy link
Author

jhump commented Mar 14, 2022

@ianlancetaylor, sorry if this is abnormal. Creating a new issue and linking to it was recommended by @dmitshur in a comment in CL 386914.

@ianlancetaylor
Copy link
Member

Ah, I see, to capture the information in the description. OK, sorry.

rinchsan pushed a commit to rinchsan/gosimports that referenced this issue Aug 14, 2022
This fixes the regexp used to extract the import path from a line of code
when inserting newlines to split a sequence of imports into groups.

By using a non-greedy match, the regexp now functions correctly in the
face of an extra quote after the import path (such as when there is a
trailing comment that includes a quote).

Fixes golang/go#51671

Change-Id: Id7fd0b1d794f989d8f3d47336c5b5454cddd6237
GitHub-Last-Rev: b934371fa6d9ded902deac2268658b4485d9ce70
GitHub-Pull-Request: golang/tools#365
Reviewed-on: https://go-review.googlesource.com/c/tools/+/386914
Reviewed-by: Heschi Kreinick <[email protected]>
Trust: Dmitri Shuralyov <[email protected]>
Run-TryBot: Ian Lance Taylor <[email protected]>
gopls-CI: kokoro <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
@golang golang locked and limited conversation to collaborators Mar 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants