easyjson icon indicating copy to clipboard operation
easyjson copied to clipboard

"duplicate symbol" error when running easyjson on a file in the main package (go1.12)

Open roberth-k opened this issue 5 years ago • 5 comments

When running easyjson -all ./path/to/file.go on a file that's in package main, the program fails with the following output:

# command-line-arguments
2019/06/21 22:54:25 duplicate symbol runtime/debug.modinfo (types 30 and 30) in main and $WORK/b002/_pkg_.a(_go_.o)
Bootstrap failed: exit status 2

The ..._easyjson.go file will be present, but generated only up until the type EasyJSON_exporter_ ... declaration. The error disappears if the package is renamed to something other than main.

The issue can be reproduced with a minimal file such as:

package main

type MyStruct struct {
	MyField string `json:"my_field"`
}

I couldn't find caveats in the README about package main limitations, so I assume it's a bug. Running an identical test with go1.11.11 (no error) and go1.12.0 (error) suggests that this is specific to go1.12+. Travis only tests with 1.11, which is why this would not be showing up in CI.

Environment: go1.12.6 on macOS 10.14.5 easyjson version: da37f6c1e481

roberth-k avatar Jun 21 '19 22:06 roberth-k

easyjson parser and codegen based on reflection, so it wont works on package main files, because they cant be imported by parser.

I added this note to readme.

Fixing this requires to full rewrite parser from reflection to AST traversal, and this not in plan. See #1 for details

rvasily avatar Jun 26 '19 09:06 rvasily

Thanks for the clarification. I couldn't find anything in the go1.12.0 changelog to explain why it would stop working only at that point, but it could have been undefined behaviour to begin with.

It's a wild shot and likely not feasible, but I'm wondering if the package main could be re-written with a randomly generated package name (e.g. into a temporary directory) just for the duration of reflection.

roberth-k avatar Jun 26 '19 21:06 roberth-k

I think we can copy to separate dir with pkg renaming, this is very good idea. PR welcomed.

GoWebProd avatar Mar 26 '20 13:03 GoWebProd

It would be nice to add into error message that main package is not supported.

stokito avatar Sep 23 '21 17:09 stokito

This is ridiculous; easyjson can't handle main package...

rustyx avatar Dec 19 '21 15:12 rustyx