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

x/tools/gopls: find-references doesn't always search tests #43144

Closed
muirdm opened this issue Dec 11, 2020 · 2 comments
Closed

x/tools/gopls: find-references doesn't always search tests #43144

muirdm opened this issue Dec 11, 2020 · 2 comments
Labels
FrozenDueToAge gopls/metadata Issues related to metadata loading in gopls gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@muirdm
Copy link

muirdm commented Dec 11, 2020

On master (d93e913), it seems like find-references doesn't return results from test files unless you've opened the test's package.

-- go.mod --
module foo

-- foo.go --
package main

import "foo/bar"

func main() {
	_ = bar.Blah
}

-- bar/bar.go --
package bar

var Blah = 123

-- bar/bar_test.go --
package bar

func TestBar() {
	_ = Blah
}

Open only foo.go in your editor and run find-references on Blah. I expect to get foo.go and bar_test.go, but I only get foo.go:

[Trace - 02:30:05 PM] Sending request 'initialize - (43205)'.
Params: {
  "processId": null,
  "rootPath": "/Users/muir/scratch/foo",
  "clientInfo": {
    "name": "emacs",
    "version": "GNU Emacs 28.0.50 (build 1, x86_64-apple-darwin19.6.0, NS appkit-1894.60 Version 10.15.7 (Build 19H2))\n of 2020-11-06"
  },
  "rootUri": "file:///Users/muir/scratch/foo",
  "capabilities": {
    "workspace": {
      "workspaceEdit": {
        "documentChanges": true,
        "resourceOperations": [
          "create",
          "rename",
          "delete"
        ]
      },
      "applyEdit": true,
      "symbol": {
        "symbolKind": {
          "valueSet": [
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12,
            13,
            14,
            15,
            16,
            17,
            18,
            19,
            20,
            21,
            22,
            23,
            24,
            25,
            26
          ]
        }
      },
      "executeCommand": {
        "dynamicRegistration": false
      },
      "workspaceFolders": true,
      "configuration": true
    },
    "textDocument": {
      "declaration": {
        "linkSupport": true
      },
      "definition": {
        "linkSupport": true
      },
      "implementation": {
        "linkSupport": true
      },
      "typeDefinition": {
        "linkSupport": true
      },
      "synchronization": {
        "willSave": true,
        "didSave": true,
        "willSaveWaitUntil": true
      },
      "documentSymbol": {
        "symbolKind": {
          "valueSet": [
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12,
            13,
            14,
            15,
            16,
            17,
            18,
            19,
            20,
            21,
            22,
            23,
            24,
            25,
            26
          ]
        },
        "hierarchicalDocumentSymbolSupport": true
      },
      "formatting": {
        "dynamicRegistration": true
      },
      "rangeFormatting": {
        "dynamicRegistration": true
      },
      "semanticTokens": {
        "dynamicRegistration": true,
        "requests": {
          "range": true,
          "full": true
        },
        "tokenModifiers": [],
        "tokenTypes": [
          "comment",
          "keyword",
          "string",
          "number",
          "regexp",
          "operator",
          "namespace",
          "type",
          "struct",
          "class",
          "interface",
          "enum",
          "typeParameter",
          "function",
          "member",
          "property",
          "macro",
          "variable",
          "parameter",
          "label",
          "enumConstant",
          "dependent",
          "concept"
        ],
        "formats": [
          "relative"
        ]
      },
      "rename": {
        "dynamicRegistration": true,
        "prepareSupport": true
      },
      "codeAction": {
        "dynamicRegistration": true,
        "isPreferredSupport": true,
        "codeActionLiteralSupport": {
          "codeActionKind": {
            "valueSet": [
              "",
              "quickfix",
              "refactor",
              "refactor.extract",
              "refactor.inline",
              "refactor.rewrite",
              "source",
              "source.organizeImports"
            ]
          }
        }
      },
      "completion": {
        "completionItem": {
          "snippetSupport": true,
          "documentationFormat": [
            "markdown"
          ],
          "resolveAdditionalTextEditsSupport": true
        },
        "contextSupport": true
      },
      "signatureHelp": {
        "signatureInformation": {
          "parameterInformation": {
            "labelOffsetSupport": true
          }
        }
      },
      "documentLink": {
        "dynamicRegistration": true,
        "tooltipSupport": true
      },
      "hover": {
        "contentFormat": [
          "markdown",
          "plaintext"
        ]
      },
      "foldingRange": {
        "dynamicRegistration": true
      },
      "callHierarchy": {
        "dynamicRegistration": false
      },
      "publishDiagnostics": {
        "relatedInformation": true,
        "tagSupport": {
          "valueSet": [
            1,
            2
          ]
        },
        "versionSupport": true
      }
    },
    "window": {
      "workDoneProgress": true
    }
  },
  "initializationOptions": null,
  "workDoneToken": "1"
}


[Trace - 02:30:05 PM] Received response 'initialize - (43205)' in 35ms.
Result: {
  "capabilities": {
    "textDocumentSync": {
      "openClose": true,
      "change": 2,
      "save": {}
    },
    "completionProvider": {
      "triggerCharacters": [
        "."
      ]
    },
    "hoverProvider": true,
    "signatureHelpProvider": {
      "triggerCharacters": [
        "(",
        ","
      ]
    },
    "definitionProvider": true,
    "typeDefinitionProvider": true,
    "implementationProvider": true,
    "referencesProvider": true,
    "documentHighlightProvider": true,
    "documentSymbolProvider": true,
    "codeActionProvider": {
      "codeActionKinds": [
        "quickfix",
        "refactor.extract",
        "refactor.rewrite",
        "source.fixAll",
        "source.organizeImports"
      ]
    },
    "codeLensProvider": {},
    "documentLinkProvider": {},
    "workspaceSymbolProvider": true,
    "documentFormattingProvider": true,
    "documentOnTypeFormattingProvider": {
      "firstTriggerCharacter": ""
    },
    "renameProvider": {
      "prepareProvider": true
    },
    "foldingRangeProvider": true,
    "executeCommandProvider": {
      "commands": [
        "gopls.generate",
        "gopls.fill_struct",
        "gopls.regenerate_cgo",
        "gopls.test",
        "gopls.tidy",
        "gopls.update_go_sum",
        "gopls.undeclared_name",
        "gopls.go_get_package",
        "gopls.add_dependency",
        "gopls.upgrade_dependency",
        "gopls.remove_dependency",
        "gopls.vendor",
        "gopls.extract_variable",
        "gopls.extract_function",
        "gopls.gc_details",
        "gopls.generate_gopls_mod"
      ]
    },
    "callHierarchyProvider": true,
    "workspace": {
      "workspaceFolders": {
        "supported": true,
        "changeNotifications": "workspace/didChangeWorkspaceFolders"
      }
    }
  },
  "serverInfo": {
    "name": "gopls",
    "version": "{\"path\":\"golang.org/x/tools/gopls\",\"version\":\"(devel)\",\"deps\":[{\"path\":\"github.com/BurntSushi/toml\",\"version\":\"v0.3.1\",\"sum\":\"h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=\"},{\"path\":\"github.com/google/go-cmp\",\"version\":\"v0.5.1\",\"sum\":\"h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=\"},{\"path\":\"github.com/sergi/go-diff\",\"version\":\"v1.1.0\",\"sum\":\"h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=\"},{\"path\":\"golang.org/x/mod\",\"version\":\"v0.3.0\",\"sum\":\"h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=\"},{\"path\":\"golang.org/x/sync\",\"version\":\"v0.0.0-20201020160332-67f06af15bc9\",\"sum\":\"h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck=\"},{\"path\":\"golang.org/x/tools\",\"version\":\"v0.0.0-20201021214918-23787c007979\",\"replace\":{\"path\":\"../\"}},{\"path\":\"golang.org/x/xerrors\",\"version\":\"v0.0.0-20200804184101-5ec99f83aff1\",\"sum\":\"h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=\"},{\"path\":\"honnef.co/go/tools\",\"version\":\"v0.0.1-2020.1.6\",\"sum\":\"h1:W18jzjh8mfPez+AwGLxmOImucz/IFjpNlrKVnaj2YVc=\"},{\"path\":\"mvdan.cc/gofumpt\",\"version\":\"v0.0.0-20200927160801-5bfeb2e70dd6\",\"sum\":\"h1:z+/YqapuV7VZPvBb3GYmuEJbA88M3PFUxaHilHYVCpQ=\"},{\"path\":\"mvdan.cc/xurls/v2\",\"version\":\"v2.2.0\",\"sum\":\"h1:NSZPykBXJFCetGZykLAxaL6SIpvbVy/UFEniIfHAa8A=\"}]}"
  }
}


[Trace - 02:30:05 PM] Sending notification 'initialized'.
Params: {}


[Trace - 02:30:05 PM] Sending notification 'textDocument/didOpen'.
Params: {
  "textDocument": {
    "uri": "file:///Users/muir/scratch/foo/foo.go",
    "languageId": "go",
    "version": 3185,
    "text": "package main\n\nimport \"foo/bar\"\n\nfunc main() {\n\t_ = bar.Blah\n}\n"
  }
}


[Trace - 02:30:05 PM] Sending request 'textDocument/codeAction - (43206)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/muir/scratch/foo/foo.go"
  },
  "range": {
    "start": {
      "line": 7,
      "character": 0
    },
    "end": {
      "line": 7,
      "character": 0
    }
  },
  "context": {
    "diagnostics": []
  }
}


[Trace - 02:30:05 PM] Sending request 'textDocument/documentHighlight - (43207)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/muir/scratch/foo/foo.go"
  },
  "position": {
    "line": 7,
    "character": 0
  }
}


[Trace - 02:30:05 PM] Received request 'window/workDoneProgress/create - (1).
Params: {
  "token": "5577006791947779410"
}


[Trace - 02:30:05 PM] Sending response 'window/workDoneProgress/create - (1)'. Processing request took 0ms
Params: {
  "jsonrpc": "2.0",
  "id": 1,
  "result": null
}


[Trace - 02:30:05 PM] Received notification '$/progress'.
Params: {
  "token": "5577006791947779410",
  "value": {
    "kind": "begin",
    "title": "Setting up workspace",
    "message": "Loading packages..."
  }
}


[Trace - 02:30:05 PM] Received request 'workspace/configuration - (2).
Params: {
  "items": [
    {
      "scopeUri": "file:///Users/muir/scratch/foo",
      "section": "gopls"
    }
  ]
}


[Trace - 02:30:05 PM] Sending response 'workspace/configuration - (2)'. Processing request took 0ms
Params: {
  "jsonrpc": "2.0",
  "id": 2,
  "result": [
    {
      "completeUnimported": true,
      "gofumpt": true,
      "completionDocumentation": false,
      "staticcheck": true,
      "verboseOutput": false,
      "deepCompletion": true,
      "linksInHover": false,
      "hoverKind": "SynopsisDocumentation",
      "allExperiments": true,
      "experimentalDiagnosticsDelay": "10s",
      "semanticTokens": true,
      "codelenses": {
        "gc_details": false,
        "test": false
      },
      "analyses": {
        "ST1000": false,
        "ST1003": false,
        "ST1016": false,
        "ST1021": false,
        "SA5011": false,
        "ST1020": false,
        "unusedparams": false
      }
    }
  ]
}


[Trace - 02:30:05 PM] Received notification 'window/logMessage'.
Params: {
  "type": 3,
  "message": "2020/12/11 14:30:05 go env for /Users/muir/scratch/foo\n(root /Users/muir/scratch/foo)\n(go version go version go1.15.3 darwin/amd64)\n(valid build configuration = true)\n(build flags: [])\nGOPROXY=https://proxy.golang.org,direct\nGOCACHE=/Users/muir/Library/Caches/go-build\nGONOSUMDB=\nGONOPROXY=\nGOPATH=/Users/muir/go\nGOSUMDB=sum.golang.org\nGOINSECURE=\nGOMODCACHE=/Users/muir/go/pkg/mod\nGOPRIVATE=\nGO111MODULE=\nGOFLAGS=\nGOMOD=/Users/muir/scratch/foo/go.mod\nGOROOT=/usr/local/Cellar/go/1.15.3/libexec\n\n"
}


[Trace - 02:30:05 PM] Received notification 'window/logMessage'.
Params: {
  "type": 3,
  "message": "2020/12/11 14:30:05 go/packages.Load\n\tsnapshot=0\n\tdirectory=/Users/muir/scratch/foo\n\tquery=[builtin foo/...]\n\tpackages=12\n"
}


[Trace - 02:30:06 PM] Received notification '$/progress'.
Params: {
  "token": "5577006791947779410",
  "value": {
    "kind": "end",
    "message": "Finished loading packages."
  }
}


[Trace - 02:30:06 PM] Received response 'textDocument/codeAction - (43206)' in 587ms.
Result: null


[Trace - 02:30:06 PM] Received response 'textDocument/documentHighlight - (43207)' in 586ms.
Result: []


[Trace - 02:30:26 PM] Sending request 'textDocument/codeAction - (43208)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/muir/scratch/foo/foo.go"
  },
  "range": {
    "start": {
      "line": 5,
      "character": 12
    },
    "end": {
      "line": 5,
      "character": 12
    }
  },
  "context": {
    "diagnostics": []
  }
}


[Trace - 02:30:26 PM] Sending request 'textDocument/documentHighlight - (43209)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/muir/scratch/foo/foo.go"
  },
  "position": {
    "line": 5,
    "character": 12
  }
}


[Trace - 02:30:26 PM] Sending request 'textDocument/hover - (43210)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/muir/scratch/foo/foo.go"
  },
  "position": {
    "line": 5,
    "character": 12
  }
}


[Trace - 02:30:26 PM] Received response 'textDocument/codeAction - (43208)' in 22ms.
Result: null


[Trace - 02:30:26 PM] Received response 'textDocument/documentHighlight - (43209)' in 22ms.
Result: [
  {
    "range": {
      "start": {
        "line": 5,
        "character": 9
      },
      "end": {
        "line": 5,
        "character": 13
      }
    },
    "kind": 1
  }
]


[Trace - 02:30:26 PM] Received response 'textDocument/hover - (43210)' in 22ms.
Result: {
  "contents": {
    "kind": "markdown",
    "value": "```go\nvar bar.Blah int\n```"
  },
  "range": {
    "start": {
      "line": 5,
      "character": 9
    },
    "end": {
      "line": 5,
      "character": 13
    }
  }
}


[Trace - 02:30:28 PM] Sending request 'textDocument/references - (43211)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/muir/scratch/foo/foo.go"
  },
  "position": {
    "line": 5,
    "character": 12
  },
  "context": {
    "includeDeclaration": false
  }
}


[Trace - 02:30:28 PM] Received response 'textDocument/references - (43211)' in 2ms.
Result: [
  {
    "uri": "file:///Users/muir/scratch/foo/foo.go",
    "range": {
      "start": {
        "line": 5,
        "character": 9
      },
      "end": {
        "line": 5,
        "character": 13
      }
    }
  }
]


[Trace - 02:30:29 PM] Sending request 'textDocument/codeAction - (43212)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/muir/scratch/foo/foo.go"
  },
  "range": {
    "start": {
      "line": 5,
      "character": 9
    },
    "end": {
      "line": 5,
      "character": 9
    }
  },
  "context": {
    "diagnostics": []
  }
}


[Trace - 02:30:29 PM] Received response 'textDocument/codeAction - (43212)' in 16ms.
Result: null

If I jump into bar.go and then run find-references again, it returns all expected results.

@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Dec 11, 2020
@gopherbot gopherbot added this to the Unreleased milestone Dec 11, 2020
@stamblerre stamblerre added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 16, 2020
@stamblerre stamblerre added this to Needs Triage in vscode-go: gopls by default via automation Dec 21, 2020
@stamblerre stamblerre removed this from Needs Triage in vscode-go: gopls by default Dec 23, 2020
@stamblerre
Copy link
Contributor

stamblerre commented Dec 23, 2020

Thanks for filing this -- I'm able to reproduce. I think the issue here is that test variants are not included in reverse dependencies because they cannot be found through the Imports map. My first thought is that we could look through the WorkspacePackages and check the value of forTest, looking for each reverse dependency. Alternatively, we could save a mapping from a package to its test variant.

@stamblerre stamblerre added this to To Do in gopls on-deck Feb 28, 2021
@stamblerre stamblerre moved this from To Do to P3 in gopls on-deck Aug 12, 2021
@findleyr findleyr added the gopls/metadata Issues related to metadata loading in gopls label May 10, 2022
@findleyr findleyr modified the milestones: gopls/later, gopls/v0.10.0 Oct 5, 2022
@gopherbot
Copy link

Change https://go.dev/cl/438755 mentions this issue: gopls/internal/lsp/source: find references in test packages

@golang golang locked and limited conversation to collaborators Oct 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge gopls/metadata Issues related to metadata loading in gopls gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
No open projects
Development

No branches or pull requests

4 participants