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

Throw proper error when there are circular dependencies between harmony extensions #2282

Merged
merged 2 commits into from
Feb 3, 2020
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
34 changes: 20 additions & 14 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BitCliExt } from './extensions/cli';
import defaultHandleError from './cli/default-error-handler';
import { logErrAndExit } from './cli/command-registry';
import { BitExt } from './extensions/bit';
import HarmonyError from './harmony/exceptions/harmony-error';

process.env.MEMFS_DONT_WARN = 'true'; // suppress fs experimental warnings from memfs

Expand All @@ -23,17 +24,22 @@ const config = {
components: '*'
}
};

const harmony = Harmony.load([BitCliExt, BitExt], config);
harmony
.run()
.then(() => {
const cli = harmony.get('BitCli');
// @ts-ignore
if (cli && cli.instance) return cli.instance.run([], harmony);
throw new Error('failed to load CLI');
})
.catch(err => {
const handledError = defaultHandleError(err.originalError);
logErrAndExit(handledError || err, process.argv[1] || '');
});
try {
const harmony = Harmony.load([BitCliExt, BitExt], config);
harmony
.run()
.then(() => {
const cli = harmony.get('BitCli');
// @ts-ignore
if (cli && cli.instance) return cli.instance.run([], harmony);
throw new Error('failed to load CLI');
})
.catch(err => {
const handledError = defaultHandleError(err.originalError);
logErrAndExit(handledError || err, process.argv[1] || '');
});
// Catching errors from the load phase
} catch (err) {
const handledError = err instanceof HarmonyError ? err.toString() : err;
logErrAndExit(handledError, process.argv[1] || '');
}
25 changes: 25 additions & 0 deletions src/harmony/exceptions/extension-potential-circular.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import chalk from 'chalk';
import { Extension } from '../extension';
import HarmonyError from './harmony-error';

export default class ExtensionPotentialCircular extends HarmonyError {
constructor(
/**
* failed extension
*/
private extension: Extension,

/**
* valid extensions dependencies names
*/
private validDeps: string[]
) {
super();
}

toString() {
return `Failed to load the dependencies for extension ${chalk.bold(this.extension.name)}.
This may result from a wrong import or from circular dependencies in imports.
The following dependencies succeeded loading: ${chalk.bold(this.validDeps.join(', '))}`;
}
}
1 change: 1 addition & 0 deletions src/harmony/exceptions/harmony-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default class HarmonyError extends Error {}
1 change: 1 addition & 0 deletions src/harmony/exceptions/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as ExtensionLoadError } from './extension-load-error';
export { default as ExtensionPotentialCircular } from './extension-potential-circular';
export { HarmonyAlreadyRunning } from './harmony-already-running';
6 changes: 5 additions & 1 deletion src/harmony/extension-graph/from-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Vertex, Edge } from 'cleargraph';
import { AnyExtension } from '../types';
import { ExtensionManifest } from '../extension-manifest';
import { extensionFactory } from '../factory';
import ExtensionPotentialCircular from '../exceptions/extension-potential-circular';

/**
* build vertices and edges from the given extension
Expand All @@ -15,8 +16,11 @@ export function fromExtension(extension: ExtensionManifest) {
if (vertices[id]) return;

const instance = extensionFactory(root);
const validDeps = instance.dependencies.filter(dep => dep).map(dep => dep.name);
if (instance.dependencies.length > validDeps.length) {
throw new ExtensionPotentialCircular(instance, validDeps);
}
vertices[id] = new Vertex<AnyExtension>(id, instance);

const newEdges = instance.dependencies.map(dep => {
return Edge.fromObject({
srcId: id,
Expand Down