diff --git a/modules/lsp/servers/default.nix b/modules/lsp/servers/default.nix index b0293b6f..3427f81d 100644 --- a/modules/lsp/servers/default.nix +++ b/modules/lsp/servers/default.nix @@ -40,10 +40,10 @@ let # Get suboptions of `lsp.servers.` (opts: opts.${name}.type.getSubOptions opts.${name}.loc) # Get the default package - (opts: opts.package.default or null) - # The default throws if mkPackageOption can't find the package - # E.g. mismatched nixpkgs revision - (package: (builtins.tryEval package).value) + # + # Use tryEval to catch throws when mkPackageOption can't find the package, + # e.g., due to a mismatched nixpkgs revision + (opts: (builtins.tryEval (opts.package.default or null)).value) # Get package's homepage (package: package.meta.homepage or null) ]; diff --git a/tests/test-sources/modules/lsp.nix b/tests/test-sources/modules/lsp.nix index ea6c36bb..7bdaee08 100644 --- a/tests/test-sources/modules/lsp.nix +++ b/tests/test-sources/modules/lsp.nix @@ -1,3 +1,4 @@ +{ pkgs }: { example = { lsp.servers = { @@ -254,4 +255,51 @@ } ]; }; + + # Regression test for mkServerOption: + # Ensures tryEval catches missing packages when evaluating the description. + # See https://github.com/nix-community/nixvim/issues/4033 + missing-package = + { lib, options, ... }: + let + # `lsp.servers.lua_ls` option + serverOpt = lib.pipe options.lsp.servers [ + (opt: opt.type.getSubOptions opt.loc) + (opts: opts.lua_ls) + ]; + + # `lsp.servers.lua_ls.package` option + packageOpt = (serverOpt.type.getSubOptions serverOpt.loc).package; + + # The lua_ls package attr to remove from pkgs + packageName = lib.pipe ../../../plugins/lsp/lsp-packages.nix [ + import + (lib.getAttr "packages") + (lib.getAttr "lua_ls") + lib.toList + lib.head + ]; + in + { + # Remove lua_ls's package + _module.args.pkgs = lib.mkOverride 0 (lib.removeAttrs pkgs [ packageName ]); + + assertions = [ + { + # Expect the lua_ls description to evaluate without a link + assertion = serverOpt.description == "The lua_ls language server.\n"; + message = '' + Wrong description for `${serverOpt}`. Found: + ${serverOpt.description} + ''; + } + { + # Expect the package default to throw + assertion = !(builtins.tryEval packageOpt.default).success; + message = "Expected `${packageOpt}`'s default to throw."; + } + ]; + + test.buildNixvim = false; + }; }