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
#581)

Previously the server will crash.
  • Loading branch information
inclyc authored Aug 11, 2024
1 parent 9b9ba70 commit 84bedfb
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
6 changes: 6 additions & 0 deletions nixd/lib/Controller/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ struct NotAnIdiomException : IdiomSelectorException {

struct VLAException : std::exception {};

struct NoLocationForBuiltinVariable : std::exception {
[[nodiscard]] const char *what() const noexcept override {
return "builtins are defined in the interpreter, not in the Nix files";
}
};

/// \brief No such variable.
struct NoSuchVarException : VLAException {
[[nodiscard]] const char *what() const noexcept override {
Expand Down
13 changes: 9 additions & 4 deletions nixd/lib/Controller/Definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ const Definition &findVarDefinition(const ExprVar &Var,
/// \brief Convert nixf::Definition to lspserver::Location
Location convertToLocation(llvm::StringRef Src, const Definition &Def,
URIForFile URI) {
if (!Def.syntax())
throw NoLocationForBuiltinVariable();
assert(Def.syntax());
return Location{
.uri = std::move(URI),
.range = toLSPRange(Src, Def.syntax()->range()),
Expand Down Expand Up @@ -292,9 +295,10 @@ 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::StringRef Src) {
llvm::Expected<Locations>
defineVar(const ExprVar &Var, const VariableLookupAnalysis &VLA,
const ParentMapAnalysis &PM, AttrSetClient &NixpkgsClient,
const URIForFile &URI, llvm::StringRef Src) {
try {
Locations StaticLocs = defineVarStatic(Var, VLA, URI, Src);

Expand All @@ -309,8 +313,9 @@ Locations defineVar(const ExprVar &Var, const VariableLookupAnalysis &VLA,
}
} catch (std::exception &E) {
elog("definition/static: {0}", E.what());
return Locations{};
}
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: "id": 2,
CHECK-NEXT: "jsonrpc": "2.0",
CHECK-NEXT: "result": null
```


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

0 comments on commit 84bedfb

Please sign in to comment.