vim-rescript icon indicating copy to clipboard operation
vim-rescript copied to clipboard

node_modules are not located correctly

Open wokalski opened this issue 4 years ago • 7 comments

For some reason finddir returns the full path on my system so getCwd() . finddir() causes the path to be /Users/user/path/to/project//Users/user/path/to/project/node_modules/.... I had to remove getCwd() calls from rescript#UpdateProjectEnv() and it all works. It's probably something about my setup but I cannot figure it out.

wokalski avatar Jun 20 '21 16:06 wokalski

what vim / nvim version?

ryyppy avatar Jul 06 '21 17:07 ryyppy

I have the same issue.

Vim: 8.2 Included patches: 1-2891

leeN avatar Aug 20 '21 09:08 leeN

I've been running into an issue that looks very similar.

I can reproduce it by simply cloning the official project template and changing the current working directory to src. Plugin no longer works.

$ git clone https://github.com/rescript-lang/rescript-project-template.git
$ cd rescript-project-template
$ npm install
$ vim src/Demo.src # Works
$ cd src
$ vim Demo.src # FAIL

A bit more involved way to reproduce the problem is to create a monorepo setup with a single top-level node_modules and to change the current working directory to something more specific than the project root.

Minimal setup using npm workspaces (requires a relatively recent version of npm):

$ mkdir demo
$ cd demo
$ npm init --yes
$ npm init --yes -w packages/mydemo
$ npm install rescript --save -w packages/mydemo
$ cat >packages/mydemo/bsconfig.json <<END
{
  "name": "mydemo",
  "sources": [
    {
      "dir": "src",
      "subdirs": true
    }
  ],
  "package-specs": [
    {
      "module": "es6",
      "in-source": true
    }
  ],
  "suffix": ".bs.js",
  "bs-dependencies": []
}
END
$ mkdir packages/mydemo/src
$ echo 'Js.log("Hello, World!")' > packages/mydemo/src/Demo.res

Reproduction using Apple's default vim:

$ vim --version|head -n 1
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Nov 13 2021 05:04:57)
$ vim packages/mydemo/src/Demo.res # Works
$ (cd ..; vim demo/packages/mydemo/src/Demo.res) # Works
$ (cd packages; vim mydemo/src/Demo.res) # FAIL
$ (cd packages/mydemo; vim src/Demo.res) # FAIL
$ (cd packages/mydemo/src; vim Demo.res) # FAIL

Reproduction using latest Neovim:

$ nvim --version|head -n 1
NVIM v0.6.1
$ nvim packages/mydemo/src/Demo.res # Works
$ (cd ..; nvim demo/packages/mydemo/src/Demo.res) # Works
$ (cd packages; nvim mydemo/src/Demo.res) # FAIL
$ (cd packages/mydemo; nvim src/Demo.res) # FAIL
$ (cd packages/mydemo/src; nvim Demo.res) # FAIL

These changes fix the issue for me:

diff --git a/autoload/rescript.vim b/autoload/rescript.vim
index 14a7486..a3dcbc0 100644
--- a/autoload/rescript.vim
+++ b/autoload/rescript.vim
@@ -38,16 +38,16 @@ function! rescript#UpdateProjectEnv()
   else
     " Here we are handling a project that is based on the rescript npm package
     " This package only uses a rescript.exe, no bsc, nor bsb
-    let g:rescript_exe = getcwd() . "/" . l:res_bin_dir . "/rescript.exe"
+    let g:rescript_exe = fnamemodify(l:res_bin_dir, ":p:h") . "/rescript.exe"
   endif
 
   " These variables are only used in legacy mode (bs-platform based projects)
-  let g:rescript_bsc_exe = getcwd() . "/" . l:res_bin_dir . "/bsc.exe"
-  let g:rescript_bsb_exe = getcwd() . "/" . l:res_bin_dir . "/bsb.exe"
+  let g:rescript_bsc_exe = fnamemodify(l:res_bin_dir, ":p:h") . "/bsc.exe"
+  let g:rescript_bsb_exe = fnamemodify(l:res_bin_dir, ":p:h") . "/bsb.exe"
 
   " Note that this doesn't find bsconfig when the editor was started without a
   " specific file within the project
-  let g:rescript_project_config = getcwd() . "/" . findfile("bsconfig.json", ".;")
+  let g:rescript_project_config = findfile("bsconfig.json", ".;")
 
   " Try to find the nearest .git folder instead
   if g:rescript_project_config == ""

After applying the patch all working directories seem to work as expected (both in vim and in Neovim).

rnmmnen avatar Feb 02 '22 15:02 rnmmnen

-    let g:rescript_exe = getcwd() . "/" . l:res_bin_dir . "/rescript.exe"
+    let g:rescript_exe = fnamemodify(l:res_bin_dir, ":p:h") . "/rescript.exe"
   endif
 
   " These variables are only used in legacy mode (bs-platform based projects)
-  let g:rescript_bsc_exe = getcwd() . "/" . l:res_bin_dir . "/bsc.exe"
-  let g:rescript_bsb_exe = getcwd() . "/" . l:res_bin_dir . "/bsb.exe"
+  let g:rescript_bsc_exe = fnamemodify(l:res_bin_dir, ":p:h") . "/bsc.exe"
+  let g:rescript_bsb_exe = fnamemodify(l:res_bin_dir, ":p:h") . "/bsb.exe"

…or one could modify the l:res_bin_dir variable before these lines to save some cycles. 😉

rnmmnen avatar Feb 02 '22 16:02 rnmmnen

Okay, the changes above were just a partial fix.

They fixed these:

  • rescript#Format (:RescriptFormat)
  • rescript#Clean (:RescriptClean and :RescriptCleanWithDeps)
  • rescript#Build (:RescriptBuild and :RescriptBuildWithDeps)
  • probably rescript#ReasonToRescript, too (:RescriptUpgradeFromReason)

Didn't fix these:

  • rescript#TypeHint (:RescriptTypeHint)
  • rescript#JumpToDefinition (:RescriptJumpToDefinition)
  • rescript#Complete (omni completion).

The latter three will call rescript-editor-analysis.exe. The detected path shown by :RescriptInfo remains correct but the returned data is just null whenever the current working directory is something more specific than project root. This can be demonstrated with the official project template just by running cd src and trying to use those commands.

rnmmnen avatar Feb 11 '22 17:02 rnmmnen

As a side note, I noticed that the analysis exe returns null type hint when no type hint is available. This causes a nasty-looking error message which could be handled better.

rnmmnen avatar Feb 11 '22 17:02 rnmmnen

Interesting 🤔

Thanks for the instructions to reproduce this issue. Will try it myself.

ryyppy avatar Feb 14 '22 06:02 ryyppy