Skip to content

Commit 4b7352e

Browse files
committed
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 emacs-lsp#4202 · emacs-lsp/lsp-mode](emacs-lsp#4202) * [ts-ls: Wrong lsp-clients-typescript-server-path · Issue emacs-lsp#4254 · emacs-lsp/lsp-mode](emacs-lsp#4254)
1 parent f414a44 commit 4b7352e

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

clients/lsp-javascript.el

+20-9
Original file line numberDiff line numberDiff line change
@@ -790,17 +790,28 @@ 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-
(f-join (lsp-workspace-root) "node_modules" "typescript" "lib" "tsserver.js"))
793+
(defun lsp-clients-typescript-server-path-by-node-require ()
794+
"Get the location of the typescript library.
795+
Use Node.js.
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+
(let ((output
801+
(string-trim-right
802+
(shell-command-to-string
803+
"node -e \"console.log(require.resolve('typescript'))\" 2>/dev/null"))))
804+
(if (string-empty-p output)
805+
nil
806+
(f-parent output))))
795807

796808
(defun lsp-clients-typescript-server-path ()
797-
(cond
798-
((and
799-
lsp-clients-typescript-prefer-use-project-ts-server
800-
(f-exists? (lsp-clients-typescript-project-ts-server-path)))
801-
(lsp-clients-typescript-project-ts-server-path))
802-
(t
803-
(f-join (f-parent (lsp-package-path 'typescript)) "node_modules" "typescript" "lib"))))
809+
(if lsp-clients-typescript-prefer-use-project-ts-server
810+
(let ((server-path (lsp-clients-typescript-server-path-by-node-require)))
811+
(if (f-exists? server-path)
812+
server-path
813+
(lsp-package-path 'typescript)))
814+
(lsp-package-path 'typescript)))
804815

805816
(lsp-register-client
806817
(make-lsp-client :new-connection (lsp-stdio-connection (lambda ()

0 commit comments

Comments
 (0)