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.Node internal error (1:13: expected ';', found '-') #30950

Closed
bronze1man opened this issue Mar 20, 2019 · 2 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@bronze1man
Copy link
Contributor

bronze1man commented Mar 20, 2019

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

$ go version
go version go1.11.4 darwin/amd64

Does this issue reproduce with the latest release?

may be, It reproduce in https://play.golang.org/

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/a/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/a/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/m9/qtbxkp6s3p96fk54rln7qhj80000gp/T/go-build885997839=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

https://play.golang.org/p/yGvWuAzSUTB

package main

import (
	"bytes"
	"go/format"
	"fmt"
	"go/token"
	"go/parser"
)

func main(){
	fs:=token.NewFileSet()
	f,err:=parser.ParseFile(fs,"1.go",`package main

import (
	"fmt"
	"strconv"
)
func main(){
	fmt.Println(strconv.Itoa(1))
}`,parser.AllErrors)
	if err!=nil{
		panic(err)
	}
	f.Name.Name = "main-go"
	_buf:=&bytes.Buffer{}
	err=format.Node(_buf,fs,f)
	if err!=nil{
		panic(err)
	}
	fmt.Println(_buf.String())
}

What did you expect to see?

Some good formated error says that the bad package name.

What did you see instead?

format.Node internal error (1:13: expected ';', found '-')
@bronze1man bronze1man changed the title format.Node: internal error (1:13: expected ';', found '-') go/format: format.Node internal error (1:13: expected ';', found '-') Mar 20, 2019
@ianlancetaylor
Copy link
Contributor

CC @griesemer

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 20, 2019
@ianlancetaylor ianlancetaylor added this to the Go1.13 milestone Mar 20, 2019
@griesemer
Copy link
Contributor

The reason is that the program in question explicitly changes the package name from main to main-go, and main-go is not a valid identifier.

One might expect the formatter not to care, but (as an implementation detail) it currently parses the source a 2nd time, at which point the parser complains.

Generally, the various go/* libraries may behave in undefined ways if the source or the AST is incorrect. Here, a valid AST is explicitly made incorrect. Don't do that.

Working as intended. Closing.

@golang golang locked and limited conversation to collaborators Mar 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants