easyjson doesn't work with vendored modules.
it should. ;)
#277 was closed without actually resolving the issue. its unclear what the issue is, is it a bug in golang tooling? or in easyjson? the linked issue implies golang tooling since it was closed? if so is there a relavant golang issue tracking the problem?
Hello. Can you give example?
i'll see if i can spin up a minimal working example this weekend.
I faced the same problem.
Simple example
▶ tree
.
└── model
└── user.go
// model/user.go
package model
//go:generate easyjson --disallow_unknown_fields -output_filename=user_easyjson_gen.go
type User struct {
ID string
Email string
Age int
}
First time generation is ok
$ go version
go version go1.14 darwin/amd64
$ go mod init easyjsonexample
$ go get -u github.com/mailru/easyjson/...
$ go generate ./...
$ go mod vendor
▶ tree
.
├── go.mod
├── go.sum
├── model
│ ├── user.go
│ └── user_easyjson_gen.go
└── vendor
├── github.com
│ └── mailru
│ └── easyjson
│ ├── LICENSE
│ ...
└── modules.txt
Try to generate second time
$ go generate ./...
easyjson-bootstrap646749611.go:12:3: cannot find package "." in:
/private/tmp/easyjsonexample/vendor/github.com/mailru/easyjson/gen
Bootstrap failed: exit status 1
model/user.go:9: running "easyjson": exit status 1
FYI: mockgen resolve this problem through --build_flags=-mod=mod.
Faced the same issue
hi @GoWebProd I see the same issue.
what is the right way to vendor easyjson and what version of go can I use?
./request_easyjson.go:31:12: in.UnsafeFieldName undefined (type *jlexer.Lexer has no field or method UnsafeFieldName)
./request_easyjson.go:97:12: in.UnsafeFieldName undefined (type *jlexer.Lexer has no field or method UnsafeFieldName)
before go modules times, I used https://github.com/golang/dep the following packages got vendor:
[[projects]]
branch = "master"
digest = "1:d1c28806c67f4d859a28255b347e3ab5bc4a571475dbbf3df220f978bea0f8af"
name = "github.com/mailru/easyjson"
packages = [
".",
"buffer",
"gen",
"jlexer",
"jwriter",
]
pruneopts = "UT"
revision = "b2ccc519800e761ac8000b95e5d57c80a897ff9e"
The issue is probably because it tries to vendor a tagged version (in my case v0.7.1), but UnsafeFieldName seems to be added after that.
So changing to vendoring master works. Also releasing a new version will alleviate the issue.
hi @GoWebProd - I see same issue with go 1.15:
easyjson-bootstrap431139195.go:12:3: cannot find package "." in:
/go/src/github.com/blabla/app/app-ai/vendor/github.com/mailru/easyjson/gen
Bootstrap failed: exit status 1
format/code.go:3: running "easyjson": exit status 1
make: *** [Makefile:134: codegen] Error 1
how to fix it?
when I run: go get github.com/mailru/easyjson and run go mod vendor I can't see any eayjson source code...
the go.mod shows: github.com/mailru/easyjson v0.7.6 // indirect
Hello.
vendoring does't save generator code.
If you want to save generator code to vendor, you need to add to any source file:
import _ "github.com/mailru/easyjson/easyjson"
And after you can run:
go mod vendor
go build -o bin/easyjson github.com/mailru/easyjson/easyjson
hi @GoWebProd,
im getting:
code.go:3:8: import "github.com/mailru/easyjson/easyjson" is a program, not an importable package when running go generate ./format/...
my ./format/code.go file looks like this:
package format
import json "encoding/json"
import _ "github.com/mailru/easyjson/easyjson"
//go:generate easyjson -all $GOFILE
//easyjson:json
type foo struct {
Val string
ID int `json:"id"`
}
I call go generate every time before go build -mod vendor...
@GoWebProd any idea about that?
Hello, yes, create separate (not used module) and add comment to begin of file:
// +build some_tag
Go do not build this file but vendor packages from it
@GoWebProd
What's about a real issue?
@GoWebProd
What's about a real issue?
This not issue. Go mod don't vendoring packages that not program not using. Also, there is no way to adequately vendor the application.
I don't mean @Arnold1 problem, but about https://github.com/mailru/easyjson/issues/293#issuecomment-656142950
The vendor receives the packages that easyjson uses in its generated files. They should not break the work of the binary, which, in theory, has nothing to do with them.
Earlier I already gave an example of another tool that works great with vendor: https://github.com/golang/mock
Hello, yes, create separate (not used module) and add comment to begin of file:
// +build some_tag
Go do not build this file but vendor packages from it
@GoWebProd Can you give an example of this ? I do not understand.
I have the following structure:
my-root
|
|--- go.mod
|--- go.sum
|--- my-package
| |--- my-model.go // contains the structs with the easyjson build flags as comments above the struct definition
|--- vendor
Now where should I create the not used module that you mentioned ? If I add the
import _ "github.com/mailru/easyjson/easyjson"
to the my-model.go file, I get the same error as in comment https://github.com/mailru/easyjson/issues/293#issuecomment-689131283 mentioned by @Arnold1 If I create a new module outside of my-root that will not help solve the issue here. What is the correct fix/workaround now ?
Update:
I was able to resolve my problem by adding the following line to the my-model.go file.
import _ "github.com/mailru/easyjson/gen"
I hope that this is a valid workaround.
importing _ "github.com/mailru/easyjson/gen" worked for me too... @sankarp-kavach you've got an extra easyjson in there
@sankarp-kavach thanks for your workaround, its works for me too.
After add importing _ "github.com/mailru/easyjson/gen" i was still getting same error as https://github.com/mailru/easyjson/issues/293#issuecomment-689131283. I could solve it by going to .../vendor/github.com/mailru/easyjson/easyjson and changing package main for package easyjson