Skip to content

Commit

Permalink
fix(assembler): handle unknown types without crashing (#501)
Browse files Browse the repository at this point in the history
After failing compilation we proceed with jsii analysis, to give as much
information as possible in one go.

However, some compilation failures lead to a lack of information that
make the jsii analyzer crash, which ultimately leads jsii to fail with
no more information than:

    Error: TypeError: Cannot read property 'getJsDocTags' of undefined

Fix this in two ways:

- First, detect the situation where this occurs and produce a more
  useful error message.
- Guard against this happening in general by catching exceptions
  during type analysis and still printing the compilation errors.
  • Loading branch information
rix0rrr committed May 7, 2019
1 parent 04c061e commit 7ba1aab
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
24 changes: 22 additions & 2 deletions packages/jsii/lib/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ export class Assembler implements Emitter {

const processBaseTypes = (types: ts.Type[]) => {
for (const iface of types) {

// base is private/internal, so we continue recursively with it's own bases
if (this._isPrivateOrInternal(iface.symbol)) {
erasedBases.push(iface);
Expand Down Expand Up @@ -507,7 +506,7 @@ export class Assembler implements Emitter {
// process all "implements" clauses
const allInterfaces = new Set<string>();
for (const clause of implementsClauses) {
const { interfaces } = await this._processBaseInterfaces(fqn, clause.types.map(t => this._typeChecker.getTypeFromTypeNode(t)));
const { interfaces } = await this._processBaseInterfaces(fqn, clause.types.map(t => this._getTypeFromTypeNode(t)));
for (const ifc of (interfaces || [])) {
allInterfaces.add(ifc.fqn);
}
Expand Down Expand Up @@ -623,6 +622,17 @@ export class Assembler implements Emitter {
return _sortMembers(jsiiType);
}

/**
* Use the TypeChecker's getTypeFromTypeNode, but throw a descriptive error if it fails
*/
private _getTypeFromTypeNode(t: ts.TypeNode) {
const type = this._typeChecker.getTypeFromTypeNode(t);
if (isErrorType(type)) {
throw new Error(`Unable to resolve type: ${t.getFullText()}. This typically happens if something is wrong with your dependency closure.`);
}
return type;
}

/**
* Check that this class doesn't declare any members that are of different staticness in itself or any of its bases
*/
Expand Down Expand Up @@ -1585,3 +1595,13 @@ function noEmptyDict<T>(xs: {[key: string]: T}): {[key: string]: T} | undefined
if (Object.keys(xs).length === 0) { return undefined; }
return xs;
}

/**
* Check whether this type is the intrinsic TypeScript "error type"
*
* This type is returned if type lookup fails. Unfortunately no public
* accessors for it are exposed.
*/
function isErrorType(t: ts.Type) {
return (t as any).intrinsicName === 'error';
}
32 changes: 22 additions & 10 deletions packages/jsii/lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,30 @@ export class Compiler implements Emitter {

private async _consumeProgram(program: ts.Program, stdlib: string): Promise<EmitResult> {
const emit = program.emit();
if (emit.emitSkipped) {
let hasErrors = emitHasErrors(emit);
const diagnostics = [...emit.diagnostics];

if (hasErrors) {
LOG.error('Compilation errors prevented the JSII assembly from being created');
}

// we continue to do jsii checker even if there are compilation errors so that
// jsii warnings will appear.
const assembler = new Assembler(this.options.projectInfo, program, stdlib);
const assmEmit = await assembler.emit();
if (assmEmit.hasErrors) {
LOG.error('Type model errors prevented the JSII assembly from being created');
// jsii warnings will appear. However, the Assembler might throw an exception
// because broken/missing type information might lead it to fail completely.
try {
const assembler = new Assembler(this.options.projectInfo, program, stdlib);
const assmEmit = await assembler.emit();
if (assmEmit.hasErrors) {
LOG.error('Type model errors prevented the JSII assembly from being created');
}

hasErrors = hasErrors || assmEmit.hasErrors;
diagnostics.push(...assmEmit.diagnostics);
} catch (e) {
LOG.error(`Error during type model analysis: ${e}`);
}

return {
hasErrors: emit.emitSkipped || assmEmit.hasErrors,
diagnostics: [...emit.diagnostics, ...assmEmit.diagnostics]
};
return { hasErrors, diagnostics };
}

/**
Expand Down Expand Up @@ -353,3 +361,7 @@ function parseConfigHostFromCompilerHost(host: ts.CompilerHost): ts.ParseConfigH
trace: host.trace ? (s) => host.trace!(s) : undefined
};
}

function emitHasErrors(result: ts.EmitResult) {
return result.diagnostics.some(d => d.category === ts.DiagnosticCategory.Error) || result.emitSkipped;
}

0 comments on commit 7ba1aab

Please sign in to comment.