Skip to content

Commit 2980b53

Browse files
committed
feat: Use nixpkgs to resolve with liveness
Since 7683220 we always mark nested `with` expressions alive. This is technically sound because, indeed, there's no general way to determine whether a particular attribute comes from one of the nested `with`s. Sadly, this change made it impossible to detect common problematic patterns in nixpkgs tree like: ``` meta = with lib; { maintainers = with lib.maintainers; [ ... ]; }; ``` This would be very useful in our ongoing attempt to automate cleanup of these patterns from the whole `nixpkgs` tree, see: technowledgy/refactor-tractor#14 This change attempts to address this problem. It is observed that nixd completion controllers already use nixpkgs attrset and heuristics to look for known attributes in `pkgs` and `lib`, assuming these belong to nixpkgs. We can do the same when analysing `with` scopes. 1. This patch reuses the existing nixpkgs client infrastructure for `Sema/VariableLookup.cpp`. Since variable analysis is part of `libnixf` while nixpkgs client and LSP are part of `nixd`, we have to relocate a number of modules plus the `nixd-attrset-eval` tool outside the `nixd` directory under `common`. 2. The `nixd-attrset-eval` tool was truncating the number of returned values by 30, which is useful for interactive use in completion helper context, but is not enough for comprehensive variable scope analysis. For this reason, the json protocol for the tool was extended to support `MaxItems` attribute that can be set to `0` to indicate that no truncation should occur. 3. On performance optimizations: The new nixpkgs client is initialized once per call to `VLA`. It will load and cache discovered attributes as needed for the `with` resolution procedure. (In the future, it may be used for other needs as needed.) Note: It could be beneficial to allow reuse of the same nixpkgs client with its cached results across multiple calls to `VLA`. This is left for a future exercise, if and when consuming tools like `nixd-tidy` gain support for multi-file operation. 4. On build system: Because the project supports "global" meson build from the root directory as well as component specific builds for `libnixf` and `nixd`, we have to setup scaffolding to reuse the `common` library in both. This is achieved with subprojects and a bunch of symlinks. 5. Because `libnixf` / `nixd-tiny` can be used interactively, and each call to `nixd-attrset-eval` is logged in extreme details (including complete json protocol comms between the tool and its driver), we have to suppress this output not to pollute the caller's stdout with these debug messages. Some adjustments were done to allow to disable these log messages when the tool is called from `VLA` nixpkgs client. 6. VLA test timeout is bumped from 30s to 120s because it now has to run nixd-attrset-eval. The Sema test now takes time comparable to nixd regression suite for this reason. 120s is the timeout taken from nixd regression suite.
1 parent 7ba686e commit 2980b53

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+562
-166
lines changed

nixd/include/nixd/Eval/AttrSetClient.h renamed to common/include/nixd/Eval/AttrSetClient.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ class AttrSetClient : public lspserver::LSPServer {
6767

6868
void exit() { Exit(nullptr); }
6969

70+
void setLoggingEnabled(bool Enabled) {
71+
LSPServer::setLoggingEnabled(Enabled);
72+
}
73+
7074
/// Get executable path for launching the server.
7175
/// \returns null terminated string.
7276
static const char *getExe();

common/include/nixd/Eval/Spawn.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include "nixd/Eval/AttrSetClient.h"
4+
5+
#include <memory>
6+
#include <string_view>
7+
8+
namespace nixd {
9+
10+
/// Launches an attrset evaluator process whose stderr is redirected to
11+
/// `WorkerStderr`.
12+
void spawnAttrSetEval(std::string_view WorkerStderr,
13+
std::unique_ptr<AttrSetClientProc> &Worker);
14+
15+
} // namespace nixd

nixd/include/nixd/Protocol/AttrSet.h renamed to common/include/nixd/Protocol/AttrSet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ struct AttrPathCompleteParams {
108108
Selector Scope;
109109
/// \brief Search for packages prefixed with this "prefix"
110110
std::string Prefix;
111+
std::optional<int> MaxItems;
111112
};
112113

113114
llvm::json::Value toJSON(const AttrPathCompleteParams &Params);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)