Skip to content

Commit 445ae8b

Browse files
committed
fix: resolve duplicate PATH environment variable in Rust compiler
- Extract updatePathInEnv helper function to properly handle PATH updates - Remove existing PATH entry before adding updated one - Fixes TestRustCompiler_GetRustEnv_WithBothHomes failure on Windows CI - Prevents duplicate PATH entries that caused test assertion failures
1 parent 922411b commit 445ae8b

File tree

1 file changed

+24
-3
lines changed
  • internal/features/scripting/infrastructure/compilers

1 file changed

+24
-3
lines changed

internal/features/scripting/infrastructure/compilers/rust.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,15 +303,36 @@ func (c *RustCompiler) getRustEnv() []string {
303303

304304
if c.cargoHome != "" {
305305
env = append(env, fmt.Sprintf("CARGO_HOME=%s", c.cargoHome))
306-
// Also add cargo/bin to PATH
306+
// Update PATH to include cargo/bin
307307
cargoPath := filepath.Join(c.cargoHome, "bin")
308-
pathEnv := fmt.Sprintf("PATH=%s%c%s", cargoPath, filepath.ListSeparator, os.Getenv("PATH"))
309-
env = append(env, pathEnv)
308+
env = updatePathInEnv(env, cargoPath)
310309
}
311310

312311
return env
313312
}
314313

314+
// updatePathInEnv updates the PATH environment variable by prepending newPath
315+
// It removes any existing PATH entry and adds a new one with newPath at the beginning
316+
func updatePathInEnv(env []string, newPath string) []string {
317+
var existingPath string
318+
var result []string
319+
320+
// Find and extract existing PATH, filter it out
321+
for _, e := range env {
322+
if strings.HasPrefix(e, "PATH=") {
323+
existingPath = strings.TrimPrefix(e, "PATH=")
324+
} else {
325+
result = append(result, e)
326+
}
327+
}
328+
329+
// Prepend newPath to existing PATH
330+
updatedPath := fmt.Sprintf("PATH=%s%c%s", newPath, filepath.ListSeparator, existingPath)
331+
result = append(result, updatedPath)
332+
333+
return result
334+
}
335+
315336
// parseRustError parses Rust compiler error output
316337
func (c *RustCompiler) parseRustError(output string) error {
317338
// Rust error format example:

0 commit comments

Comments
 (0)