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

Couldn't load correct replaced go.mod file #3343

Closed
crazytaxi824 opened this issue Jan 19, 2022 · 6 comments · Fixed by #3345
Closed

Couldn't load correct replaced go.mod file #3343

crazytaxi824 opened this issue Jan 19, 2022 · 6 comments · Fixed by #3345

Comments

@crazytaxi824
Copy link

crazytaxi824 commented Jan 19, 2022

example package

go get github.com/hashicorp/consul/api

$GOPATH/pkg/mod/github.com/hashicorp/consul/api@v1.12.0/go.mod file content

module github.com/hashicorp/consul/api

go 1.12

replace github.com/hashicorp/consul/sdk => ../sdk

require (
	github.com/hashicorp/consul/sdk v0.8.0
	// ...
)

problem

  1. after go mod tidy, $GOPATH/pkg/mod/github.com/hashicorp/consul/sdk@v0.8.0/... is created.

  2. after :GoDef jump into .../consul/api@v1.12.0/xxx.go file, vim-go tries to read .../consul/sdk/go.mod, and .../consul/sdk/ is not exist, so it cause GoDoc, GoDef ... stop working.

vim-go error message:

vim-go: Finished loading packages.k@v0.8.0 (replaced by ../sdk): reading ../sdk/go.mod: open .../pkg/mod/github.com/hashicorp/consul/sdk/go.mod: no such file or directory

env:

vim v8.2.4100

vim-go a319aaf

gopls v0.7.4

@bhcleek
Copy link
Collaborator

bhcleek commented Jan 19, 2022

I can't quite duplicate the problem you're describing. I suspect some steps are missing from your reproduction instrucdtions.

  1. What directory are you in when you run go get github.com/hashicorp/consul/api ?
  2. What directory are you in when you run go mod tidy?
  3. What file are you editing when you use :GoDef to jump into a file in github.com/hashicorp/consul/api?

@crazytaxi824
Copy link
Author

crazytaxi824 commented Jan 20, 2022

@bhcleek sorry, i didn't make it clearly.

  1. I have a project foo
foo
├── go.mod
└── src
    └── main.go
  1. run go get github.com/hashicorp/consul/api in .../foo project root directory.

  2. run go mod tidy in .../foo project root directory.

  3. my .../foo/src/main.go content

package main

import (
	"log"

	"github.com/hashicorp/consul/api"
)

func main() {
	client, err := api.NewClient(&api.Config{
		Address: "xxx",
	})

	// ...
}
  1. cursor on NewClient(), run :GoDef. it will jump to $GOPATH/pkg/mod/github.com/hashicorp/consul/api@v1.12.0/api.go
// NewClient returns a new client
func NewClient(config *Config) (*Client, error) {
	// bootstrap the config
	defConfig := DefaultConfig()

	// ...
}

Problem

My problem is when i try to :GoDef the DefaultConfig() in this function, it won't work. vim-go shows vim-go: : packages.Load error.

So I dig a little deeper, found that vim-go is trying to load $GOPATH/pkg/mod/github.com/hashicorp/consul/sdk/go.mod instead of $GOPATH/pkg/mod/github.com/hashicorp/consul/sdk@v0.8.0/go.mod, because $GOPATH/pkg/mod/github.com/hashicorp/consul/api@v1.12.0/go.mod shows

module github.com/hashicorp/consul/api

go 1.12

replace github.com/hashicorp/consul/sdk => ../sdk   (❌ this is where replace goes wrong)

require (
	github.com/hashicorp/consul/sdk v0.8.0
	//...
)

If I change the line to replace github.com/hashicorp/consul/sdk => ../sdk@v0.8.0 vim-go works perfect. So I think it's the directory replace problem.

Thanks a lot for your help.

@bhcleek
Copy link
Collaborator

bhcleek commented Jan 20, 2022

What version of Go are you using?

@crazytaxi824
Copy link
Author

crazytaxi824 commented Jan 20, 2022

What version of Go are you using?

go version go1.17.6 darwin/amd64

bhcleek added a commit to bhcleek/vim-go that referenced this issue Jan 20, 2022
@bhcleek
Copy link
Collaborator

bhcleek commented Jan 20, 2022

Thank you. I was able to reproduce the problem you're seeing and have a fix nearly ready.

@crazytaxi824
Copy link
Author

@bhcleek Big help, Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants