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

Commit

Permalink
main: fix -m mode stale binary bug (#84)
Browse files Browse the repository at this point in the history
In 6038506 we incorrectly optimised away an install step when -m is
supplied. We must always install if we are in -m mode, because the main
module is responsible for resolving versions. We therefore utilise the
install step to effectively ensure the target binary is up to date.

This logic would change if were to adopt #81 because we would then only
install non-versioned module packages (i.e. non-main module,
non-directory replaced), or in the case a versioned target does not
exist in the gobin cache.
  • Loading branch information
myitcv committed Sep 2, 2019
1 parent 6038506 commit da8791e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
23 changes: 20 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,27 @@ func mainerr() error {
// safe as it could/should be, but people shouldn't be messing with
// the cache anyway. The target in the cache is already hash by
// build tags so we should never have "overlapping" gobin runs.
if _, err := os.Stat(target); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("failed to read %v: %v", target, err)
//
// Always install if we are in -m mode, because the main module is
// responsible for resolving versions. We therefore utilise the
// install step to effectively ensure the target binary is up to date.
// This logic would change if were to adopt
// https://github.com/myitcv/gobin/issues/81 because we would then
// only install non-versioned module packages (i.e. non-main module,
// non-directory replaced), or in the case a versioned target does not
// exist in the gobin cache.
var install bool
if *fMainMod {
install = true
} else {
if _, err := os.Stat(target); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("failed to read %v: %v", target, err)
}
install = true
}
}
if install {
// optimistically remove our target in case we are installing over self
// TODO work out what to do for Windows
if mp.ImportPath == "github.com/myitcv/gobin" {
Expand Down
36 changes: 36 additions & 0 deletions testdata/main-module-change.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Specifically test the logic that ensures -m packages are always
# installed. Per the code comment, we always install as a means of
# establishing whether the target is current. If we instead chose
# to perform these checks ourself, we'd do just the same amount of
# work.

# Before
cp msg.go.before msg.go
gobin -m -run .
stdout ^before$
! stderr .+

# After
cp msg.go.after msg.go
gobin -m -run .
stdout ^after$
! stderr .+

-- go.mod --
module mod.com
-- main.go --
package main

import "fmt"

func main() {
fmt.Println(msg)
}
-- msg.go.before --
package main

var msg = "before"
-- msg.go.after --
package main

var msg = "after"

0 comments on commit da8791e

Please sign in to comment.