Skip to content

Commit

Permalink
Fix #4421 -C DIR support go1.19
Browse files Browse the repository at this point in the history
Signed-off-by: Roger Standridge <[email protected]>
  • Loading branch information
archie2x committed Aug 20, 2024
1 parent df5b35b commit 480c74c
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"regexp"
"runtime"
"runtime/pprof"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -1963,6 +1962,7 @@ type outputEntry struct {
//
// 2. A toolchain switch later on reinvokes the new go command with the same arguments.
// The parent toolchain has already done the chdir; the child must not try to do it again.

func handleChdirFlag() {
used := 2 // b.c. command at os.Args[1]
if used >= len(os.Args) {
Expand All @@ -1979,15 +1979,32 @@ func handleChdirFlag() {
return
}
dir = os.Args[used+1]
os.Args = slices.Delete(os.Args, used, used+2)
os.Args = slicesDelete(os.Args, used, used+2)

case strings.HasPrefix(a, "-C="), strings.HasPrefix(a, "--C="):
_, dir, _ = strings.Cut(a, "=")
os.Args = slices.Delete(os.Args, used, used+1)
os.Args = slicesDelete(os.Args, used, used+1)
}

if err := os.Chdir(dir); err != nil {
fmt.Fprintln(os.Stderr, "cannot chdir:", err)
os.Exit(1)
}
}

// go1.19 compatibility: lacks slices package
func slicesDelete[S ~[]E, E any](s S, i, j int) S {
_ = s[i:j:len(s)] // bounds check

if i == j {
return s
}

oldlen := len(s)
s = append(s[:i], s[j:]...)
for idx := range s[len(s):oldlen] {
var zero E
s[idx] = zero
}
return s
}

0 comments on commit 480c74c

Please sign in to comment.