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: imports fail to resolve in module mode when file contains a leading BOM #35726

Open
andyTsing opened this issue Nov 21, 2019 · 10 comments

Comments

@andyTsing
Copy link

@andyTsing andyTsing commented Nov 21, 2019

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

$ go version
go version go1.13.4 windows/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
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\andyTsing\AppData\Local\go-build
set GOENV=C:\Users\andyTsing\AppData\Roaming\go\env  
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\andyTsing\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=D:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=D:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\Temp\TXSSPrinter\BEService\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\ANDYTS~1\AppData\Local\Temp\go-build458686182=/tmp/go-build -gno-record-gcc-switches

What did you do?

D:\Temp\TXSSPrinter\BEService>go run .
D:\Temp\TXSSPrinter\BEService>go build

https://github.com/andyTsing/BEService
This is a single-module, multi-package project that worked fine in the early 1.12 release and has had problems since the upgrade to 1.3.
Also, packages that also belong to different subdirectories, some of which have normal imports and some of which have abnormal imports, when I comment out the API section, are
The normal

What did you expect to see?

A successful retrieval of a dependency.

What did you see instead?

D:\Temp\TXSSPrinter\BEService>go run .
build github.com/andyTsing/BEService: cannot find module for path github.com/andyTsing/BEService/api

D:\Temp\TXSSPrinter\BEService>go build
build github.com/andyTsing/BEService: cannot find module for path github.com/andyTsing/BEService/api

@toothrot toothrot changed the title build github.com/andyTsing/BEService: cannot find module for path github.com/andyTsing/BEService/api cmd/go: difficult to diagnose error when building github.com/andyTsing/BEService Nov 22, 2019
@toothrot toothrot added this to the Backlog milestone Nov 22, 2019
@toothrot

This comment has been minimized.

Copy link
Contributor

@toothrot toothrot commented Nov 22, 2019

/cc @bcmills @jayconrod It looks like it is related to a leading BOM character in router/Router.go.

Reproduced using andyTsing/BEService@07f4a7f

$ xxd router/Router.go 
00000000: efbb bf70 6163 6b61 6765 2072 6f75 7465  ...package route

This is corrected by gofmt:

$ xxd router/Router.go 
00000000: 7061 636b 6167 6520 726f 7574 6572 0a0a  package router..
@toothrot

This comment has been minimized.

Copy link
Contributor

@toothrot toothrot commented Nov 22, 2019

@andyTsing Thanks for the report! Does go fmt ./router/Router.go resolve this issue on Windows?

@andyTsing

This comment has been minimized.

Copy link
Author

@andyTsing andyTsing commented Nov 23, 2019

@toothrot Thank u,following your advice,when i do: go fmt ./router/Router.go,the problem was solved

@andyTsing andyTsing closed this Nov 23, 2019
@dmitshur

This comment has been minimized.

Copy link
Member

@dmitshur dmitshur commented Nov 23, 2019

Glad to hear that @andyTsing!

If you don't mind, let's re-open this issue to track improving cmd/go to report a better error message when this kind of a situation happens.

@dmitshur dmitshur reopened this Nov 23, 2019
@andyTsing

This comment has been minimized.

Copy link
Author

@andyTsing andyTsing commented Nov 24, 2019

@dmitshur OK,It's opened again,I have submitted the correct version. As it is the first time to use github, I am not sure whether @07f4a7f is still intact.
To reproduce the error scenario, manually add the BOM prefix to the router/ router. Go file as described by @toothrot

@bcmills

This comment has been minimized.

Copy link
Member

@bcmills bcmills commented Nov 25, 2019

This is probably a bug in the parser or loader somewhere. Note that the Go spec explicitly allows a leading BOM:

For compatibility with other tools, a compiler may ignore a UTF-8-encoded byte order mark (U+FEFF) if it is the first Unicode code point in the source text. A byte order mark may be disallowed anywhere else in the source.

@bcmills

This comment has been minimized.

Copy link
Member

@bcmills bcmills commented Nov 25, 2019

Curiously, this only manifests as an error in module mode:

_gopath/src/github.com/andyTsing/BEService$ git checkout 07f4a7f
[…]

_gopath/src/github.com/andyTsing/BEService$ GO111MODULE=off go build .

_gopath/src/github.com/andyTsing/BEService$ GO111MODULE=on go build .
router/Router.go:13:2: cannot find package
@bcmills bcmills added the modules label Nov 25, 2019
@bcmills bcmills changed the title cmd/go: difficult to diagnose error when building github.com/andyTsing/BEService cmd/go: imports fail to resolve in module mode when file contains a leading BOM Nov 25, 2019
@andyTsing

This comment has been minimized.

Copy link
Author

@andyTsing andyTsing commented Nov 26, 2019

Under normal circumstances, it is unlikely to encounter such errors...
Later, I recalled that the error was probably caused by opening the.go file using Windows' noe.exe,Windows' note.exe processes utf8 format, attaching BOM,The process of the second round made me pay attention to this point. Otherwise, this mistake would be hard to find. Fortunately, @toothrot has rich experience and found the reason.

@andyTsing andyTsing closed this Dec 2, 2019
@dmitshur

This comment has been minimized.

Copy link
Member

@dmitshur dmitshur commented Dec 2, 2019

@andyTsing Why close this issue? We'd like to improve the go command to report a better error message in this rare situation. Having this issue open is how we can track that work. Do you mind if I re-open it?

@dmitshur dmitshur reopened this Dec 2, 2019
@andyTsing

This comment has been minimized.

Copy link
Author

@andyTsing andyTsing commented Dec 2, 2019

Ok, I don't mind. I thought there had been no response recently, so I shut it down. Sorry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.