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

main: fix gopkg.in path handling #72

Merged
merged 1 commit into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be path, not filepath? since it's a module import path, and not a path of a directory on disk.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filepath is correct here I think. md is the file path of the encoded module path. Hence it only makes sense to join another filepath.

} 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$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 .+

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$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 .+

Expand Down