Skip to content

Commit

Permalink
nixd: use orphan nixpkgs, nixos default in lit-test mode (#431)
Browse files Browse the repository at this point in the history
To speed up lit-testing, we don't need to evaluate full nixpkgs.
  • Loading branch information
inclyc authored Apr 19, 2024
1 parent 29ccbfc commit 24c9e40
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 27 deletions.
3 changes: 3 additions & 0 deletions nixd/include/nixd/CommandLine/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ namespace nixd {

extern llvm::cl::OptionCategory NixdCategory;

/// \brief Indicating that we are in lit-test mode.
extern llvm::cl::opt<bool> LitTest;

} // namespace nixd
5 changes: 0 additions & 5 deletions nixd/include/nixd/Controller/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t> Version);
Expand Down Expand Up @@ -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(); }
};

Expand Down
9 changes: 8 additions & 1 deletion nixd/lib/CommandLine/Options.cpp
Original file line number Diff line number Diff line change
@@ -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<bool> nixd::LitTest{
"lit-test", desc("Indicating that the server is running in lit-test mode."),
init(false), cat(NixdCategory)};
20 changes: 18 additions & 2 deletions nixd/lib/Controller/LifeTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ opt<std::string> DefaultNixOSOptionsExpr{
"= (import <nixpkgs/nixos/modules/module-list.nix>) ++ [ ({...}: { "
"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,
Expand Down Expand Up @@ -147,7 +162,7 @@ void Controller::
startNixpkgs(NixpkgsEval);

if (nixpkgsClient()) {
evalExprWithProgress(*nixpkgsClient(), DefaultNixpkgsExpr,
evalExprWithProgress(*nixpkgsClient(), getDefaultNixpkgsExpr(),
"nixpkgs entries");
}

Expand All @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion nixd/lib/Controller/Support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void Controller::createWorkDoneProgress(

Controller::Controller(std::unique_ptr<lspserver::InboundPort> In,
std::unique_ptr<lspserver::OutboundPort> 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);
Expand Down
8 changes: 2 additions & 6 deletions nixd/tools/nixd-attrset-eval.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "nixd-config.h"

#include "nixd/CommandLine/Options.h"
#include "nixd/Eval/AttrSetProvider.h"

#include <llvm/Support/CommandLine.h>
Expand All @@ -10,6 +11,7 @@

using namespace llvm::cl;
using namespace lspserver;
using namespace nixd;

namespace {

Expand All @@ -29,12 +31,6 @@ opt<JSONStreamStyle> InputStyle{
Hidden,
};

opt<bool> LitTest{
"lit-test",
desc("Abbreviation for -input-style=delimited -pretty -log=verbose. "
"Intended to simplify lit tests"),
init(false), cat(Debug)};

opt<Logger::Level> LogLevel{
"log", desc("Verbosity of log messages written to stderr"),
values(
Expand Down
10 changes: 2 additions & 8 deletions nixd/tools/nixd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <llvm/Support/CommandLine.h>

using namespace lspserver;
using namespace nixd;

namespace {

Expand All @@ -32,11 +33,7 @@ opt<JSONStreamStyle> InputStyle{
cat(Debug),
Hidden,
};
opt<bool> LitTest{
"lit-test",
desc("Abbreviation for -input-style=delimited -pretty -log=verbose. "
"Intended to simplify lit tests"),
init(false), cat(Debug)};

opt<Logger::Level> LogLevel{
"log", desc("Verbosity of log messages written to stderr"),
values(
Expand Down Expand Up @@ -81,9 +78,6 @@ int main(int argc, char *argv[]) {
auto Controller =
std::make_unique<nixd::Controller>(std::move(In), std::move(Out));

if (LitTest)
Controller->setLitTest(LitTest);

Controller->run();

return 0;
Expand Down
8 changes: 4 additions & 4 deletions nixd/tools/nixd/test/completion-nixpkgs.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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: },
```


Expand Down

0 comments on commit 24c9e40

Please sign in to comment.