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
Comments
/cc @bcmills @jayconrod It looks like it is related to a leading BOM character in Reproduced using https://github.com/andyTsing/BEService/commit/07f4a7fdc545629d7e25a2fc88cffad67ad8ee0e
This is corrected by gofmt:
|
@andyTsing Thanks for the report! Does |
@toothrot Thank u,following your advice,when i do: go fmt ./router/Router.go,the problem was solved |
Glad to hear that @andyTsing! If you don't mind, let's re-open this issue to track improving |
This is probably a bug in the parser or loader somewhere. Note that the Go spec explicitly allows a leading BOM:
|
Curiously, this only manifests as an error in module mode:
|
Under normal circumstances, it is unlikely to encounter such errors... |
@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? |
Ok, I don't mind. I thought there had been no response recently, so I shut it down. Sorry |
How's progress? This issue means on Windows go fail in most of the cases when people use any common text editor. |
I found a solution good for me. that's #46290 you can try to cedit the golang source file func scanFiles(files []string, tags map[string]bool, explicitFiles bool) ([]string, []string, error) {
imports := make(map[string]bool)
testImports := make(map[string]bool)
numFiles := 0
Files:
for _, name := range files {
r, err := os.Open(name)
if err != nil {
return nil, nil, err
}
// I add these code to check if the source code is with BOM, if yes, drop the BOM
var fheader = make([]byte, 3)
_, err = r.Read(fheader)
if err != nil {
fmt.Println("error: ", err)
}
if fheader[0] == 0xef && fheader[1] == 0xbb && fheader[2] == 0xbf {
r.Seek(3, 0)
} else {
r.Seek(0, 0)
}
////////// edit end
var list []string
// if a file is with BOM, the return list is empty
data, err := ReadImports(r, false, &list)
r.Close()
if err != nil {
return nil, nil, fmt.Errorf("reading %s: %v", name, err)
}
// ... ...
} Hope this method is useful for you. |
Change https://golang.org/cl/325990 mentions this issue: |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat 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
The text was updated successfully, but these errors were encountered: