Repository: go
Issue: cmd/go: go.work: `go build` requires versioned replace directive when replacing a used module with a local path
Labels: Thinking,NeedsInvestigation,GoCommand,modules

<!--
Please answer these questions before submitting your issue. Thanks!
-->

### What version of Go are you using (`go version`)?

<pre>
$ go version
go version go1.18.5 linux/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=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/mlaventure/.cache/go-build"
GOENV="/home/mlaventure/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/mlaventure/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/mlaventure/go"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.5"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/mlaventure/tmp/go/workspace/realdeal/go.mod"
GOWORK="/home/mlaventure/tmp/go/workspace/go.work"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2178506260=/tmp/go-build -gno-record-gcc-switches"
</pre></details>

### What did you do?

Given the following workspace structure:
```
~/tmp/go/workspace $ tree
.
├── fubar
│   ├── fubar.go
│   └── go.mod
├── go.work
└── realdeal
    ├── go.mod
    ├── realdeal
    └── realdeal.go

2 directories, 6 files
```

With the following `go.work`:
```
~/tmp/go/workspace $ cat go.work 
go 1.18

use ./fubar
use ./realdeal

replace github.com/foo/fubar   => ./fubar
```

Build `realdeal` which depends on `fubar`:
```
~/tmp/go/workspace/realdeal $ go build .
```

<!--
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on go.dev/play is best.
-->



### What did you expect to see?

Nothing, the build should succeed

### What did you see instead?
```
go: workspace module github.com/foo/fubar is replaced at all versions in the go.work file. To fix, remove the replacement from the go.work file or specify the version at which to replace the module.
```

### Found workaround

The only way I've found to fix this is to update the `go.work` to be as follow:
```
~/tmp/go/workspace $ cat go.work 
go 1.18

use ./fubar
use ./realdeal

replace github.com/foo/fubar v0.0.0-00010101000000-000000000000 => ./fubar
```

This is not needed if a workspace is not used. It also seems to contradict the example from the documentation (https://go.dev/ref/mod#workspaces):
```
replace (
    golang.org/x/net v1.2.3 => example.com/fork/net v1.4.5
    golang.org/x/net => example.com/fork/net v1.4.5
    golang.org/x/net v1.2.3 => ./fork/net
    golang.org/x/net => ./fork/net
)
```