-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
cmd/go: track tool dependencies in go.mod #48429
Comments
I like this, I find it annoying to use the If this proposal moves forward, where does the dependency go in the |
Good question! I'm not so familiar with the reasoning behind the multiple blocks... something to do with lazy loading? I'd defer to those with more experience in this area |
Personally, I think #42088 is already a pretty good solution. With it, one can write
Similarly, Another big advantage is that you can pick specific versions of tools, and they won't interfere with your main |
The only downside to #42088 is that, if you repeat the same |
Or |
Oh interesting, thanks @mvdan I wasn't aware of that solution. 🤔 A few concerns immediately come to mind...
|
Also this |
In module mode, |
It certainly warrants a mention. I'm not sure we should bless it as the only best practice, though, because there can be legitimate reasons for versioning, downloading, and running tools some other way. Perhaps some of your tools aren't written in Go, such as protoc, so you use a "tool bundler" that's entirely separate to Go. Or perhaps you do need your tools to share the same MVS graph with your main module for proper compatibility, so you want them to share a |
Gotta say though...
|
So even with the |
Also worth noting: codegen tools like gqlgen and protobuf are often comprised of a generator command and a runtime, both of which typically need to be versioned in lock-step. This proposal solves that case rather neatly, allowing go.mod to manage both generator and runtime versions. |
We used to do that. Then people would have that replicated across different files and the version wouldn't always match, and we wanted to automate tool updating, so we figured that migrating to Again, |
@jayconrod has previously suggested something similar, using a new directive (perhaps Personally, I prefer the approach of adding a new directive — today we do treat requirements with A new |
@bcmills would such tool requirements be part of the same MVS module graph? |
The In particular:
|
Or |
I like this proposal. I've had something similar in my drafts folder for a while. @bcmills touched on the main difference.
I don't think |
Yeah I like the A |
Or have I got that wrong? Is sharing indirect dependencies between tools and other dependencies a desirable feature? |
Right. The go command reports errors for unknown directives in the main module's go.mod file, but it ignores unknown directives in dependencies' go.mod files. So everyone working on a module that used this would need to upgrade to a version of Go that supports it (same as most other new features), but their users would be unaffected.
My suggestion is to have If you don't want to mix tool and library dependencies in |
Yep that makes a lot of sense @jayconrod |
This proposal has been added to the active column of the proposals project |
@jayconrod Did you want to write up the |
While the cached name of an executable is set based on the base name of the package path, the executable produced as the output of link doesn't have ExeName set on it and is just called a.out (with a .exe suffix on Windows). Set ExeName so that the first time the binary is run, from the directory link is run in, it has the right name for ps. For #48429 Change-Id: Ic049304ec6fd5b23c2f5aaaf91aa58d79fe5a7ba Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest,gotip-windows-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/go/+/630695 Reviewed-by: Conrad Irwin <[email protected]> Reviewed-by: Hongxiang Jiang <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
Playing with tip I can do: go install tool github.com/alta/protopatch/cmd/protoc-gen-go-patch
go get -tool google.golang.org/protobuf/cmd/protoc-gen-go But these do not appear to work (tools metapackage): go install tools
go build tools Is the tools metapackage planned for Go1.24? |
@meling Apologies- the design doc is now a little out of date. We changed the naming of the metapackage to A few other major changes in the final
|
Adding a release blocker to track mentioning this new cmd/go feature in the Go 1.24 release notes. |
@matloob @samthanawalla to confirm the code has been completed and only release notes are pending for 1.24. |
@dmitshur the Modules FAQ may also need to be updated |
Change https://go.dev/cl/632555 mentions this issue: |
Change https://go.dev/cl/632595 mentions this issue: |
Change https://go.dev/cl/632556 mentions this issue: |
Change https://go.dev/cl/632596 mentions this issue: |
The RC is planned for next week, and we need a full draft of the release notes before then. Please prioritize writing the release notes for this. Thanks! |
For golang/go#48429 Change-Id: I4db7ec6855665c123f62e8a8a625baeedd31dd7f Reviewed-on: https://go-review.googlesource.com/c/wiki/+/632596 Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
Using
But using
I don't know if that was a deliberate choice (including I didn't notice Checks above were using tip from earlier today (c46ba1f), and it might be I misunderstood the new capabilities or otherwise made a mistake. |
@thepudds This will be fixed by https://go-review.googlesource.com/c/go/+/632575 |
Is the intention that this should work? $ gotip mod -modfile=tools.mod init foo
go mod: unknown command
Run 'go help mod' for usage I'm on |
Hi @ConradIrwin, great, thanks! 🚀🎉 @fredrikaverpil, if you want to use
|
Change https://go.dev/cl/632575 mentions this issue: |
When adding support for module tools, we added the ability for `go tool` to edit the module cache. For users with `GOFLAGS=-modcacherw` this could have led to a situation where some of the files in the mod cache were unexpectedly not deletable. We also allow -modfile so that people can select which module they are working in when looking for tools. We still do not support arbitrary build flags for tools with `go tool`. If you want those, use `go run` or `go build`, etc. instead. Updates #48429 Change-Id: Ic3c56bb8b6ba46114196465ca6ee2dcb08b9dcc0 Reviewed-on: https://go-review.googlesource.com/c/go/+/632575 Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
For golang/go#48429 Change-Id: I81344da002afe92c8ec60ad5acef0b11e487934e Reviewed-on: https://go-review.googlesource.com/c/website/+/632595 Reviewed-by: Sam Thanawalla <[email protected]> Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
For #48429 Change-Id: I6932853c3156a68d099a749431d15d2c37785649 Reviewed-on: https://go-review.googlesource.com/c/go/+/632555 Reviewed-by: Sam Thanawalla <[email protected]> Auto-Submit: Sam Thanawalla <[email protected]> Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
UPDATE: 2024-07-29: the latest proposal can be found here.
Background
The current best-practice to track tool dependencies for a module is to add a
tools.go
file to your module that includes import statements for the tools of interest. This has been extensively discussed in #25922 and is the recommended approach in the Modules FAQThis approach works, but managing the tool dependencies still feels like a missing piece in the
go mod
toolchain. For example, the instructions for getting a user set up with a new project using gqlgen (a codegen tool) looks like thisThe
printf
line above really stands out as an arbitrary command to "add a tool" and reflects a poor developer experience when managing tools. For example, an immediate problem is that theprintf
line will only work on unix systems and not windows. And what happens iftools.go
already exists?So while we have some excellent tools for managing dependencies within the
go.mod
file usinggo get
andgo mod edit
, there is no such equivalent for managing tools in thetools.go
file.Proposed Solution
The
go.mod
file uses the// indirect
comment to track some dependencies. An// indirect
comment indicates that no package from the required module is directly imported by any package in the main module (source).I propose that this same mechanism be used to add tool dependencies, using a
// tool
comment.Users could add a tool with something like
or
A
go.mod
would then look something likeAnd would allow users to subsequently run the tool with
go run github.com/99designs/gqlgen
This would mean a separate
tools.go
file would no longer be required as the tool dependency is tracked in thego.mod
file.Go modules would be "tool" aware. For example:
go mod tidy
would not remove the// tool
dependency, even though it is not referenced directly in the module// tool
dependency is imported by another module, Go modules understands that the// tool
dependency is not required as an indirect dependency. Currently when usingtools.go
, go modules does not have that context and the tool is treated like any other indirect dependencygo get -tool [packages]
would only add a dependency with amain
packageThe text was updated successfully, but these errors were encountered: