Skip to content

Commit

Permalink
Fix building when GOBIN is set in the GOENV file (#73)
Browse files Browse the repository at this point in the history
compute basename manually when target is missing

fixes #71
  • Loading branch information
oliverpool authored Jul 6, 2024
1 parent fe5fe16 commit 05d1f9d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
30 changes: 28 additions & 2 deletions packer/gotool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"os/exec"
"path"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -315,7 +316,7 @@ func (be *BuildEnv) Build(bindir string, packages []string, packageBuildFlags, p
args := []string{
"build",
"-mod=mod",
"-o", filepath.Join(bindir, filepath.Base(pkg.Target)),
"-o", filepath.Join(bindir, pkg.Basename()),
}
tags := append(DefaultTags(), packageBuildTags[pkg.ImportPath]...)
args = append(args, "-tags="+strings.Join(tags, ","))
Expand Down Expand Up @@ -347,7 +348,32 @@ type Pkg struct {
}

func (p *Pkg) Basename() string {
return filepath.Base(p.Target)
if p.Target != "" {
return filepath.Base(p.Target)
}
// when GOBIN is set in the GOENV file, the target field is empty
// see https://github.com/gokrazy/tools/issues/71
base := path.Base(p.ImportPath)
if isVersionElement(base) {
return path.Base(path.Dir(p.ImportPath))
}
return base
}

// isVersionElement reports whether s is a well-formed path version element:
// v2, v3, v10, etc, but not v0, v05, v1.
// copied from https://github.com/golang/go/blob/go1.22.5/src/cmd/go/internal/load/pkg.go#L1338
// governed by a BSD-style license
func isVersionElement(s string) bool {
if len(s) < 2 || s[0] != 'v' || s[1] == '0' || s[1] == '1' && len(s) == 2 {
return false
}
for i := 1; i < len(s); i++ {
if s[i] < '0' || '9' < s[i] {
return false
}
}
return true
}

func (be *BuildEnv) mainPackage(pkg string) ([]Pkg, error) {
Expand Down
47 changes: 47 additions & 0 deletions packer/gotool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package packer

import "testing"

func TestPkgBasename(t *testing.T) {
tests := []struct {
pkg Pkg
wantBasename string
}{
{
Pkg{
Target: "target-name",
},
"target-name",
},
{
Pkg{
ImportPath: "example.com/import/path",
Target: "target-name",
},
"target-name",
},
{
Pkg{
ImportPath: "example.com/import/path",
Target: "",
},
"path",
},
{
Pkg{
ImportPath: "example.com/import/path/v2",
Target: "",
},
"path",
},
}

for _, tt := range tests {
t.Run(tt.pkg.Target+"/"+tt.pkg.ImportPath, func(t *testing.T) {
got := tt.pkg.Basename()
if got != tt.wantBasename {
t.Errorf("pkg.Basename got %q, want %q", got, tt.wantBasename)
}
})
}
}

0 comments on commit 05d1f9d

Please sign in to comment.