Skip to content

Commit

Permalink
Fix tinygo-org#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 a8b2f6e
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 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,26 @@ 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
}

return append(s[:i], s[j:]...)
}

0 comments on commit a8b2f6e

Please sign in to comment.