Skip to content

Commit

Permalink
Adopt go 1.21
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 committed Aug 8, 2023
1 parent 6d0dc5c commit 2cb90f5
Show file tree
Hide file tree
Showing 27 changed files with 44 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cli-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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
Expand Down
4 changes: 4 additions & 0 deletions cli/azd/ci-build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ param(
[switch] $BuildRecordMode
)

# 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"

# Remove any previously built binaries
go clean

Expand Down
4 changes: 4 additions & 0 deletions cli/azd/ci-test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ param(
[string] $IntegrationTestCoverageDir = 'cover-int'
)

# 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"

$ErrorActionPreference = 'Stop'

$gopath = go env GOPATH
Expand Down
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 All @@ -38,7 +39,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 @@ -715,8 +715,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
6 changes: 3 additions & 3 deletions cli/azd/pkg/prompt/prompter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"slices"
"strings"

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

type LocationFilterPredicate func(loc account.Location) bool
Expand Down Expand Up @@ -112,8 +112,8 @@ func (p *DefaultPrompter) PromptResourceGroup(ctx context.Context) (string, erro
return "", fmt.Errorf("listing resource groups: %w", err)
}

slices.SortFunc(groups, func(a, b azcli.AzCliResource) bool {
return strings.Compare(a.Name, b.Name) < 0
slices.SortFunc(groups, func(a, b azcli.AzCliResource) int {
return strings.Compare(a.Name, b.Name)
})

choices := make([]string, len(groups)+1)
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/tools/azcli/container_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net/http"
"net/url"
"slices"
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
Expand All @@ -17,7 +18,6 @@ import (
"github.com/azure/azure-dev/cli/azd/pkg/azure"
"github.com/azure/azure-dev/cli/azd/pkg/httputil"
"github.com/azure/azure-dev/cli/azd/pkg/tools/docker"
"golang.org/x/exp/slices"
)

type dockerCredentials struct {
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/test/functional/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"path"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
"time"
Expand All @@ -43,7 +44,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"golang.org/x/exp/slices"
)

// The current running configuration for the test suite.
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/test/recording/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
"fmt"
"io"
"io/fs"
"log/slog"
"net"
"net/http"
"net/url"
"strings"

"golang.org/x/exp/slog"
"gopkg.in/dnaeon/go-vcr.v3/recorder"
)

Expand Down
2 changes: 1 addition & 1 deletion cli/azd/test/recording/recording.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -24,7 +25,6 @@ import (
"testing"
"time"

"golang.org/x/exp/slog"
"gopkg.in/dnaeon/go-vcr.v3/cassette"
"gopkg.in/dnaeon/go-vcr.v3/recorder"
"gopkg.in/yaml.v3"
Expand Down
2 changes: 1 addition & 1 deletion eng/pipelines/templates/steps/setup-go.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
GoVersion: 1.20.3
GoVersion: 1.21.0
Condition: succeeded()

steps:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/azure/azure-dev

go 1.20
go 1.21

require (
github.com/AlecAivazis/survey/v2 v2.3.2
Expand Down
Loading

0 comments on commit 2cb90f5

Please sign in to comment.