Repository: go
Issue: cmd/go: go get -u can't upgrade packages with invalid or non-existent versions in go.mod
Labels: NeedsInvestigation,modules

<!--
Please answer these questions before submitting your issue. Thanks!
For questions please use one of our forums: https://github.com/golang/go/wiki/Questions
-->

### What version of Go are you using (`go version`)?

<pre>
$ go version
go version go1.16.3 darwin/amd64
</pre>

### Does this issue reproduce with the latest release?

Yes

### What operating system and processor architecture are you using (`go env`)?

<details><summary><code>go env</code> Output</summary><br><pre>
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/m/Library/Caches/go-build"
GOENV="/Users/m/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/m/dev/pkg/mod"
GONOPROXY="github.com/go-duo/duo"
GONOSUMDB="github.com/go-duo/duo"
GOOS="darwin"
GOPATH="/Users/m/dev"
GOPRIVATE="github.com/go-duo/duo"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.3"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/m/dev/src/github.com/go-duo/welcome/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/4f/tcxcr6_55v9bp38d8g4hjlf80000gn/T/go-build2786980060=/tmp/go-build -gno-record-gcc-switches -fno-common"
</pre></details>

### What did you do?

<!--
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
-->

I'm changing the location of source code for one of my dependencies, so I change the module path in go.mod.

**go.mod**

```diff
module github.com/org/repo

go 1.16

- require github.com/org/dep v0.0.0-20210410204338-0d7a392d906a
+ require gitlab.com/org/dep v0.0.0-20210410204338-0d7a392d906a
```

Note that I'm just changing the name using a find and replace, not the version information. 

I was expecting to be able to run:

```sh
go get -u gitlab.com/org/dep
```

And it would update to whatever `gitlab.com/org/dep` has as its latest. Unfortunately, you run into this:

```
go: gitlab.com/org/dep@v0.0.0-20210410204338-0d7a392d906a: parsing go.mod:
        module declares its path as: github.com/org/dep
                but was required as: gitlab.com/org/dep
```

As a fallback, I try pointing to the SHA:

```
go get -u gitlab.com/org/dep@1ccc8c5848cd752f9b00c35cdafa8923bd9dcd22
```

I still see the same error.

```
go: gitlab.com/org/dep@v0.0.0-20210410204338-0d7a392d906a: parsing go.mod:
        module declares its path as: github.com/org/dep
                but was required as: gitlab.com/org/dep
```

FYI, I'm not certain this error is repeatable, this might be specific to my mistake of not replacing the imports. Regardless of this particular error, you will always get an error for having an invalid version in there, you can try `v0.0.0-foo` and you'll be greeted with an error.

### What did you expect to see?

I feel like it's failing too early. In my mind, upgrade should automatically upgrade to a non-faulty version.

If `go.mod` needs to be validated to resolving semver, then at least pointing directly to a SHA/branch/latest should work regardless of what you have as a version in `go.mod`.

### What did you see instead?

I think it should perform the upgrade, then update go.mod. No error. The current workaround is to delete the dependency then run the command:

```diff
module github.com/org/repo

go 1.16

- require github.com/org/dep v0.0.0-20210410204338-0d7a392d906a
```

```
go get -u gitlab.com/org/dep
```

This all seems a bit unnecessary.