Skip to content

Commit 5ed2e81

Browse files
authored
fix: remove direct references to the node_modules directory (#4333)
* fix: remove direct references to the node_modules directory Stop reading `node_modules` directly by the file system. The problem with this direct loading is that projects without `node_modules` in the top-level directory will not work at all. For example, when I develop with aws cdk, I cut off the `cdk` or `infra` directory and create a new `package.json` and put `node_modules` there too, but in that case, the code before this modification will not work at all. I solved this problem by simply inserting an abstraction layer. Relation. * [lsp-javascript: supply correct path to tsserver for ts-ls by kiennq · Pull Request #4202 · emacs-lsp/lsp-mode](#4202) * [ts-ls: Wrong lsp-clients-typescript-server-path · Issue #4254 · emacs-lsp/lsp-mode](#4254) * fix(lsp-javascript): remove shell redirect when call node In the case of Windows, etc., `shell-command-to-string` is to use a non-bash shell. To begin with, `shell-command-to-string` seems to ignore standard error output. * refactor(lsp-javascript): rename function and add docs `lsp-clients-typescript-server-path-by-node-require` is too long. * fix(lsp-javascript): use Node.js require to explore Pros === Since Node.js require indicates the path where the file actually exists, it automatically adapts to various environments unless there are significant system changes or changes in the usage environment. Cons === There is a small overhead at first startup due to the command execution. * refactor: use `if-let*` and `when-let*` Simplified based on code review.
1 parent 7db060a commit 5ed2e81

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

clients/lsp-javascript.el

+28-11
Original file line numberDiff line numberDiff line change
@@ -790,20 +790,37 @@ name (e.g. `data' variable passed as `data' parameter)."
790790
(when-let ((workspace (lsp-find-workspace 'ts-ls (buffer-file-name))))
791791
(eq 'initialized (lsp--workspace-status workspace))))
792792

793-
(defun lsp-clients-typescript-project-ts-server-path ()
794-
"Return the project local TS server path."
795-
(f-join (lsp-workspace-root) "node_modules" "typescript" "lib" "tsserver.js"))
793+
(defun lsp-clients-typescript-require-resolve (&optional dir)
794+
"Get the location of the typescript.
795+
Use Node.js require.
796+
The node_modules directory structure is suspect
797+
and should be trusted as little as possible.
798+
If you call require in Node.js,
799+
it should take into account the various hooks.
800+
For example, yarn PnP.
801+
802+
Optional argument DIR specifies the working directory
803+
to run the command in."
804+
(when-let*
805+
((default-directory (or dir default-directory))
806+
(output
807+
(string-trim-right
808+
(shell-command-to-string
809+
"node -e \"console.log(require.resolve('typescript'))\"")))
810+
(not-empty (not (string-empty-p output))))
811+
(f-parent output)))
796812

797813
(defun lsp-clients-typescript-server-path ()
798-
"Return the TS sever path base on settings."
799-
(cond
800-
((and lsp-clients-typescript-prefer-use-project-ts-server
801-
(f-exists? (lsp-clients-typescript-project-ts-server-path)))
802-
(lsp-clients-typescript-project-ts-server-path))
803-
(t
814+
"Return the TS server path based on settings."
815+
(if-let* ((use-project-ts lsp-clients-typescript-prefer-use-project-ts-server)
816+
(server-path (lsp-clients-typescript-require-resolve))
817+
(server-path-exist (f-exists? server-path)))
818+
server-path
804819
(if (memq system-type '(cygwin windows-nt ms-dos))
805-
(f-join (f-parent (lsp-package-path 'typescript)) "node_modules" "typescript" "lib")
806-
(f-join (f-parent (f-parent (lsp-package-path 'typescript))) "lib" "node_modules" "typescript" "lib")))))
820+
;; The Windows environment does not recognize the top-level PATH returned by `lsp-package-path',
821+
;; so the real PATH is returned through Node.js.
822+
(lsp-clients-typescript-require-resolve (f-parent (lsp-package-path 'typescript)))
823+
(lsp-package-path 'typescript))))
807824

808825
(lsp-register-client
809826
(make-lsp-client :new-connection (lsp-stdio-connection (lambda ()

0 commit comments

Comments
 (0)