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

node_modules are not located correctly #45

Open
wokalski opened this issue Jun 20, 2021 · 7 comments
Open

node_modules are not located correctly #45

wokalski opened this issue Jun 20, 2021 · 7 comments

Comments

@wokalski
Copy link

wokalski commented Jun 20, 2021

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.

@ryyppy
Copy link
Member

ryyppy commented Jul 6, 2021

what vim / nvim version?

@leeN
Copy link

leeN commented Aug 20, 2021

I have the same issue.

Vim: 8.2
Included patches: 1-2891

@rnmmnen
Copy link

rnmmnen commented Feb 2, 2022

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
Copy link

rnmmnen commented Feb 2, 2022

-    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
Copy link

rnmmnen commented Feb 11, 2022

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
Copy link

rnmmnen commented Feb 11, 2022

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.

@ryyppy
Copy link
Member

ryyppy commented Feb 14, 2022

Interesting 🤔

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants