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: go mod edit -replace succeeds even when there isn't a module to replace #39009

Closed
james-lawrence opened this issue May 11, 2020 · 4 comments
Labels
FrozenDueToAge GoCommand cmd/go modules NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@james-lawrence
Copy link
Contributor

james-lawrence commented May 11, 2020

I believe this is invalid behavior and unexpected.

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

go version
go version go1.14.2 linux/amd64

What did you do?

cat go.mod 
module example

go 1.13
go mod edit -replace github.com/pkg/errors=/tmp/errors
cat go.mod 
module example

go 1.13

replace github.com/pkg/errors => /tmp/errors

What did you expect to see?

an error about replacing a unknown module.

What did you see instead?

a replace statement was added to go.mod.

@dmitshur dmitshur changed the title go mod edit -replace succeeds even when there isn't a module to replace. cmd/go: go mod edit -replace succeeds even when there isn't a module to replace May 12, 2020
@dmitshur dmitshur added GoCommand cmd/go modules NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels May 12, 2020
@dmitshur dmitshur added this to the Backlog milestone May 12, 2020
@dmitshur
Copy link
Contributor

/cc @bcmills @jayconrod @matloob

@jayconrod
Copy link
Contributor

Thanks for reporting, but this looks like it's working as intended.

go mod edit is meant to be a low-level tool for editing go.mod files. It does very little validation of the go.mod beyond checking the syntax. In particular, it doesn't fetch or read go.mod files for dependencies. That fetch would be necessary to check whether a replaced module exists or not, since a replaced module could be indirectly required.

@james-lawrence
Copy link
Contributor Author

james-lawrence commented May 14, 2020

@jabagawee so the answer here is that this that resulting go.mod file is valid. even though its unclear what it even does?

what happens when other go mod commands are executed on the resulting file?

  • go mod download: does it fetch?
  • go mod tidy: does it get removed properly?
  • go mod verify: does it validate the dependency as unnecessary?

I don't really care if go mod edit directly verifies the mod file i suppose. but there should be a command that does. because I'm unsure if this go.mod is valid and i know of at least 1 project that is generating mod files like the above.

@jayconrod
Copy link
Contributor

If a later command needs to fetch any version of github.com/pkg/errors, it will read from /tmp/errors instead of going out to the network. The command doesn't need to fetch that module (including if the module is not required by anything), the replace will be ignored.

go mod tidy doesn't remove replace directives. #30516 is a proposal suggesting that it should. I think that's close to what you're asking for here.

go mod verify doesn't check replacements. It checks that modules downloaded into the cache match go.sum. It's mainly used to detect cache corruption, since modules are also verified when they're downloaded.

@golang golang locked and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge GoCommand cmd/go modules NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants