Skip to content

Commit

Permalink
Adopt go 1.21 (#2614)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
ellismg authored Aug 21, 2023
1 parent cb50a37 commit 7a7f4e7
Show file tree
Hide file tree
Showing 27 changed files with 95 additions and 69 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cli-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: "^1.20.0"
go-version: "^1.21.0"
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.51.2
version: v1.54.1
args: -v --timeout 10m0s
working-directory: cli/azd

Expand Down
77 changes: 43 additions & 34 deletions cli/azd/ci-build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -132,43 +132,52 @@ function PrintFlags() {
}
}

Write-Host "Running: go build ``"
PrintFlags -flags $buildFlags
go build @buildFlags

if ($BuildRecordMode) {
$recordFlagPresent = $false
for ($i = 0; $i -lt $buildFlags.Length; $i++) {
if ($buildFlags[$i].StartsWith("-tags=")) {
$recordFlagPresent = $true
$buildFlags[$i] += ",record"
$oldGOEXPERIMENT = $env:GOEXPERIMENT
# Enable the loopvar experiment, which makes the loop variaible for go loops like `range` behave as most folks would expect.
# the go team is exploring making this default in the future, and we'd like to opt into the behavior now.
$env:GOEXPERIMENT="loopvar"

try {
Write-Host "Running: go build ``"
PrintFlags -flags $buildFlags
go build @buildFlags

if ($BuildRecordMode) {
$recordFlagPresent = $false
for ($i = 0; $i -lt $buildFlags.Length; $i++) {
if ($buildFlags[$i].StartsWith("-tags=")) {
$recordFlagPresent = $true
$buildFlags[$i] += ",record"
}
}

if (-not $recordFlagPresent) {
$buildFlags[$i] += "-tags=record"
}

$outputFlag = "-o=azd-record"
if ($IsWindows) {
$outputFlag += ".exe"
}
$buildFlags += $outputFlag

Write-Host "Running: go build (record) ``"
PrintFlags -flags $buildFlags
go build @buildFlags
}

if (-not $recordFlagPresent) {
$buildFlags[$i] += "-tags=record"

if ($LASTEXITCODE) {
Write-Host "Error running go build"
exit $LASTEXITCODE
}

$outputFlag = "-o=azd-record"
Write-Host "go build succeeded"

if ($IsWindows) {
$outputFlag += ".exe"
Write-Host "Windows exe file version info"
$azdExe = Get-Item azd.exe
Write-Host "File Version: $($azdExe.VersionInfo.FileVersionRaw)"
Write-Host "Product Version: $($azdExe.VersionInfo.ProductVersionRaw)"
}
$buildFlags += $outputFlag

Write-Host "Running: go build (record) ``"
PrintFlags -flags $buildFlags
go build @buildFlags
}

if ($LASTEXITCODE) {
Write-Host "Error running go build"
exit $LASTEXITCODE
}
Write-Host "go build succeeded"

if ($IsWindows) {
Write-Host "Windows exe file version info"
$azdExe = Get-Item azd.exe
Write-Host "File Version: $($azdExe.VersionInfo.FileVersionRaw)"
Write-Host "Product Version: $($azdExe.VersionInfo.ProductVersionRaw)"
} finally {
$env:GOEXPERIMENT = $oldGOEXPERIMENT
}
19 changes: 15 additions & 4 deletions cli/azd/ci-test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,22 @@ if ($ShortMode) {
Write-Host "Running integration tests..."
$intCoverDir = New-EmptyDirectory -Path $IntegrationTestCoverageDir

$oldGOCOVERDIR = $env:GOCOVERDIR
$oldGOEXPERIMENT = $env:GOEXPERIMENT

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

& $gotestsum -- ./test/... -v -timeout $IntegrationTestTimeout
if ($LASTEXITCODE) {
exit $LASTEXITCODE
}
try {
& $gotestsum -- ./test/... -v -timeout $IntegrationTestTimeout
if ($LASTEXITCODE) {
exit $LASTEXITCODE
}
} finally {
$env:GOCOVERDIR = $oldGOCOVERDIR
$env:GOEXPERIMENT = $oldGOEXPERIMENT
}
2 changes: 1 addition & 1 deletion cli/azd/cmd/cmd_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ package cmd

import (
"fmt"
"slices"
"strings"

"github.com/azure/azure-dev/cli/azd/pkg/output"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/exp/slices"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/cmd/cobra_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"slices"
"strconv"
"strings"

Expand All @@ -19,7 +20,6 @@ import (
"github.com/azure/azure-dev/cli/azd/pkg/tools"
"github.com/azure/azure-dev/cli/azd/pkg/tools/azcli"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
)

const cDocsFlagName = "docs"
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/internal/repository/initializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"

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

func Test_Initializer_Initialize(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/internal/telemetry/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"testing"
"time"

"github.com/azure/azure-dev/cli/azd/pkg/osutil"
"github.com/benbjohnson/clock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
)

// The tests in this file intentionally interacts with the filesystem (important implementation detail).
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/internal/telemetry/uploader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"fmt"
"math/rand"
"slices"
"strconv"
"testing"
"time"

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

type InMemoryItem struct {
Expand Down
3 changes: 2 additions & 1 deletion cli/azd/internal/tracing/baggage/baggage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
package baggage

import (
"go.opentelemetry.io/otel/attribute"
"golang.org/x/exp/maps"

"go.opentelemetry.io/otel/attribute"
)

// An immutable object safe for concurrent use.
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/account/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"fmt"
"log"
"os"
"slices"

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

// JSON document path locations for default subscription & location
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/account/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"net/http"
"slices"
"testing"

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

func Test_GetAccountDefaults(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/auth/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"io"
"log"
"net/http"
"slices"

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

const cLoginCmd = "azd auth login"
Expand Down
6 changes: 3 additions & 3 deletions cli/azd/pkg/azureutil/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"context"
"fmt"
"os"
"slices"
"strings"

"github.com/azure/azure-dev/cli/azd/pkg/account"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"golang.org/x/exp/slices"
)

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

slices.SortFunc(locations, func(a, b account.Location) bool {
slices.SortFunc(locations, func(a, b account.Location) int {
return strings.Compare(
strings.ToLower(a.RegionalDisplayName), strings.ToLower(b.RegionalDisplayName)) < 0
strings.ToLower(a.RegionalDisplayName), strings.ToLower(b.RegionalDisplayName))
})

// Allow the environment variable `AZURE_LOCATION` to control the default value for the location
Expand Down
3 changes: 2 additions & 1 deletion cli/azd/pkg/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
"regexp"
"strings"

"maps"

"github.com/azure/azure-dev/cli/azd/internal/tracing"
"github.com/azure/azure-dev/cli/azd/internal/tracing/fields"
"github.com/azure/azure-dev/cli/azd/pkg/config"
"github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext"
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
"github.com/joho/godotenv"
"golang.org/x/exp/maps"
)

// EnvNameEnvVarName is the name of the key used to store the envname property in the environment.
Expand Down
6 changes: 3 additions & 3 deletions cli/azd/pkg/infra/provisioning/bicep/bicep_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"math"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -39,7 +40,6 @@ import (
"github.com/benbjohnson/clock"
"github.com/drone/envsubst"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)

const DefaultModule = "main"
Expand Down Expand Up @@ -739,8 +739,8 @@ func (p *BicepProvider) findCompletedDeployments(
return nil, err
}

slices.SortFunc(deployments, func(x, y *armresources.DeploymentExtended) bool {
return x.Properties.Timestamp.After(*y.Properties.Timestamp)
slices.SortFunc(deployments, func(x, y *armresources.DeploymentExtended) int {
return x.Properties.Timestamp.Compare(*y.Properties.Timestamp)
})

// If hint is not provided, use the environment name as the hint
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/infra/provisioning/bicep/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"context"
"encoding/json"
"fmt"
"slices"
"strconv"

"github.com/azure/azure-dev/cli/azd/pkg/account"
"github.com/azure/azure-dev/cli/azd/pkg/azure"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/output"
"golang.org/x/exp/slices"

. "github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning"
)
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/pipeline/github_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/url"
"path/filepath"
"regexp"
"slices"
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
Expand All @@ -31,7 +32,6 @@ import (
"github.com/azure/azure-dev/cli/azd/pkg/tools/azcli"
"github.com/azure/azure-dev/cli/azd/pkg/tools/git"
"github.com/azure/azure-dev/cli/azd/pkg/tools/github"
"golang.org/x/exp/slices"
)

// GitHubScmProvider implements ScmProvider using GitHub as the provider
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/pipeline/pipeline_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"log"
"path/filepath"
"slices"
"strings"
"time"

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

type PipelineAuthType string
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"
"path/filepath"
"slices"
"strings"

"github.com/azure/azure-dev/cli/azd/internal"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning"
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
"github.com/blang/semver/v4"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
)

Expand Down
Loading

0 comments on commit 7a7f4e7

Please sign in to comment.