Skip to content

Commit

Permalink
fix: use uri instead of path for clearing diagnostics on valid compil…
Browse files Browse the repository at this point in the history
…ation (#360)

* fix: use uri instead of path for clearing diagnostics on valid compilation

* fix linting

* fix: diagnostics on foundry on windows
  • Loading branch information
antico5 authored Jan 9, 2023
1 parent 9b56d3c commit 69ea6ce
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
13 changes: 6 additions & 7 deletions server/src/services/validation/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
BuildInputFailed,
} from "../../types";
import { getOpenDocumentsInProject } from "../../queries/getOpenDocumentsInProject";
import { runningOnWindows } from "../../utils/operatingSystem";
import { CompilationDetails } from "../../frameworks/base/CompilationDetails";
import { DiagnosticConverter } from "./DiagnosticConverter";
import { CompilationService } from "./CompilationService";
Expand Down Expand Up @@ -323,14 +322,14 @@ function validationPass(
): void {
for (const source of message.sources) {
// TODO: improve this. Currently necessary because on hardhat source names are not full paths
let uri = openDocuments
const docPath = openDocuments
.map((doc) => doc.uri)
.find((u) => toUnixStyle(u).endsWith(source));
if (uri === undefined) {
.find((u) => toUnixStyle(u).endsWith(toUnixStyle(source)));
if (docPath === undefined) {
continue;
}

uri = runningOnWindows() ? `/${uri}` : uri;
const uri = URI.file(docPath).toString();

clearDiagnostics(serverState, uri);
}
Expand All @@ -354,8 +353,8 @@ function validationFail(
diagnosticConverter.convertErrors(change.document, message.errors);

const diagnosticsInOpenEditor = Object.entries(diagnostics)
.filter(([diagnosticUri]) =>
decodeURIComponent(document.uri).includes(diagnosticUri)
.filter(([diagSourceName]) =>
decodeURIComponent(document.uri).includes(toUnixStyle(diagSourceName))
)
.flatMap(([, diagnostic]) => diagnostic);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pragma solidity >=0.7.0 <0.9.0;

contract Foo {}
14 changes: 14 additions & 0 deletions test/protocol/src/TestLanguageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
DefinitionParams,
DefinitionRequest,
Diagnostic,
DidChangeTextDocumentParams,
DidCloseTextDocumentNotification,
DidCloseTextDocumentParams,
DidOpenTextDocumentNotification,
Expand All @@ -25,6 +26,7 @@ import {
LogMessageNotification,
Position,
PublishDiagnosticsNotification,
Range,
ReferenceParams,
ReferencesRequest,
RenameParams,
Expand Down Expand Up @@ -108,6 +110,18 @@ export class TestLanguageClient {
await document.waitAnalyzed
}

public changeDocument(documentPath: string, range: Range, text: string) {
const params: DidChangeTextDocumentParams = {
textDocument: {
uri: toUri(documentPath),
version: 2,
},
contentChanges: [{ range, text }],
}

this.connection!.sendNotification('textDocument/didChange', params)
}

public closeAllDocuments() {
for (const [uri] of Object.entries(this.documents)) {
this.closeDocument(uri)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { expect } from 'chai'
import _ from 'lodash'
import { test } from 'mocha'
import { DiagnosticSeverity } from 'vscode-languageserver-protocol'
import { toUri } from '../../../../src/helpers'
import { TestLanguageClient } from '../../../../src/TestLanguageClient'
import { getInitializedClient } from '../../../client'
import { getProjectPath } from '../../../helpers'
import { getProjectPath, makeRange, sleep } from '../../../helpers'

let client!: TestLanguageClient

Expand Down Expand Up @@ -140,4 +143,22 @@ describe('[hardhat] publishDiagnostics', () => {
},
})
})

test('clear diagnostics on valid compilation', async function () {
const documentPath = getProjectPath('hardhat/contracts/diagnostics/NoLicense.sol')
await client.openDocument(documentPath)

// First assert the diagnostic is present
await client.getDiagnostic(documentPath, {
message: 'SPDX license identifier not provided',
})
expect(client.documents[toUri(documentPath)].diagnostics.length).to.eq(1)

// Edit the file to make it correct
client.changeDocument(documentPath, makeRange(0, 0, 0, 0), '// SPDX-License-Identifier: MIT\n')

// Assert diagnostics are gone
await sleep(300) // TODO: change this to proper event listening for diagnostics
expect(client.documents[toUri(documentPath)].diagnostics.length).to.eq(0)
})
})

0 comments on commit 69ea6ce

Please sign in to comment.