solidity-language-server 0.1.37

A fast Solidity language server powered by solc and Foundry
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! Diagnostics must be positioned against the text that was compiled.
//!
//! `on_change` hands solc the editor's live buffer, so the byte offsets it
//! returns describe that buffer. Mapping them through the on-disk file instead
//! puts every squiggle on the wrong line whenever the two differ — which is the
//! normal state of an editor with unsaved edits.
//!
//! Requires the Foundry toolchain on PATH, like `tests/build.rs`. CI installs it
//! via `foundry-rs/foundry-toolchain` before `cargo test`.

use serde_json::{Value, json};
use std::collections::VecDeque;
use std::fs;
use std::io::{BufRead, BufReader, Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio};
use std::sync::mpsc::{self, Receiver, RecvTimeoutError};
use std::thread;
use std::time::{Duration, Instant};
use tempfile::TempDir;
use tower_lsp::lsp_types::Url;

/// What sits on disk: compiles cleanly.
const ON_DISK: &str = r#"// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Probe {
    uint256 public total;

    function ok() external view returns (uint256) {
        return total;
    }
}
"#;

/// The unsaved buffer: four extra lines above, and a type error further down.
const IN_BUFFER: &str = r#"// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// unsaved line A
// unsaved line B
// unsaved line C
// unsaved line D

contract Probe {
    uint256 public total;

    function ok() external view returns (uint256) {
        total = "not a number";
        return total;
    }
}
"#;

/// On disk for the second case: same contract, padded well past the buffer's
/// length so a bad offset still lands *inside* this text.
const PADDED_ON_DISK: &str = r#"// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// Padding so this file is comfortably longer than the buffer below.
// Padding line 2.
// Padding line 3.
// Padding line 4.
// Padding line 5.
// Padding line 6.
// Padding line 7.
// Padding line 8.

contract Probe {
    uint256 public total;

    function ok() external view returns (uint256) {
        return total;
    }
}
"#;

/// The unsaved buffer for the second case: shorter than what is on disk, with
/// the same injected type error.
const SHORT_BUFFER: &str = r#"// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Probe {
    uint256 public total;

    function ok() external view returns (uint256) {
        total = "not a number";
        return total;
    }
}
"#;

struct LspProc {
    child: Child,
    stdin: ChildStdin,
    rx: Receiver<Value>,
    stash: VecDeque<Value>,
    next_id: u64,
    /// Set by `shutdown`; without it `Drop` reaps a server orphaned by a panic.
    graceful: bool,
}

impl LspProc {
    fn spawn(cwd: &Path) -> Self {
        let bin = option_env!("CARGO_BIN_EXE_solidity-language-server")
            .or(option_env!("CARGO_BIN_EXE_solidity_language_server"))
            .map(PathBuf::from)
            .unwrap_or_else(|| {
                PathBuf::from(env!("CARGO_MANIFEST_DIR"))
                    .join("target")
                    .join("debug")
                    .join("solidity-language-server")
            });

        let mut child = Command::new(bin)
            .arg("--stdio")
            .current_dir(cwd)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .spawn()
            .expect("spawn solidity-language-server");

        let stdin = child.stdin.take().expect("stdin");
        let stdout = child.stdout.take().expect("stdout");
        let rx = spawn_reader(stdout);
        Self {
            child,
            stdin,
            rx,
            stash: VecDeque::new(),
            next_id: 1,
            graceful: false,
        }
    }

    fn send_notification(&mut self, method: &str, params: Value) {
        self.write_msg(&json!({ "jsonrpc": "2.0", "method": method, "params": params }));
    }

    fn send_request(&mut self, method: &str, params: Value) -> u64 {
        let id = self.next_id;
        self.next_id += 1;
        self.write_msg(&json!({
            "jsonrpc": "2.0",
            "id": id,
            "method": method,
            "params": params,
        }));
        id
    }

    fn write_msg(&mut self, msg: &Value) {
        let body = serde_json::to_vec(msg).expect("serialize");
        let header = format!("Content-Length: {}\r\n\r\n", body.len());
        self.stdin
            .write_all(header.as_bytes())
            .expect("write header");
        self.stdin.write_all(&body).expect("write body");
        self.stdin.flush().expect("flush");
    }

    fn wait_response(&mut self, id: u64, timeout: Duration) -> Value {
        let deadline = Instant::now() + timeout;
        loop {
            if let Some(idx) = self
                .stash
                .iter()
                .position(|m| m.get("id").and_then(Value::as_u64) == Some(id))
            {
                return self.stash.remove(idx).expect("stashed response");
            }
            let remaining = deadline.saturating_duration_since(Instant::now());
            assert!(!remaining.is_zero(), "timed out waiting for response {id}");
            match self.rx.recv_timeout(remaining) {
                Ok(msg) => self.stash.push_back(msg),
                Err(RecvTimeoutError::Timeout) => panic!("timed out waiting for response {id}"),
                Err(RecvTimeoutError::Disconnected) => panic!("server exited unexpectedly"),
            }
        }
    }

    /// Wait for `n` `publishDiagnostics` payloads for `uri`, empty included.
    ///
    /// One is not enough to mean "the build finished": `on_change` publishes an
    /// empty payload up front to clear stale squiggles, before solc is invoked
    /// at all. The second payload is the compiled result, so that is what tells
    /// us the initial build is done and the buffer is safe to dirty.
    fn wait_for_diagnostics_count(&mut self, uri: &str, n: usize, timeout: Duration) -> bool {
        let deadline = Instant::now() + timeout;
        let is_match = |msg: &Value| {
            msg.get("method").and_then(Value::as_str) == Some("textDocument/publishDiagnostics")
                && msg.pointer("/params/uri").and_then(Value::as_str) == Some(uri)
        };
        let mut seen = 0;
        loop {
            while let Some(idx) = self.stash.iter().position(is_match) {
                self.stash.remove(idx);
                seen += 1;
                if seen >= n {
                    return true;
                }
            }
            let remaining = deadline.saturating_duration_since(Instant::now());
            if remaining.is_zero() {
                return false;
            }
            match self.rx.recv_timeout(remaining) {
                Ok(msg) => {
                    if is_match(&msg) {
                        seen += 1;
                        if seen >= n {
                            return true;
                        }
                    } else {
                        self.stash.push_back(msg);
                    }
                }
                Err(_) => return false,
            }
        }
    }

    /// Wait for a `publishDiagnostics` for `uri` that carries at least one
    /// error-severity entry. Empty payloads are published routinely (to clear
    /// stale squiggles), so they are skipped rather than accepted.
    fn wait_for_error_diagnostics(&mut self, uri: &str, timeout: Duration) -> Vec<Value> {
        let deadline = Instant::now() + timeout;
        loop {
            while let Some(msg) = self.stash.pop_front() {
                if let Some(diags) = error_diagnostics_for(&msg, uri) {
                    return diags;
                }
            }
            let remaining = deadline.saturating_duration_since(Instant::now());
            if remaining.is_zero() {
                return vec![];
            }
            match self.rx.recv_timeout(remaining) {
                Ok(msg) => {
                    if let Some(diags) = error_diagnostics_for(&msg, uri) {
                        return diags;
                    }
                }
                Err(_) => return vec![],
            }
        }
    }

    fn shutdown(mut self) {
        self.graceful = true;
        let id = self.send_request("shutdown", Value::Null);
        let _ = self.wait_response(id, Duration::from_secs(5));
        self.send_notification("exit", Value::Null);
        let deadline = Instant::now() + Duration::from_secs(2);
        loop {
            match self.child.try_wait() {
                Ok(Some(_)) => break,
                Ok(None) if Instant::now() < deadline => {
                    thread::sleep(Duration::from_millis(20));
                }
                Ok(None) | Err(_) => {
                    let _ = self.child.kill();
                    let _ = self.child.wait();
                    break;
                }
            }
        }
    }
}

/// Any assertion that fires before `shutdown` unwinds past it, and dropping a
/// `Child` does not kill the process, so reap it here.
impl Drop for LspProc {
    fn drop(&mut self) {
        if !self.graceful {
            let _ = self.child.kill();
            let _ = self.child.wait();
        }
    }
}

fn error_diagnostics_for(msg: &Value, uri: &str) -> Option<Vec<Value>> {
    if msg.get("method").and_then(Value::as_str) != Some("textDocument/publishDiagnostics") {
        return None;
    }
    let params = msg.get("params")?;
    if params.get("uri").and_then(Value::as_str) != Some(uri) {
        return None;
    }
    let errors: Vec<Value> = params
        .get("diagnostics")?
        .as_array()?
        .iter()
        .filter(|d| d.get("severity").and_then(Value::as_u64) == Some(1))
        .cloned()
        .collect();
    (!errors.is_empty()).then_some(errors)
}

fn spawn_reader(stdout: ChildStdout) -> Receiver<Value> {
    let (tx, rx) = mpsc::channel();
    thread::spawn(move || {
        let mut reader = BufReader::new(stdout);
        let mut line = String::new();
        loop {
            let mut content_length: usize = 0;
            line.clear();
            match reader.read_line(&mut line) {
                Ok(0) | Err(_) => return,
                Ok(_) => {}
            }
            loop {
                if line == "\r\n" {
                    break;
                }
                if let Some(v) = line.strip_prefix("Content-Length:") {
                    content_length = v.trim().parse::<usize>().unwrap_or(0);
                }
                line.clear();
                match reader.read_line(&mut line) {
                    Ok(0) | Err(_) => return,
                    Ok(_) => {}
                }
            }
            if content_length == 0 {
                continue;
            }
            let mut body = vec![0u8; content_length];
            if reader.read_exact(&mut body).is_err() {
                return;
            }
            if let Ok(msg) = serde_json::from_slice::<Value>(&body) {
                let _ = tx.send(msg);
            }
        }
    });
    rx
}

fn write_project(dir: &Path, on_disk: &str) -> (String, String) {
    let src = dir.join("src");
    fs::create_dir_all(&src).expect("create src");
    // Lint is deliberately left at its default (on). That is the branch a real
    // Foundry project takes, and it is the one of the two changed call sites
    // that would otherwise go untested. The assertions filter to severity 1, so
    // forge-lint warnings cannot interfere.
    fs::write(
        dir.join("foundry.toml"),
        "[profile.default]\nsrc = \"src\"\n",
    )
    .expect("write foundry.toml");

    let probe = src.join("Probe.sol");
    fs::write(&probe, on_disk).expect("write Probe.sol");

    let root_uri = Url::from_file_path(dir).expect("root uri").to_string();
    let probe_uri = Url::from_file_path(&probe).expect("probe uri").to_string();
    (root_uri, probe_uri)
}

fn initialize_server(lsp: &mut LspProc, root_uri: &str) {
    let id = lsp.send_request(
        "initialize",
        json!({
            "processId": null,
            "rootUri": root_uri,
            "capabilities": {},
            "initializationOptions": {
                "projectIndex": { "fullProjectScan": false }
            }
        }),
    );
    let resp = lsp.wait_response(id, Duration::from_secs(30));
    assert!(resp.get("result").is_some(), "initialize failed: {resp}");
    lsp.send_notification("initialized", json!({}));
}

/// Drive one disk-vs-buffer divergence and return the first error diagnostic's
/// range as (start_line, start_char, end_line, end_char).
fn reported_error_range(on_disk: &str, in_buffer: &str) -> (u64, u64, u64, u64) {
    let dir = TempDir::new().expect("tempdir");
    let (root_uri, probe_uri) = write_project(dir.path(), on_disk);

    let mut lsp = LspProc::spawn(dir.path());
    initialize_server(&mut lsp, &root_uri);

    lsp.send_notification(
        "textDocument/didOpen",
        json!({
            "textDocument": {
                "uri": probe_uri,
                "languageId": "solidity",
                "version": 1,
                "text": on_disk,
            }
        }),
    );

    // Two payloads: the up-front clear, then the compiled result. Waiting for
    // only the first would return before solc has run and race the edit below.
    assert!(
        lsp.wait_for_diagnostics_count(&probe_uri, 2, Duration::from_secs(180)),
        "server never published a compiled result for the freshly opened file — \
         is the Foundry toolchain on PATH?"
    );

    // Unsaved edit: the buffer now differs from what is on disk.
    lsp.send_notification(
        "textDocument/didChange",
        json!({
            "textDocument": { "uri": probe_uri, "version": 2 },
            "contentChanges": [{ "text": in_buffer }],
        }),
    );

    // didSave without the optional `text`, so the server compiles from its
    // text_cache — the dirty buffer — while the file on disk is unchanged.
    lsp.send_notification(
        "textDocument/didSave",
        json!({ "textDocument": { "uri": probe_uri } }),
    );

    let diags = lsp.wait_for_error_diagnostics(&probe_uri, Duration::from_secs(120));
    lsp.shutdown();

    assert!(
        !diags.is_empty(),
        "no error diagnostics arrived — the Foundry toolchain may be missing, \
         or solc never reported the injected type error"
    );

    let range = &diags[0]["range"];
    let field = |a: &str, b: &str| {
        range[a][b]
            .as_u64()
            .unwrap_or_else(|| panic!("diagnostic range missing {a}.{b}"))
    };
    (
        field("start", "line"),
        field("start", "character"),
        field("end", "line"),
        field("end", "character"),
    )
}

/// Locate `needle` in `text` as (line, start_char, end_char), all zero-indexed
/// and in UTF-16 units — the fixtures are ASCII, so bytes and units coincide.
fn locate(text: &str, needle: &str) -> (u64, u64, u64) {
    let (line_no, line) = text
        .lines()
        .enumerate()
        .find(|(_, l)| l.contains(needle))
        .unwrap_or_else(|| panic!("fixture does not contain {needle:?}"));
    let col = line.find(needle).expect("needle on the located line") as u64;
    (line_no as u64, col, col + needle.len() as u64)
}

/// Buffer LONGER than disk: the error's byte offset runs past the end of the
/// on-disk text, so a disk-based mapping clamps to end-of-file.
#[test]
fn diagnostics_are_positioned_against_the_live_buffer() {
    let (line, start_col, end_col) = locate(IN_BUFFER, "\"not a number\"");
    let got = reported_error_range(ON_DISK, IN_BUFFER);

    assert_eq!(
        got,
        (line, start_col, line, end_col),
        "diagnostic landed at {got:?}, but the error spans line {line} \
         cols {start_col}..{end_col} of the live buffer; offsets were mapped \
         against the on-disk text"
    );
}

/// Buffer SHORTER than disk: the offset still resolves inside the on-disk text,
/// so a disk-based mapping yields a wrong-but-plausible line rather than an
/// obvious end-of-file clamp. This is the variant that hides in a bug report.
#[test]
fn diagnostics_are_positioned_against_a_buffer_shorter_than_disk() {
    let (line, start_col, end_col) = locate(SHORT_BUFFER, "\"not a number\"");
    let got = reported_error_range(PADDED_ON_DISK, SHORT_BUFFER);

    assert_eq!(
        got,
        (line, start_col, line, end_col),
        "diagnostic landed at {got:?}, but the error spans line {line} \
         cols {start_col}..{end_col} of the live buffer"
    );
}