Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/go/analysis/analysistest: analysistest.RunWithSuggestedFixes doesn't fail when a golden file is provided without suggested fixes #71130

Closed
ldez opened this issue Jan 5, 2025 · 2 comments
Labels
Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@ldez
Copy link

ldez commented Jan 5, 2025

Go version

go version go1.23.4 linux/amd64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/ldez/.cache/go-build'
GOENV='/home/ldez/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/ldez/sources/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/ldez/sources/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.4'
GODEBUG=''
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/ldez/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/ldez/sources/golangci/sandbox/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2319871150=/tmp/go-build -gno-record-gcc-switches'

What did you do?

In the following example, the suggested fixes are not created for all reports (this is expected).

The golden files and the sources are different.

The Analyzer implementation should be considered as bugged: i == 0 should be i == 1.

When you run the test (analysistest.RunWithSuggestedFixes), it doesn't fail because the golden file is only checked if there are suggested fixes.

This hides the bug inside the Analyzer implementation.

.
├── go.mod
├── go.sum
├── main.go
├── main_test.go
└── testdata
    └── src
        └── foo
            ├── foo.go
            └── foo.go.golden
main.go
package main

import (
	"strings"

	"golang.org/x/tools/go/analysis"
	"golang.org/x/tools/go/analysis/singlechecker"
)

var Analyzer = &analysis.Analyzer{
	Name: "example",
	Doc:  "foo",
	Run:  run,
}

func run(pass *analysis.Pass) (interface{}, error) {
	for _, file := range pass.Files {
		for _, group := range file.Comments {
			for i, comment := range group.List {
				originalText := comment.Text
				newText := strings.ReplaceAll(originalText, "foo", "bar")

				if originalText == newText {
					continue
				}

				diagnostic := analysis.Diagnostic{
					Pos:     comment.Pos(),
					Message: "Word detected",
				}

				if i == 0 {
					diagnostic.SuggestedFixes = append(diagnostic.SuggestedFixes, analysis.SuggestedFix{
						Message: "Replace 'foo' with 'bar'",
						TextEdits: []analysis.TextEdit{{
							Pos:     comment.Pos(),
							End:     comment.End(),
							NewText: []byte(newText),
						}},
					})
				}

				pass.Report(diagnostic)
			}
		}
	}

	return nil, nil
}

func main() {
	singlechecker.Main(Analyzer)
}
main_test.go
package main

import (
	"testing"

	"golang.org/x/tools/go/analysis/analysistest"
)

func TestAnalyzer(t *testing.T) {
	analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), Analyzer, "foo")
}
testdata/src/foo/foo.go
package foo

import "fmt"

// want +3 "Word detected"

// This is an example.
// Here is a word foo.
func _() {
	fmt.Println("foo")
}
testdata/src/foo/foo.go.golden
package foo

import "fmt"

// want +3 "Word detected"

// This is an example.
// Here is a word bar.
func _() {
	fmt.Println("foo")
}

What did you see happen?

The test doesn't fail.

What did you expect to see?

A failure of the test.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Jan 5, 2025
@gopherbot gopherbot added this to the Unreleased milestone Jan 5, 2025
@ldez
Copy link
Author

ldez commented Jan 5, 2025

Duplicate of #47128

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants