google / wire

Compile-time Dependency Injection for Go
Apache License 2.0
13.13k stars 623 forks source link

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

Open jhamill34 opened 4 months ago

jhamill34 commented 4 months ago

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.

google-cla[bot] commented 4 months ago

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 commented 4 months ago

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