Skip to content

Commit 6265bf6

Browse files
author
Elad Ben-Israel
authored
fix: exit with non-zero exit code if there are compilation errors (#442)
In #383 we modified the compiler to continue jsii validation despite compilation errors but did not propagate the fact that there were compilation errors. This resulted in "jsii" exiting with a 0 exit code even in case of errors. Fixes #441
1 parent 23babed commit 6265bf6

File tree

7 files changed

+14
-26
lines changed

7 files changed

+14
-26
lines changed

packages/jsii/bin/jsii.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import { VERSION } from '../lib/version';
4343
for (const diagnostic of emitResult.diagnostics) {
4444
utils.logDiagnostic(diagnostic, projectRoot);
4545
}
46-
if (emitResult.emitSkipped) {
46+
if (emitResult.hasErrors) {
4747
process.exit(1);
4848
}
4949
}).catch(e => {

packages/jsii/lib/assembler.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import path = require('path');
88
import ts = require('typescript');
99
import { JSII_DIAGNOSTICS_CODE } from './compiler';
1010
import { getReferencedDocParams, parseSymbolDocumentation } from './docs';
11-
import { Diagnostic, Emitter } from './emitter';
11+
import { Diagnostic, Emitter, EmitResult } from './emitter';
1212
import literate = require('./literate');
1313
import { ProjectInfo } from './project-info';
1414
import utils = require('./utils');
@@ -90,7 +90,7 @@ export class Assembler implements Emitter {
9090
// Clearing ``this._types`` to allow contents to be garbage-collected.
9191
delete this._types;
9292
try {
93-
return { diagnostics: this._diagnostics, emitSkipped: true };
93+
return { diagnostics: this._diagnostics, hasErrors: true };
9494
} finally {
9595
// Clearing ``this._diagnostics`` to allow contents to be garbage-collected.
9696
delete this._diagnostics;
@@ -121,7 +121,7 @@ export class Assembler implements Emitter {
121121

122122
const validator = new Validator(this.projectInfo, assembly);
123123
const validationResult = await validator.emit();
124-
if (!validationResult.emitSkipped) {
124+
if (!validationResult.hasErrors) {
125125
const assemblyPath = path.join(this.projectInfo.projectRoot, '.jsii');
126126
LOG.trace(`Emitting assembly: ${colors.blue(assemblyPath)}`);
127127
await fs.writeJson(assemblyPath, _fingerprint(assembly), { replacer: utils.filterEmpty, spaces: 2 });
@@ -130,7 +130,7 @@ export class Assembler implements Emitter {
130130
try {
131131
return {
132132
diagnostics: [...this._diagnostics, ...validationResult.diagnostics],
133-
emitSkipped: validationResult.emitSkipped
133+
hasErrors: validationResult.hasErrors
134134
};
135135
} finally {
136136
// Clearing ``this._types`` to allow contents to be garbage-collected.
@@ -1232,22 +1232,6 @@ export class Assembler implements Emitter {
12321232
}
12331233
}
12341234

1235-
/**
1236-
* The result of ``Assembler#emit()``.
1237-
*/
1238-
export interface EmitResult {
1239-
/**
1240-
* @return all diagnostic information produced by the assembler's emit process
1241-
*/
1242-
readonly diagnostics: ts.Diagnostic[];
1243-
1244-
/**
1245-
* @return true if the assembly was not written to disk (as the consequence
1246-
* of errors, which are visible in ``#diagnostics``)
1247-
*/
1248-
readonly emitSkipped: boolean;
1249-
}
1250-
12511235
function _fingerprint(assembly: spec.Assembly): spec.Assembly {
12521236
delete assembly.fingerprint;
12531237
assembly = sortJson(assembly);

packages/jsii/lib/compiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ export class Compiler implements Emitter {
149149
// jsii warnings will appear.
150150
const assembler = new Assembler(this.options.projectInfo, program, stdlib);
151151
const assmEmit = await assembler.emit();
152-
if (assmEmit.emitSkipped) {
152+
if (assmEmit.hasErrors) {
153153
LOG.error('Type model errors prevented the JSII assembly from being created');
154154
}
155155

156156
return {
157-
emitSkipped: assmEmit.emitSkipped,
157+
hasErrors: emit.emitSkipped || assmEmit.hasErrors,
158158
diagnostics: [...emit.diagnostics, ...assmEmit.diagnostics]
159159
};
160160
}

packages/jsii/lib/emitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface Emitter {
1717
*/
1818
export interface EmitResult {
1919
/** Whether the emit was skipped as a result of errors (found in ``diagnostics``) */
20-
emitSkipped: boolean;
20+
hasErrors: boolean;
2121

2222
/** Diagnostic information created when trying to emit stuff */
2323
diagnostics: ReadonlyArray<ts.Diagnostic | Diagnostic>;

packages/jsii/lib/validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class Validator implements Emitter {
2626
try {
2727
return {
2828
diagnostics: this._diagnostics,
29-
emitSkipped: this._diagnostics.find(diag => diag.category === ts.DiagnosticCategory.Error) != null
29+
hasErrors: this._diagnostics.find(diag => diag.category === ts.DiagnosticCategory.Error) != null
3030
};
3131
} finally {
3232
// Clearing ``this._diagnostics`` to allow contents to be garbage-collected.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
///!MATCH_ERROR: Cannot find name 'boom'.
2+
///!MATCH_ERROR: Cannot find name 'CompilerErrorIsHere'.
3+
4+
boom! >CompilerErrorIsHere

packages/jsii/test/test.negatives.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ for (const source of fs.readdirSync(SOURCE_DIR)) {
1717
test.ok(expectations.length > 0, `Expected error messages should be specified using ${MATCH_ERROR_MARKER}`);
1818
const compiler = new Compiler({ projectInfo: _makeProjectInfo(source), watch: false });
1919
const emitResult = await compiler.emit(path.join(SOURCE_DIR, source));
20-
test.equal(emitResult.emitSkipped, true, `emitSkipped should be true`);
20+
test.equal(emitResult.hasErrors, true, `hasErrors should be true`);
2121
const errors = emitResult.diagnostics.filter(diag => diag.category === ts.DiagnosticCategory.Error);
2222
for (const expectation of expectations) {
2323
test.notEqual(errors.find(e => _messageText(e).indexOf(expectation) !== -1),

0 commit comments

Comments
 (0)