Skip to content

Commit f25177d

Browse files
vhvb1989Copilot
andcommitted
Enforce go fix in CI and apply modernizations
Add go fix -diff as a quality gate across all Go modules (azd core and extensions). This ensures code stays up to date with modern Go idioms such as interface{} -> any, range-over-int loops, maps.Copy, slices.Contains, min/max builtins, and more. Changes: - Apply go fix ./... to azd core and all 6 extensions (307 files) - Add go-fix job to reusable lint-go.yml workflow - Add go fix -diff check to mage preflight (step 2) - Update CONTRIBUTING.md and AGENTS.md with go fix docs - Remove unused Ptr helpers that were inlined by go fix - Refactor ioc/container.go to avoid go fix false positive with unexported types from the container package Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent cfaadb6 commit f25177d

File tree

307 files changed

+1845
-2027
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

307 files changed

+1845
-2027
lines changed

.github/workflows/lint-go.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,23 @@ jobs:
3939
version: ${{ inputs.golangci-lint-version }}
4040
args: -v --timeout 10m0s
4141
working-directory: ${{ inputs.working-directory }}
42+
43+
go-fix:
44+
runs-on: ubuntu-latest
45+
steps:
46+
- uses: actions/setup-go@v6
47+
with:
48+
go-version: ${{ inputs.go-version }}
49+
- uses: actions/checkout@v4
50+
- name: Check for go fix suggestions
51+
working-directory: ${{ inputs.working-directory }}
52+
run: |
53+
DIFF=$(go fix -diff ./... 2>/dev/null)
54+
if [ -n "$DIFF" ]; then
55+
echo "::error::go fix found code that should be modernized."
56+
echo "Run 'go fix ./...' locally and commit the changes."
57+
echo ""
58+
echo "$DIFF"
59+
exit 1
60+
fi
61+
echo "No go fix suggestions — code is up to date."

cli/azd/AGENTS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ When writing tests, prefer table-driven tests. Use testify/mock for mocking.
7373

7474
```bash
7575
gofmt -s -w .
76+
go fix ./...
7677
golangci-lint run ./...
7778
cspell lint "**/*.go" --relative --config ./.vscode/cspell.yaml --no-progress
7879
../../eng/scripts/copyright-check.sh . --fix
@@ -81,6 +82,8 @@ cspell lint "**/*.go" --relative --config ./.vscode/cspell.yaml --no-progress
8182
- **Line length**: 125 chars max for Go (enforced by `lll` linter); no limit for Markdown
8283
- **Spelling**: Add technical terms to `cli/azd/.vscode/cspell.yaml` overrides
8384
- **Copyright**: All Go files need the Microsoft header (handled by copyright-check.sh)
85+
- **Code modernization**: `go fix ./...` applies automatic modernizations (e.g. `interface{}``any`,
86+
loop simplifications, use of `slices`/`maps` packages). CI enforces this check.
8487

8588
### Avoiding Unrelated go.mod/go.sum Changes
8689

cli/azd/CONTRIBUTING.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ Run linter (install [golangci-lint](https://golangci-lint.run/welcome/install/#l
8080
golangci-lint run ./...
8181
```
8282

83+
Run code modernization check ([go fix](https://go.dev/blog/gofix)):
84+
85+
```bash
86+
go fix -diff ./...
87+
```
88+
89+
If `go fix -diff` reports any changes, apply them with `go fix ./...` and commit the result.
90+
CI enforces this check — PRs with pending `go fix` suggestions will fail the lint workflow.
91+
8392
> Note: On Windows you may need to add `C:\Program Files\Git\usr\bin` to `%PATH%`
8493
8594
### Debugging (with VSCode)

cli/azd/cmd/auto_install.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"log"
1212
"os"
13+
"slices"
1314
"strings"
1415

1516
"github.com/azure/azure-dev/cli/azd/internal"
@@ -202,10 +203,8 @@ func isBuiltInCommand(rootCmd *cobra.Command, commandName string) bool {
202203
return true
203204
}
204205
// Also check aliases
205-
for _, alias := range cmd.Aliases {
206-
if alias == commandName {
207-
return true
208-
}
206+
if slices.Contains(cmd.Aliases, commandName) {
207+
return true
209208
}
210209
}
211210

@@ -218,10 +217,8 @@ func hasSubcommand(cmd *cobra.Command, name string) bool {
218217
if sub.Name() == name {
219218
return true
220219
}
221-
for _, alias := range sub.Aliases {
222-
if alias == name {
223-
return true
224-
}
220+
if slices.Contains(sub.Aliases, name) {
221+
return true
225222
}
226223
}
227224
return false

cli/azd/cmd/auto_install_integration_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package cmd
55

66
import (
77
"os"
8+
"slices"
89
"testing"
910

1011
"github.com/azure/azure-dev/cli/azd/internal"
@@ -182,12 +183,7 @@ func TestAgentDetectionIntegration(t *testing.T) {
182183

183184
// containsNoPromptFalse checks if args contain --no-prompt=false
184185
func containsNoPromptFalse(args []string) bool {
185-
for _, arg := range args {
186-
if arg == "--no-prompt=false" {
187-
return true
188-
}
189-
}
190-
return false
186+
return slices.Contains(args, "--no-prompt=false")
191187
}
192188

193189
// clearAgentEnvVarsForTest clears all environment variables that could trigger agent detection.

cli/azd/cmd/hooks.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"fmt"
99

10-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
1110
"github.com/azure/azure-dev/cli/azd/cmd/actions"
1211
"github.com/azure/azure-dev/cli/azd/internal"
1312
"github.com/azure/azure-dev/cli/azd/pkg/environment"
@@ -258,7 +257,7 @@ func (hra *hooksRunAction) execHook(
258257

259258
// Always run in interactive mode for 'azd hooks run', to help with testing/debugging
260259
runOptions := &tools.ExecOptions{
261-
Interactive: to.Ptr(true),
260+
Interactive: new(true),
262261
}
263262

264263
err := hooksRunner.RunHooks(ctx, hookType, runOptions, commandName)

cli/azd/cmd/init.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/azure/azure-dev/cli/azd/internal/agent"
2020
"github.com/azure/azure-dev/cli/azd/internal/agent/consent"
2121
"github.com/azure/azure-dev/cli/azd/internal/agent/feedback"
22-
"github.com/azure/azure-dev/cli/azd/internal/agent/tools/common"
2322
"github.com/azure/azure-dev/cli/azd/internal/repository"
2423
"github.com/azure/azure-dev/cli/azd/internal/tracing"
2524
"github.com/azure/azure-dev/cli/azd/internal/tracing/fields"
@@ -414,7 +413,7 @@ func (i *initAction) initAppWithAgent(ctx context.Context) error {
414413
ServerName: "*",
415414
Operation: consent.OperationTypeTool,
416415
Annotations: mcp.ToolAnnotation{
417-
ReadOnlyHint: common.ToPtr(true),
416+
ReadOnlyHint: new(true),
418417
},
419418
},
420419
)

cli/azd/cmd/middleware/error.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ func promptForErrorHandlingConsent(
518518
Message: message,
519519
HelpMessage: helpMessage,
520520
Choices: choices,
521-
EnableFiltering: uxlib.Ptr(false),
521+
EnableFiltering: new(false),
522522
DisplayCount: len(choices),
523523
})
524524

@@ -566,7 +566,7 @@ func (e *ErrorMiddleware) promptExplanationWithConsent(ctx context.Context) (str
566566
" Edit permissions anytime by running %s.",
567567
output.WithHighLightFormat("azd mcp consent")),
568568
Choices: choices,
569-
EnableFiltering: uxlib.Ptr(false),
569+
EnableFiltering: new(false),
570570
DisplayCount: len(choices),
571571
})
572572

@@ -582,8 +582,8 @@ func (e *ErrorMiddleware) promptExplanationWithConsent(ctx context.Context) (str
582582
selected := choices[*choiceIndex].Value
583583

584584
// Handle "always" variants — save to config and return the scope
585-
if strings.HasPrefix(selected, "always.") {
586-
scope := strings.TrimPrefix(selected, "always.")
585+
if after, ok := strings.CutPrefix(selected, "always."); ok {
586+
scope := after
587587
configKey := configPrefix + "." + scope
588588
if err := userConfig.Set(configKey, "allow"); err != nil {
589589
return "", fmt.Errorf("failed to set config %s: %w", configKey, err)
@@ -615,7 +615,7 @@ func (e *ErrorMiddleware) promptForFixGuidance(ctx context.Context) (bool, error
615615
selector := uxlib.NewSelect(&uxlib.SelectOptions{
616616
Message: "Do you want to generate step-by-step fix guidance?",
617617
Choices: choices,
618-
EnableFiltering: uxlib.Ptr(false),
618+
EnableFiltering: new(false),
619619
DisplayCount: len(choices),
620620
})
621621

@@ -686,7 +686,7 @@ func promptUserForSolution(ctx context.Context, solutions []string, agentName st
686686
Message: fmt.Sprintf("Allow %s to fix the error?", agentName),
687687
HelpMessage: "Select a suggested fix, or let AI decide",
688688
Choices: choices,
689-
EnableFiltering: uxlib.Ptr(false),
689+
EnableFiltering: new(false),
690690
DisplayCount: len(choices),
691691
})
692692

cli/azd/cmd/usage_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func usageSnapshot(t *testing.T, cmd *cobra.Command) {
6868
})
6969
}
7070

71-
func resolveTemplate(text string, data interface{}) (string, error) {
71+
func resolveTemplate(text string, data any) (string, error) {
7272
finalBuffer := &bytes.Buffer{}
7373
t := template.New("resolve template with command")
7474
template.Must(t.Parse(text))

cli/azd/cmd/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type CmdAnnotations map[string]string
2727
// CmdCalledAs provides access to the cmd.CalledAs() value through dependency injection
2828
type CmdCalledAs string
2929

30-
type Asker func(p survey.Prompt, response interface{}) error
30+
type Asker func(p survey.Prompt, response any) error
3131

3232
func serviceNameWarningCheck(console input.Console, serviceNameFlag string, commandName string) {
3333
if serviceNameFlag == "" {

0 commit comments

Comments
 (0)