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

cmd/go: test Example with wrong type results in unclear error message #35284

Closed
gertcuykens opened this issue Oct 31, 2019 · 5 comments
Closed
Labels
FrozenDueToAge GoCommand cmd/go help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@gertcuykens
Copy link
Contributor

GO111MODULE="on"
create a new module

module github.com/t/crypto
go 1.14

Although I think modules have nothing to do with it

Create a test file that contains a Example, and deliberately give it the wrong function definition like so

func ExampleCipherBlock(b []byte) {
  fmt.Println("hello")
  // Output:
}
% go test -v
# github.com/t/crypto.test
/var/folders/dv/8tlwvjr91zjdyq4rk14lkkfm0000gn/T/go-build917091680/b001/_testmain.go:30:30: cannot use crypto.ExampleCipherBlock (type func([]byte)) as type func() in field value
FAIL    github.com/t/crypto [build failed]

Now do the same but make a other error like so

k
func ExampleCipherBlock() {
  fmt.Println("hello")
  // Output:
}
% go test -v
# github.com/t/crypto
block_test.go:10:1: expected declaration, found k
FAIL    github.com/t/crypto [setup failed]
gert@mac crypto % 

Second one is the kind of error messages I expect, the first one completely messes up vscode because there is no way vscode can figure out where to point to.

@jayconrod jayconrod changed the title cmd/go: ExampleTest(b []byte) results in completely different error message cmd/go: test Example with wrong type results in unclear error message Nov 1, 2019
@jayconrod jayconrod added GoCommand cmd/go NeedsFix The path to resolution is known, but the work has not been done. labels Nov 1, 2019
@jayconrod jayconrod added this to the Backlog milestone Nov 1, 2019
@jayconrod
Copy link
Contributor

In cmd/go/internal/load/test.go, we extract TestMain, Test, and Benchmark functions and check their signatures with checkTestFunc. We should do the same with examples but we extract those with go/doc.Examples and don't check the signatures.

The error should look like;

block_test.go:10:1: wrong signature for ExampleCipherBlock, must be: func ExampleCipherBlock()`

@jayconrod
Copy link
Contributor

cc @dmitshur

@mohamednizar
Copy link

mohamednizar commented Nov 2, 2019

@jayconrod , I whish to contribute to this issue. How can start work on this ? I have started working on this.

@jayconrod
Copy link
Contributor

Hey @mohamednizar, if you're interested in working on this, please follow the instructions in https://golang.org/doc/contribute.html for submitting a Gerrit CL or GitHub PR.

Any fix should include a small regression test in cmd/go/testdata/script. See the README there for information on writing tests.

@gopherbot
Copy link

Change https://golang.org/cl/211357 mentions this issue: go/doc: ignore example functions with arguments

@golang golang locked and limited conversation to collaborators Dec 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge GoCommand cmd/go help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants