diff --git a/pkgs/development/tools/rust/rust-analyzer/default.nix b/pkgs/by-name/ru/rust-analyzer-unwrapped/package.nix similarity index 64% rename from pkgs/development/tools/rust/rust-analyzer/default.nix rename to pkgs/by-name/ru/rust-analyzer-unwrapped/package.nix index 986458e90b27..7c1c1484edd8 100644 --- a/pkgs/development/tools/rust/rust-analyzer/default.nix +++ b/pkgs/by-name/ru/rust-analyzer-unwrapped/package.nix @@ -1,30 +1,39 @@ -{ lib -, stdenv -, callPackage -, fetchFromGitHub -, rustPlatform -, CoreServices -, cmake -, libiconv -, useMimalloc ? false -, doCheck ? true -, nix-update-script +{ + lib, + stdenv, + fetchFromGitHub, + rustPlatform, + cmake, + libiconv, + useMimalloc ? false, + doCheck ? true, + nix-update-script, }: rustPlatform.buildRustPackage rec { pname = "rust-analyzer-unwrapped"; - version = "2024-09-02"; - cargoHash = "sha256-t45RzYkuywGByGWwUON3dW0aKjLYcFXB8uy4CybPuf4="; + version = "2024-11-11"; + cargoHash = "sha256-lzbk/APerZih7+1ZxBKl0rUHCJJA8W8RIqalEfu+MFI="; src = fetchFromGitHub { owner = "rust-lang"; repo = "rust-analyzer"; rev = version; - hash = "sha256-YH0kH5CSOnAuPUB1BUzUqvnKiv5SgDhfMNjrkki9Ahk="; + hash = "sha256-TDI1s2Sc/rpMLwful1NcTjYBbqzbQ4Mjw5PPOuOXVfg="; }; - cargoBuildFlags = [ "--bin" "rust-analyzer" "--bin" "rust-analyzer-proc-macro-srv" ]; - cargoTestFlags = [ "--package" "rust-analyzer" "--package" "proc-macro-srv-cli" ]; + cargoBuildFlags = [ + "--bin" + "rust-analyzer" + "--bin" + "rust-analyzer-proc-macro-srv" + ]; + cargoTestFlags = [ + "--package" + "rust-analyzer" + "--package" + "proc-macro-srv-cli" + ]; # Code format check requires more dependencies but don't really matter for packaging. # So just ignore it. @@ -33,7 +42,6 @@ rustPlatform.buildRustPackage rec { nativeBuildInputs = lib.optional useMimalloc cmake; buildInputs = lib.optionals stdenv.hostPlatform.isDarwin [ - CoreServices libiconv ]; @@ -58,13 +66,18 @@ rustPlatform.buildRustPackage rec { passthru = { updateScript = nix-update-script { }; # FIXME: Pass overrided `rust-analyzer` once `buildRustPackage` also implements #119942 - tests.neovim-lsp = callPackage ./test-neovim-lsp.nix { }; + # FIXME: test script can't find rust std lib so hover doesn't return expected result + # https://github.com/NixOS/nixpkgs/pull/354304 + # tests.neovim-lsp = callPackage ./test-neovim-lsp.nix { }; }; meta = with lib; { description = "Modular compiler frontend for the Rust language"; homepage = "https://rust-analyzer.github.io"; - license = with licenses; [ mit asl20 ]; + license = with licenses; [ + mit + asl20 + ]; maintainers = with maintainers; [ oxalica ]; mainProgram = "rust-analyzer"; }; diff --git a/pkgs/by-name/ru/rust-analyzer-unwrapped/test-neovim-lsp.nix b/pkgs/by-name/ru/rust-analyzer-unwrapped/test-neovim-lsp.nix new file mode 100644 index 000000000000..9e7219763b89 --- /dev/null +++ b/pkgs/by-name/ru/rust-analyzer-unwrapped/test-neovim-lsp.nix @@ -0,0 +1,75 @@ +{ + runCommand, + cargo, + neovim, + rust-analyzer, + rustc, +}: +runCommand "test-neovim-rust-analyzer" + { + nativeBuildInputs = [ + cargo + neovim + rust-analyzer + rustc + ]; + + testRustSrc = '' + fn main() { + let mut var = vec![None]; + var.push(Some("hello".to_owned())); + } + ''; + + # NB. Wait for server done type calculations before sending `hover` request, + # otherwise it would return `{unknown}`. + # Ref: https://github.com/rust-lang/rust-analyzer/blob/7b11fdeb681c12002861b9804a388efde81c9647/docs/dev/lsp-extensions.md#server-status + nvimConfig = '' + local caps = vim.lsp.protocol.make_client_capabilities() + caps["experimental"] = { serverStatusNotification = true } + vim.lsp.buf_attach_client(vim.api.nvim_get_current_buf(), vim.lsp.start_client({ + cmd = { "rust-analyzer" }, + capabilities = caps, + handlers = { + ["experimental/serverStatus"] = function(_, msg, ctx) + if msg.health == "ok" then + if msg.quiescent then + vim.cmd("goto 23") -- let mut |var =... + vim.lsp.buf.hover() + end + else + print("error: server status is not ok: ") + vim.cmd("q") + end + end, + ["textDocument/hover"] = function(_, msg, ctx) + if msg then + -- Keep newlines. + io.write(msg.contents.value) + vim.cmd("q") + end + end, + }, + on_error = function(code) + print("error: " .. code) + vim.cmd("q") + end + })) + ''; + + } + '' + # neovim requires a writable HOME. + export HOME="$(pwd)" + + cargo new --bin test-rust-analyzer + cd test-rust-analyzer + cat <<<"$testRustSrc" >src/main.rs + cat <<<"$nvimConfig" >script.lua + + # `-u` doesn't work + result="$(nvim --headless +'lua dofile("script.lua")' src/main.rs)" + echo "$result" + [[ "$result" == *"var: Vec>"* ]] + touch $out + '' diff --git a/pkgs/by-name/ru/rust-analyzer/package.nix b/pkgs/by-name/ru/rust-analyzer/package.nix new file mode 100644 index 000000000000..cfd03a62a601 --- /dev/null +++ b/pkgs/by-name/ru/rust-analyzer/package.nix @@ -0,0 +1,21 @@ +{ + rustPlatform, + runCommand, + makeWrapper, + rust-analyzer-unwrapped, + pname ? "rust-analyzer", + version ? rust-analyzer-unwrapped.version, + # Use name from `RUST_SRC_PATH` + rustSrc ? rustPlatform.rustLibSrc, +}: +runCommand "${pname}-${version}" + { + inherit pname version; + inherit (rust-analyzer-unwrapped) src meta; + nativeBuildInputs = [ makeWrapper ]; + } + '' + mkdir -p $out/bin + makeWrapper ${rust-analyzer-unwrapped}/bin/rust-analyzer $out/bin/rust-analyzer \ + --set-default RUST_SRC_PATH "${rustSrc}" + '' diff --git a/pkgs/development/tools/rust/rust-analyzer/test-neovim-lsp.nix b/pkgs/development/tools/rust/rust-analyzer/test-neovim-lsp.nix deleted file mode 100644 index 49be63a2b99c..000000000000 --- a/pkgs/development/tools/rust/rust-analyzer/test-neovim-lsp.nix +++ /dev/null @@ -1,62 +0,0 @@ -{ runCommand, cargo, neovim, rust-analyzer, rustc }: -runCommand "test-neovim-rust-analyzer" { - nativeBuildInputs = [ cargo neovim rust-analyzer rustc ]; - - testRustSrc = /* rust */ '' - fn main() { - let mut var = vec![None]; - var.push(Some("hello".to_owned())); - } - ''; - - # NB. Wait for server done type calculations before sending `hover` request, - # otherwise it would return `{unknown}`. - # Ref: https://github.com/rust-lang/rust-analyzer/blob/7b11fdeb681c12002861b9804a388efde81c9647/docs/dev/lsp-extensions.md#server-status - nvimConfig = /* lua */ '' - local caps = vim.lsp.protocol.make_client_capabilities() - caps["experimental"] = { serverStatusNotification = true } - vim.lsp.buf_attach_client(vim.api.nvim_get_current_buf(), vim.lsp.start_client({ - cmd = { "rust-analyzer" }, - capabilities = caps, - handlers = { - ["experimental/serverStatus"] = function(_, msg, ctx) - if msg.health == "ok" then - if msg.quiescent then - vim.cmd("goto 23") -- let mut |var =... - vim.lsp.buf.hover() - end - else - print("error: server status is not ok: ") - vim.cmd("q") - end - end, - ["textDocument/hover"] = function(_, msg, ctx) - if msg then - -- Keep newlines. - io.write(msg.contents.value) - vim.cmd("q") - end - end, - }, - on_error = function(code) - print("error: " .. code) - vim.cmd("q") - end - })) - ''; - -} '' - # neovim requires a writable HOME. - export HOME="$(pwd)" - - cargo new --bin test-rust-analyzer - cd test-rust-analyzer - cat <<<"$testRustSrc" >src/main.rs - cat <<<"$nvimConfig" >script.lua - - # `-u` doesn't work - result="$(nvim --headless +'lua dofile("script.lua")' src/main.rs)" - echo "$result" - [[ "$result" == *"var: Vec>"* ]] - touch $out -'' diff --git a/pkgs/development/tools/rust/rust-analyzer/wrapper.nix b/pkgs/development/tools/rust/rust-analyzer/wrapper.nix deleted file mode 100644 index af0a2953edee..000000000000 --- a/pkgs/development/tools/rust/rust-analyzer/wrapper.nix +++ /dev/null @@ -1,15 +0,0 @@ -{ rustPlatform, runCommand, makeWrapper, rust-analyzer-unwrapped -, pname ? "rust-analyzer" -, version ? rust-analyzer-unwrapped.version - # Use name from `RUST_SRC_PATH` -, rustSrc ? rustPlatform.rustLibSrc -}: -runCommand "${pname}-${version}" { - inherit pname version; - inherit (rust-analyzer-unwrapped) src meta; - nativeBuildInputs = [ makeWrapper ]; -} '' - mkdir -p $out/bin - makeWrapper ${rust-analyzer-unwrapped}/bin/rust-analyzer $out/bin/rust-analyzer \ - --set-default RUST_SRC_PATH "${rustSrc}" -'' diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 18cf18955c4c..b08d97d5d06c 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -7079,10 +7079,6 @@ with pkgs; opensyclWithRocm = opensycl.override { rocmSupport = true; }; rustfmt = rustPackages.rustfmt; - rust-analyzer-unwrapped = callPackage ../development/tools/rust/rust-analyzer { - inherit (darwin.apple_sdk.frameworks) CoreServices; - }; - rust-analyzer = callPackage ../development/tools/rust/rust-analyzer/wrapper.nix { }; rust-bindgen-unwrapped = callPackage ../development/tools/rust/bindgen/unwrapped.nix { }; rust-bindgen = callPackage ../development/tools/rust/bindgen { }; rust-cbindgen = callPackage ../development/tools/rust/cbindgen {