8000 Fix //go:generate code generation to match go1.16 by utamori · Pull Request #1300 · ent/ent · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix //go:generate code generation to match go1.16 #1300

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

Merged
merged 13 commits into from
Mar 9, 2021
Merged

Fix //go:generate code generation to match go1.16 #1300

merged 13 commits into from
Mar 9, 2021

Conversation

utamori
Copy link
Contributor
@utamori utamori commented Mar 3, 2021

Starting with go1.16, the current generate.go statement will result in an error.

To make it behave the same as before, we need to change the description to the following.

- //go:generate go run entgo.io/ent/cmd/ent generate ./schema
+ //go:generate go install entgo.io/ent/cmd/ent@latest
+ //go:generate ent generate ./schema

Reproducing errors

❯ go version       
go version go1.16 darwin/amd64

Global installation of go1.16 style (not reflected in go.mod)

❯ go install entgo.io/ent/cmd/ent@latest
❯ ent init User

edit ent/schema/user.go

package schema

import (
	"entgo.io/ent"
	"entgo.io/ent/schema/field"
)

// Fields of the User.
func (User) Fields() []ent.Field {
	return []ent.Field{
		field.Int("age").
			Positive(),
		field.String("name").
			Default("unknown"),
	}
}

Since go1.16, go.mod is not automatically updated, so you need to do go mod tidy more often

❯ go mod tidy
go: finding module for package entgo.io/ent/schema/field
go: finding module for package entgo.io/ent
go: found entgo.io/ent in entgo.io/ent v0.6.0
go: found entgo.io/ent/schema/field in entgo.io/ent v0.6.0

When I try to generate the code, I get the following error:

❯ go generate ./ent
/Users/user/go/pkg/mod/entgo.io/ent@v0.6.0/entc/gen/func.go:22:2: missing go.sum entry for module providing package github.com/go-openapi/inflect (imported by entgo.io/ent/entc/gen); to add:
        go get entgo.io/ent/entc/gen@v0.6.0
/Users/user/go/pkg/mod/entgo.io/ent@v0.6.0/cmd/internal/printer/printer.go:16:2: missing go.sum entry for module providing package github.com/olekukonko/tablewriter (imported by entgo.io/ent/cmd/internal/printer); to add:
        go get entgo.io/ent/cmd/internal/printer@v0.6.0
/Users/user/go/pkg/mod/entgo.io/ent@v0.6.0/cmd/internal/base/base.go:25:2: missing go.sum entry for module providing package github.com/spf13/cobra (imported by entgo.io/ent/cmd/ent); to add:
        go get entgo.io/ent/cmd/ent@v0.6.0
/Users/user/go/pkg/mod/entgo.io/ent@v0.6.0/entc/load/load.go:29:2: missing go.sum entry for module providing package golang.org/x/tools/go/packages (imported by entgo.io/ent/cmd/internal/base); to add:
        go get entgo.io/ent/cmd/internal/base@v0.6.0
/Users/user/go/pkg/mod/entgo.io/ent@v0.6.0/entc/gen/graph.go:24:2: missing go.sum entry for module providing package golang.org/x/tools/imports (imported by entgo.io/ent/entc/gen); to add:
        go get entgo.io/ent/entc/gen@v0.6.0
ent/generate.go:3: running "go": exit status 1

Same result with go generate ./...

❯ go generate ./...
/Users/user/go/pkg/mod/entgo.io/ent@v0.6.0/entc/gen/func.go:22:2: missing go.sum entry for module providing package github.com/go-openapi/inflect (imported by entgo.io/ent/entc/gen); to add:
        go get entgo.io/ent/entc/gen@v0.6.0
/Users/user/go/pkg/mod/entgo.io/ent@v0.6.0/cmd/internal/printer/printer.go:16:2: missing go.sum entry for module providing package github.com/olekukonko/tablewriter (imported by entgo.io/ent/cmd/internal/printer); to add:
        go get entgo.io/ent/cmd/internal/printer@v0.6.0
/Users/user/go/pkg/mod/entgo.io/ent@v0.6.0/cmd/internal/base/base.go:25:2: missing go.sum entry for module providing package github.com/spf13/cobra (imported by entgo.io/ent/cmd/ent); to add:
        go get entgo.io/ent/cmd/ent@v0.6.0
/Users/user/go/pkg/mod/entgo.io/ent@v0.6.0/entc/load/load.go:29:2: missing go.sum entry for module providing package golang.org/x/tools/go/packages (imported by entgo.io/ent/cmd/internal/base); to add:
        go get entgo.io/ent/cmd/internal/base@v0.6.0
/Users/user/go/pkg/mod/entgo.io/ent@v0.6.0/entc/gen/graph.go:24:2: missing go.sum entry for module providing package golang.org/x/tools/imports (imported by entgo.io/ent/entc/gen); to add:
        go get entgo.io/ent/entc/gen@v0.6.0
ent/generate.go:3: running "go": exit status 1

Reference

Go 1.16 Release Notes: https://golang.org/doc/go1.16

go install now accepts arguments with version suffixes (for example, go install example.com/cmd@v1.0.0). This causes go install to build and install packages in module-aware mode, ignoring the go.mod file in the current directory or any parent directory, if there is one. This is useful for installing executables without affecting the dependencies of the main module.

go install, with or without a version suffix (as described above), is now the recommended way to build and install packages in module mode. go get should be used with the -d flag to adjust the current module's dependencies without building packages, and use of go get to build and install packages is deprecated. In a future release, the -d flag will always be enabled.

@rotemtam
Copy link
Collaborator
rotemtam commented Mar 3, 2021

Hey @uta-mori,
Can you please provide more details? What error are you seeing? How is it relevant to changes in go1.16?
Thanks

@utamori
Copy link
Contributor Author
utamori commented Mar 4, 2021

@rotemtam
ok. I've added the error and the changes in go1.16.

@rotemtam
Copy link
Collaborator
rotemtam commented Mar 7, 2021

Thanks for updating the issue @uta-mori.
In general, it is less advised to use global installs, since some teams have multiple ent projects, and don't all use the same version. SInce the gen tool must be the same version as the ent library you are using, this can raise many day to day issues.

I've tried to reproduce your issue with go get and it seems like codegen works:

go116 go mod init repro
go: creating new go.mod: module reprogo116 go get entgo.io/ent/cmd/ent@latest
go get: added entgo.io/ent v0.6.0go116 go run entgo.io/ent/cmd/ent init Usergo116 go generate ./...go116 tree ./ent
./ent
├── client.go
├── config.go
├── context.go
├── ent.go
├── enttest
│   └── enttest.go
├── generate.go
├── hook
│   └── hook.go
├── migrate
│   ├── migrate.go
│   └── schema.go
├── mutation.go
├── predicate
│   └── predicate.go
├── runtime
│   └── runtime.go
├── runtime.go
├── schema
│   └── user.go
├── tx.go
├── user
│   ├── user.go
│   └── where.go
├── user.go
├── user_create.go
├── user_delete.go
├── user_query.go
└── user_update.go

7 directories, 22 files

WDYT?

@rotemtam
Copy link
Collaborator
rotemtam commented Mar 7, 2021

Re-reading your issue description, I'm able to reproduce the issue, even without global installed binaries. Running go mod tidy will cause the error you described.

This is related to the discussion in golang/go#43653. (cc @marwan-at-work )

To fix the issue, we can add this import to the generate.go file:

import _ "entgo.io/ent/cmd/ent"

This makes the package explicitly depend on the ent command line and its transitive dependencies, in which case go mod tidy will not remove them.

@uta-mori , would you like to contribute a fix to change the generated code in this spirit?

@a8m
Copy link
Member
a8m commented Mar 8, 2021

Thanks for your contribution @uta-mori 🙏

Any update on this?

@utamori
Copy link
Contributor Author
utamori commented Mar 8, 2021

thank you

added import _ "entgo.io/ent/cmd/ent" to generate.go


go get will default to behavior with the -d flag in the future.
This is a behavior that changes only go.mod.

https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them
The -d flag instructs get not to build or install packages. get will only update go.mod and download source code needed to build packages.

Building and installing packages with get is deprecated. In a future release, the -d flag will be enabled by default, and ‘go get’ will be only be used to adjust dependencies of the current module. To install a package using dependencies from the current module, use ‘go install’. To install a package ignoring the current module, use ‘go install’ with an @Version suffix like “@latest” after each argument.

@marwan-at-work
Copy link
Contributor

@uta-mori @a8m why not instead of changing:

//go:generate go run entgo.io/ent/cmd/ent generate ./schema

To

+ //go:generate go install entgo.io/ent/cmd/ent@latest
+ //go:generate ent generate ./schema

Which would only work in Go1.16 and fail for users who have not upgraded yet. Why don't we do this:

//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate ./schema

This would work for both 1.16 and previous Go versions and more importantly: it would ensure the version compatibility between the library ent and the cmd line ent.

@utamori
Copy link
Contributor Author
utamori commented Mar 8, 2021

@marwan-at-work
I did not know that. This is the best one. Thank you!

@a8m
Copy link
Member
a8m commented Mar 9, 2021

You nailed it @marwan-at-work! Thanks for your help.

Copy link
Member
@a8m a8m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful! Thanks for fixing this @uta-mori.

@a8m a8m merged commit e52439c into ent:master Mar 9, 2021
@utamori utamori deleted the go1.16-go-generate branch March 10, 2021 01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0