godepgraph icon indicating copy to clipboard operation
godepgraph copied to clipboard

Same packaged referenced from two paths isn't recognized

Open nathan-fiscaletti opened this issue 3 years ago • 5 comments

If you have a package in a directory named somepackage and import it from your main package using

import (
    "./somepackage"
)

and then you have another package named otherpackage and import somepackage in it using

import(
    "../somepackage"
)

It will be picked up as two separate packages, one being ./somepackage and the other being ../somepackage.

Potential Fix

For local packages like this, would it work better to resolve the absolute paths of relative packages. If two are the same, ommit one. Afterwards, you can update their names again by generating the relative path to the package from the main package. Does this make sense?

nathan-fiscaletti avatar Nov 13 '20 03:11 nathan-fiscaletti

yeah I think it makes sense to resolve everything to absolute package paths

kisielk avatar Nov 13 '20 22:11 kisielk

Actually I think #40 addresses that, sorry it's been a while since I looked. Do you mind giving that PR a try, and if it works for you, I can merge it.

kisielk avatar Nov 13 '20 22:11 kisielk

I'll give it a try when I'm back in the office on Monday.

nathan-fiscaletti avatar Nov 14 '20 17:11 nathan-fiscaletti

It seems like #40 only makes the situation worse as I can no longer even generate a graph for the project.

I've created this sample project to work along with: https://github.com/nathan-fiscaletti/godepgraph-example

If i use the regular tool, it works just fine (minus the issue of showing the same package twice with different paths)

~/go/bin/godepgraph -horizontal ./depgraphtest
digraph godep {
rankdir="LR"
splines=ortho
nodesep=0.4
ranksep=0.8
node [shape="box",style="rounded,filled"]
edge [arrowsize="0.5"]
"../somepackage" [label="../somepackage" color="paleturquoise" URL="https://godoc.org/../somepackage" target="_blank"];
"./depgraphtest" [label="./depgraphtest" color="paleturquoise" URL="https://godoc.org/./depgraphtest" target="_blank"];
"./depgraphtest" -> "./otherpackage";
"./depgraphtest" -> "./somepackage";
"./otherpackage" [label="./otherpackage" color="paleturquoise" URL="https://godoc.org/./otherpackage" target="_blank"];
"./otherpackage" -> "../somepackage";
"./otherpackage" -> "fmt";
"./somepackage" [label="./somepackage" color="paleturquoise" URL="https://godoc.org/./somepackage" target="_blank"];
"fmt" [label="fmt" color="palegreen" URL="https://godoc.org/fmt" target="_blank"];
}

However, if i use the version compiled from #40 i see this:

~/git-repos/personal/godepgraph/godepgraph -horizontal ./depgraphtest
2020/11/16 13:24:21 failed to import _/Users/nathanf/git-repos/personal/depgraphtest (imported at level 1 by ):
cannot find package "_/Users/nathanf/git-repos/personal/depgraphtest" in any of:
	/usr/local/Cellar/go/1.13.8/libexec/src/_/Users/nathanf/git-repos/personal/depgraphtest (from $GOROOT)
	/Users/nathanf/go/src/_/Users/nathanf/git-repos/personal/depgraphtest (from $GOPATH)

Additionally, If i try to use the github URL itself I see this:

~/git-repos/personal/godepgraph/godepgraph -horizontal github.com/nathan-fiscaletti/godepgraph-example
unexpected directory layout:
	import path: _/Users/nathanf/go/src/github.com/nathan-fiscaletti/godepgraph-example/otherpackage
	root: /Users/nathanf/go/src
	dir: /Users/nathanf/go/src/github.com/nathan-fiscaletti/godepgraph-example/otherpackage
	expand root: /Users/nathanf/go
	expand dir: /Users/nathanf/go/src/github.com/nathan-fiscaletti/godepgraph-example/otherpackage
	separator: /
2020/11/16 13:26:40 exit status 1

(although this may be related to using relative paths in a github repository, I'm not sure)

nathan-fiscaletti avatar Nov 16 '20 19:11 nathan-fiscaletti

It seems like #40 only makes the situation worse as I can no longer even generate a graph for the project.

I've created this sample project to work along with: https://github.com/nathan-fiscaletti/godepgraph-example

If i use the regular tool, it works just fine (minus the issue of showing the same package twice with different paths)

~/go/bin/godepgraph -horizontal ./depgraphtest
digraph godep {
rankdir="LR"
splines=ortho
nodesep=0.4
ranksep=0.8
node [shape="box",style="rounded,filled"]
edge [arrowsize="0.5"]
"../somepackage" [label="../somepackage" color="paleturquoise" URL="https://godoc.org/../somepackage" target="_blank"];
"./depgraphtest" [label="./depgraphtest" color="paleturquoise" URL="https://godoc.org/./depgraphtest" target="_blank"];
"./depgraphtest" -> "./otherpackage";
"./depgraphtest" -> "./somepackage";
"./otherpackage" [label="./otherpackage" color="paleturquoise" URL="https://godoc.org/./otherpackage" target="_blank"];
"./otherpackage" -> "../somepackage";
"./otherpackage" -> "fmt";
"./somepackage" [label="./somepackage" color="paleturquoise" URL="https://godoc.org/./somepackage" target="_blank"];
"fmt" [label="fmt" color="palegreen" URL="https://godoc.org/fmt" target="_blank"];
}

However, if i use the version compiled from #40 i see this:

~/git-repos/personal/godepgraph/godepgraph -horizontal ./depgraphtest
2020/11/16 13:24:21 failed to import _/Users/nathanf/git-repos/personal/depgraphtest (imported at level 1 by ):
cannot find package "_/Users/nathanf/git-repos/personal/depgraphtest" in any of:
	/usr/local/Cellar/go/1.13.8/libexec/src/_/Users/nathanf/git-repos/personal/depgraphtest (from $GOROOT)
	/Users/nathanf/go/src/_/Users/nathanf/git-repos/personal/depgraphtest (from $GOPATH)

Additionally, If i try to use the github URL itself I see this:

~/git-repos/personal/godepgraph/godepgraph -horizontal github.com/nathan-fiscaletti/godepgraph-example
unexpected directory layout:
	import path: _/Users/nathanf/go/src/github.com/nathan-fiscaletti/godepgraph-example/otherpackage
	root: /Users/nathanf/go/src
	dir: /Users/nathanf/go/src/github.com/nathan-fiscaletti/godepgraph-example/otherpackage
	expand root: /Users/nathanf/go
	expand dir: /Users/nathanf/go/src/github.com/nathan-fiscaletti/godepgraph-example/otherpackage
	separator: /
2020/11/16 13:26:40 exit status 1

(although this may be related to using relative paths in a github repository, I'm not sure)

Hi @kisielk and @nathan-fiscaletti , it's not the case that #40 is trying to resolve here. In your case, it's go list producing the error message for the relative path import, and you can see the same thing when you try this:

~/go/src/github.com/nathan-fiscaletti/godepgraph-example$ GO111MODULE=off go list github.com/nathan-fiscaletti/godepgraph-example                                   
unexpected directory layout:
        import path: _/home/levente/go/src/github.com/nathan-fiscaletti/godepgraph-example/otherpackage
        root: /home/levente/go/src
        dir: /home/levente/go/src/github.com/nathan-fiscaletti/godepgraph-example/otherpackage
        expand root: /home/levente/go
        expand dir: /home/levente/go/src/github.com/nathan-fiscaletti/godepgraph-example/otherpackage
        separator: /

~/go/src/github.com/nathan-fiscaletti/godepgraph-example$ GO111MODULE=off go list .
unexpected directory layout:
        import path: _/home/levente/go/src/github.com/nathan-fiscaletti/godepgraph-example/otherpackage
        root: /home/levente/go/src
        dir: /home/levente/go/src/github.com/nathan-fiscaletti/godepgraph-example/otherpackage
        expand root: /home/levente/go
        expand dir: /home/levente/go/src/github.com/nathan-fiscaletti/godepgraph-example/otherpackage
        separator: /

Things have changed since then and I have no idea how go package listing deals with it, I guess it just doesn't deal with it properly yet. But using relative path is not a good practice and should be avoided in most cases.

Using go mod may allow go list to work here but still not solving your problem about the same package being two:

~/go/src/github.com/nathan-fiscaletti/godepgraph-example$ ~/go/bin/godepgraph -horizontal github.com/nathan-fiscaletti/godepgraph-example                           
digraph godep {     
rankdir="LR"                                                                                       
splines=ortho                                                                                      
nodesep=0.4                                                                                                                                                                                           ranksep=0.8                                                                                        
node [shape="box",style="rounded,filled"]                                                          
edge [arrowsize="0.5"]                                                                             
"../somepackage" [label="../somepackage" color="paleturquoise" URL="https://godoc.org/../somepackage" target="_blank"];                                                                               "./otherpackage" [label="./otherpackage" color="paleturquoise" URL="https://godoc.org/./otherpackage" target="_blank"];
"./otherpackage" -> "../somepackage";
"./otherpackage" -> "fmt";                                                                                                                                                                            
"./somepackage" [label="./somepackage" color="paleturquoise" URL="https://godoc.org/./somepackage" target="_blank"];                        
"fmt" [label="fmt" color="palegreen" URL="https://godoc.org/fmt" target="_blank"];                                                                                                                    
"github.com/nathan-fiscaletti/godepgraph-example" [label="github.com/nathan-fiscaletti/godepgraph-example" color="paleturquoise" URL="https://godoc.org/github.com/nathan-fiscaletti/godepgraph-exampl
e" target="_blank"];                                                                                                                                                                                  
"github.com/nathan-fiscaletti/godepgraph-example" -> "./otherpackage";
"github.com/nathan-fiscaletti/godepgraph-example" -> "./somepackage";                                                                                                                                 
}

leventeliu avatar Mar 19 '21 12:03 leventeliu