Repository: go
Issue: cmd/go: dependency module in workspace has go version requirement loaded before local replacement
Labels: NeedsInvestigation,GoCommand

### Go version

1.21.7

### Output of `go env` in your module/workspace:

```shell
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/steve/.cache/go-build'
GOENV='/home/steve/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/steve/Go/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/steve/Go/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/steve/Go/go1.21.7/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/home/steve/Go/go1.21.7/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.7'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/steve/Source/andy/f1gopher/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3910143555=/tmp/go-build -gno-record-gcc-switches'
```


### What did you do?

I want to build a project with Go version 1.21.7

The go.mod file says version 1.22.0 is required. However, I know this isn't strictly true and will compile with <1.22.0 except for the go.mod file

The project has a dependency which also requires version 1.22.0 according to its go.mod file, but which uses no 1.22.0 features and will compile with the lower version of the Go compiler (except for the go.mod file)

I have local copies of both the project and the dependency cloned from github

I change the version requirement in the go.mod of both projects and begin a go.work file with "go work init"

I add both projects to the go.work file with "go work use <path>"


### What did you see happen?

The project will not build with the lower Go version despite the changes to the version requirements in the local copies of the go.mod files

The error message shown as below: 

`go: module github.com/project/project@v0.9.1-0.20240221103349-46ee8a99748c requires go >= 1.22 (running go 1.21.7; GOTOOLCHAIN=local)
`

This project (name changed because it is not a public repository) is the dependency that was cloned and added to the go.work file after changing the Go version requirement in the go.mod file

### What did you expect to see?

I expected the project to be compiled because there are no 1.22.0 features used in either project and because I changed the Go version requirements in the local copy of both the project and the dependency

It seems like Go is checking the version requirements of the remote version of the dependency and not the local version pointed to by the go.work file

**NOTE**

I understand that if I set GOTOOLCHAIN to auto then this would work. But I believe that a GOTOOLCHAIN set to local should still allow me to change the Go version requirements in local copies of projects in the way described

**NOTE 2**

I have checked that my Go workspace is working correctly by compiling the project with a local version of 1.22.0. In that case the version check passes and local copy of the dependency is used