vfmt-server (new crate, binary vfmt-server): LSP over stdio on
rust-analyzer's minimal stack (lsp-server/lsp-types/serde/serde_json,
exact-pinned, 18 packages audited incl. all build scripts). Ships
formatting (byte-identical to the CLI), tolerant-parse diagnostics
with UTF-16-correct positions (CRLF collapse pinned; EOF spans
widened to drawable lines), semanticTokens/full, hover with `define
expansion (file table -> workspace fallback), definition through
both resolution layers (Ambiguous -> all locations), references,
documentSymbol and capped workspace/symbol. Deferred capabilities
are not advertised. 860-file corpus session: 0.79s, 29.5MB. QA drove
a real stdio session: diagnostics clear on fix, staleness model
honored, malformed frames survived.
linter-core (new crate vlog_linter + vfmt check): ruff-style rules
with Safe/Unsafe fix applicability, inline waivers with unused-
waiver detection (Verible-spelling waivers for unimplemented rules
deliberately tolerated -- corpus-driven), seven seed rules, --fix /
--select / --ignore / --output-format json, frozen exit codes, bare
vfmt byte-identical to before (worktree-proven). Corpus baseline:
7806 violations in 93 files, deterministic. Lead-directed fix:
--fix never edits inside // vfmt: off fences (reported, not
auto-fixed), with a scan backstop for tolerant-parse trees where
fence::collect sees no regions.
Both dev agents were interrupted by a session limit mid-round and
resumed from transcripts; the workspace survived intact.
Dev: 2 Opus agents; QA: 2 Sonnet agents; reviews in
docs/features/{vfmt-server,linter-core}/README.md.
Co-Authored-By: Claude Fable 5 <[email protected]>
151 lines
6.4 KiB
Rust
151 lines
6.4 KiB
Rust
//! One end-to-end smoke test: spawn the real binary and speak framed
|
|
//! JSON-RPC at it over stdio.
|
|
//!
|
|
//! Everything else in this crate is tested by calling handlers directly with
|
|
//! constructed state, which is faster and pins behavior more precisely. This
|
|
//! test exists to catch the things those cannot: that the binary starts, that
|
|
//! the `Content-Length` framing works in both directions, that the
|
|
//! initialize/initialized/shutdown/exit handshake completes, and that a
|
|
//! notification the server *pushes* (diagnostics) actually reaches the wire.
|
|
//!
|
|
//! The framing is hand-rolled here on purpose — a test that reused the
|
|
//! server's own transport could not catch a framing bug.
|
|
|
|
use std::io::{BufRead, BufReader, Write};
|
|
use std::path::PathBuf;
|
|
use std::process::{Command, Stdio};
|
|
|
|
/// Frame one JSON payload the way LSP wants it.
|
|
fn frame(payload: &str) -> String {
|
|
format!("Content-Length: {}\r\n\r\n{payload}", payload.len())
|
|
}
|
|
|
|
/// Split a stdout dump into its JSON payloads.
|
|
fn unframe(mut input: impl BufRead) -> Vec<serde_json::Value> {
|
|
let mut out = Vec::new();
|
|
loop {
|
|
let mut length = None;
|
|
// Headers, terminated by an empty line.
|
|
loop {
|
|
let mut line = String::new();
|
|
if input.read_line(&mut line).unwrap_or(0) == 0 {
|
|
return out;
|
|
}
|
|
let trimmed = line.trim_end_matches(['\r', '\n']);
|
|
if trimmed.is_empty() {
|
|
break;
|
|
}
|
|
if let Some(value) = trimmed.strip_prefix("Content-Length: ") {
|
|
length = value.parse::<usize>().ok();
|
|
}
|
|
}
|
|
let Some(length) = length else { return out };
|
|
let mut body = vec![0u8; length];
|
|
if input.read_exact(&mut body).is_err() {
|
|
return out;
|
|
}
|
|
match serde_json::from_slice(&body) {
|
|
Ok(value) => out.push(value),
|
|
Err(_) => return out,
|
|
}
|
|
}
|
|
}
|
|
|
|
fn temp_dir() -> PathBuf {
|
|
let dir = std::env::temp_dir().join(format!("vfmt_server_e2e_{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
std::fs::create_dir_all(&dir).expect("temp dir");
|
|
dir
|
|
}
|
|
|
|
/// A buffer that is broken *and* still has a resolvable reference: error
|
|
/// recovery is what makes both true at once, and the LSP depends on it.
|
|
const SOURCE: &str = "module top;\n wire a;\n assign a = 1;\n wire ;\nendmodule\n";
|
|
|
|
#[test]
|
|
fn the_binary_serves_a_whole_session_over_stdio() {
|
|
let dir = temp_dir();
|
|
let file = dir.join("top.sv");
|
|
std::fs::write(&file, SOURCE).expect("write");
|
|
let root = format!("file://{}", dir.display());
|
|
let uri = format!("file://{}", file.display());
|
|
|
|
// Every message is written before anything is read: the server processes
|
|
// them in order and exits on `exit`, so stdout closes and the test can
|
|
// never deadlock waiting on a message that is not coming.
|
|
let session = [
|
|
frame(&format!(
|
|
r#"{{"jsonrpc":"2.0","id":1,"method":"initialize","params":{{"processId":null,"rootUri":"{root}","capabilities":{{}}}}}}"#
|
|
)),
|
|
frame(r#"{"jsonrpc":"2.0","method":"initialized","params":{}}"#),
|
|
frame(&format!(
|
|
r#"{{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{{"textDocument":{{"uri":"{uri}","languageId":"systemverilog","version":1,"text":{}}}}}}}"#,
|
|
serde_json::to_string(SOURCE).expect("json")
|
|
)),
|
|
// The `a` of `assign a` — line 2, character 9.
|
|
frame(&format!(
|
|
r#"{{"jsonrpc":"2.0","id":2,"method":"textDocument/definition","params":{{"textDocument":{{"uri":"{uri}"}},"position":{{"line":2,"character":9}}}}}}"#
|
|
)),
|
|
frame(r#"{"jsonrpc":"2.0","id":3,"method":"shutdown","params":null}"#),
|
|
frame(r#"{"jsonrpc":"2.0","method":"exit","params":null}"#),
|
|
]
|
|
.concat();
|
|
|
|
let mut child = Command::new(env!("CARGO_BIN_EXE_vfmt-server"))
|
|
.stdin(Stdio::piped())
|
|
.stdout(Stdio::piped())
|
|
.stderr(Stdio::null())
|
|
.spawn()
|
|
.expect("the server binary starts");
|
|
|
|
child.stdin.take().expect("stdin").write_all(session.as_bytes()).expect("write session");
|
|
let stdout = child.stdout.take().expect("stdout");
|
|
let messages = unframe(BufReader::new(stdout));
|
|
let status = child.wait().expect("the server exits");
|
|
assert!(status.success(), "clean exit, got {status:?}");
|
|
|
|
// 1. The initialize response advertises what we implement.
|
|
let initialize = messages
|
|
.iter()
|
|
.find(|m| m["id"] == 1)
|
|
.unwrap_or_else(|| panic!("no initialize response in {messages:#?}"));
|
|
let capabilities = &initialize["result"]["capabilities"];
|
|
assert_eq!(capabilities["positionEncoding"], "utf-16");
|
|
assert_eq!(capabilities["documentFormattingProvider"], true);
|
|
assert_eq!(capabilities["definitionProvider"], true);
|
|
assert!(capabilities["semanticTokensProvider"].is_object());
|
|
assert_eq!(initialize["result"]["serverInfo"]["name"], "vfmt-server");
|
|
|
|
// 2. Diagnostics were pushed for the broken buffer.
|
|
let published = messages
|
|
.iter()
|
|
.find(|m| m["method"] == "textDocument/publishDiagnostics")
|
|
.unwrap_or_else(|| panic!("no diagnostics in {messages:#?}"));
|
|
assert_eq!(published["params"]["uri"], uri.as_str());
|
|
let diagnostics = published["params"]["diagnostics"].as_array().expect("an array");
|
|
assert_eq!(diagnostics.len(), 1, "one error for `wire ;`");
|
|
assert_eq!(diagnostics[0]["severity"], 1, "an error");
|
|
assert_eq!(diagnostics[0]["source"], "vfmt");
|
|
assert_eq!(diagnostics[0]["range"]["start"]["line"], 3, "the `wire ;` line");
|
|
|
|
// 3. Definition jumped to the declaration, in the same file.
|
|
let definition = messages
|
|
.iter()
|
|
.find(|m| m["id"] == 2)
|
|
.unwrap_or_else(|| panic!("no definition response in {messages:#?}"));
|
|
let locations = definition["result"].as_array().expect("an array of locations");
|
|
assert_eq!(locations.len(), 1, "got {locations:#?}");
|
|
assert_eq!(locations[0]["uri"], uri.as_str());
|
|
assert_eq!(locations[0]["range"]["start"]["line"], 1, "the `wire a;` line");
|
|
assert_eq!(locations[0]["range"]["start"]["character"], 7);
|
|
|
|
// 4. Shutdown was acknowledged.
|
|
let shutdown = messages
|
|
.iter()
|
|
.find(|m| m["id"] == 3)
|
|
.unwrap_or_else(|| panic!("no shutdown response in {messages:#?}"));
|
|
assert!(shutdown["error"].is_null(), "shutdown must succeed");
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
}
|