NomicFoundation / hardhat-vscode

Solidity and Hardhat support for Visual Studio Code
https://hardhat.org
MIT License
163 stars 36 forks source link

Language server: Cannot find module '@nomicfoundation/slang-linux-x64-gnu' #564

Open beeb opened 2 months ago

beeb commented 2 months ago

Hey there.

I've been using the language server with helix editor for quite some time, and recently wanted to upgrade to 0.8.x.

I have installed the npm packge through nix using node2nix which normally always works fine (and it did before the upgrade to 0.8).

After the upgrade, I'm seeing the following error in helix's logs:

2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "/nix/store/00rkpmpvbj91lkyvbsa5ydg49kq9y3dd-_at_nomicfoundation_slash_solidity-language-server-0.8.2/lib/node_modules/@nomicfoundation/solidity-language-server/node_modules/@nomicfoundation/slang/generated/index.js:276\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "    throw loadError;\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "    ^\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "Error: Cannot find module '@nomicfoundation/slang-linux-x64-gnu'\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "Require stack:\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "- /nix/store/00rkpmpvbj91lkyvbsa5ydg49kq9y3dd-_at_nomicfoundation_slash_solidity-language-server-0.8.2/lib/node_modules/@nomicfoundation/solidity-language-server/node_modules/@nomicfoundation/slang/generated/index.js\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "- /nix/store/00rkpmpvbj91lkyvbsa5ydg49kq9y3dd-_at_nomicfoundation_slash_solidity-language-server-0.8.2/lib/node_modules/@nomicfoundation/solidity-language-server/node_modules/@nomicfoundation/slang/language/index.js\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "- /nix/store/00rkpmpvbj91lkyvbsa5ydg49kq9y3dd-_at_nomicfoundation_slash_solidity-language-server-0.8.2/lib/node_modules/@nomicfoundation/solidity-language-server/out/index.js\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "    at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "    at Module._resolveFilename (/nix/store/00rkpmpvbj91lkyvbsa5ydg49kq9y3dd-_at_nomicfoundation_slash_solidity-language-server-0.8.2/lib/node_modules/@nomicfoundation/solidity-language-server/out/index.js:2:3675)\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "    at Module._load (node:internal/modules/cjs/loader:985:27)\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "    at Module.require (node:internal/modules/cjs/loader:1235:19)\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "    at require (node:internal/modules/helpers:176:18)\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "    at Object.<anonymous> (/nix/store/00rkpmpvbj91lkyvbsa5ydg49kq9y3dd-_at_nomicfoundation_slash_solidity-language-server-0.8.2/lib/node_modules/@nomicfoundation/solidity-language-server/node_modules/@nomicfoundation/slang/generated/index.js:185:31)\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "    at Module._compile (node:internal/modules/cjs/loader:1376:14)\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "    at Module.load (node:internal/modules/cjs/loader:1207:32)\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "    at Module._load (node:internal/modules/cjs/loader:1023:12) {\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "  code: 'MODULE_NOT_FOUND',\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "  requireStack: [\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "    '/nix/store/00rkpmpvbj91lkyvbsa5ydg49kq9y3dd-_at_nomicfoundation_slash_solidity-language-server-0.8.2/lib/node_modules/@nomicfoundation/solidity-language-server/node_modules/@nomicfoundation/slang/generated/index.js',\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "    '/nix/store/00rkpmpvbj91lkyvbsa5ydg49kq9y3dd-_at_nomicfoundation_slash_solidity-language-server-0.8.2/lib/node_modules/@nomicfoundation/solidity-language-server/node_modules/@nomicfoundation/slang/language/index.js',\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "    '/nix/store/00rkpmpvbj91lkyvbsa5ydg49kq9y3dd-_at_nomicfoundation_slash_solidity-language-server-0.8.2/lib/node_modules/@nomicfoundation/solidity-language-server/out/index.js'\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "  ]\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "}\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "\n"
2024-04-29T08:13:57.981 helix_lsp::transport [ERROR] solidity-language-server err <- "Node.js v20.11.1\n"
2024-04-29T08:13:57.985 helix_lsp [ERROR] failed to initialize language server: server closed the stream
2024-04-29T08:13:57.985 helix_lsp::transport [ERROR] solidity-language-server err: <- StreamClosed

Platform: Ubuntu 23.10 x86-64

Note that when I check the installed package and its dependencies at /nix/store/00rkpmpvbj91lkyvbsa5ydg49kq9y3dd-_at_nomicfoundation_slash_solidity-language-server-0.8.2/lib/node_modules/@nomicfoundation/solidity-language-server/node_modules/, I cannot indeed see the @nomicfoundation/slang-linux-x64-gnu package:

$ pwd
/nix/store/00rkpmpvbj91lkyvbsa5ydg49kq9y3dd-_at_nomicfoundation_slash_solidity-language-server-0.8.2/lib/node_modules/@nomicfoundation/solidity-language-server/node_modules/@nomicfoundation
$ ls -la
dr-xr-xr-x - root  1 Jan  1970  slang
dr-xr-xr-x - root  1 Jan  1970  solidity-analyzer
dr-xr-xr-x - root  1 Jan  1970  solidity-analyzer-darwin-arm64
dr-xr-xr-x - root  1 Jan  1970  solidity-analyzer-darwin-x64
dr-xr-xr-x - root  1 Jan  1970  solidity-analyzer-freebsd-x64
dr-xr-xr-x - root  1 Jan  1970  solidity-analyzer-linux-arm64-gnu
dr-xr-xr-x - root  1 Jan  1970  solidity-analyzer-linux-arm64-musl
dr-xr-xr-x - root  1 Jan  1970  solidity-analyzer-linux-x64-gnu
dr-xr-xr-x - root  1 Jan  1970  solidity-analyzer-linux-x64-musl
dr-xr-xr-x - root  1 Jan  1970  solidity-analyzer-win32-arm64-msvc
dr-xr-xr-x - root  1 Jan  1970  solidity-analyzer-win32-ia32-msvc
dr-xr-xr-x - root  1 Jan  1970  solidity-analyzer-win32-x64-msvc
Xanewok commented 2 months ago

I debugged it a bit, and without going too much into the details, it looks like @nomicfoundation/slang@v0.14.0 has correctly specified optionalDependencies, but the npm registry does not list these under dependencies anymore (unlike older versions or solidity-analyzer), see https://github.com/svanderburg/node2nix/issues/73#issuecomment-396720927 for more context on how this might impact the nix build process.

Whether this is a bug that's been finally fixed on the npm registry side or something's changed between Node.js 19.9 and Node.js 21.6, which we updated between the releases, I unfortunately do not know; I couldn't find any publishing-related changes in the diff between these versions.

As to how we can solve this, I don't think we can mark these platform-specific npm packages/binaries as regular dependencies in package.json since a failure to install either of them would error out (failure is expected; only a single platform-specific package should match host OS/CPU setup), which would be incorrect.

In this case, assuming that it's npm registry that has changed/fixed its behaviour, it's probably on node2nix to support this, if I understand the root issue correctly. (cc @svanderburg is that correct?)

@beeb in the meantime, is it possible to patch this package during the build time to mark it as explicitly depending (in a package.json's dependencies sense) on @nomicfoundation/slang-linux-x64-gnu locally, in your case? I know this is not a proper fix but it might unblock you or others who might be facing the same issue.

Sorry for the trouble and I hope that at least the workaround works!

beeb commented 2 months ago

Hey @Xanewok thanks a lot for the investigative work and explanation.

I'll have a look at patching the node2nix package output although I have little experience doing so. If any nix wizard happens to read this, any input would be greatly appreciated!

beeb commented 2 months ago

I managed to patch the install by modifying the generated node-packages.nix and adding an item to the package dependencies:

let sources = {
      # ...
      "@nomicfoundation/slang-linux-x64-gnu-0.14.2" = {
      name = "_at_nomicfoundation_slash_slang-linux-x64-gnu";
      packageName = "@nomicfoundation/slang-linux-x64-gnu";
      version = "0.14.2";
      src = fetchurl {
        url = "https://registry.npmjs.org/@nomicfoundation/slang-linux-x64-gnu/-/slang-linux-x64-gnu-0.14.2.tgz";
        sha256 = "sha256-92JYoB9hL0BCZ4nKOcYdLE+a2wvxzt2kh2i6DRhfxKw=";
      };
    };
};
in
{
    "@nomicfoundation/solidity-language-server" = nodeEnv.buildNodePackage {
        # ...
        dependencies = [
            # ...
            sources."@nomicfoundation/slang-linux-x64-gnu-0.14.2"
        ];
    };
}

Not pretty though, I didn't find a way to patch it without modifying the generated file, but I'm no expert.