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

go/format: format.Source errors on seemingly correct code; doesn't provide details #42172

Closed
jeanbza opened this issue Oct 23, 2020 · 2 comments

Comments

@jeanbza
Copy link
Member

jeanbza commented Oct 23, 2020

What version of Go are you using (go version)?

$ go version
go version go1.15.3 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/deklerk/.cache/go-build"
GOENV="/home/deklerk/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/deklerk/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/deklerk/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/deklerk/workspace/tools/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build692544628=/tmp/go-build -gno-record-gcc-switches"

What did you do?

	secondFormat, err := format.Source(firstPass)
	if err != nil {
		return nil, fmt.Errorf("failed to run second format on:\n%s\ngot err: %v", string(firstPass), err)
	}

As part of https://go-review.googlesource.com/c/tools/+/262018, on,

        {
        		ValuePos: 0,
        		Kind:     0,
        		Value:    "",
        	}

What did you expect to see?

{
	ValuePos: 0,
	Kind:     0,
	Value:    "",
}

What did you see instead?

    lsp_test.go:535: error converting command "gopls.fill_struct" to edits: error calling command.SuggestedFix: failed to run second format on:
        {
        		ValuePos: 0,
        		Kind:     0,
        		Value:    "",
        	}
        got err: 2:13: expected 1 expression (and 2 more errors)

My question: why does format.Source blow up on this code? And, what does expected 1 expression mean - isn't what I passed in an expression [1]? And, what are the other 2 errors?

1: I'm not particularly well versed on the nouns that comprise a programming language - sorry!

@stamblerre
Copy link
Contributor

You can think of an expression here as any AST node that implements ast.Expr. I often use http://goast.yuroyoro.net/ to inspect the Go AST and understand it better. In this case, you've written an ast.CompositeLit without the Type field before the curly brace (Lbrace), so the expression does not parse.

@jeanbza
Copy link
Member Author

jeanbza commented Oct 23, 2020

Thank you!

@jeanbza jeanbza closed this as completed Oct 23, 2020
@golang golang locked and limited conversation to collaborators Oct 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants