coc-groovy icon indicating copy to clipboard operation
coc-groovy copied to clipboard

Groovy Language Server Fails When File and Current Directory Are Not in the Same Path

Open marslo opened this issue 10 months ago • 1 comments

The Groovy language server only works when the current directory and the Groovy file are in the same location ( except $HOME/Desktop ). On $HOME/Desktop, even when the current directory matches the Groovy file’s location, the Groovy LSP does not work.

sample gif:

Image

In additional, I'd like to use the git root dir ( git rev-parse --show-toplevel ) as workspace if I'm working inside a git repo

The sample groovy script : `a.groovy` ...
#!/usr/bin/env groovy

import groovy.transform.Field

def foo( String str1, String str2 ) {
  println "${str1} ~~> ${str2}"
}

def foo( List<String> list ) {
  println list.join( ' ~~> ' )
}

def foo( Map<String, String> map ) {
  map.each { foo(it.key, it.value) }
}

// vim:tabstop=2:softtabstop=2:shiftwidth=2:expandtab:filetype=groovy
OS and Environment ...
$ sw_vers 
ProductName:		macOS
ProductVersion:		14.7.4
BuildVersion:		23H420

## :CocInfo
vim version: NVIM v0.11.0-dev-1668+g0985e784d8-Homebrew
node version: v23.7.0
coc.nvim version: 0.0.82-89cd1a23 2025-02-06 13:10:23 +0800
coc.nvim directory: /Users/marslo/.vim/plugged/coc.nvim
term: iTerm.app
platform: darwin
`:CocConfig`
{
  "snippets.ultisnips.pythonPrompt": false,
  "codeLens.enable": true,
  "groovy.java.home": "/Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home",
  "groovy.enable": true,
  "groovy.project.referencedLibraries": [
    "/opt/homebrew/opt/groovy/libexec/lib/*",
    "/opt/jenkins/latest/WEB-INF/lib/*",
    "/opt/jenkins/plugins/*/WEB-INF/lib/*"
  ],
  "groovy.trace.server": "verbose",
  "java.referencesCodeLens.enabled": true,
  "java.implementationsCodeLens.enabled": true,
  "java.signatureHelp.enabled": true,
  "diagnostic.displayByAle": false,
  "diagnostic.errorSign": "✘",
  "diagnostic.infoSign": "ᓆ",
  "diagnostic.warningSign": "ᑹ",
  "diagnostic.hintSign": "➤",
  "diagnostic-languageserver.filetypes": {
    "yaml": [ "yamllint" ],
    "vim": "vint",
    "sh": "shellcheck",
    "groovy": "npm-groovy-lint"
  },
  "html-css-support.styleSheets": [
      "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css",
      "/style.css",
      "style.css"
  ],
  "html-css-support.enabledLanguages": [ "html", "markdown" ],
  "snippets.userSnippetsDirectory": "~/.marslo/vimrc.d/snips",
  "coc.source.file.ignoreHidden": false,
  "coc.preferences.extensionUpdateCheck": "daily",
  "coc.preferences.currentFunctionSymbolAutoUpdate": true,
  "git.enableGutters": true,
  "java.imports.gradle.wrapper.checksums": [
    {
      "sha256": "af4ff7fffa2c25ebe61221050cc27f5482b14960f3b049f9abe5cadbf0b08085",
      "allowed": true
    }
  ]
}
`:CocCommand extensions.forceUpgradeAll` ...
Install finished

- ✓ coc-groovy Move extension [email protected] to /Users/marslo/.config/coc/extensions/node_modules/coc-groovy
- ✓ coc-json Move extension [email protected] to /Users/marslo/.config/coc/extensions/node_modules/coc-json
- ✓ coc-css Move extension [email protected] to /Users/marslo/.config/coc/extensions/node_modules/coc-css
- ✓ coc-sh Move extension [email protected] to /Users/marslo/.config/coc/extensions/node_modules/coc-sh
- ✓ coc-html-css-support Move extension [email protected] to /Users/marslo/.config/coc/extensions/node_modules/coc-html-css-support
- ✓ coc-java Move extension [email protected] to /Users/marslo/.config/coc/extensions/node_modules/coc-java
- ✓ coc-snippets Move extension [email protected] to /Users/marslo/.config/coc/extensions/node_modules/coc-snippets
- ✓ coc-htmlhint Move extension [email protected] to /Users/marslo/.config/coc/extensions/node_modules/coc-htmlhint
- ✓ coc-git Move extension [email protected] to /Users/marslo/.config/coc/extensions/node_modules/coc-git
- ✓ coc-emoji Move extension [email protected] to /Users/marslo/.config/coc/extensions/node_modules/coc-emoji
- ✓ coc-docker Move extension [email protected] to /Users/marslo/.config/coc/extensions/node_modules/coc-docker
- ✓ coc-pyright Move extension [email protected] to /Users/marslo/.config/coc/extensions/node_modules/coc-pyright
- ✓ coc-diagnostic Move extension [email protected] to /Users/marslo/.config/coc/extensions/node_modules/coc-diagnostic

Working scenario:

  • pwd: $HOME
  • script path: $HOME/a.groovy
$ realpath ~/a.groovy
/Users/marslo/a.groovy

$ cd ~
$ nvim a.groovy
# ---- it will shows in vim ----
#  foo     f [LS]
Image

Failed scenario - 1:

  • pwd: $HOME\Desktop
  • script path: $HOME/a.groovy
$ realpath ~/a.groovy
/Users/marslo/a.groovy

$ cd ~/Desktop
$ nvim ~/a.groovy
# ---- it will shows in vim ----
# foo        [A]
Image

Failed scenario - 2:

  • pwd: $HOME/Desktop
  • script path: $HOME/Desktop/a.groovy
$ cp ~/a.groovy ~/Desktop
$ cd ~/Desktop
$ nvim a.groovy
# ---- it will shows in vim ----
# foo        [A] 
Image

marslo avatar Feb 20 '25 22:02 marslo

The Groovy language server only works when the current directory and the Groovy file are in the same location

I've created a branch that adds a new configuration option to enable running the Groovy language server without a workspace root. This was the only way I could find to have the Groovy language server track and compile individual groovy files that aren't in the current workspace directory. I think this will only work well for standalone scripts.

Since it's a language server configuration, it has to be set at the time the language server is started. If it is changed the language server will automatically be restarted.

In additional, I'd like to use the git root dir ( git rev-parse --show-toplevel ) as workspace if I'm working inside a git repo

If the current directory contains a .git directory it should automatically be used as the current workspace root.


Can you help me test this branch to be sure it fixes your issues? Below are the steps to load the coc-groovy extension from the branch checked out on your local file system.

Clone the Git repository.

git clone https://github.com/dansomething/coc-groovy.git

Checkout the branch.

git checkout ls-feature-noroot

Install Node.js. This is needed to build the extension. I'm using version v23.10.0 provided by Homebrew.

brew install nodejs

Install dependencies and run the build. This creates the compiled index.js file that coc.nvim can load as an extension.

cd coc-groovy && npm install

Run vim and uninstall coc-groovy if it's currently installed. This needs to be done to allow loading the extension from your local file system.

:CocUninstall coc-groovy

Add the local coc-groovy extension to your vim configuration by including the path to the coc-groovy directory to your vim runtime path.

set rtp+=/full/path/to/coc-groovy

Restart vim and verify the extension is loaded.

:CocList extensions

This should print the list of installed extensions and include coc-groovy [RTP]. It will look something like this: coc-groovy [RTP] 1.3.0 /full/path/to/coc-groovy

As the last step, run vim and use CocConfig to set groovy.ls.feature.noRoot to true or false (the default value) and then open a groovy file for testing the desired behavior.

Let me know if you have any questions.

dansomething avatar Mar 25 '25 02:03 dansomething

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 26 '25 03:04 stale[bot]

This is now supported by enabling the experimental groovy.noRoot configuration in release v1.5.0.

dansomething avatar Jul 02 '25 02:07 dansomething