Skip to content
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

fix: generated wire file to be compatible with workspaces #410

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jhamill34
Copy link

@jhamill34 jhamill34 commented Jul 23, 2024

Fixes #403

Problem

After trying to run wire ./... on a module inside of a go workspace subsequent calls to go generate ./... fail

go: -mod may only be set to readonly or vendor when in workspace mode, but it is set to "mod"
        Remove the -mod flag to use the default readonly value,
        or set GOWORK=off to disable workspace mode.
internal/app/wire_gen.go:3: running "go": exit status 1

This is because the wire_gen.go files contain the following generate command (see #403 for more details)

// go:generate go run -mod=mod github.com/google/wire/cmd/wire

Solution

This fix adds a flag to the compiled binary to allow this flag to be either unset or overridden with a different value.

For example running this command:

wire gen -mod=readonly ./...

Would generate the following wire_gen.go file

// Code generated byWire. DO NOT EDIT.

//go:generate go run -mod=readonly github.com/google/wire/cmd/wire
//go:build !wireinject

// .. rest of the code ..

Calling wire gen -mod=unset ./... would remove the flag in the generated file all together and calling without the flag (i.e. wire gen ./...) will default to the current behavior of setting -mod=mod.

Copy link

google-cla bot commented Jul 23, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@jhamill34
Copy link
Author

I'm realizing that this fix works until you run go generate ./... and then the issue comes back because the added flag needs to be present in the generate command in the file's header. So for this solution to work it'll need to play nice with the existing -tag flag.

For example:

// go:generate go run github.com/google/wire gen -mod=readonly

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.

go generate ./... no longer works with go 1.22.0 and wire 0.6.0
1 participant