From fd5726db1110b6a856c54be39a315eea86bdf480 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Sat, 23 Mar 2019 08:26:55 +0000 Subject: [PATCH] main: fix gopkg.in path handling Also fix a bug whereby the full import path was being used after the module path for locations within the cache. For example, previously we had a cache path like: /github.com/gobin-testrepos/simple-main/@v/v1.0.0/github.com/gobin-testrepos/simple-main/simple-main whereas this should actually have been: github.com/gobin-testrepos/simple-main/@v/v1.0.0/simple-main Fixes #65. --- main.go | 26 +++++++++++++------ testdata/download.txt | 6 ++--- testdata/go111module.txt | 12 ++++----- testdata/gopkg.in.txt | 14 ++++++++++ .../gopkg.in_src-d_go-kallax.v1_v1.3.5.txt | 15 +++++++++++ testdata/print-global-non-module.txt | 12 ++++----- testdata/print-global-simple-main.txt | 12 ++++----- testdata/print-main-module-non-module.txt | 5 ++-- testdata/print-main-module-simple-main.txt | 5 ++-- 9 files changed, 72 insertions(+), 35 deletions(-) create mode 100644 testdata/gopkg.in.txt create mode 100644 testdata/mod/gopkg.in_src-d_go-kallax.v1_v1.3.5.txt diff --git a/main.go b/main.go index da90a66..6da9d55 100644 --- a/main.go +++ b/main.go @@ -306,19 +306,29 @@ func mainerr() error { md = filepath.Join(md, "@v", emv) } - epp, err := module.EncodePath(mp.ImportPath) - if err != nil { - return fmt.Errorf("failed to encode package relative path %v: %v", mp.ImportPath, err) + if mp.Module.Path != mp.ImportPath { + // We don't need to encode what remains in the pkg path because + // we've already uniquely identified the module. If there are + // case flips within a module then... well, we'll see. + pkgRem := strings.TrimPrefix(mp.ImportPath, mp.Module.Path+"/") + mainrel = filepath.Join(md, filepath.FromSlash(pkgRem)) + } else { + mainrel = md } - mainrel = filepath.Join(md, filepath.FromSlash(epp)) } gobin := filepath.Join(gobinCache, mainrel) - pref, _, ok := module.SplitPathVersion(mp.ImportPath) - if !ok { - return fmt.Errorf("failed to derive non-version prefix from %v", mp.ImportPath) + + var base string + if mp.Module.Path == mp.ImportPath { + pref, _, ok := module.SplitPathVersion(mp.ImportPath) + if !ok { + return fmt.Errorf("failed to derive non-version prefix from %v", mp.ImportPath) + } + base = path.Base(pref) + } else { + base = path.Base(mp.ImportPath) } - base := path.Base(pref) target := filepath.Join(gobin, base) if runtime.GOOS == "windows" { diff --git a/testdata/download.txt b/testdata/download.txt index b6c410b..2a9b88e 100644 --- a/testdata/download.txt +++ b/testdata/download.txt @@ -1,7 +1,7 @@ gobin -d github.com/gobin-testrepos/simple-main@v1.0.0 ! stdout .+ ! stderr .+ -[linux] exists $HOME/.cache/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/github.com/gobin-testrepos/simple-main/simple-main -[darwin] exists $HOME/Library/Caches/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/github.com/gobin-testrepos/simple-main/simple-main -[windows] exists $LOCALAPPDATA/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/github.com/gobin-testrepos/simple-main/simple-main.exe +[linux] exists $HOME/.cache/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/simple-main +[darwin] exists $HOME/Library/Caches/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/simple-main +[windows] exists $LOCALAPPDATA/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/simple-main.exe diff --git a/testdata/go111module.txt b/testdata/go111module.txt index 7a8d4a3..a56a758 100644 --- a/testdata/go111module.txt +++ b/testdata/go111module.txt @@ -2,13 +2,13 @@ env GO111MODULE=off gobin -p github.com/gobin-testrepos/simple-main@v1.0.0 -[linux] stdout ^$HOME\Q/.cache/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/github.com/gobin-testrepos/simple-main/simple-main\E'$' -[darwin] stdout ^$HOME\Q/Library/Caches/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/github.com/gobin-testrepos/simple-main/simple-main\E'$' -[windows] stdout ^${LOCALAPPDATA@R}\Q\gobin\github.com\gobin-testrepos\simple-main\@v\v1.0.0\github.com\gobin-testrepos\simple-main\simple-main.exe\E'$' +[linux] stdout ^$HOME\Q/.cache/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/simple-main\E'$' +[darwin] stdout ^$HOME\Q/Library/Caches/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/simple-main\E'$' +[windows] stdout ^${LOCALAPPDATA@R}\Q\gobin\github.com\gobin-testrepos\simple-main\@v\v1.0.0\simple-main.exe\E'$' ! stderr .+ -[linux] exec $HOME/.cache/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/github.com/gobin-testrepos/simple-main/simple-main -[darwin] exec $HOME/Library/Caches/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/github.com/gobin-testrepos/simple-main/simple-main -[windows] exec $LOCALAPPDATA/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/github.com/gobin-testrepos/simple-main/simple-main.exe +[linux] exec $HOME/.cache/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/simple-main +[darwin] exec $HOME/Library/Caches/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/simple-main +[windows] exec $LOCALAPPDATA/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/simple-main.exe stdout '^Simple module-based main v1.0.0$' ! stderr .+ diff --git a/testdata/gopkg.in.txt b/testdata/gopkg.in.txt new file mode 100644 index 0000000..6a9fe4c --- /dev/null +++ b/testdata/gopkg.in.txt @@ -0,0 +1,14 @@ +# Test that we have the correct logic for gopkg.in paths +# per https://github.com/myitcv/gobin/issues/65 + +gobin -p gopkg.in/src-d/go-kallax.v1/generator/cli/kallax +[linux] stdout ^$HOME\Q/.cache/gobin/gopkg.in/src-d/go-kallax.v1/@v/v1.3.5/generator/cli/kallax/kallax\E'$' +[darwin] stdout ^$HOME\Q/Library/Caches/gobin/gopkg.in/src-d/go-kallax.v1/@v/v1.3.5/generator/cli/kallax/kallax\E'$' +[windows] stdout ^${LOCALAPPDATA@R}\Q\gobin\gopkg.in\src-d\go-kallax.v1\@v\v1.3.5\generator\cli\kallax\kallax.exe\E'$' +! stderr .+ + +[linux] exec $HOME/.cache/gobin/gopkg.in/src-d/go-kallax.v1/@v/v1.3.5/generator/cli/kallax/kallax +[darwin] exec $HOME/Library/Caches/gobin/gopkg.in/src-d/go-kallax.v1/@v/v1.3.5/generator/cli/kallax/kallax +[windows] exec $LOCALAPPDATA/gobin/gopkg.in\src-d\go-kallax.v1\@v\v1.3.5\generator\cli\kallax\kallax.exe +stdout '^This is kallax$' +! stderr .+ diff --git a/testdata/mod/gopkg.in_src-d_go-kallax.v1_v1.3.5.txt b/testdata/mod/gopkg.in_src-d_go-kallax.v1_v1.3.5.txt new file mode 100644 index 0000000..73e01e4 --- /dev/null +++ b/testdata/mod/gopkg.in_src-d_go-kallax.v1_v1.3.5.txt @@ -0,0 +1,15 @@ +module gopkg.in/src-d/go-kallax.v1@v1.3.5 + +-- .mod -- +module gopkg.in/src-d/go-kallax.v1 +-- .info -- +{"Version":"v1.3.5","Time":"2018-06-07T08:58:58Z"} + +-- generator/cli/kallax/main.go -- +package main + +import "fmt" + +func main() { + fmt.Println("This is kallax") +} diff --git a/testdata/print-global-non-module.txt b/testdata/print-global-non-module.txt index c6106a4..bee7212 100644 --- a/testdata/print-global-non-module.txt +++ b/testdata/print-global-non-module.txt @@ -1,11 +1,11 @@ gobin -p github.com/gobin-testrepos/non-module@v1.0.0 -[linux] stdout ^$HOME\Q/.cache/gobin/github.com/gobin-testrepos/non-module/@v/v1.0.0/github.com/gobin-testrepos/non-module/non-module\E'$' -[darwin] stdout ^$HOME\Q/Library/Caches/gobin/github.com/gobin-testrepos/non-module/@v/v1.0.0/github.com/gobin-testrepos/non-module/non-module\E'$' -[windows] stdout ^${LOCALAPPDATA@R}\Q\gobin\github.com\gobin-testrepos\non-module\@v\v1.0.0\github.com\gobin-testrepos\non-module\non-module.exe\E'$' +[linux] stdout ^$HOME\Q/.cache/gobin/github.com/gobin-testrepos/non-module/@v/v1.0.0/non-module\E'$' +[darwin] stdout ^$HOME\Q/Library/Caches/gobin/github.com/gobin-testrepos/non-module/@v/v1.0.0/non-module\E'$' +[windows] stdout ^${LOCALAPPDATA@R}\Q\gobin\github.com\gobin-testrepos\non-module\@v\v1.0.0\non-module.exe\E'$' ! stderr .+ -[linux] exec $HOME/.cache/gobin/github.com/gobin-testrepos/non-module/@v/v1.0.0/github.com/gobin-testrepos/non-module/non-module -[darwin] exec $HOME/Library/Caches/gobin/github.com/gobin-testrepos/non-module/@v/v1.0.0/github.com/gobin-testrepos/non-module/non-module -[windows] exec $LOCALAPPDATA\gobin\github.com\gobin-testrepos\non-module\@v\v1.0.0\github.com\gobin-testrepos\non-module\non-module.exe +[linux] exec $HOME/.cache/gobin/github.com/gobin-testrepos/non-module/@v/v1.0.0/non-module +[darwin] exec $HOME/Library/Caches/gobin/github.com/gobin-testrepos/non-module/@v/v1.0.0/non-module +[windows] exec $LOCALAPPDATA\gobin\github.com\gobin-testrepos\non-module\@v\v1.0.0\non-module.exe stdout '^I am not a module$' ! stderr .+ diff --git a/testdata/print-global-simple-main.txt b/testdata/print-global-simple-main.txt index 9c32be7..ce5acd0 100644 --- a/testdata/print-global-simple-main.txt +++ b/testdata/print-global-simple-main.txt @@ -1,11 +1,11 @@ gobin -p github.com/gobin-testrepos/simple-main@v1.0.0 -[linux] stdout ^$HOME\Q/.cache/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/github.com/gobin-testrepos/simple-main/simple-main\E'$' -[darwin] stdout ^$HOME\Q/Library/Caches/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/github.com/gobin-testrepos/simple-main/simple-main\E'$' -[windows] stdout ^${LOCALAPPDATA@R}\Q\gobin\github.com\gobin-testrepos\simple-main\@v\v1.0.0\github.com\gobin-testrepos\simple-main\simple-main.exe\E'$' +[linux] stdout ^$HOME\Q/.cache/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/simple-main\E'$' +[darwin] stdout ^$HOME\Q/Library/Caches/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/simple-main\E'$' +[windows] stdout ^${LOCALAPPDATA@R}\Q\gobin\github.com\gobin-testrepos\simple-main\@v\v1.0.0\simple-main.exe\E'$' ! stderr .+ -[linux] exec $HOME/.cache/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/github.com/gobin-testrepos/simple-main/simple-main -[darwin] exec $HOME/Library/Caches/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/github.com/gobin-testrepos/simple-main/simple-main -[windows] exec $LOCALAPPDATA/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/github.com/gobin-testrepos/simple-main/simple-main.exe +[linux] exec $HOME/.cache/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/simple-main +[darwin] exec $HOME/Library/Caches/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/simple-main +[windows] exec $LOCALAPPDATA/gobin/github.com/gobin-testrepos/simple-main/@v/v1.0.0/simple-main.exe stdout '^Simple module-based main v1.0.0$' ! stderr .+ diff --git a/testdata/print-main-module-non-module.txt b/testdata/print-main-module-non-module.txt index db17535..a767c6b 100644 --- a/testdata/print-main-module-non-module.txt +++ b/testdata/print-main-module-non-module.txt @@ -1,10 +1,9 @@ cd repo gobin -m -p github.com/gobin-testrepos/non-module -[!windows] stdout ^${WORK@R}\Q/repo/.gobincache/github.com/gobin-testrepos/non-module/@v/v1.0.0/github.com/gobin-testrepos/non-module/non-module\E'$' -[windows] stdout ^${WORK@R}\Q\repo\.gobincache\github.com\gobin-testrepos\non-module\@v\v1.0.0\github.com\gobin-testrepos\non-module\non-module.exe\E'$' +stdout ^${WORK@R}[/\\]repo[/\\].gobincache[/\\]github.com[/\\]gobin-testrepos[/\\]non-module[/\\]@v[/\\]v1.0.0[/\\]non-module$exe'$' ! stderr .+ -exec $WORK/repo/.gobincache/github.com/gobin-testrepos/non-module/@v/v1.0.0/github.com/gobin-testrepos/non-module/non-module$exe +exec $WORK/repo/.gobincache/github.com/gobin-testrepos/non-module/@v/v1.0.0/non-module$exe stdout '^I am not a module$' ! stderr .+ diff --git a/testdata/print-main-module-simple-main.txt b/testdata/print-main-module-simple-main.txt index 484b069..02e3b24 100644 --- a/testdata/print-main-module-simple-main.txt +++ b/testdata/print-main-module-simple-main.txt @@ -1,10 +1,9 @@ cd repo gobin -m -p github.com/gobin-testrepos/simple-main -[!windows] stdout ^${WORK@R}\Q/repo/.gobincache/github.com/gobin-testrepos/simple-main/@v/v1.0.0/github.com/gobin-testrepos/simple-main/simple-main\E'$' -[windows] stdout ^${WORK@R}\Q\repo\.gobincache\github.com\gobin-testrepos\simple-main\@v\v1.0.0\github.com\gobin-testrepos\simple-main\simple-main.exe\E'$' +stdout ^${WORK@R}[/\\]repo[/\\].gobincache[/\\]github.com[/\\]gobin-testrepos[/\\]simple-main[/\\]@v[/\\]v1.0.0[/\\]simple-main$exe'$' ! stderr .+ -exec $WORK/repo/.gobincache/github.com/gobin-testrepos/simple-main/@v/v1.0.0/github.com/gobin-testrepos/simple-main/simple-main$exe +exec $WORK/repo/.gobincache/github.com/gobin-testrepos/simple-main/@v/v1.0.0/simple-main$exe stdout '^Simple module-based main v1.0.0$' ! stderr .+