From 24c9e40df8c8c075625ed3e4605a95e708159c35 Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Fri, 19 Apr 2024 10:08:07 +0800 Subject: [PATCH] nixd: use orphan nixpkgs, nixos default in lit-test mode (#431) To speed up lit-testing, we don't need to evaluate full nixpkgs. --- nixd/include/nixd/CommandLine/Options.h | 3 +++ nixd/include/nixd/Controller/Controller.h | 5 ----- nixd/lib/CommandLine/Options.cpp | 9 ++++++++- nixd/lib/Controller/LifeTime.cpp | 20 ++++++++++++++++++-- nixd/lib/Controller/Support.cpp | 2 +- nixd/tools/nixd-attrset-eval.cpp | 8 ++------ nixd/tools/nixd.cpp | 10 ++-------- nixd/tools/nixd/test/completion-nixpkgs.md | 8 ++++---- 8 files changed, 38 insertions(+), 27 deletions(-) diff --git a/nixd/include/nixd/CommandLine/Options.h b/nixd/include/nixd/CommandLine/Options.h index eea5e19e7..d5867502d 100644 --- a/nixd/include/nixd/CommandLine/Options.h +++ b/nixd/include/nixd/CommandLine/Options.h @@ -4,4 +4,7 @@ namespace nixd { extern llvm::cl::OptionCategory NixdCategory; +/// \brief Indicating that we are in lit-test mode. +extern llvm::cl::opt LitTest; + } // namespace nixd diff --git a/nixd/include/nixd/Controller/Controller.h b/nixd/include/nixd/Controller/Controller.h index dcfae494b..cfad6331d 100644 --- a/nixd/include/nixd/Controller/Controller.h +++ b/nixd/include/nixd/Controller/Controller.h @@ -136,9 +136,6 @@ class Controller : public lspserver::LSPServer { boost::asio::thread_pool Pool; - /// In lit-test mode. Disable some concurrency for better text-testing. - bool LitTest; - /// Action right after a document is added (including updates). void actOnDocumentAdd(lspserver::PathRef File, std::optional Version); @@ -222,8 +219,6 @@ class Controller : public lspserver::LSPServer { ~Controller() override { Pool.join(); } - void setLitTest(bool LitTest) { this->LitTest = LitTest; } - bool isReadyToEval() { return Eval && Eval->ready(); } }; diff --git a/nixd/lib/CommandLine/Options.cpp b/nixd/lib/CommandLine/Options.cpp index 4333d2e3d..4eb7521e3 100644 --- a/nixd/lib/CommandLine/Options.cpp +++ b/nixd/lib/CommandLine/Options.cpp @@ -1,3 +1,10 @@ #include "nixd/CommandLine/Options.h" -llvm::cl::OptionCategory nixd::NixdCategory("nixd library options"); +using namespace llvm::cl; +using namespace nixd; + +OptionCategory nixd::NixdCategory("nixd library options"); + +opt nixd::LitTest{ + "lit-test", desc("Indicating that the server is running in lit-test mode."), + init(false), cat(NixdCategory)}; diff --git a/nixd/lib/Controller/LifeTime.cpp b/nixd/lib/Controller/LifeTime.cpp index 4edb3638a..4354f6478 100644 --- a/nixd/lib/Controller/LifeTime.cpp +++ b/nixd/lib/Controller/LifeTime.cpp @@ -30,6 +30,21 @@ opt DefaultNixOSOptionsExpr{ "= (import ) ++ [ ({...}: { " "nixpkgs.hostPlatform = builtins.currentSystem;} ) ] ; })).options")}; +// Here we try to wrap nixpkgs, nixos options in a single emtpy attrset in test. +std::string getDefaultNixpkgsExpr() { + if (LitTest && !DefaultNixpkgsExpr.getNumOccurrences()) { + return "{ }"; + } + return DefaultNixpkgsExpr; +} + +std::string getDefaultNixOSOptionsExpr() { + if (LitTest && !DefaultNixOSOptionsExpr.getNumOccurrences()) { + return "{ }"; + } + return DefaultNixOSOptionsExpr; +} + } // namespace void Controller::evalExprWithProgress(AttrSetClient &Client, @@ -147,7 +162,7 @@ void Controller:: startNixpkgs(NixpkgsEval); if (nixpkgsClient()) { - evalExprWithProgress(*nixpkgsClient(), DefaultNixpkgsExpr, + evalExprWithProgress(*nixpkgsClient(), getDefaultNixpkgsExpr(), "nixpkgs entries"); } @@ -157,7 +172,8 @@ void Controller:: startOption("nixos", Options["nixos"]); if (AttrSetClient *Client = Options["nixos"]->client()) - evalExprWithProgress(*Client, DefaultNixOSOptionsExpr, "nixos options"); + evalExprWithProgress(*Client, getDefaultNixOSOptionsExpr(), + "nixos options"); } fetchConfig(); } diff --git a/nixd/lib/Controller/Support.cpp b/nixd/lib/Controller/Support.cpp index 78b65d582..a3a303c98 100644 --- a/nixd/lib/Controller/Support.cpp +++ b/nixd/lib/Controller/Support.cpp @@ -107,7 +107,7 @@ void Controller::createWorkDoneProgress( Controller::Controller(std::unique_ptr In, std::unique_ptr Out) - : LSPServer(std::move(In), std::move(Out)), LitTest(false) { + : LSPServer(std::move(In), std::move(Out)) { // Life Cycle Registry.addMethod("initialize", this, &Controller::onInitialize); diff --git a/nixd/tools/nixd-attrset-eval.cpp b/nixd/tools/nixd-attrset-eval.cpp index d280ebe41..64cf5d0d3 100644 --- a/nixd/tools/nixd-attrset-eval.cpp +++ b/nixd/tools/nixd-attrset-eval.cpp @@ -1,5 +1,6 @@ #include "nixd-config.h" +#include "nixd/CommandLine/Options.h" #include "nixd/Eval/AttrSetProvider.h" #include @@ -10,6 +11,7 @@ using namespace llvm::cl; using namespace lspserver; +using namespace nixd; namespace { @@ -29,12 +31,6 @@ opt InputStyle{ Hidden, }; -opt LitTest{ - "lit-test", - desc("Abbreviation for -input-style=delimited -pretty -log=verbose. " - "Intended to simplify lit tests"), - init(false), cat(Debug)}; - opt LogLevel{ "log", desc("Verbosity of log messages written to stderr"), values( diff --git a/nixd/tools/nixd.cpp b/nixd/tools/nixd.cpp index 5a7b1fb98..002cc1df8 100644 --- a/nixd/tools/nixd.cpp +++ b/nixd/tools/nixd.cpp @@ -10,6 +10,7 @@ #include using namespace lspserver; +using namespace nixd; namespace { @@ -32,11 +33,7 @@ opt InputStyle{ cat(Debug), Hidden, }; -opt LitTest{ - "lit-test", - desc("Abbreviation for -input-style=delimited -pretty -log=verbose. " - "Intended to simplify lit tests"), - init(false), cat(Debug)}; + opt LogLevel{ "log", desc("Verbosity of log messages written to stderr"), values( @@ -81,9 +78,6 @@ int main(int argc, char *argv[]) { auto Controller = std::make_unique(std::move(In), std::move(Out)); - if (LitTest) - Controller->setLitTest(LitTest); - Controller->run(); return 0; diff --git a/nixd/tools/nixd/test/completion-nixpkgs.md b/nixd/tools/nixd/test/completion-nixpkgs.md index 71343ea39..936851447 100644 --- a/nixd/tools/nixd/test/completion-nixpkgs.md +++ b/nixd/tools/nixd/test/completion-nixpkgs.md @@ -1,4 +1,4 @@ -# RUN: nixd --lit-test < %s | FileCheck %s +# RUN: nixd --nixpkgs-expr='{ a = 1; b = 2; }' --lit-test < %s | FileCheck %s <-- initialize(0) @@ -57,15 +57,15 @@ ``` ``` - CHECK: "label": "AMB-plugins", + CHECK: "kind": 5, +CHECK-NEXT: "label": "a", CHECK-NEXT: "score": 0 CHECK-NEXT: }, CHECK-NEXT: { CHECK-NEXT: "data": "{\"Prefix\":\"\",\"Scope\":[]}", CHECK-NEXT: "kind": 5, -CHECK-NEXT: "label": "ArchiSteamFarm", +CHECK-NEXT: "label": "b", CHECK-NEXT: "score": 0 -CHECK-NEXT: }, ```