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

fix: remove direct references to the node_modules directory #4333

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

ncaq
Copy link
Contributor

@ncaq ncaq commented Feb 14, 2024

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.

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)
@github-actions github-actions bot added the client One or more of lsp-mode language clients label Feb 14, 2024
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.
`lsp-clients-typescript-server-path-by-node-require` is too long.
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.
@ncaq
Copy link
Contributor Author

ncaq commented Mar 27, 2024

As of the first modified PR, it was not working on Windows, so I modified it and confirmed that it works on Linux (WSL2) and Windows (Emacs installed with winget).

clients/lsp-javascript.el Outdated Show resolved Hide resolved
clients/lsp-javascript.el Outdated Show resolved Hide resolved
clients/lsp-javascript.el Outdated Show resolved Hide resolved
e.g., on Windows."
(if (memq system-type '(cygwin windows-nt ms-dos))
(lsp-clients-typescript-require-resolve (f-parent (lsp-package-path 'typescript)))
(lsp-package-path 'typescript)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need some clarification here. We were previously using (f-join (f-parent (f-parent (lsp-package-path 'typescript))) "lib" "node_modules" "typescript" "lib"), and now just (lsp-package-path 'typescript)? Am I missing something? Can you elaborate it? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Until the following PR, (lsp-package-path 'typescript) was just called.
lsp-javascript: supply correct path to tsserver for ts-ls by kiennq · Pull Request #4202 · emacs-lsp/lsp-mode
Maybe that was a problem in the environment of the person who made this modification, so a direct directory deep dive was done to avoid that.
I honestly don't know why different environments have different directory path locations.
My suggestion this time is to just use (lsp-package-path 'typescript) by default, and only use Node.js calls that return a wide range of real PATHs in special environments.
If it is fundamentally buggy, we should fix lsp-package-path in the first place.
By this logic, the Windows-only branching on the caller side this time is also subtle, but I was puzzled as to why the TypeScirpt server did not read the PATH with tsserver.cmd at the top level and did not know how to fix it, so I used this method as a stopgap measure. I used this method as a stopgap measure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting thing is (lsp-package-path 'typescript) gives me tsserver and not the full path. 🤔

Simplified based on code review.
@ncaq ncaq requested a review from jcs090218 March 28, 2024 13:57
(string-trim-right
(shell-command-to-string
"node -e \"console.log(require.resolve('typescript'))\"")))
(not-empty (not (string-empty-p output))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable not-empty is not used. May be we can leave it as blank?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am aware that it is not used.
I was told that it would be better to use a form that would not proceed unless something other than nil was bound to it.
If you want to take advantage of this, I think the format before branching with (string-empty-p output) is correct.
I used (string-empty-p output) for the name, but it gave me a warning.

@ncaq
Copy link
Contributor Author

ncaq commented Apr 12, 2024

I am late in responding because my grandmother passed away and I caught a cold. My apologies.

@ncaq ncaq requested a review from jcs090218 April 12, 2024 05:12
@jcs090218
Copy link
Member

I am late in responding because my grandmother passed away and I caught a cold. My apologies.

Oh, I'm sorry for your loss. No worries, there is no reason to rush here!

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.

At first, I thought you were right, but it was working on my side. Can you confirm this once again? 🤔 (Although I couldn't get the completion working)

@ncaq
Copy link
Contributor Author

ncaq commented Apr 17, 2024

At first, I thought you were right, but it was working on my side. Can you confirm this once again? 🤔 (Although I couldn't get the completion working)

I was having problems in a Linux environment (WSL) when writing cdk's etc. when node_modules are in a place that is not repository top level.
The lsp server itself would not start and all the documentation references, code jumps, etc. would not work.
To be honest, I don't really know the exact mechanism of why the problem occurs, but using lsp-mode and the official Node approach worked fine, so I reverted back to the original approach because I thought it was the more logical way to go.
Also, although I haven't actually tried it, I suspect that when using yarn PnP, etc., it may be a problem because node_modules does not exist.

@ncaq
Copy link
Contributor Author

ncaq commented Apr 25, 2024

@jcs090218
Am I missing something?

@jcs090218
Copy link
Member

Am I missing something?

I'm sorry, I'm a bit busy. I will take a look when I have time! 😓

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client One or more of lsp-mode language clients
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants