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/astutil: use goimports import path to import name heuristic in UsesImport #30331

josharian opened this issue Feb 20, 2019 · 4 comments


Copy link

@josharian josharian commented Feb 20, 2019

goimports has a sophisticated heuristic to guess a package import name from its import path. We might even eventually enshrine that heuristic in the language (#29036).

astutil.UsesImport uses a much less sophisticated heuristic. We should move the goimports heuristic to somewhere exported (maybe in astutil?) and use it in astutil.UsesImport.

(astutil.UsesImport could also use some expanded docs, but that is another matter.)

cc @heschik

@gopherbot gopherbot added this to the Unreleased milestone Feb 20, 2019
Copy link

@heschi heschi commented Feb 20, 2019

Ian's been making changes to that heuristic under the assumption that it only affects goimports, where the worst case is that some user code churns. Dunno how safe it is to export it.


Copy link

@mvdan mvdan commented Feb 21, 2019

Perhaps we can export it once the heuristic has stabilized?

Copy link

@dmitshur dmitshur commented Feb 21, 2019

I think the original issue isn't about astutil.UsesImport primarily, but since it is mentioned, I want to provide the following information about it.

I have a local WIP commit that mentions astutil.UsesImport. I discovered a fundamental issue with it. Fixing the issue may only be possible by breaking the API. As a result, it may make more sense to deprecate the current UsesImport. I wasn't sure if many people use it, and whether it'd be worthwhile to deal with it, so the WIP commit left sitting behind.

I'll copy the commit message here, since it's relevant:

commit 16fa2d7c70732cd3ee2b4e007b4cda9e4ed52e6a
Author: Dmitri Shuralyov <>
Date:   Thu Nov 1 00:37:20 2018 -0400

    WIP: go/ast/astutil: try to fix UsesImport
    UsesImport is broken in that it will return true or false
    on the same input if you just reorder the imports.
    UsesImport is hard to fix properly without breaking its API.
    To make some sort of sense, it'd need to accept both import name
    and path. Not just path.
    So, before doing anything about it, see if it's even worth it.
    Maybe the best way forward is to deprecate it or remove it.
    Change-Id: If878f625fec48ae36d510a93b11285e0c6003fd6

 go/ast/astutil/imports.go      | 61 +++++++++++++++++++++++-------------------
 go/ast/astutil/imports_test.go | 12 +++++++++
 2 files changed, 46 insertions(+), 27 deletions(-)
Copy link

@ianthehat ianthehat commented Feb 22, 2019

The heuristic was deliberately extracted to the function importPathToAssumedName so that it could possibly be exposed one day.
Probably needs some more discussion about whether the heuristic is reasonable and complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants