go: cmd/go: "go get" should create a replcae line automatically if the get path and moudle path don't match
What version of Go are you using (go version
)?
go version go1.12 linux/amd64
Does this issue reproduce with the latest release?
yes
What did you do?
$ go get github.com/go101/tinyrouter@v1.0.1
The module path set in go.mod
is go101.org/tinyrouter
, not github.com/go101/tinyrouter
.
What did you expect to see?
no errors, and automatically create a replace line in go.mod file of the app project after parsing the go-got go.mod file:
replace go101.org/tinyrouter => github.com/go101/tinyrouter v1.0.1
What did you see instead?
go: github.com/go101/tinyrouter@v1.0.1: parsing go.mod: unexpected module path "go101.org/tinyrouter"
go: error loading module requirements
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 15 (5 by maintainers)
@go101 When a user imports
go101.org/tinyrouter
, the Go command needs to find the location of that repository, so it sends a request tohttps://go101.org/tinyrouter?go-get=1
. That allows people to “register” unique module paths by serving that URL, using existing DNS and HTTPS infrastructure.If it didn’t work this way, anyone would be able to write
module go101.org/tinyrouter
in their module. When someone importsgo101.org/tinyrouter
, we’d have no way to know which one was intended. Replace directives are not intended for this purpose.Module paths are canonical: if a module sets the path
go101.org/tinyrouter
in itsgo.mod
, users must import it by that name in module mode (and as @dmitshur said, there must be an HTTPS server there pointing to wherever the repository is located). This constraint makes a lot of things simpler. Packages can only be imported by one name, and there’s much less risk the same package being built multiple times.replace
directives don’t seem like the right solution for this. There are a few limited use cases for them (e.g., a short-term fork of an upstream module), but it’s better to avoid them if at all possible. Keep in mind thatreplace
directives are only effective for builds in the main module, so adding them is very likely to break downstream users.Please see When should I use the replace directive? on the module wiki for more information on
replace
.See Remove import paths for more information on custom imports. These are popularly (but not officially) called “vanity imports”, and there are some articles on setting that up. You definitely need to serve
https://go101.org/tinyrouter?go-get=1
though.I just hope
is a shortcut for
I think this a reasonable hope.
@jayconrod
I did run the
go get
command under the path of main module. My intention of running this command is I hope this command will add areplace
line in thego.mod
file of the main module.Do you mean I should not place a
replace
line in thego.mod
file of tinyrouter package? This is not my intention.@dmitshur
I just think it is totally unnecessary to let
go get
makes a request tohttps://website.noexist/tinyrouter?go-get=1
. When it gets the responses from by callinggithub.com/go101/tinyrouter@v1.0.1
, it has succeeded to get thego.mod
file of the package. Thego.mod
file clearly specifies that the canonical import path of the package iswebsite.noexist/tinyrouter
. The information is ~efficient~sufficient, why will the command still make a request tohttps://website.noexist/tinyrouter?go-get=1
?If
go build
thinks it is ok, why notgo get
?The intention is to avoid my library users being locked down to
github.com
. I would like they usego101.org/tinyrouter
as import paths in their source code. When later, I move the package to another place, such asgitlab.com
, users can just modify the line ingo.mod
, no needs to modify all the import paths in source code.Here, the
go101.org
could be anything other thangithub.com
, for example,website.noexist/tinyrouter
. I never prepare to create a http service to response thehttps://go101.org/tinyrouter?go-get=1
request. Is this not ok?