Skip to content

Commit

Permalink
Allow relative paths in GOHACK env var (#45)
Browse files Browse the repository at this point in the history
Prior to this change, we were using `filepath.Join()` to derive a
 replacement for the module dir. But this is not enough to
 support relative directories, as `Join()` cleans the resulting path,
 removing any `"./"` prefix (which is needed by the replace drective when
 using a local directory).

 This change adds the `"./"` prefix if the user specified it in the GOHACK
 env var.

 Note that relative parent directory `".."` is not cleaned by
 `filepath.Join()`, so we do not need to cater to this case, and an added
 test proves that this works.

 fixes #44
  • Loading branch information
davidovich authored and myitcv committed Mar 29, 2019
1 parent dfc57bb commit 01736fb
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
13 changes: 11 additions & 2 deletions mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,18 @@ func getVCSInfoForModule(m *listModule) (*moduleVCSInfo, error) {
// used for storing the module with the given path.
func moduleDir(module string) string {
// TODO decide what color this bikeshed should be.
d := os.Getenv("GOHACK")
d := filepath.FromSlash(os.Getenv("GOHACK"))
if d == "" {
d = filepath.Join(os.Getenv("HOME"), "gohack")
}
return filepath.Join(d, filepath.FromSlash(module))

return join(d, filepath.FromSlash(module))
}

func join(ps ...string) string {
res := filepath.Join(ps...)
if !filepath.IsAbs(res) && res[0] != '.' {
res = "." + string(os.PathSeparator) + res
}
return res
}
29 changes: 29 additions & 0 deletions testdata/get-relative-parent.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cd repo
go get rsc.io/[email protected]
env GOHACK=../gohack/
gohack get rsc.io/quote
stdout '^rsc.io/quote => \.\./gohack/rsc.io/quote$'
! stderr .+

# Check that the replace statement is there.
grep -count=1 '^replace rsc\.io/quote => \.\./gohack/rsc.io/quote$' go.mod

# Check that the source files have been copied.
exists ../gohack/rsc.io/quote/quote.go

# Check that we can compile the command OK.
go install example.com/repo

-- repo/main.go --
package main
import (
"fmt"
"rsc.io/quote"
)

func main() {
fmt.Println(quote.Glass())
}

-- repo/go.mod --
module example.com/repo
32 changes: 32 additions & 0 deletions testdata/get-relative.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cd repo
go get rsc.io/[email protected]
env GOHACK=.
gohack get rsc.io/quote
stdout '^rsc.io/quote => \./rsc.io/quote$'
! stderr .+

# Check that the replace statement is there.
grep -count=1 '^replace rsc\.io/quote => \./rsc.io/quote$' go.mod

# Check that the source files have been copied.
exists ./rsc.io/quote/quote.go

# It should not be a git repository.
! exists ./rsc.io/quote/.git

# Check that we can compile the command OK.
go install example.com/repo

-- repo/main.go --
package main
import (
"fmt"
"rsc.io/quote"
)

func main() {
fmt.Println(quote.Glass())
}

-- repo/go.mod --
module example.com/repo

0 comments on commit 01736fb

Please sign in to comment.