Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Commit

Permalink
main: fix gopkg.in path handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
myitcv committed Mar 23, 2019
1 parent f3d42eb commit 96e86c0
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 35 deletions.
26 changes: 18 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
6 changes: 3 additions & 3 deletions testdata/download.txt
Original file line number Diff line number Diff line change
@@ -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

12 changes: 6 additions & 6 deletions testdata/go111module.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 .+
14 changes: 14 additions & 0 deletions testdata/gopkg.in.txt
Original file line number Diff line number Diff line change
@@ -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 .+
15 changes: 15 additions & 0 deletions testdata/mod/gopkg.in_src-d_go-kallax.v1_v1.3.5.txt
Original file line number Diff line number Diff line change
@@ -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")
}
12 changes: 6 additions & 6 deletions testdata/print-global-non-module.txt
Original file line number Diff line number Diff line change
@@ -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 .+
12 changes: 6 additions & 6 deletions testdata/print-global-simple-main.txt
Original file line number Diff line number Diff line change
@@ -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 .+
5 changes: 2 additions & 3 deletions testdata/print-main-module-non-module.txt
Original file line number Diff line number Diff line change
@@ -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'$'
! 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 .+

Expand Down
5 changes: 2 additions & 3 deletions testdata/print-main-module-simple-main.txt
Original file line number Diff line number Diff line change
@@ -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'$'
! 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 .+

Expand Down

0 comments on commit 96e86c0

Please sign in to comment.