Skip to content

Commit

Permalink
Improve error handling for MOPS and Vessel integrations (#149)
Browse files Browse the repository at this point in the history
* Resolve 'ic-mops' npm package instead of global 'mops' binary

* Improve error handling for missing MOPS or Vessel installation

* Detect global 'ic-mops'

* 0.7.3

* Clarify possible MOPS installation locations in error message

* Use more detailed error message for Vessel sources

* Automatically retry package configuration on file change

* Run 'sources' command asynchronously

* 0.7.4
  • Loading branch information
rvanasa authored Jan 12, 2023
1 parent b5c0b9f commit 6b0a9f7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-motoko",
"displayName": "Motoko",
"description": "Motoko language support",
"version": "0.7.3",
"version": "0.7.4",
"publisher": "dfinity-foundation",
"repository": "https://github.com/dfinity/vscode-motoko",
"engines": {
Expand Down
31 changes: 20 additions & 11 deletions src/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { execSync } from 'child_process';
import { exec } from 'child_process';
import * as glob from 'fast-glob';
import { existsSync, readFileSync } from 'fs';
import { Node } from 'motoko/lib/ast';
Expand Down Expand Up @@ -39,22 +39,21 @@ import {
resetContexts,
} from './context';
import DfxResolver from './dfx';
import { organizeImports } from './imports';
import { getAstInformation } from './information';
import {
findDefinition,
findMostSpecificNodeForPosition,
locationFromDefinition,
rangeFromNode,
} from './navigation';
import { vesselSources } from './rust';
import { Program, asNode, findNodes } from './syntax';
import {
formatMotoko,
getFileText,
resolveFilePath,
resolveVirtualPath,
} from './utils';
import { organizeImports } from './imports';

interface Settings {
motoko: MotokoSettings;
Expand All @@ -74,11 +73,14 @@ const ignoreGlobs = [
async function getPackageSources(
directory: string,
): Promise<[string, string][]> {
function sourcesFromCommand(command: string) {
async function sourcesFromCommand(command: string) {
console.log(`Running \`${command}\` in directory: ${directory}`);
const result = execSync(command, {
cwd: directory,
}).toString('utf8');
const result = await new Promise<string>((resolve, reject) =>
exec(command, { cwd: directory }, (err, stdout) =>
// @ts-ignore
err ? reject(err) : resolve(stdout.toString('utf8')),
),
);
const args = result.split(/\s/); // TODO: account for quoted strings
console.log('Received:', args);
if (!args) {
Expand Down Expand Up @@ -136,24 +138,26 @@ async function getPackageSources(
try {
return sourcesFromCommand(command);
} catch (err: any) {
console.error(
throw new Error(
`Error while running \`${command}\`.\nMake sure Vessel is installed (https://github.com/dfinity/vessel/#getting-started).\n${
err?.message || err
}`,
);
return vesselSources(directory);
// return vesselSources(directory);
}
} else {
return [];
}
}

let packageConfigChangeTimeout: ReturnType<typeof setTimeout>;
let loadingPackages = false;
let packageConfigError = false;
let packageConfigChangeTimeout: ReturnType<typeof setTimeout>;
function notifyPackageConfigChange() {
clearTimeout(packageConfigChangeTimeout);
loadingPackages = true;
setTimeout(async () => {
packageConfigError = false;
try {
resetContexts();

Expand Down Expand Up @@ -214,12 +218,13 @@ function notifyPackageConfigChange() {
},
);
} catch (err) {
// context.error = `unable to load project dependencies: ${err}`;
packageConfigError = true;
context.error = String(err);
console.warn(err);
return;
}
} catch (err) {
packageConfigError = true;
console.error(
`Error while configuring Vessel directory (${dir}): ${err}`,
);
Expand All @@ -239,6 +244,7 @@ function notifyPackageConfigChange() {
notifyWorkspace(); // Update virtual file system
notifyDfxChange(); // Reload dfx.json
} catch (err) {
packageConfigError = true;
loadingPackages = false;
console.error(`Error while loading packages: ${err}`);
}
Expand Down Expand Up @@ -1150,6 +1156,9 @@ connection.onReferences(
let validatingTimeout: ReturnType<typeof setTimeout>;
let validatingUri: string | undefined;
documents.onDidChangeContent((event) => {
if (packageConfigError) {
notifyPackageConfigChange();
}
const document = event.document;
const { uri } = document;
if (uri === validatingUri) {
Expand Down

0 comments on commit 6b0a9f7

Please sign in to comment.