Skip to content

Commit 8cb63e1

Browse files
committed
Adopt go 1.21
This change moves us to use Go 1.21 for building `azd`. As part of go 1.21, the `maps`, `slices` and `slog` packages, previously in `golang.org/x/exp` have been moved into the standard libary. As part of this change, I've moved to use these versions of the packages instead of using the experimental versions. However, there were a few places where we were using `maps.Keys`, which was not added to the standard library. https://go-review.googlesource.com/c/go/+/513715 explains the rationale here, they may want `Keys` to return an iterator, and were not comfortable locking the name at this time. This functionality will be added to the `maps` package in a future release, it seems, likely under a name like `KeysSlice`. For now, I just kept the existing use of the the experimental package. We can update the import path and move to the stdlib version when it lands later. This change also updates `go.mod` to declare that we need at least go 1.21 to build `azd`. Developers which haven't upgraded will see build errors due to references of `maps`, `slices` and `log/slog` which do not exist in earlier versions of the standard libary. I've also enabled the loopvar experiment for our CI builds and tests. https://github.com/golang/go/wiki/LoopvarExperiment gives more information on what this does, but the long and short of it is that this behavior is likely to become the default in a future version of go, and the semantics of it more closely match what developers expect, so I would like us to just lock in the changes now (we are presently clean on this, so there are no changes, I just don't want us to introduce any new bad uses).
1 parent 6d0dc5c commit 8cb63e1

File tree

27 files changed

+95
-69
lines changed

27 files changed

+95
-69
lines changed

.github/workflows/cli-ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ jobs:
2121
steps:
2222
- uses: actions/setup-go@v3
2323
with:
24-
go-version: "^1.20.0"
24+
go-version: "^1.21.0"
2525
- uses: actions/checkout@v3
2626
- name: golangci-lint
2727
uses: golangci/golangci-lint-action@v3
2828
with:
29-
version: v1.51.2
29+
version: v1.54.1
3030
args: -v --timeout 10m0s
3131
working-directory: cli/azd
3232

cli/azd/ci-build.ps1

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -132,43 +132,52 @@ function PrintFlags() {
132132
}
133133
}
134134

135-
Write-Host "Running: go build ``"
136-
PrintFlags -flags $buildFlags
137-
go build @buildFlags
138-
139-
if ($BuildRecordMode) {
140-
$recordFlagPresent = $false
141-
for ($i = 0; $i -lt $buildFlags.Length; $i++) {
142-
if ($buildFlags[$i].StartsWith("-tags=")) {
143-
$recordFlagPresent = $true
144-
$buildFlags[$i] += ",record"
135+
$oldGOEXPERIMENT = $env:GOEXPERIMENT
136+
# Enable the loopvar experiment, which makes the loop variaible for go loops like `range` behave as most folks would expect.
137+
# the go team is exploring making this default in the future, and we'd like to opt into the behavior now.
138+
$env:GOEXPERIMENT="loopvar"
139+
140+
try {
141+
Write-Host "Running: go build ``"
142+
PrintFlags -flags $buildFlags
143+
go build @buildFlags
144+
145+
if ($BuildRecordMode) {
146+
$recordFlagPresent = $false
147+
for ($i = 0; $i -lt $buildFlags.Length; $i++) {
148+
if ($buildFlags[$i].StartsWith("-tags=")) {
149+
$recordFlagPresent = $true
150+
$buildFlags[$i] += ",record"
151+
}
152+
}
153+
154+
if (-not $recordFlagPresent) {
155+
$buildFlags[$i] += "-tags=record"
156+
}
157+
158+
$outputFlag = "-o=azd-record"
159+
if ($IsWindows) {
160+
$outputFlag += ".exe"
145161
}
162+
$buildFlags += $outputFlag
163+
164+
Write-Host "Running: go build (record) ``"
165+
PrintFlags -flags $buildFlags
166+
go build @buildFlags
146167
}
147-
148-
if (-not $recordFlagPresent) {
149-
$buildFlags[$i] += "-tags=record"
168+
169+
if ($LASTEXITCODE) {
170+
Write-Host "Error running go build"
171+
exit $LASTEXITCODE
150172
}
151-
152-
$outputFlag = "-o=azd-record"
173+
Write-Host "go build succeeded"
174+
153175
if ($IsWindows) {
154-
$outputFlag += ".exe"
176+
Write-Host "Windows exe file version info"
177+
$azdExe = Get-Item azd.exe
178+
Write-Host "File Version: $($azdExe.VersionInfo.FileVersionRaw)"
179+
Write-Host "Product Version: $($azdExe.VersionInfo.ProductVersionRaw)"
155180
}
156-
$buildFlags += $outputFlag
157-
158-
Write-Host "Running: go build (record) ``"
159-
PrintFlags -flags $buildFlags
160-
go build @buildFlags
161-
}
162-
163-
if ($LASTEXITCODE) {
164-
Write-Host "Error running go build"
165-
exit $LASTEXITCODE
166-
}
167-
Write-Host "go build succeeded"
168-
169-
if ($IsWindows) {
170-
Write-Host "Windows exe file version info"
171-
$azdExe = Get-Item azd.exe
172-
Write-Host "File Version: $($azdExe.VersionInfo.FileVersionRaw)"
173-
Write-Host "Product Version: $($azdExe.VersionInfo.ProductVersionRaw)"
181+
} finally {
182+
$env:GOEXPERIMENT = $oldGOEXPERIMENT
174183
}

cli/azd/ci-test.ps1

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,22 @@ if ($ShortMode) {
5050
Write-Host "Running integration tests..."
5151
$intCoverDir = New-EmptyDirectory -Path $IntegrationTestCoverageDir
5252

53+
$oldGOCOVERDIR = $env:GOCOVERDIR
54+
$oldGOEXPERIMENT = $env:GOEXPERIMENT
55+
5356
# GOCOVERDIR enables any binaries (in this case, azd.exe) built with '-cover',
5457
# to write out coverage output to the specific directory.
5558
$env:GOCOVERDIR = $intCoverDir.FullName
59+
# Enable the loopvar experiment, which makes the loop variaible for go loops like `range` behave as most folks would expect.
60+
# the go team is exploring making this default in the future, and we'd like to opt into the behavior now.
61+
$env:GOEXPERIMENT="loopvar"
5662

57-
& $gotestsum -- ./test/... -v -timeout $IntegrationTestTimeout
58-
if ($LASTEXITCODE) {
59-
exit $LASTEXITCODE
60-
}
63+
try {
64+
& $gotestsum -- ./test/... -v -timeout $IntegrationTestTimeout
65+
if ($LASTEXITCODE) {
66+
exit $LASTEXITCODE
67+
}
68+
} finally {
69+
$env:GOCOVERDIR = $oldGOCOVERDIR
70+
$env:GOEXPERIMENT = $oldGOEXPERIMENT
71+
}

cli/azd/cmd/cmd_help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ package cmd
55

66
import (
77
"fmt"
8+
"slices"
89
"strings"
910

1011
"github.com/azure/azure-dev/cli/azd/pkg/output"
1112
"github.com/spf13/cobra"
1213
"github.com/spf13/pflag"
13-
"golang.org/x/exp/slices"
1414
)
1515

1616
const (

cli/azd/cmd/cobra_builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"log"
7+
"slices"
78
"strconv"
89
"strings"
910

@@ -19,7 +20,6 @@ import (
1920
"github.com/azure/azure-dev/cli/azd/pkg/tools"
2021
"github.com/azure/azure-dev/cli/azd/pkg/tools/azcli"
2122
"github.com/spf13/cobra"
22-
"golang.org/x/exp/slices"
2323
)
2424

2525
const cDocsFlagName = "docs"

cli/azd/internal/repository/initializer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"runtime"
10+
"slices"
1011
"strings"
1112
"testing"
1213

@@ -21,7 +22,6 @@ import (
2122
"github.com/azure/azure-dev/cli/azd/test/mocks/mockinput"
2223
"github.com/stretchr/testify/assert"
2324
"github.com/stretchr/testify/require"
24-
"golang.org/x/exp/slices"
2525
)
2626

2727
func Test_Initializer_Initialize(t *testing.T) {

cli/azd/internal/telemetry/storage_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"slices"
89
"testing"
910
"time"
1011

1112
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
1213
"github.com/benbjohnson/clock"
1314
"github.com/stretchr/testify/assert"
1415
"github.com/stretchr/testify/require"
15-
"golang.org/x/exp/slices"
1616
)
1717

1818
// The tests in this file intentionally interacts with the filesystem (important implementation detail).

cli/azd/internal/telemetry/uploader_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import (
44
"context"
55
"fmt"
66
"math/rand"
7+
"slices"
78
"strconv"
89
"testing"
910
"time"
1011

1112
appinsightsexporter "github.com/azure/azure-dev/cli/azd/internal/telemetry/appinsights-exporter"
1213
"github.com/benbjohnson/clock"
1314
"github.com/stretchr/testify/assert"
14-
"golang.org/x/exp/slices"
1515
)
1616

1717
type InMemoryItem struct {

cli/azd/internal/tracing/baggage/baggage.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
package baggage
55

66
import (
7-
"go.opentelemetry.io/otel/attribute"
87
"golang.org/x/exp/maps"
8+
9+
"go.opentelemetry.io/otel/attribute"
910
)
1011

1112
// An immutable object safe for concurrent use.

cli/azd/pkg/account/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"fmt"
77
"log"
88
"os"
9+
"slices"
910

1011
"github.com/azure/azure-dev/cli/azd/pkg/config"
11-
"golang.org/x/exp/slices"
1212
)
1313

1414
// JSON document path locations for default subscription & location

cli/azd/pkg/account/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"net/http"
7+
"slices"
78
"testing"
89

910
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions"
@@ -15,7 +16,6 @@ import (
1516
"github.com/azure/azure-dev/cli/azd/test/mocks/mockhttp"
1617
"github.com/azure/azure-dev/cli/azd/test/mocks/mockinput"
1718
"github.com/stretchr/testify/require"
18-
"golang.org/x/exp/slices"
1919
)
2020

2121
func Test_GetAccountDefaults(t *testing.T) {

cli/azd/pkg/auth/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"io"
99
"log"
1010
"net/http"
11+
"slices"
1112

1213
msal "github.com/AzureAD/microsoft-authentication-library-for-go/apps/errors"
13-
"golang.org/x/exp/slices"
1414
)
1515

1616
const cLoginCmd = "azd auth login"

cli/azd/pkg/azureutil/location.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
"context"
88
"fmt"
99
"os"
10+
"slices"
1011
"strings"
1112

1213
"github.com/azure/azure-dev/cli/azd/pkg/account"
1314
"github.com/azure/azure-dev/cli/azd/pkg/environment"
1415
"github.com/azure/azure-dev/cli/azd/pkg/input"
15-
"golang.org/x/exp/slices"
1616
)
1717

1818
// PromptLocation asks the user to select a location from a list of supported azure locations for a given subscription.
@@ -48,9 +48,9 @@ func PromptLocationWithFilter(
4848
}
4949
}
5050

51-
slices.SortFunc(locations, func(a, b account.Location) bool {
51+
slices.SortFunc(locations, func(a, b account.Location) int {
5252
return strings.Compare(
53-
strings.ToLower(a.RegionalDisplayName), strings.ToLower(b.RegionalDisplayName)) < 0
53+
strings.ToLower(a.RegionalDisplayName), strings.ToLower(b.RegionalDisplayName))
5454
})
5555

5656
// Allow the environment variable `AZURE_LOCATION` to control the default value for the location

cli/azd/pkg/environment/environment.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import (
1111
"regexp"
1212
"strings"
1313

14+
"maps"
15+
1416
"github.com/azure/azure-dev/cli/azd/internal/tracing"
1517
"github.com/azure/azure-dev/cli/azd/internal/tracing/fields"
1618
"github.com/azure/azure-dev/cli/azd/pkg/config"
1719
"github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext"
1820
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
1921
"github.com/joho/godotenv"
20-
"golang.org/x/exp/maps"
2122
)
2223

2324
// EnvNameEnvVarName is the name of the key used to store the envname property in the environment.

cli/azd/pkg/infra/provisioning/bicep/bicep_provider.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"math"
1313
"os"
1414
"path/filepath"
15+
"slices"
1516
"strconv"
1617
"strings"
1718
"time"
@@ -38,7 +39,6 @@ import (
3839
"github.com/benbjohnson/clock"
3940
"github.com/drone/envsubst"
4041
"golang.org/x/exp/maps"
41-
"golang.org/x/exp/slices"
4242
)
4343

4444
const DefaultModule = "main"
@@ -715,8 +715,8 @@ func (p *BicepProvider) findCompletedDeployments(
715715
return nil, err
716716
}
717717

718-
slices.SortFunc(deployments, func(x, y *armresources.DeploymentExtended) bool {
719-
return x.Properties.Timestamp.After(*y.Properties.Timestamp)
718+
slices.SortFunc(deployments, func(x, y *armresources.DeploymentExtended) int {
719+
return x.Properties.Timestamp.Compare(*y.Properties.Timestamp)
720720
})
721721

722722
// If hint is not provided, use the environment name as the hint

cli/azd/pkg/infra/provisioning/bicep/prompt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"slices"
78
"strconv"
89

910
"github.com/azure/azure-dev/cli/azd/pkg/account"
1011
"github.com/azure/azure-dev/cli/azd/pkg/azure"
1112
"github.com/azure/azure-dev/cli/azd/pkg/input"
1213
"github.com/azure/azure-dev/cli/azd/pkg/output"
13-
"golang.org/x/exp/slices"
1414

1515
. "github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning"
1616
)

cli/azd/pkg/pipeline/github_provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net/url"
1414
"path/filepath"
1515
"regexp"
16+
"slices"
1617
"strings"
1718

1819
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
@@ -31,7 +32,6 @@ import (
3132
"github.com/azure/azure-dev/cli/azd/pkg/tools/azcli"
3233
"github.com/azure/azure-dev/cli/azd/pkg/tools/git"
3334
"github.com/azure/azure-dev/cli/azd/pkg/tools/github"
34-
"golang.org/x/exp/slices"
3535
)
3636

3737
// GitHubScmProvider implements ScmProvider using GitHub as the provider

cli/azd/pkg/pipeline/pipeline_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"log"
1111
"path/filepath"
12+
"slices"
1213
"strings"
1314
"time"
1415

@@ -25,7 +26,6 @@ import (
2526
"github.com/azure/azure-dev/cli/azd/pkg/tools/azcli"
2627
"github.com/azure/azure-dev/cli/azd/pkg/tools/git"
2728
"github.com/sethvargo/go-retry"
28-
"golang.org/x/exp/slices"
2929
)
3030

3131
type PipelineAuthType string

cli/azd/pkg/project/project.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"log"
88
"os"
99
"path/filepath"
10+
"slices"
1011
"strings"
1112

1213
"github.com/azure/azure-dev/cli/azd/internal"
@@ -16,7 +17,6 @@ import (
1617
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning"
1718
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
1819
"github.com/blang/semver/v4"
19-
"golang.org/x/exp/slices"
2020
"gopkg.in/yaml.v3"
2121
)
2222

0 commit comments

Comments
 (0)