golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
121.11k stars 17.37k forks source link

x/tools/gopls: Bad behavior after updating gopls, duplicated suggestions and unexpected placeholders. #60959

Closed miranquil closed 12 months ago

miranquil commented 1 year ago

gopls version

golang.org/x/tools/gopls v0.12.3
    golang.org/x/tools/gopls@v0.12.3 h1:u0wCI9uvt7mnmri6bFBIaWw1XCN6PN8hKv55Zwd+GbE=

go env

GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/miranquil/Library/Caches/go-build"
GOENV="/Users/miranquil/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/miranquil/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/miranquil/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/miranquil/sdk/go1.19.5"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/miranquil/sdk/go1.19.5/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.19.5"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/miranquil/lumibay/donkey/go.mod"
GOWORK="/Users/miranquil/lumibay/donkey/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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/0n/d4s955_n0m748tnz3whw42n00000gn/T/go-build3368496042=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

First, I have a project in struct like this:

|- dao
  |- generic.go
    |- func GWhereFirst
|- service
  |- teaching
    |- teaching_auth.go

I'm trying to call dao.GWhereFirst from teaching_auth.go.

And I'm sure that "ui.completion.usePlaceholders" is false in settings.

What did you expect to see?

Regular suggestions, and no placeholders placed when I press ENTER.

And after founding out this issue, I rollbacked gopls to v0.11.0, which runs perfectly: good

What did you see instead?

bad

The interesting thing is that if I delete the code generated with "import dao" left, then everything works fine. Just like the gif shows.

Editor and settings

VSCode

Version: 1.79.2
Commit: 695af097c7bd098fbf017ce3ac85e09bbc5dda06
Date: 2023-06-14T08:58:33.551Z (1 wk ago)
Electron: 22.5.7
Chromium: 108.0.5359.215
Node.js: 16.17.1
V8: 10.8.168.25-electron.0
OS: Darwin arm64 22.5.0

settings:

{
  "vim.autoSwitchInputMethod.enable": true,
  "editor.fontFamily": "'Iosevka', 'Fantasque Sans Mono','MonoLisa Miranquil', 'LXGW WenKai Mono',  Menlo, Monaco, 'Courier New', 微软雅黑, monospace",
  "editor.fontLigatures": "'dlig', 'VXLA' 2",
  // "editor.fontLigatures": "'ss15', 'dlig', 'VXLA' 2",
  "files.autoSave": "afterDelay",
  "workbench.editorAssociations": {},
  "workbench.colorCustomizations": {
    "editorCursor.foreground": "#B79147",
    "terminalCursor.foreground": "#B79147"
  },
  "python.languageServer": "Pylance",
  "editor.smoothScrolling": true,
  "terminal.integrated.fontFamily": "\"CaskaydiaCove Nerd Font Mono\"",
  "terminal.integrated.cursorStyle": "underline",
  "terminal.integrated.inheritEnv": false,
  "vim.cursorStylePerMode.insert": "underline",
  "security.workspace.trust.untrustedFiles": "open",
  "vim.startofline": false,
  "go.toolsManagement.autoUpdate": true,
  "editor.semanticHighlighting.enabled": true,
  "editor.cursorSmoothCaretAnimation": "on",
  "rust-analyzer.debug.engine": "vadimcn.vscode-lldb",
  "todo-tree.ripgrep.ripgrep": "/Users/miranquil/.cargo/bin/rg",
  "editor.accessibilitySupport": "off",
  "editor.inlineSuggest.enabled": true,
  "rust-analyzer.hover.actions.references.enable": true,
  "go.inlayHints.parameterNames": true,
  "editor.formatOnPaste": true,
  "workbench.list.smoothScrolling": true,
  "terminal.integrated.smoothScrolling": true,
  "go.inlayHints.constantValues": true,
  "go.inlayHints.rangeVariableTypes": true,
  "git.autofetch": true,
  "git.confirmSync": false,
  "editor.foldingImportsByDefault": true,
  "editor.cursorSurroundingLines": 8,
  "vim.foldfix": true,
  "vim.normalModeKeyBindingsNonRecursive": [
    {
      "before": [
        "<C-d>"
      ],
      "after": [
        "1",
        "5",
        "g",
        "j",
        "z",
        "z",
      ]
    },
    {
      "before": [
        "<C-u>"
      ],
      "after": [
        "1",
        "5",
        "g",
        "k",
        "z",
        "z"
      ]
    },
    {
      "before": [
        "u"
      ],
      "after": [],
      "commands": [
        {
          "command": "undo",
          "args": []
        }
      ]
    },
    {
      "before": [
        "<C-r>"
      ],
      "after": [],
      "commands": [
        {
          "command": "redo",
          "args": []
        }
      ]
    }
  ],
  "files.insertFinalNewline": true,
  "files.trimFinalNewlines": true,
  "search.showLineNumbers": true,
  "gitlens.hovers.avatars": false,
  "gitlens.views.commits.avatars": false,
  "gitlens.views.repositories.avatars": false,
  "gitlens.views.fileHistory.avatars": false,
  "gitlens.views.lineHistory.avatars": false,
  "gitlens.views.branches.avatars": false,
  "gitlens.views.remotes.avatars": false,
  "gitlens.views.tags.avatars": false,
  "gitlens.views.worktrees.avatars": false,
  "gitlens.views.contributors.avatars": false,
  "gitlens.views.searchAndCompare.avatars": false,
  "gitlens.blame.avatars": false,
  "go.inlayHints.functionTypeParameters": true,
  "terminal.integrated.cursorBlinking": true,
  "editor.inlayHints.fontFamily": "Menlo",
  "editor.inlayHints.fontSize": 10,
  "[python]": {
    "editor.formatOnType": true
  },
  "files.exclude": {
    "**/.trunk/*out": true,
    "**/.trunk/*out/": true,
    "**/.trunk/*actions/": true,
    "**/.trunk/*logs/": true,
    "**/.trunk/*plugins/": true
  },
  "trunk.trunkGrayOutNonBlockingIssues": false,
  "files.watcherExclude": {
    "**/.trunk/*out": true,
    "**/.trunk/*out/": true,
    "**/.trunk/*actions/": true,
    "**/.trunk/*logs/": true,
    "**/.trunk/*plugins/": true
  },
  "go.editorContextMenuCommands": {
    "fillStruct": true,
    "benchmarkAtCursor": true
  },
  "todo-tree.general.showActivityBarBadge": true,
  "debug.console.fontFamily": "'CaskaydiaCove Nerd Font Mono', 'Sarasa Term SC'",
  "debug.console.fontSize": 12,
  "editor.lineNumbers": "relative",
  "editor.fontWeight": 500,
  "workbench.editor.titleScrollbarSizing": "large",
  "workbench.editor.highlightModifiedTabs": true,
  "editor.acceptSuggestionOnCommitCharacter": false,
  "editor.unfoldOnClickAfterEndOfLine": true,
  "editor.fontVariations": false,
  "editor.guides.bracketPairs": true,
  "workbench.colorTheme": "98878c8e-9f91-4e25-930d-dd7d280d9e35",
  "extensions.experimental.affinity": {
    "asvetliakov.vscode-neovim": 1
  },
  "editor.cursorStyle": "underline",
  "[markdown]": {
    "editor.defaultFormatter": "DavidAnson.vscode-markdownlint"
  },
  "markdownlint.config": {
    "MD010": {
      "spaces_per_tab": 4
    }
  },
  "pangu.auto_format_on_save": true,
  "editor.formatOnSave": true,
  "[go]": {
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
      "source.organizeImports": true
    }
  },
  "gopls": {
    "formatting.local": "infra",
  },
  "go.formatTool": "goimports",
  "workbench.iconTheme": "eq-material-theme-icons",
  "vscode-neovim.neovimExecutablePaths.darwin": "/opt/homebrew/bin/nvim",
  "lldb.library": "/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/LLDB",
  "codeium.enableConfig": {
    "*": true,
    "go": false
  },
  "editor.inlayHints.padding": true,
  "editor.inlayHints.enabled": "offUnlessPressed",
  "workbench.startupEditor": "none",
  "editor.minimap.enabled": false,
  "tabout.charactersToTabOutFrom": [
    {
      "open": "[",
      "close": "]"
    },
    {
      "open": "{",
      "close": "}"
    },
    {
      "open": "(",
      "close": ")"
    },
    {
      "open": "'",
      "close": "'"
    },
    {
      "open": "\"",
      "close": "\""
    },
    {
      "open": ":",
      "close": ":"
    },
    {
      "open": "=",
      "close": "="
    },
    {
      "open": ">",
      "close": ">"
    },
    {
      "open": "<",
      "close": "<"
    },
    {
      "open": ".",
      "close": "."
    },
    {
      "open": "`",
      "close": "`"
    },
    {
      "open": ";",
      "close": ";"
    }
  ],
  "protoc": {
    "options": [
      "--proto_path=${workspaceFolder}/protopb"
    ]
  },
  "editor.fontSize": 14,
  "evenBetterToml.semanticTokens": true,
  "explorer.autoReveal": "focusNoScroll",
  "vim.autoSwitchInputMethod.defaultIM": "com.apple.keylayout.ABC",
  "vim.autoSwitchInputMethod.obtainIMCmd": "/usr/local/bin/im-select",
  "vim.autoSwitchInputMethod.switchIMCmd": "/usr/local/bin/im-select {im}",
  "go.languageServerFlags": [],
  "go.trace.server": "verbose",
}

Logs

I've added -logFile=/Users/miranquil/go.log to go.languageServerFlag. But after same operations nothing was written into this file.

findleyr commented 1 year ago

Thank you for the report.

This looks like a regression in unimported completion, however I can't quite reproduce the behavior you're seeing. I don't see the duplicated completion items, for example.

I see a couple bugs, which is that we don't populate a placeholder for type parameters, and aren't honoring the usePlaceholders setting.

Are you working in GOPATH mode (as it appears from the import path of the dao package)? Does the issue reproduce when you work within a module?

miranquil commented 1 year ago

Sorry English is not my native language so I'm not sure I've quite understanded what "GOPATH mode" means. The main project is already go mod init and also the dao package. And on the other hand my project is working with go.work.

Project's go.work:

go 1.18

use (
    .
    component
    dao
)

There's nothing about dao in Project's go.mod.

Also I've found that only the dao suffers from this issue. The packages like internal packages works well(excluding the placeholders' behavior).

I'm not sure if I have provided enough information. Please let me know if I can provice anything else.

gopherbot commented 1 year ago

Change https://go.dev/cl/506935 mentions this issue: gopls/internal/lsp/source/completion: placeholders for type params

gopherbot commented 1 year ago

Change https://go.dev/cl/506936 mentions this issue: gopls/internal/lsp/source/completion: honor usePlaceholders

adonovan commented 1 year ago

I've sent CLs to fix the two problems @findleyr identified in https://github.com/golang/go/issues/60959#issuecomment-1604398224. I've reproduced the third problem (duplicates) and confirmed that the cause is the presence of test variants in the known map used by unimported completions. I'll send a fix momentarily.

gopherbot commented 1 year ago

Change https://go.dev/cl/506937 mentions this issue: gopls/internal/lsp/source/completion: avoid duplicates from variants

miranquil commented 1 year ago

OMG it's Donovan!!!! I'm so excited for this.