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

Improve: use list command to resolve relative import path #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

leventeliu
Copy link

@leventeliu leventeliu commented Jun 15, 2020

Why this change?

With a very simple repository for demonstration:

git clone https://github.com/leventeliu/godepgraph-demo.git
cd godepgraph-demo

Initially, when I was trying to get dep-graph inside a go module (given filter -o <prefix>) with a relative import path syntax, godepgraph gave an empty result:

$ godepgraph -o github.com/leventeliu ./cmd/nop
digraph godep {
splines=ortho
nodesep=0.4
ranksep=0.8
node [shape="box",style="rounded,filled"]
edge [arrowsize="0.5"]
}

After some code reading, I found the correct way to do it cloud be:

$ godepgraph -o .,github.com/leventeliu ./cmd/nop
digraph godep {
splines=ortho
nodesep=0.4
ranksep=0.8
node [shape="box",style="rounded,filled"]
edge [arrowsize="0.5"]
"./cmd/nop" [label="./cmd/nop" color="paleturquoise" URL="https://godoc.org/./cmd/nop" target="_blank"];
"./cmd/nop" -> "github.com/leventeliu/godepgraph-demo/a";
"./cmd/nop" -> "github.com/leventeliu/godepgraph-demo/b";
"github.com/leventeliu/godepgraph-demo/a" [label="github.com/leventeliu/godepgraph-demo/a" color="paleturquoise" URL="https://godoc.org/github.com/leventeliu/godepgraph-demo/a" target="_blank"];
"github.com/leventeliu/godepgraph-demo/b" [label="github.com/leventeliu/godepgraph-demo/b" color="paleturquoise" URL="https://godoc.org/github.com/leventeliu/godepgraph-demo/b" target="_blank"];
}

But it's weird, isn't it? The problem here is that the relative path ./cmd/nop could (and should) be resolved as a full package path github.com/leventeliu/godepgraph-demo/cmd/nop, which will not be ignored by the filter -o github.com/leventeliu.

And after I tried the list command:

$ go list ./cmd/nop
github.com/leventeliu/godepgraph-demo/cmd/nop

I thought this is the proper way to solve the problem.

List command is implemented by an internal package, which will be tricky to call directly. Alternatively, I use an os/exec.Command to call go list and pass the parent environment to the command (any better idea?)

Now it works well:

$ godepgraph -o github.com/leventeliu ./cmd/nop
digraph godep {
splines=ortho
nodesep=0.4
ranksep=0.8
node [shape="box",style="rounded,filled"]
edge [arrowsize="0.5"]
"github.com/leventeliu/godepgraph-demo/a" [label="github.com/leventeliu/godepgraph-demo/a" color="paleturquoise" URL="https://godoc.org/github.com/leventeliu/godepgraph-demo/a" target="_blank"];
"github.com/leventeliu/godepgraph-demo/b" [label="github.com/leventeliu/godepgraph-demo/b" color="paleturquoise" URL="https://godoc.org/github.com/leventeliu/godepgraph-demo/b" target="_blank"];
"github.com/leventeliu/godepgraph-demo/cmd/nop" [label="github.com/leventeliu/godepgraph-demo/cmd/nop" color="paleturquoise" URL="https://godoc.org/github.com/leventeliu/godepgraph-demo/cmd/nop" target="_blank"];
"github.com/leventeliu/godepgraph-demo/cmd/nop" -> "github.com/leventeliu/godepgraph-demo/a";
"github.com/leventeliu/godepgraph-demo/cmd/nop" -> "github.com/leventeliu/godepgraph-demo/b";
}

This improvement applies to the case when you are working with "GOPATH mode", instead of go module, too.

Also, this implies a special pattern /... will be supported:

$ godepgraph -o github.com/leventeliu ./...
digraph godep {
splines=ortho
nodesep=0.4
ranksep=0.8
node [shape="box",style="rounded,filled"]
edge [arrowsize="0.5"]
"github.com/leventeliu/godepgraph-demo/a" [label="github.com/leventeliu/godepgraph-demo/a" color="paleturquoise" URL="https://godoc.org/github.com/leventeliu/godepgraph-demo/a" target="_blank"];
"github.com/leventeliu/godepgraph-demo/b" [label="github.com/leventeliu/godepgraph-demo/b" color="paleturquoise" URL="https://godoc.org/github.com/leventeliu/godepgraph-demo/b" target="_blank"];
"github.com/leventeliu/godepgraph-demo/cmd/nop" [label="github.com/leventeliu/godepgraph-demo/cmd/nop" color="paleturquoise" URL="https://godoc.org/github.com/leventeliu/godepgraph-demo/cmd/nop" target="_blank"];
"github.com/leventeliu/godepgraph-demo/cmd/nop" -> "github.com/leventeliu/godepgraph-demo/a";
"github.com/leventeliu/godepgraph-demo/cmd/nop" -> "github.com/leventeliu/godepgraph-demo/b";
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant