-
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: go directive is not only added during go mod init, but also under other conditions that are hard to deduce #30790
Comments
Likely related: #30043 (comment) |
Before thinking about what should happen, why is the unpredictable |
The intent of the CL was to add a "go" statement to a go.mod file, if one did not already exist, when running either So it sounds like there is a bug: if the existing go.mod file contains anything other than just a "module" statement, then running |
The behavior I've observed is that various people are either uncertain about when the If people choose to not add the
I've observed it occur during code review, and while people are running Edit: I've also seen it occur during testing of module-related code, as demonstrated by the commit message of CL 168757. |
Currently, it also gets added when running
Is that a bug or intended behavior?
Making both I've considered suggesting that we change the behavior such that |
I don't know. I don't really understand how |
The empty-requirements behavior is probably a side effect of this case: go/src/cmd/go/internal/modload/init.go Lines 376 to 380 in 5f40351
The go/src/cmd/go/internal/modload/init.go Line 424 in 5f40351
Perhaps that call should be moved inside the |
Or, going in the other direction, perhaps we should always add the |
CC @jayconrod |
Change https://golang.org/cl/168757 mentions this issue: |
Stop fighting the behavior of the go tool when run in these directories. Updates golang/go#30790 Change-Id: I32dfeb0bafa3ed3664500f1768b2293e5257d09b Reviewed-on: https://go-review.googlesource.com/c/tools/+/168757 Run-TryBot: Michael Matloob <matloob@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Change https://golang.org/cl/169877 mentions this issue: |
@gopherbot, please backport to 1.12: the current behavior is confusing, and the fix is small |
Backport issue(s) opened: #31117 (for 1.12). Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases. |
Change https://golang.org/cl/176925 mentions this issue: |
Updates #30790 Fixes #31960 Change-Id: Ib3ac074cf1f98fe69f53af82d2a0441582116570 Reviewed-on: https://go-review.googlesource.com/c/go/+/176925 Run-TryBot: Bryan C. Mills <bcmills@google.com> Reviewed-by: Jay Conrod <jayconrod@google.com>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Run the following commands in one module that has no module requirements:
Now run the same commands in another module that has 1 or more module requirements:
Additionally, if the source code in module m2 is modified to no longer import anything from the
rsc.io/quote
module, andgo mod tidy
is run, the first time it removes therequire rsc.io/quote v1.0.0
line, leaving justmodule m2
. The second time it adds ago
directive.What did you expect to see?
For modules that already exist, and do not already contain a
go
directive in their go.mod file, thego
directive should either be always added automatically (i.e., during any ofgo build
,go test
,go mod tidy
, etc., operations), or it should never be added.What did you see instead?
Sometimes it's added, sometimes it isn't added.
Based on investigating the code with @julieqiu and @heschik, we've found that the
go
directive is always added to go.mod on anygo build
,go test
,go mod tidy
, etc., operation whenever the go.mod file has exactly one statement: the module statement. If the go.mod file has at least 1 require, or replace, or exclude directive, in addition to the module statement, then variousgo
operations do not add ago
directive.This behavior is surprising and hard to predict (without looking into the source code to figure it out). I suspect it's an unintentional behavior. The commit message of CL 147281 that implemented this behavior says:
Based on that, I suspect the originally intended behavior was to make it so that a
go
directive is automatically inserted only when creating a new Go module (viago mod init
), but not when working with an existing module that has a go.mod file. I will assign this to @ianlancetaylor to confirm if this is a bug, and perhaps make the decision on how and whether this issue should be resolved.I think we should fix this and even consider backporting to Go 1.12.1. Doing so should would make the logic of when a
go
directive is added easier to predict and understand, improving the user experience when in module mode./cc @bcmills @matloob
The text was updated successfully, but these errors were encountered: