Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/pages/reference/diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ Diagnostic fields:
- `code`: numeric solc error code (e.g. `2072`)
- `severity`: mapped from solc severity string (`"error"` → `ERROR`, `"warning"` → `WARNING`, `"info"` → `INFORMATION`)

#### Which text offsets are resolved against

solc reports locations as byte offsets into the source it was given, so each
offset must be converted using that same text — otherwise the reported range
drifts.

- **The file being compiled**: its offsets are resolved against the buffer text
passed to `solc_ast()`, which is what the editor currently shows. Resolving
them against the file on disk instead puts every range in the file at the
wrong position whenever the buffer has unsaved edits, which happens routinely
because `run_did_save()` falls back to the text cache when a client omits the
optional `text` field on `didSave`.
- **Its imports**: those reach solc as `"urls"` rather than inlined content, so
solc reads them from disk itself and `build::cross_file_error_diagnostics()`
correctly resolves their offsets against disk.

### Source 2: forge lint

Lint diagnostics come from `forge lint --json`.
Expand Down
14 changes: 5 additions & 9 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,14 +804,13 @@ impl ForgeLsp {
);
match solc {
Ok(data) => {
// Extract diagnostics from the same solc output
let content = tokio::fs::read_to_string(&file_path)
.await
.unwrap_or_default();
// Extract diagnostics from the same live buffer text
// passed to solc so byte offsets and LSP ranges stay
// aligned with the editor, not a stale on-disk file.
let build_diags = crate::build::build_output_to_diagnostics(
&data,
&file_path,
&content,
&params.text,
&foundry_cfg.ignored_error_codes,
);
(Some(lint), Ok(build_diags), Ok(data))
Expand Down Expand Up @@ -839,13 +838,10 @@ impl ForgeLsp {
.await;
match solc_future.await {
Ok(data) => {
let content = tokio::fs::read_to_string(&file_path)
.await
.unwrap_or_default();
let build_diags = crate::build::build_output_to_diagnostics(
&data,
&file_path,
&content,
&params.text,
&foundry_cfg.ignored_error_codes,
);
(None, Ok(build_diags), Ok(data))
Expand Down
Loading
Loading