fix(compiler-cli): Fix swallowed Error messages (#20846)
This commit fixes a bug in which non-formatted errors are silently dropped. Internal issue: b/67739418 PR Close #20846
This commit is contained in:
parent
c2dbc55f11
commit
073f485c72
|
@ -869,17 +869,21 @@ function syntaxErrorToDiagnostics(error: Error): Diagnostic[] {
|
|||
source: SOURCE,
|
||||
code: DEFAULT_ERROR_CODE
|
||||
}));
|
||||
} else {
|
||||
if (isFormattedError(error)) {
|
||||
return [{
|
||||
messageText: error.message,
|
||||
chain: error.chain && diagnosticChainFromFormattedDiagnosticChain(error.chain),
|
||||
category: ts.DiagnosticCategory.Error,
|
||||
source: SOURCE,
|
||||
code: DEFAULT_ERROR_CODE,
|
||||
position: error.position
|
||||
}];
|
||||
}
|
||||
} else if (isFormattedError(error)) {
|
||||
return [{
|
||||
messageText: error.message,
|
||||
chain: error.chain && diagnosticChainFromFormattedDiagnosticChain(error.chain),
|
||||
category: ts.DiagnosticCategory.Error,
|
||||
source: SOURCE,
|
||||
code: DEFAULT_ERROR_CODE,
|
||||
position: error.position
|
||||
}];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
// Produce a Diagnostic anyway since we know for sure `error` is a SyntaxError
|
||||
return [{
|
||||
messageText: error.message,
|
||||
category: ts.DiagnosticCategory.Error,
|
||||
code: DEFAULT_ERROR_CODE,
|
||||
source: SOURCE,
|
||||
}];
|
||||
}
|
||||
|
|
|
@ -948,6 +948,35 @@ describe('ng program', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('should include non-formatted errors (e.g. invalid templateUrl)', () => {
|
||||
testSupport.write('src/index.ts', `
|
||||
import {Component, NgModule} from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'my-component',
|
||||
templateUrl: 'template.html', // invalid template url
|
||||
})
|
||||
export class MyComponent {}
|
||||
|
||||
@NgModule({
|
||||
declarations: [MyComponent]
|
||||
})
|
||||
export class MyModule {}
|
||||
`);
|
||||
|
||||
const options = testSupport.createCompilerOptions();
|
||||
const host = ng.createCompilerHost({options});
|
||||
const program = ng.createProgram({
|
||||
rootNames: [path.resolve(testSupport.basePath, 'src/index.ts')],
|
||||
options,
|
||||
host,
|
||||
});
|
||||
|
||||
const structuralErrors = program.getNgStructuralDiagnostics();
|
||||
expect(structuralErrors.length).toBe(1);
|
||||
expect(structuralErrors[0].messageText).toContain('Couldn\'t resolve resource template.html');
|
||||
});
|
||||
|
||||
it('should be able report structural errors with noResolve:true and generateCodeForLibraries:false ' +
|
||||
'even if getSourceFile throws for non existent files',
|
||||
() => {
|
||||
|
|
Loading…
Reference in New Issue