easyjson icon indicating copy to clipboard operation
easyjson copied to clipboard

easyjson doesn't work with vendored modules.

Open james-lawrence opened this issue 5 years ago • 19 comments

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?

james-lawrence avatar Jun 17 '20 00:06 james-lawrence

Hello. Can you give example?

GoWebProd avatar Jun 17 '20 07:06 GoWebProd

i'll see if i can spin up a minimal working example this weekend.

james-lawrence avatar Jun 17 '20 11:06 james-lawrence

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

Antonboom avatar Jul 09 '20 13:07 Antonboom

FYI: mockgen resolve this problem through --build_flags=-mod=mod.

Antonboom avatar Jul 09 '20 13:07 Antonboom

Faced the same issue

Snegirekk avatar Jul 10 '20 11:07 Snegirekk

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"

Arnold1 avatar Jul 18 '20 01:07 Arnold1

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.

mstoykov avatar Jul 24 '20 11:07 mstoykov

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

Arnold1 avatar Sep 03 '20 17:09 Arnold1

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

GoWebProd avatar Sep 04 '20 08:09 GoWebProd

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...

Arnold1 avatar Sep 08 '20 20:09 Arnold1

@GoWebProd any idea about that?

Arnold1 avatar Sep 11 '20 17:09 Arnold1

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 avatar Sep 16 '20 18:09 GoWebProd

@GoWebProd

What's about a real issue?

Antonboom avatar Sep 16 '20 19:09 Antonboom

@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.

GoWebProd avatar Sep 17 '20 10:09 GoWebProd

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

Antonboom avatar Sep 17 '20 14:09 Antonboom

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.

sankarp-kavach avatar Oct 22 '20 04:10 sankarp-kavach

importing _ "github.com/mailru/easyjson/gen" worked for me too... @sankarp-kavach you've got an extra easyjson in there

0x-2a avatar Dec 15 '20 15:12 0x-2a

@sankarp-kavach thanks for your workaround, its works for me too.

outdead avatar Dec 22 '20 18:12 outdead

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

bvcl avatar Aug 25 '21 06:08 bvcl