Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled exceptions #496

Merged
merged 2 commits into from
May 23, 2023
Merged
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
2 changes: 2 additions & 0 deletions client/scripts/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ async function main() {
"../node_modules/@nomicfoundation/solidity-language-server/src/hardhat.config.ts",
"./tmp/server/out/worker/WorkerProcess":
"../node_modules/@nomicfoundation/solidity-language-server/src/frameworks/Hardhat/worker/WorkerProcess.ts",
"./tmp/server/out/ConfigLoader":
"../node_modules/@nomicfoundation/solidity-language-server/src/frameworks/Truffle/ConfigLoader.ts",
},
bundle: true,
minifyWhitespace: true,
Expand Down
1 change: 1 addition & 0 deletions server/scripts/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ async function main() {
"./out/hardhat.config": "./src/hardhat.config.ts",
"./out/worker/WorkerProcess":
"./src/frameworks/Hardhat/worker/WorkerProcess.ts",
"./out/ConfigLoader": "./src/frameworks/Truffle/ConfigLoader.ts",
},
bundle: true,
minifyWhitespace: true,
Expand Down
3 changes: 1 addition & 2 deletions server/src/frameworks/Hardhat/HardhatProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export class HardhatProject extends Project {
try {
await this._handleMessage(message);
} catch (error) {
this.serverState.telemetry.captureException(error);
this.logger.error(
`Error while handling worker message: ${error}. Full Message: ${JSON.stringify(
message
Expand All @@ -114,7 +113,7 @@ export class HardhatProject extends Project {
});

this.workerProcess.on("error", (err) => {
this.logger.error(err);
this.logger.info(`Worker exited with error: ${err}`);
});

this.workerProcess.on("exit", async (code) => {
Expand Down
2 changes: 1 addition & 1 deletion server/src/frameworks/Hardhat/worker/WorkerProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class WorkerProcess {
try {
this._loadHRE();
} catch (err: any) {
this.logger.error(`Error loading HRE: ${err}`);
this.logger.info(`Error loading HRE: ${err}`);
let errorMessage;
if (err.message.includes("Cannot find module 'hardhat'")) {
errorMessage =
Expand Down
10 changes: 10 additions & 0 deletions server/src/frameworks/Truffle/ConfigLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/no-var-requires */

// This module is meant to be spawned by TruffleProject to load
// truffle-config.js files without any side effects
import { argv } from "process";

const config = require(argv[2]);

process.send!(config);
31 changes: 28 additions & 3 deletions server/src/frameworks/Truffle/TruffleProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-empty */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { execSync } from "child_process";
import { execSync, fork } from "child_process";
import { existsSync } from "fs";
import { readdir } from "fs/promises";
import _ from "lodash";
Expand Down Expand Up @@ -74,8 +74,7 @@ export class TruffleProject extends Project {

try {
// Load config file
delete require.cache[require.resolve(this.configPath)];
const config = require(this.configPath);
const config: any = await this._loadConfig();

// Find solc version statement
const configSolcVersion = config?.compilers?.solc?.version;
Expand Down Expand Up @@ -119,6 +118,32 @@ export class TruffleProject extends Project {
}
}

private async _loadConfig(): Promise<any> {
return new Promise((resolve, reject) => {
// Spawn a child process with the only purpose of requiring the truffle config js file
const childProcess = fork(path.resolve(__dirname, "ConfigLoader.js"), [
this.configPath,
]);

// Child process will send back the exported config. It's killed afterwards to prevent
// any unhandled errors that the config file or its dependencies might have.
childProcess.on("message", async (message: any) => {
childProcess.kill("SIGKILL");
resolve(message);
});

childProcess.on("error", (err) => {
reject(err);
});

childProcess.on("exit", async (code) => {
if (code === 1) {
reject("Error loading config file");
}
});
});
}

public async fileBelongs(file: string): Promise<FileBelongsResult> {
let belongs: boolean;
if (this.status === Status.INITIALIZED_SUCCESS) {
Expand Down