-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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 |
@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]>
Now this is out in RC, I've put a blog post explaining some of the motivation for the change here: https://cirw.in/blog/go-tools |
For clarification:
|
Quick reply with this blog post. It doesn't answer all your questions, but shows (at the bottom of the post) that you can install tools both globally and locally for the current module. |
@meling nice post! You might want to recommend |
Change https://go.dev/cl/638296 mentions this issue: |
It's a common misperception that -u is needed more often than it is, perhaps in part due to muscle memory from the GOPATH days. The current release notes suggest that 'go get -u tool' will upgrade all of the current module's tools, but it does more than that -- it will also upgrade all of the direct and indirect non-test dependencies of those tools too. The simplest fix is probably just to remove the -u from this section of the release notes. There are various other details that could be covered, like the -u or -t flags, or that 'go get tool' is a shorthand for 'go get tool@upgrade', but maybe this is sufficient. Updates golang/go#48429 Updates golang/go#68545 Change-Id: I31eeb96dae996a7ffde24d9bae6788d6889e2044 Reviewed-on: https://go-review.googlesource.com/c/website/+/638296 Reviewed-by: David Chase <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Conrad Irwin <[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: