x/tools/cmd/goimports: make "-local" flag match exactly path without final "/" in addition to prefix #24368
Labels
FeatureRequest
FrozenDueToAge
help wanted
NeedsFix
The path to resolution is known, but the work has not been done.
Milestone
Currently, it is not possible for
goimports
to group imports in a manner that encapsulates the operation "group all packages from a repository into the same group" using the-local
flag due to the nature of prefix matches.If a file imports
github.com/org/project
,github.com/org/project/pkg
andgithub.com/org/project2/pkg
,-local github.com/org/project
will match the undesired packagegithub.com/org/project2
, while-local github.com/org/project/
will not match imports for the root packagegithub.com/org/project
.Proposal
As a special case, if a prefix provided to
-local
ends in a slash (/
) and the import being considered is exactly the prefix without the slash, treat it as a match. Although this modifies the current behavior, I believe that it semantically captures what such a prefix match would be trying to achieve (the fix is also very small and targeted). I believe that any other fix would require adding flags or options, which I know is not desirable.Example repros
Groups
github.com/org/project2/pkg
with imports fromgithub.com/org/project
:Does not group import for
github.com/org/project
withgithub.com/org/project/pkg
:The proposed behavior would result in:
The text was updated successfully, but these errors were encountered: