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

proposal: cmd/go: 'go mod tidy' should remove stale 'replace' directives #30516

neclepsio opened this issue Mar 1, 2019 · 4 comments

proposal: cmd/go: 'go mod tidy' should remove stale 'replace' directives #30516

neclepsio opened this issue Mar 1, 2019 · 4 comments
NeedsInvestigation Proposal


Copy link

@neclepsio neclepsio commented Mar 1, 2019

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

$ go version
go version go1.12 windows/amd64

Does this issue reproduce with the latest release?


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

go env Output
$ go env
set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows

What did you do?

I created a go.mod file with two replaces in separate lines, then run go mod tidy. foo3 replacement is unused.

replace foo => bar
replace foo2 => bar2
replace foo3 => bar3

What did you expect to see?

A nicely formatted go.mod. Moreover, I think the unused replace should be removed.

replace (
        foo => bar
        foo2 => bar2

What did you see instead?

The file is unchanged. Also, go mod edit -fmt does not format replaces.

@gopherbot gopherbot added this to the Proposal milestone Mar 1, 2019
Copy link

@bcmills bcmills commented Mar 1, 2019

See also #29813.

@bcmills bcmills changed the title proposal: go mod tidy should reformat replaces proposal: cmd/go: 'go mod tidy' should reformat replaces Mar 1, 2019
@bcmills bcmills added the NeedsInvestigation label Mar 1, 2019
Copy link

@rsc rsc commented Mar 7, 2019

In general we don't override grouping. If you wrote three replace lines, they should stay three separate replace lines. (Especially if there are comments involved, but even if not.)

I will retitle this to be about unused replaces, which are not as clearly off the table. It's tied up with #29813, though, and in general I'm hesitant to make tidy throw away info that might be needed.

@rsc rsc changed the title proposal: cmd/go: 'go mod tidy' should reformat replaces proposal: cmd/go: 'go mod tidy' should remove stale 'replace' directives Mar 7, 2019
Copy link

@stone-z stone-z commented Nov 17, 2020

+1 for removing unused replaces automatically.

My use case is that we sometimes have to replace nested dependencies in order to avoid using versions with known vulnerabilities. When the vulnerable version is no longer referenced by a direct or indirect dependency, it would be nice for the replaces to disappear.

I feel this also makes sense to preserve the expected future behavior. If a dependency is removed and then re-added sometime in the future, it probably should not be overridden by default. The user adding the dependency may not know anything about the context for the older replace (which may or may not still be valid), but probably expects to have the dependency they added.

Copy link

@jboelter jboelter commented Jan 25, 2021

Ran into a scenario where I typo'd a path in a replacement directive and spent way too long trying to figure out why my replacement wasn't working.

An error or warning for unused replace directives for tidy, vendor, and verify and allow the user to identify and correct the intended behavior.

This replace will silently do nothing:

module example

go 1.15

require ( v1.0.0

replace => v1.2.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
NeedsInvestigation Proposal

No branches or pull requests

6 participants