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

Fix GoToDefinition issues in non-compliant repositories lacking go.sum in GOMODCACHE #1777

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions ycmd/completers/go/go_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# You should have received a copy of the GNU General Public License
# along with ycmd. If not, see <http://www.gnu.org/licenses/>.

from pathlib import Path
import subprocess
import json
import logging
import os
Expand All @@ -34,6 +36,18 @@
utils.ExecutableName( 'gopls' ) ) )


def GetGoModCachePath():
try:
result = subprocess.run(['go', 'env', 'GOMODCACHE'], capture_output=True, text=True, check=True)
return [ result.stdout.strip() ]
except subprocess.CalledProcessError as e:
utils.LOGGER.info( 'Error getting GOMODCACHE: %s', e )
return []


PATHS_TO_GO_MOD_CACHE = GetGoModCachePath()


def ShouldEnableGoCompleter( user_options ):
server_exists = utils.FindExecutableWithFallback(
user_options[ 'gopls_binary_path' ],
Expand All @@ -51,6 +65,12 @@ def __init__( self, user_options ):
self._gopls_path = utils.FindExecutableWithFallback(
user_options[ 'gopls_binary_path' ],
PATH_TO_GOPLS )
# Exclude paths within the Go module cache or other directories where a go.sum cannot be created.
excluded_workspace_paths = user_options.get('gopls_excluded_workspace_paths')
if isinstance(excluded_workspace_paths, list):
self._excluded_workspace_paths = excluded_workspace_paths
else:
self._excluded_workspace_paths = PATHS_TO_GO_MOD_CACHE


def GetServerName( self ):
Expand All @@ -65,6 +85,18 @@ def GetProjectRootFiles( self ):
return [ 'go.mod' ]


def GetWorkspaceForFilepath( self, filepath, strict = False ):
filepath_abs = os.path.abspath(filepath)
if any(excluded_path in filepath_abs for excluded_path in self._excluded_workspace_paths):
return None
project_root_files = self.GetProjectRootFiles()
for folder in utils.PathsToAllParentFolders( filepath ):
for root_file in project_root_files:
if next( Path( folder ).glob( root_file ), [] ):
return folder
return None if strict else os.path.dirname( filepath )


def GetCommandLine( self ):
cmdline = [ self._gopls_path ] + self._user_supplied_gopls_args + [
'-logfile',
Expand Down
1 change: 1 addition & 0 deletions ycmd/default_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"disable_signature_help": 0,
"gopls_binary_path": "",
"gopls_args": [],
"gopls_excluded_workspace_paths": [],
"rust_toolchain_root": "",
"tsserver_binary_path": "",
"roslyn_binary_path": "",
Expand Down