Skip to content

Commit

Permalink
Fix: Add directory containing binary to PATH (ko-build#127)
Browse files Browse the repository at this point in the history
Fixes the PATH environment variable so it contains the directory
containing the binary (i.e., /ko-app) instead of the binary itself
(e.g., /ko-app/ko).

See ko-build#114.
  • Loading branch information
halvards authored and jonjohnsonjr committed Jan 28, 2020
1 parent f12b9e6 commit ca1b2a1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
12 changes: 6 additions & 6 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ func (gb *gobuild) Build(ctx context.Context, s string) (v1.Image, error) {

cfg = cfg.DeepCopy()
cfg.Config.Entrypoint = []string{appPath}
updatePath(cfg, appPath)
updatePath(cfg)
cfg.Config.Env = append(cfg.Config.Env, "KO_DATA_PATH="+kodataRoot)
cfg.Author = "github.com/google/ko"

Expand All @@ -467,9 +467,9 @@ func (gb *gobuild) Build(ctx context.Context, s string) (v1.Image, error) {
return image, nil
}

// Append appPath to the PATH environment variable, if it exists. Otherwise,
// set the PATH environment variable to appPath.
func updatePath(cf *v1.ConfigFile, appPath string) {
// Append appDir to the PATH environment variable, if it exists. Otherwise,
// set the PATH environment variable to appDir.
func updatePath(cf *v1.ConfigFile) {
for i, env := range cf.Config.Env {
parts := strings.SplitN(env, "=", 2)
if len(parts) != 2 {
Expand All @@ -478,12 +478,12 @@ func updatePath(cf *v1.ConfigFile, appPath string) {
}
key, value := parts[0], parts[1]
if key == "PATH" {
value = fmt.Sprintf("%s:%s", value, appPath)
value = fmt.Sprintf("%s:%s", value, appDir)
cf.Config.Env[i] = "PATH=" + value
return
}
}

// If we get here, we never saw PATH.
cf.Config.Env = append(cf.Config.Env, "PATH="+appPath)
cf.Config.Env = append(cf.Config.Env, "PATH="+appDir)
}
14 changes: 10 additions & 4 deletions pkg/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,22 @@ func TestGoBuild(t *testing.T) {
}
})

// Check that PATH contains the produced binary.
// Check that PATH contains the directory of the produced binary.
t.Run("check PATH env var", func(t *testing.T) {
cfg, err := img.ConfigFile()
if err != nil {
t.Errorf("ConfigFile() = %v", err)
}
found := false
for _, entry := range cfg.Config.Env {
if strings.HasPrefix(entry, "PATH=") && strings.Contains(entry, "/ko-app/test") {
found = true
for _, envVar := range cfg.Config.Env {
if strings.HasPrefix(envVar, "PATH=") {
pathValue := strings.TrimPrefix(envVar, "PATH=")
pathEntries := strings.Split(pathValue, ":")
for _, pathEntry := range pathEntries {
if pathEntry == appDir {
found = true
}
}
}
}
if !found {
Expand Down

0 comments on commit ca1b2a1

Please sign in to comment.