Skip to content

Commit

Permalink
chore: link to error file on status item (#238)
Browse files Browse the repository at this point in the history
Added a link on the status item linking to the file that is importing a non existent file.
  • Loading branch information
antico5 committed Aug 24, 2022
1 parent ffdf791 commit 79d9eb1
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 16 deletions.
13 changes: 1 addition & 12 deletions client/src/languageitems/hardhatProject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { languages, LanguageStatusSeverity } from "vscode";
import { RequestType } from "vscode-languageclient/node";
import { ExtensionState } from "../types";
import { ensureFilePrefix } from "../utils/files";

interface GetSolFileDetailsParams {
uri: string;
Expand Down Expand Up @@ -78,15 +79,3 @@ export function clearHardhatConfigState(extensionState: ExtensionState): void {
extensionState.hardhatConfigStatusItem.dispose();
extensionState.hardhatConfigStatusItem = null;
}

function ensureFilePrefix(path: string) {
if (path.startsWith("file://")) {
return path;
}

if (path.startsWith("/")) {
return `file://${path}`;
} else {
return `file:///${path}`;
}
}
14 changes: 14 additions & 0 deletions client/src/popups/setupValidationJobHooks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "path";
import { LanguageStatusItem, languages, LanguageStatusSeverity } from "vscode";
import { LanguageClient } from "vscode-languageclient/node";
import { ExtensionState } from "../types";
Expand All @@ -13,6 +14,7 @@ export interface ValidationJobFailureNotification {
reason: string;
displayText: string;
projectBasePath: string;
errorFile?: string;
}

export type ValidationJobStatusNotification =
Expand Down Expand Up @@ -66,5 +68,17 @@ function updateValidationStatusItem(
: notification.displayText;
statusItem.busy = false;

if (!notification.validationRun && notification.errorFile !== undefined) {
statusItem.command = {
title: "Open file",
command: "vscode.open",
arguments: [
path.join(notification.projectBasePath, notification.errorFile),
],
};
} else {
statusItem.command = undefined;
}

return statusItem;
}
11 changes: 11 additions & 0 deletions client/src/utils/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function ensureFilePrefix(path: string) {
if (path.startsWith("file://")) {
return path;
}

if (path.startsWith("/")) {
return `file://${path}`;
} else {
return `file:///${path}`;
}
}
14 changes: 13 additions & 1 deletion server/src/services/validation/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { deserializeError } from "serialize-error";
import { decodeUriAndRemoveFilePrefix } from "../../utils/index";
import {
CancelledValidation,
HardhatError,
HardhatSourceImportError,
HardhatThrownError,
JobCompletionError,
ServerState,
Expand Down Expand Up @@ -137,6 +139,9 @@ function hardhatThrownFail(
projectBasePath,
reason: "non-import line hardhat error",
displayText,
errorFile: isHardhatSourceImportError(hardhatError)
? hardhatError.messageArguments.from
: undefined,
};

serverState.connection.sendNotification(
Expand Down Expand Up @@ -188,7 +193,6 @@ function validatorErrorFail(
uri: document.uri,
diagnostics: [],
});

const data: ValidationJobStatusNotification = jobStatusFrom(validatorError);

serverState.connection.sendNotification("custom/validation-job-status", data);
Expand Down Expand Up @@ -363,3 +367,11 @@ function assertUnknownMessageStatus(completeMessage: never) {
`Unrecognized message status: ${(completeMessage as any)?.status}`
);
}

function isHardhatSourceImportError(
error: HardhatError
): error is HardhatSourceImportError {
return (
error.errorDescriptor.number >= 400 && error.errorDescriptor.number <= 499
);
}
13 changes: 12 additions & 1 deletion server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,17 @@ export interface UnknownHardhatError {
messageArguments?: unknown;
}

export type HardhatError = UnknownHardhatError | HardhatImportLineError;
export interface HardhatSourceImportError extends UnknownHardhatError {
messageArguments: {
imported: string;
from: string;
};
}

export type HardhatError =
| UnknownHardhatError
| HardhatImportLineError
| HardhatSourceImportError;

export interface ValidateCommand {
type: "VALIDATE";
Expand Down Expand Up @@ -324,6 +334,7 @@ export interface ValidationJobFailureNotification {
projectBasePath: string;
reason: string;
displayText: string;
errorFile?: string;
}

export type ValidationJobStatusNotification =
Expand Down
7 changes: 6 additions & 1 deletion server/test/services/validation/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,12 +662,16 @@ describe("Parser", () => {
hardhatError: {
name: "HardhatError",
errorDescriptor: {
number: 123,
number: 404,
message: "This is an example errror",
title: "Example error",
description: "This is an example error",
shouldBeReported: false,
},
messageArguments: {
from: "importing.sol",
imported: "nonexistent.sol",
},
},
};

Expand Down Expand Up @@ -703,6 +707,7 @@ describe("Parser", () => {
projectBasePath: "/projects/example",
reason: "non-import line hardhat error",
displayText: "Example error",
errorFile: "importing.sol",
};

assert.deepStrictEqual(
Expand Down
2 changes: 1 addition & 1 deletion test/integration/helpers/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@ const deleteFile = (file: vscode.Uri): void => {
// Some editor commands return immediately but the effect happens asynchronously
// This ensures the effect takes place before continuing execution
const waitForUI = async () => {
await sleep(400);
await sleep(500);
};

0 comments on commit 79d9eb1

Please sign in to comment.