Skip to content

Commit 717c3c0

Browse files
committed
switch go install for go build and always pass existing env vars while replacing them with custom ones
1 parent 3db549c commit 717c3c0

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

pkg/loop/cmd/loopinstall/install.go

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func downloadAndInstallPlugin(pluginType string, pluginIdx int, plugin PluginDef
123123
}
124124
}
125125

126-
// Build env vars
126+
// Build env vars from defaults, environment variable, and plugin-specific settings
127127
envVars := defaults.EnvVars
128128
if envEnvVars := os.Getenv("CL_PLUGIN_ENVVARS"); envEnvVars != "" {
129129
envVars = strings.Fields(envEnvVars)
@@ -173,43 +173,77 @@ func downloadAndInstallPlugin(pluginType string, pluginIdx int, plugin PluginDef
173173
}
174174
}
175175

176-
args := []string{"install"}
176+
binaryName := filepath.Base(installArg)
177+
if binaryName == "." {
178+
binaryName = filepath.Base(moduleURI)
179+
}
180+
181+
// Determine output directory
182+
outputDir := os.Getenv("GOBIN")
183+
if outputDir == "" {
184+
gopath := os.Getenv("GOPATH")
185+
if gopath == "" {
186+
gopath = filepath.Join(os.Getenv("HOME"), "go")
187+
}
188+
outputDir = filepath.Join(gopath, "bin")
189+
}
190+
191+
outputPath := filepath.Join(outputDir, binaryName)
192+
193+
args := []string{"build", "-o", outputPath}
177194
if goflags != "" {
178195
args = append(args, strings.Fields(goflags)...)
179196
}
180-
// Add the install path, which is now relative to the module root or ".".
181197
args = append(args, installArg)
182198

183199
cmd := exec.Command("go", args...)
184200
cmd.Dir = moduleDir
185201
cmd.Stdout = os.Stdout
186202
cmd.Stderr = os.Stderr
187203

188-
// Set GOPRIVATE environment variable while preserving other environment variables
189-
if goPrivate != "" {
190-
// Start with all current environment variables
191-
env := os.Environ()
204+
// Start with all current environment variables
205+
cmd.Env = os.Environ()
192206

207+
// Set GOPRIVATE environment variable if provided
208+
if goPrivate != "" {
193209
// Find and replace GOPRIVATE if it exists, or add it if it doesn't
194210
goprivateFound := false
195-
for i, e := range env {
211+
for i, e := range cmd.Env {
196212
if strings.HasPrefix(e, "GOPRIVATE=") {
197-
env[i] = "GOPRIVATE=" + goPrivate
213+
cmd.Env[i] = "GOPRIVATE=" + goPrivate
198214
goprivateFound = true
199215
break
200216
}
201217
}
202218

203219
// Add GOPRIVATE if it wasn't already in the environment
204220
if !goprivateFound {
205-
env = append(env, "GOPRIVATE="+goPrivate)
221+
cmd.Env = append(cmd.Env, "GOPRIVATE="+goPrivate)
206222
}
207-
208-
cmd.Env = env
209223
}
210224

225+
// Add/replace custom environment variables (e.g., GOOS, GOARCH, CGO_ENABLED)
226+
// Replace existing vars to avoid duplicates that could cause unexpected behavior
211227
for _, ev := range envVars {
212-
cmd.Env = append(cmd.Env, ev)
228+
// Parse the env var to get the key
229+
parts := strings.SplitN(ev, "=", 2)
230+
if len(parts) != 2 {
231+
continue
232+
}
233+
key := parts[0]
234+
235+
// Find and replace if it exists, otherwise append
236+
found := false
237+
for i, e := range cmd.Env {
238+
if strings.HasPrefix(e, key+"=") {
239+
cmd.Env[i] = ev
240+
found = true
241+
break
242+
}
243+
}
244+
if !found {
245+
cmd.Env = append(cmd.Env, ev)
246+
}
213247
}
214248

215249
log.Printf("Running install command: go %s (in directory: %s)", strings.Join(args, " "), moduleDir)

0 commit comments

Comments
 (0)