golang / vscode-go

Go extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=golang.Go
Other
3.85k stars 736 forks source link

'Go to definition' is not working #2713

Closed yapinxxx closed 1 year ago

yapinxxx commented 1 year ago

What version of Go, VS Code & VS Code Go extension are you using?

Version Information
* Run `go version` to get version of Go from _the VS Code integrated terminal_. `go version go1.20.2 linux/amd64` * Run `gopls -v version` to get version of Gopls from _the VS Code integrated terminal_. ``` Build info ---------- golang.org/x/tools/gopls v0.11.0 golang.org/x/tools/gopls@v0.11.0 h1:/nvKHdTtePQmrv9XN3gIUN9MOdUrKzO/dcqgbG6x8EY= github.com/BurntSushi/toml@v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= github.com/google/go-cmp@v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= golang.org/x/exp@v0.0.0-20221031165847-c99f073a8326 h1:QfTh0HpN6hlw6D3vu8DAwC8pBIwikq0AI1evdm+FksE= golang.org/x/exp/typeparams@v0.0.0-20221031165847-c99f073a8326 h1:fl8k2zg28yA23264d82M4dp+YlJ3ngDcpuB1bewkQi4= golang.org/x/mod@v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA= golang.org/x/sync@v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sys@v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= golang.org/x/text@v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= golang.org/x/tools@v0.3.1-0.20221213193459-ca17b2c27ca8 h1:7/HkGkN/2ktghBCSRRgp31wAww4syfsW52tj7yirjWk= golang.org/x/vuln@v0.0.0-20221109205719-3af8368ee4fe h1:qptQiQwEpETwDiz85LKtChqif9xhVkAm8Nhxs0xnTww= honnef.co/go/tools@v0.3.3 h1:oDx7VAwstgpYpb3wv0oxiZlxY+foCpRAwY7Vk6XpAgA= mvdan.cc/gofumpt@v0.4.0 h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM= mvdan.cc/xurls/v2@v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc= go: go1.20.2 ``` * Run `code -v` or `code-insiders -v` to get version of VS Code or VS Code Insiders. ``` 1.77.0 7f329fe6c66b0f86ae1574c2911b681ad5a45d63 x64 ``` * Check your installed extensions to get the version of the VS Code Go extension `Go v0.38.0` * Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > `Go: Locate Configured Go Tools` command. ``` Checking configured tools.... GOBIN: undefined toolsGopath: gopath: /home/wumu/go/Wumu_work/Wumu_tools:/home/wumu/go/Wumu_work/practice/First_Go:/home/wumu/go/Wumu_work/practice/Go_start:/home/wumu/go/Wumu_work/VC_project:/home/wumu/go/Wumu_work/VC_project/src/VigorConnect GOROOT: /home/wumu/Go_ver_ctrl/go PATH: /home/wumu/.nvm/versions/node/v18.0.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/wumu/Go_ver_ctrl/go/bin:/home/wumu/go/Wumu_work/Wumu_tools/bin:/home/wumu/Go_ver_ctrl/go/bin:/home/wumu/go/Wumu_work/Wumu_tools/bin go: /home/wumu/Go_ver_ctrl/go/bin/go: go version go1.20.2 linux/amd64 gotests: /home/wumu/go/Wumu_work/Wumu_tools/bin/gotests (version: v1.6.0 built with go: go1.20.2) gomodifytags: /home/wumu/go/Wumu_work/Wumu_tools/bin/gomodifytags (version: v1.16.0 built with go: go1.20.2) impl: /home/wumu/go/Wumu_work/Wumu_tools/bin/impl (version: v1.1.0 built with go: go1.20.2) goplay: /home/wumu/go/Wumu_work/Wumu_tools/bin/goplay (version: v1.0.0 built with go: go1.20.2) dlv: /home/wumu/go/Wumu_work/Wumu_tools/bin/dlv (version: v1.20.1 built with go: go1.20.2) staticcheck: /home/wumu/go/Wumu_work/Wumu_tools/bin/staticcheck (version: v0.4.3 built with go: go1.20.2) gopls: /home/wumu/go/Wumu_work/Wumu_tools/bin/gopls (version: v0.11.0 built with go: go1.20.2) go env Workspace Folder (20230331-bug): /home/wumu/go/Wumu_work/practice/Report-Error-Example/vscode-go/20230331-bug GO111MODULE="auto" GOARCH="amd64" GOBIN="" GOCACHE="/home/wumu/.cache/go-build" GOENV="/home/wumu/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/home/wumu/go/Wumu_work/Wumu_tools/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/home/wumu/go/Wumu_work/Wumu_tools:/home/wumu/go/Wumu_work/practice/First_Go:/home/wumu/go/Wumu_work/practice/Go_start:/home/wumu/go/Wumu_work/VC_project:/home/wumu/go/Wumu_work/VC_project/src/VigorConnect" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/home/wumu/Go_ver_ctrl/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/home/wumu/Go_ver_ctrl/go/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.20.2" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="" 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 -fdebug-prefix-map=/tmp/go-build1172216158=/tmp/go-build -gno-record-gcc-switches" ```

Share the Go related settings you have added/edited

Run Preferences: Open Settings (JSON) command to open your settings.json file. Share all the settings with the go. or ["go"] or gopls prefixes.

{
  "go.languageServerExperimentalFeatures": {
      "diagnostics": false
    },
    "go.gotoSymbol.includeImports": true,
    "go.gotoSymbol.includeGoroot": true
}

Describe the bug

I can run go test and the result shows success, but I am unable to use go to definition on objects imported from other modules. I have used a replace directive in the go.mod file, as shown here: replace github.com/casbin/casbin/v2 => ../../../../casbin/casbin/v2@v2.65.2.

OUTPUT gopls(server)

[Error - 11:07:34 AM] Request textDocument/definition failed.
  Message: no object found for ident Enforcer
  Code: 0 

Folder Tree

Folder Tree Information
``` ./ ├── astaxie │   └── beego │   ├── admin.go │   ├── admin_test.go │   ├── adminui.go │   ├── app.go │   ├── beego.go │   ├── cache │   │   ├── cache.go │   │   ├── cache_test.go │   │   ├── conv.go │   │   ├── conv_test.go │   │   ├── file.go │   │   ├── memcache │   │   │   ├── memcache.go │   │   │   └── memcache_test.go │   │   ├── memory.go │   │   ├── README.md │   │   ├── redis │   │   │   ├── redis.go │   │   │   └── redis_test.go │   │   └── ssdb │   │   ├── ssdb.go │   │   └── ssdb_test.go │   ├── config │   │   ├── config.go │   │   ├── config_test.go │   │   ├── env │   │   │   ├── env.go │   │   │   └── env_test.go │   │   ├── fake.go │   │   ├── ini.go │   │   ├── ini_test.go │   │   ├── json.go │   │   ├── json_test.go │   │   ├── xml │   │   │   ├── xml.go │   │   │   └── xml_test.go │   │   └── yaml │   │   ├── yaml.go │   │   └── yaml_test.go │   ├── config.go │   ├── config_test.go │   ├── context │   │   ├── acceptencoder.go │   │   ├── acceptencoder_test.go │   │   ├── context.go │   │   ├── context_test.go │   │   ├── input.go │   │   ├── input_test.go │   │   ├── output.go │   │   ├── param │   │   │   ├── conv.go │   │   │   ├── methodparams.go │   │   │   ├── options.go │   │   │   ├── parsers.go │   │   │   └── parsers_test.go │   │   ├── renderer.go │   │   └── response.go │   ├── CONTRIBUTING.md │   ├── controller.go │   ├── controller_test.go │   ├── doc.go │   ├── error.go │   ├── error_test.go │   ├── filter.go │   ├── filter_test.go │   ├── flash.go │   ├── flash_test.go │   ├── grace │   │   ├── conn.go │   │   ├── grace.go │   │   ├── listener.go │   │   └── server.go │   ├── hooks.go │   ├── httplib │   │   ├── httplib.go │   │   ├── httplib_test.go │   │   └── README.md │   ├── LICENSE │   ├── log.go │   ├── logs │   │   ├── accesslog.go │   │   ├── alils │   │   │   ├── alils.go │   │   │   ├── config.go │   │   │   ├── log_config.go │   │   │   ├── log.pb.go │   │   │   ├── log_project.go │   │   │   ├── log_store.go │   │   │   ├── machine_group.go │   │   │   ├── request.go │   │   │   └── signature.go │   │   ├── color.go │   │   ├── color_windows.go │   │   ├── color_windows_test.go │   │   ├── conn.go │   │   ├── conn_test.go │   │   ├── console.go │   │   ├── console_test.go │   │   ├── es │   │   │   └── es.go │   │   ├── file.go │   │   ├── file_test.go │   │   ├── jianliao.go │   │   ├── logger.go │   │   ├── logger_test.go │   │   ├── log.go │   │   ├── multifile.go │   │   ├── multifile_test.go │   │   ├── README.md │   │   ├── slack.go │   │   ├── smtp.go │   │   └── smtp_test.go │   ├── migration │   │   ├── ddl.go │   │   ├── doc.go │   │   └── migration.go │   ├── mime.go │   ├── namespace.go │   ├── namespace_test.go │   ├── orm │   │   ├── cmd.go │   │   ├── cmd_utils.go │   │   ├── db_alias.go │   │   ├── db.go │   │   ├── db_mysql.go │   │   ├── db_oracle.go │   │   ├── db_postgres.go │   │   ├── db_sqlite.go │   │   ├── db_tables.go │   │   ├── db_tidb.go │   │   ├── db_utils.go │   │   ├── models_boot.go │   │   ├── models_fields.go │   │   ├── models.go │   │   ├── models_info_f.go │   │   ├── models_info_m.go │   │   ├── models_test.go │   │   ├── models_utils.go │   │   ├── orm_conds.go │   │   ├── orm.go │   │   ├── orm_log.go │   │   ├── orm_object.go │   │   ├── orm_querym2m.go │   │   ├── orm_queryset.go │   │   ├── orm_raw.go │   │   ├── orm_test.go │   │   ├── qb.go │   │   ├── qb_mysql.go │   │   ├── qb_tidb.go │   │   ├── README.md │   │   ├── types.go │   │   ├── utils.go │   │   └── utils_test.go │   ├── parser.go │   ├── plugins │   │   ├── apiauth │   │   │   ├── apiauth.go │   │   │   └── apiauth_test.go │   │   ├── auth │   │   │   └── basic.go │   │   ├── authz │   │   │   ├── authz.go │   │   │   ├── authz_model.conf │   │   │   ├── authz_policy.csv │   │   │   ├── authz_test.go │   │   │   ├── go.mod │   │   │   ├── go.sum │   │   │   └── go.work │   │   └── cors │   │   ├── cors.go │   │   └── cors_test.go │   ├── policy.go │   ├── README.md │   ├── router.go │   ├── router_test.go │   ├── session │   │   ├── couchbase │   │   │   └── sess_couchbase.go │   │   ├── ledis │   │   │   └── ledis_session.go │   │   ├── memcache │   │   │   └── sess_memcache.go │   │   ├── mysql │   │   │   └── sess_mysql.go │   │   ├── postgres │   │   │   └── sess_postgresql.go │   │   ├── README.md │   │   ├── redis │   │   │   └── sess_redis.go │   │   ├── redis_cluster │   │   │   └── redis_cluster.go │   │   ├── sess_cookie.go │   │   ├── sess_cookie_test.go │   │   ├── sess_file.go │   │   ├── session.go │   │   ├── sess_mem.go │   │   ├── sess_mem_test.go │   │   ├── sess_test.go │   │   ├── sess_utils.go │   │   └── ssdb │   │   └── sess_ssdb.go │   ├── staticfile.go │   ├── staticfile_test.go │   ├── swagger │   │   └── swagger.go │   ├── templatefunc.go │   ├── templatefunc_test.go │   ├── template.go │   ├── template_test.go │   ├── testing │   │   ├── assertions.go │   │   └── client.go │   ├── toolbox │   │   ├── healthcheck.go │   │   ├── profile.go │   │   ├── profile_test.go │   │   ├── statistics.go │   │   ├── statistics_test.go │   │   ├── task.go │   │   └── task_test.go │   ├── tree.go │   ├── tree_test.go │   ├── unregroute_test.go │   ├── utils │   │   ├── caller.go │   │   ├── caller_test.go │   │   ├── captcha │   │   │   ├── captcha.go │   │   │   ├── image.go │   │   │   ├── image_test.go │   │   │   ├── LICENSE │   │   │   ├── README.md │   │   │   ├── siprng.go │   │   │   └── siprng_test.go │   │   ├── debug.go │   │   ├── debug_test.go │   │   ├── file.go │   │   ├── file_test.go │   │   ├── mail.go │   │   ├── mail_test.go │   │   ├── pagination │   │   │   ├── controller.go │   │   │   ├── doc.go │   │   │   ├── paginator.go │   │   │   └── utils.go │   │   ├── rand.go │   │   ├── rand_test.go │   │   ├── safemap.go │   │   ├── safemap_test.go │   │   ├── slice.go │   │   ├── slice_test.go │   │   ├── testdata │   │   │   └── grepe.test │   │   └── utils.go │   └── validation │   ├── README.md │   ├── util.go │   ├── util_test.go │   ├── validation.go │   ├── validation_test.go │   └── validators.go ├── casbin │   └── casbin │   └── v2@v2.65.2 │   ├── casbin-logo.png │   ├── config │   │   ├── config.go │   │   ├── config_test.go │   │   └── testdata │   │   └── testini.ini │   ├── constant │   │   └── constants.go │   ├── CONTRIBUTING.md │   ├── effector │   │   ├── default_effector.go │   │   └── effector.go │   ├── enforcer_cached_b_test.go │   ├── enforcer_cached.go │   ├── enforcer_cached_test.go │   ├── enforcer_distributed.go │   ├── enforcer.go │   ├── enforcer_interface.go │   ├── enforcer_synced.go │   ├── enforcer_synced_test.go │   ├── enforcer_test.go │   ├── errors │   │   └── rbac_errors.go │   ├── error_test.go │   ├── examples │   │   ├── abac_model.conf │   │   ├── abac_not_using_policy_model.conf │   │   ├── abac_rule_effect_policy.csv │   │   ├── abac_rule_model.conf │   │   ├── abac_rule_policy.csv │   │   ├── basic_inverse_policy.csv │   │   ├── basic_model.conf │   │   ├── basic_model_without_spaces.conf │   │   ├── basic_policy.csv │   │   ├── basic_without_resources_model.conf │   │   ├── basic_without_resources_policy.csv │   │   ├── basic_without_users_model.conf │   │   ├── basic_without_users_policy.csv │   │   ├── basic_with_root_model.conf │   │   ├── comment_model.conf │   │   ├── error │   │   │   ├── error_model.conf │   │   │   └── error_policy.csv │   │   ├── eval_operator_model.conf │   │   ├── eval_operator_policy.csv │   │   ├── glob_model.conf │   │   ├── glob_policy.csv │   │   ├── ipmatch_model.conf │   │   ├── ipmatch_policy.csv │   │   ├── keyget2_model.conf │   │   ├── keyget_model.conf │   │   ├── keymatch2_model.conf │   │   ├── keymatch2_policy.csv │   │   ├── keymatch_custom_model.conf │   │   ├── keymatch_model.conf │   │   ├── keymatch_policy.csv │   │   ├── multiple_policy_definitions_model.conf │   │   ├── multiple_policy_definitions_policy.csv │   │   ├── performance │   │   │   ├── rbac_with_pattern_large_scale_model.conf │   │   │   └── rbac_with_pattern_large_scale_policy.csv │   │   ├── priority_indeterminate_policy.csv │   │   ├── priority_model.conf │   │   ├── priority_model_explicit.conf │   │   ├── priority_model_explicit_customized.conf │   │   ├── priority_policy.csv │   │   ├── priority_policy_explicit.csv │   │   ├── priority_policy_explicit_customized.csv │   │   ├── rbac_model.conf │   │   ├── rbac_model_in_multi_line.conf │   │   ├── rbac_model_matcher_using_in_op_bracket.conf │   │   ├── rbac_model_matcher_using_in_op.conf │   │   ├── rbac_policy.csv │   │   ├── rbac_with_all_pattern_model.conf │   │   ├── rbac_with_all_pattern_policy.csv │   │   ├── rbac_with_deny_model.conf │   │   ├── rbac_with_deny_policy.csv │   │   ├── rbac_with_domain_pattern_model.conf │   │   ├── rbac_with_domain_pattern_policy.csv │   │   ├── rbac_with_domains_model.conf │   │   ├── rbac_with_domains_policy2.csv │   │   ├── rbac_with_domains_policy.csv │   │   ├── rbac_with_hierarchy_policy.csv │   │   ├── rbac_with_hierarchy_with_domains_policy.csv │   │   ├── rbac_with_multiple_policy_model.conf │   │   ├── rbac_with_multiple_policy_policy.csv │   │   ├── rbac_with_not_deny_model.conf │   │   ├── rbac_with_pattern_model.conf │   │   ├── rbac_with_pattern_policy.csv │   │   ├── rbac_with_resource_roles_model.conf │   │   ├── rbac_with_resource_roles_policy.csv │   │   ├── subject_priority_model.conf │   │   ├── subject_priority_model_with_domain.conf │   │   ├── subject_priority_policy.csv │   │   └── subject_priority_policy_with_domain.csv │   ├── filter_test.go │   ├── frontend.go │   ├── frontend_old.go │   ├── frontend_old_test.go │   ├── frontend_test.go │   ├── go.mod │   ├── go.sum │   ├── internal_api.go │   ├── LICENSE │   ├── log │   │   ├── default_logger.go │   │   ├── logger.go │   │   ├── log_util.go │   │   ├── log_util_test.go │   │   └── mocks │   │   └── mock_logger.go │   ├── Makefile │   ├── management_api.go │   ├── management_api_test.go │   ├── managerment_api_b_test.go │   ├── model │   │   ├── assertion.go │   │   ├── function.go │   │   ├── model.go │   │   ├── model_test.go │   │   └── policy.go │   ├── model_b_test.go │   ├── model_test.go │   ├── persist │   │   ├── adapter_filtered.go │   │   ├── adapter.go │   │   ├── batch_adapter.go │   │   ├── cache │   │   │   ├── cache.go │   │   │   └── default-cache.go │   │   ├── dispatcher.go │   │   ├── file-adapter │   │   │   ├── adapter_filtered.go │   │   │   ├── adapter.go │   │   │   └── adapter_mock.go │   │   ├── persist_test.go │   │   ├── string-adapter │   │   │   ├── adapter.go │   │   │   └── adapter_test.go │   │   ├── update_adapter.go │   │   ├── watcher_ex.go │   │   ├── watcher.go │   │   └── watcher_update.go │   ├── rbac │   │   ├── default-role-manager │   │   │   ├── role_manager.go │   │   │   └── role_manager_test.go │   │   └── role_manager.go │   ├── rbac_api.go │   ├── rbac_api_synced.go │   ├── rbac_api_test.go │   ├── rbac_api_with_domains.go │   ├── rbac_api_with_domains_synced.go │   ├── rbac_api_with_domains_test.go │   ├── README.md │   ├── role_manager_b_test.go │   ├── util │   │   ├── builtin_operators.go │   │   ├── builtin_operators_test.go │   │   ├── util.go │   │   └── util_test.go │   ├── watcher_ex_test.go │   ├── watcher_test.go │   └── watcher_update_test.go └── tree.md 62 directories, 369 files ```

Steps to reproduce the behavior:

  1. cd astaxie/beego/plugins/authz
  2. go test
  3. Terminal message
    PASS
    ok      beego-authz     0.007s
  4. Use go to definition on casbin object
  5. Show error message on OUTPUT
    [Error - 11:17:12 AM] Request textDocument/definition failed.
    Message: no object found for ident Enforcer
    Code: 0 

Screenshots or recordings

If applicable, add screenshots or recordings to help explain your problem.

OUTPUT message image

Terminal message image

findleyr commented 1 year ago

Hi, which directory are you opening in VS Code?

I suspect you are opening a parent of the authz directory, in which case gopls does not know which module you're working on (we're working on improving this in https://go.dev/issue/57979)

This is a multi-module repository. If you want to work on both modules simultaneously, you can use a go.work file.

go work init astaxie/beego astaxie/beego/plugins/authz casbin/casbin/v2@v2.65.2
yapinxxx commented 1 year ago

Hi, which directory are you opening in VS Code?

I suspect you are opening a parent of the authz directory, in which case gopls does not know which module you're working on (we're working on improving this in https://go.dev/issue/57979)

Thank you for your response. Yes, I had provided my folder tree and you are correct that the directory I opened in VSCode is the parent of plugins/authz and casbin/v2@v2.65.2. I had tried to create a go.work file under plugins/authz, like this:

go 1.20

use (
    .
    ../../../../casbin/casbin/v2@v2.65.2
)

However, it did not work.

This is a multi-module repository. If you want to work on both modules simultaneously, you can use a go.work file.

go work init astaxie/beego astaxie/beego/plugins/authz casbin/casbin/v2@v2.65.2

After your reminder, I found that if I open the directory where the go.work file is located, then it will work! The origin of this problem is that my project is quite large but it does not have a go.work file in the root directory, which made debugging difficult for me. But now, perhaps I can persuade the team leader to create a go.work file in the root directory. Thank you again for your help.

By the way, I noticed that even if I open the authz directory in VSCode without the go.work file, the go to definition feature still works correctly.

findleyr commented 1 year ago

Thanks, this sounds like it is WAI. We are working to improve this behavior by default in a future version of gopls: https://go.dev/issue/57979.