Skip to content

cmd/go/mod/why: failed to show shortest paths for indirect modules in the import graph #36428

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

Closed
aofei opened this issue Jan 7, 2020 · 3 comments

Comments

@aofei
Copy link
Contributor

aofei commented Jan 7, 2020

If I understand correctly, I think go mod why -m <module-path> should always show a shortest path unless the target module doesn't exist in the import graph. But it sometimes shows unexpected (main module does not need ...), even if the target module definitely exists in the import graph (can be proven by go mod graph).

Try this (from #36423 (comment)):

$ git clone -q https://github.com/appleboy/gin-jwt.git && cd gin-jwt
$ go mod graph | grep 'github.com/belogik/goes'
github.com/astaxie/beego@v1.11.1 github.com/belogik/goes@v0.0.0-20151229125003-e54d722c3aff
$ go mod why -m github.com/belogik/goes
# github.com/belogik/goes
(main module does not need module github.com/belogik/goes)

I think the expected output of go mod why -m github.com/belogik/goes here should be:

# github.com/belogik/goes
github.com/appleboy/gin-jwt/v2
github.com/appleboy/gin-jwt/v2.test
github.com/appleboy/gofight/v2
github.com/appleboy/gofight/v2/example
github.com/astaxie/beego
github.com/astaxie/beego/logs/es
github.com/belogik/goes

With this result, we can clearly know exactly how a module enters our import graph. But now we have to know it by using some workarounds like go mod graph | grep '<module-path>', this's inconvenient.

/cc @bcmills, @jayconrod

@aofei
Copy link
Contributor Author

aofei commented Jan 7, 2020

@gopherbot, please add labels GoCommand, modules

@jayconrod
Copy link
Contributor

cc @matloob

go mod why -m doesn't actually show the shortest path through the module version graph. From go help mod why:

Why shows a shortest path in the import graph from the main module to each of the listed packages. If the -m flag is given, why treats the arguments as a list of modules and finds a path to any package in each of the modules.

So in this case go mod why -m reports that the main module does not need github.com/belogik/goes because no packages in that module are transitively imported by packages in the main module. github.com/belogik/goes is transitively required via go.mod files, so it shows up in the go mod graph output.

Closing because this is working as documented. However, I admit I don't personally find this behavior intuitive, and I think most people expect go mod why -m to operate on the module graph, not the package graph. Maybe we should consider a proposal to change that.

@bcmills
Copy link
Contributor

bcmills commented Jan 7, 2020

I think most people expect go mod why -m to operate on the module graph, not the package graph. Maybe we should consider a proposal to change that.

That is #27900, I think.

@golang golang locked and limited conversation to collaborators Jan 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants