Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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/[email protected]/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/[email protected]
/Users/user/go/pkg/mod/entgo.io/[email protected]/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/[email protected]
/Users/user/go/pkg/mod/entgo.io/[email protected]/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/[email protected]
/Users/user/go/pkg/mod/entgo.io/[email protected]/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/[email protected]
/Users/user/go/pkg/mod/entgo.io/[email protected]/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/[email protected]
ent/generate.go:3: running "go": exit status 1

Same result with go generate ./...

❯ go generate ./...
/Users/user/go/pkg/mod/entgo.io/[email protected]/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/[email protected]
/Users/user/go/pkg/mod/entgo.io/[email protected]/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/[email protected]
/Users/user/go/pkg/mod/entgo.io/[email protected]/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/[email protected]
/Users/user/go/pkg/mod/entgo.io/[email protected]/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/[email protected]
/Users/user/go/pkg/mod/entgo.io/[email protected]/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/[email protected]
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/[email protected]). 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
Contributor

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
Contributor

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 repro
➜  go116 go get entgo.io/ent/cmd/ent@latest
go get: added entgo.io/ent v0.6.0
➜  go116 go run entgo.io/ent/cmd/ent init User
➜  go116 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
Contributor

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