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

cmd/go: add mod tidy -diff #27005

Closed
fishy opened this issue Aug 15, 2018 · 44 comments
Closed

cmd/go: add mod tidy -diff #27005

fishy opened this issue Aug 15, 2018 · 44 comments
Assignees
Labels
FeatureRequest FixPending Issues that have a fix which has not yet been reviewed or submitted. GoCommand cmd/go modules
Milestone

Comments

@fishy
Copy link

fishy commented Aug 15, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.11rc1 darwin/amd64

Does this issue reproduce with the latest release?

N/A

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOOS="darwin"

What did you do?

  • Remove lines from go.mod and go.sum files
  • Run go mod tidy to add them back

What did you expect to see?

go mod tidy should return non-zero as it made changes. This way it could be integrated to tools like pre-commit hooks to make sure that we don't commit untidied go.mod and go.sum files into version control

What did you see instead?

go mod tidy returned zero

@mvdan
Copy link
Member

mvdan commented Aug 15, 2018

Non-zero exit codes are usually for errors, so having it behave like this by default would be counter-intuitive.

Perhaps it could have something similar to gofmt, which has the -l and -d flags to tell if any changes were made.

@swtch1
Copy link

swtch1 commented Aug 15, 2018

If the files are successfully added why wouldn't you want to see a 0 status code? This means that the following script would fail:

#!/usr/bin/env bash
set -e
go mod tidy  # <-- non 0 return code
echo 'we never get here'

@fishy
Copy link
Author

fishy commented Aug 15, 2018

That makes sense. How about maybe make it optional? e.g. when it's executed with a flag it should return non-zero when it made any changes.

@thepudds
Copy link
Contributor

@gopherbot, please add label modules

@rsc rsc changed the title modules: go mod tidy should return non-zero when it made changes to go.mod and/or go.sum files cmd/go: add mod tidy -check Aug 18, 2018
@rsc
Copy link
Contributor

rsc commented Aug 18, 2018

We could think about adding a flag that means "don't do the update, but fail if one is needed".
Not sure exactly what to call it but I put -check in the description. Needs more thought.

@rsc rsc added this to the Go1.12 milestone Aug 18, 2018
@rsc rsc added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Aug 18, 2018
@bcmills bcmills modified the milestones: Go1.12, Go1.13 Oct 24, 2018
radeksimko added a commit to vancluever/terraform-provider-acme that referenced this issue Feb 19, 2019
 - 'go mod verify' is not that useful and isn't equivalent to govendor status.
 - 'go mod tidy -v' does not return non-zero exit code when dependencies aren't tidy
 - We might add 'go mod tidy -check' at some point, when it becomes a thing.
   See golang/go#27005
   Until then it's better to keep provider setup aligned though.
@nhooyr
Copy link
Contributor

nhooyr commented Feb 25, 2019

@rsc regarding your comment in #26850

You could use 'go list all -mod=readonly' to check that at least for the current GOOS/GOARCH no changes are needed.

go list all -mod=readonly causes changes for me to go.sum without erroring.

Is this a bug?

@jayconrod
Copy link
Contributor

When this is implemented, we should run this on the TryBots (#31640).

Ideally go mod tidy -check would not access the network, since it will commonly be run in CI environments where network access is restricted. We might want to do so to provide diagnostics though. GOPROXY=off is still a good signal that we shouldn't access the network.

@nmiyake
Copy link
Contributor

nmiyake commented May 23, 2019

This is a feature that we would like to see for our CI environment as well (we want to verify that go.mod and go.sum are in the proper state for all submissions for all possible build constraints).

I echo the sentiment expressed by @jayconrod that it would be ideal if this check could be performed without network access -- is it feasible for the -check mode to verify only that no change needs to be made without making network queries? My intuition is that the information provided by the source code, go.mod and go.sum should be sufficient to make this determination, but I'm not completely sure about this.

@andybons andybons modified the milestones: Go1.13, Go1.14 Jul 8, 2019
@detailyang
Copy link

It's would be useful to reduce git conflict in lint scenario

@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
@mcandre
Copy link

mcandre commented Nov 7, 2019

Networkless operation is a nice to have. Dry-run flags and intuitive exit codes are perhaps a more immediate concern.

@leighmcculloch
Copy link
Contributor

leighmcculloch commented Dec 12, 2019

How about maybe make it optional? e.g. when it's executed with a flag it should return non-zero when it made any changes.

This sounds to me like the solution that is most similar to how other tools solve this problem. golint uses this pattern already, and so do other tools like git diff. I'd prefer this over a -check command that alters the other behaviors of the tool because by altering the behavior of the tool there is more to learn and understand.

It'd be good to surface an exit code mode consistently across all the Go tool commands that we routinely use as checks in CI pipelines.

Today running checks for go mod, go fmt, and golint all use unique and unintuitive patterns:

OUTPUT=$(gofmt -d .)
if [[ $OUTPUT ]]; then
    exit 1
fi
golint -set_exit_status
go mod tidy
git diff --exit-code -- go.mod go.sum

We could reuse the pattern demonstrated by golint:

golint -set_exit_status
go fmt -set_exit_status
go mod tidy -set_exit_status

Or, we could take the opportunity to use a term that is used by other tools, like -exit-code:

golint -exit-code
go fmt -exit-code
go mod tidy -exit-code

@gopherbot gopherbot added this to the Go1.23 milestone Feb 6, 2024
@bcmills bcmills removed their assignment Mar 13, 2024
@bcmills
Copy link
Contributor

bcmills commented Mar 13, 2024

@samthanawalla, this should be a fairly straightforward feature to implement, and would be useful for CI tooling.

(This was in the NeedsDecision state pending a final decision about what the command-line flag should be named; @matloob and @rsc may have some thoughts on that.)

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/579055 mentions this issue: cmd/go: add mod tidy -check

@samthanawalla samthanawalla added FixPending Issues that have a fix which has not yet been reviewed or submitted. and removed NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Apr 16, 2024
@samthanawalla samthanawalla added NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. and removed FixPending Issues that have a fix which has not yet been reviewed or submitted. labels Apr 25, 2024
@samthanawalla
Copy link
Contributor

@rsc Should we proceed with -diff or wait for the proposal committee to review?

@rsc
Copy link
Contributor

rsc commented May 8, 2024

Given the discussion I think it is fine to proceed with -diff.

@samthanawalla samthanawalla changed the title cmd/go: add mod tidy -check cmd/go: add mod tidy -diff May 8, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/585401 mentions this issue: cmd/go: add go mod tidy -diff

@samthanawalla samthanawalla added FixPending Issues that have a fix which has not yet been reviewed or submitted. and removed NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels May 15, 2024
@samthanawalla samthanawalla modified the milestones: Go1.24, Go1.23 May 22, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/587695 mentions this issue: doc/next: document go mod tidy -diff flag

gopherbot pushed a commit that referenced this issue May 23, 2024
Add a release note for the flag that CL 585401 added.

For #27005.
For #65614.

Change-Id: Ib26eb1b85c511d3cb41a29a8d9354577dd9a0e14
Reviewed-on: https://go-review.googlesource.com/c/go/+/587695
Auto-Submit: Dmitri Shuralyov <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Reviewed-by: Sam Thanawalla <[email protected]>
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/587941 mentions this issue: cmd/go: add compatibility tests for go mod tidy -diff

gopherbot pushed a commit that referenced this issue May 30, 2024
For #27005
Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest,gotip-windows-amd64-longtest
Change-Id: I90ab8c21222ac2189abb40e8c8e7549e2d940dd2
Reviewed-on: https://go-review.googlesource.com/c/go/+/587941
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Michael Matloob <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest FixPending Issues that have a fix which has not yet been reviewed or submitted. GoCommand cmd/go modules
Projects
None yet
Development

No branches or pull requests