Skip to content

Commit

Permalink
nixd/Controller: report error on textDocument/definition on builtins
Browse files Browse the repository at this point in the history
Previously the server will crash.
  • Loading branch information
inclyc committed Aug 11, 2024
1 parent 4c306e7 commit 432cfa3
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
6 changes: 6 additions & 0 deletions nixd/lib/Controller/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ struct NotAnIdiomException : IdiomSelectorException {

struct VLAException : std::exception {};

struct NoLocationForBuiltinVariable : std::exception {
[[nodiscard]] const char *what() const noexcept override {
return "builtin variable is not defined in nix source location";
}
};

/// \brief No such variable.
struct NoSuchVarException : VLAException {
[[nodiscard]] const char *what() const noexcept override {
Expand Down
17 changes: 12 additions & 5 deletions nixd/lib/Controller/Definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ const Definition &findVarDefinition(const ExprVar &Var,

/// \brief Convert nixf::Definition to lspserver::Location
Location convertToLocation(const Definition &Def, URIForFile URI) {
if (!Def.syntax())
throw NoLocationForBuiltinVariable();
assert(Def.syntax());
return Location{
.uri = std::move(URI),
.range = toLSPRange(Def.syntax()->range()),
Expand Down Expand Up @@ -290,9 +293,11 @@ std::vector<T> mergeVec(std::vector<T> A, const std::vector<T> &B) {
return A;
}

Locations defineVar(const ExprVar &Var, const VariableLookupAnalysis &VLA,
const ParentMapAnalysis &PM, AttrSetClient &NixpkgsClient,
const URIForFile &URI) {
llvm::Expected<Locations> defineVar(const ExprVar &Var,
const VariableLookupAnalysis &VLA,
const ParentMapAnalysis &PM,
AttrSetClient &NixpkgsClient,
const URIForFile &URI) {
try {
Locations StaticLocs = defineVarStatic(Var, VLA, URI);

Expand All @@ -306,9 +311,11 @@ Locations defineVar(const ExprVar &Var, const VariableLookupAnalysis &VLA,
return StaticLocs;
}
} catch (std::exception &E) {
elog("definition/static: {0}", E.what());
// Pop a window, notify the user we cannot find static definition.
// Likely this will be triggerred by clicking "builtins"
return error(E.what());
}
return {};
return error("unreachable code! Please submit an issue");
}

/// \brief Squash a vector into smaller json variant.
Expand Down
67 changes: 67 additions & 0 deletions nixd/tools/nixd/test/definition/builtin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# RUN: nixd --lit-test < %s | FileCheck %s

<-- initialize(0)

```json
{
"jsonrpc":"2.0",
"id":0,
"method":"initialize",
"params":{
"processId":123,
"rootPath":"",
"capabilities":{
},
"trace":"off"
}
}
```


<-- textDocument/didOpen

```json
{
"jsonrpc":"2.0",
"method":"textDocument/didOpen",
"params":{
"textDocument":{
"uri":"file:///basic.nix",
"languageId":"nix",
"version":1,
"text":"builtins"
}
}
}
```

<-- textDocument/definition(2)


```json
{
"jsonrpc":"2.0",
"id":2,
"method":"textDocument/definition",
"params":{
"textDocument":{
"uri":"file:///basic.nix"
},
"position":{
"line": 0,
"character":3
}
}
}
```

```
CHECK: "message": "builtin variable is not defined in nix source location"
CHECK-NEXT: },
CHECK-NEXT: "id": 2,
```


```json
{"jsonrpc":"2.0","method":"exit"}
```

0 comments on commit 432cfa3

Please sign in to comment.