From 54b09e2efa0b2c501ec24d7ffeb0d95e46ca4df4 Mon Sep 17 00:00:00 2001 From: Roger Standridge <9526806+archie2x@users.noreply.github.com> Date: Tue, 20 Aug 2024 10:19:23 -0700 Subject: [PATCH] Fix #4421 `-C DIR` support go1.19 Signed-off-by: Roger Standridge <9526806+archie2x@users.noreply.github.com> --- main.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 74db8785bd..227c869c94 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,6 @@ import ( "regexp" "runtime" "runtime/pprof" - "slices" "sort" "strconv" "strings" @@ -1961,6 +1960,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) { @@ -1977,11 +1977,11 @@ 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 { @@ -1989,3 +1989,14 @@ func handleChdirFlag() { 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:]...) +}