fix(compiler-cli): diagnostics file paths relative to cwd, not tsconfig (#19748)

PR Close #19748
This commit is contained in:
Alex Eagle 2017-10-16 12:31:25 -04:00 committed by Tobias Bosch
parent c0cc6eeca1
commit 56774dfb79
6 changed files with 19 additions and 17 deletions

View File

@ -205,7 +205,7 @@ export function compile({allowNonHermeticReads, allDepsCompiledWithBazel = true,
const tsickleEmitResult = emitResult as tsickle.EmitResult; const tsickleEmitResult = emitResult as tsickle.EmitResult;
let externs = '/** @externs */\n'; let externs = '/** @externs */\n';
if (diagnostics.length) { if (diagnostics.length) {
console.error(ng.formatDiagnostics(compilerOpts, diagnostics)); console.error(ng.formatDiagnostics(diagnostics));
} else { } else {
if (bazelOpts.tsickleGenerateExterns) { if (bazelOpts.tsickleGenerateExterns) {
externs += tsickle.getGeneratedExterns(tsickleEmitResult.externs); externs += tsickle.getGeneratedExterns(tsickleEmitResult.externs);

View File

@ -28,15 +28,15 @@ export function main(
let {project, rootNames, options, errors: configErrors, watch, emitFlags} = let {project, rootNames, options, errors: configErrors, watch, emitFlags} =
config || readNgcCommandLineAndConfiguration(args); config || readNgcCommandLineAndConfiguration(args);
if (configErrors.length) { if (configErrors.length) {
return reportErrorsAndExit(options, configErrors, consoleError); return reportErrorsAndExit(configErrors, consoleError);
} }
if (watch) { if (watch) {
const result = watchMode(project, options, consoleError); const result = watchMode(project, options, consoleError);
return reportErrorsAndExit({}, result.firstCompileResult, consoleError); return reportErrorsAndExit(result.firstCompileResult, consoleError);
} }
const {diagnostics: compileDiags} = performCompilation( const {diagnostics: compileDiags} = performCompilation(
{rootNames, options, emitFlags, emitCallback: createEmitCallback(options)}); {rootNames, options, emitFlags, emitCallback: createEmitCallback(options)});
return reportErrorsAndExit(options, compileDiags, consoleError); return reportErrorsAndExit(compileDiags, consoleError);
} }
function createEmitCallback(options: api.CompilerOptions): api.TsEmitCallback|undefined { function createEmitCallback(options: api.CompilerOptions): api.TsEmitCallback|undefined {
@ -128,11 +128,10 @@ export function readCommandLineAndConfiguration(
} }
function reportErrorsAndExit( function reportErrorsAndExit(
options: api.CompilerOptions, allDiagnostics: Diagnostics, allDiagnostics: Diagnostics, consoleError: (s: string) => void = console.error): number {
consoleError: (s: string) => void = console.error): number {
const errorsAndWarnings = filterErrorsAndWarnings(allDiagnostics); const errorsAndWarnings = filterErrorsAndWarnings(allDiagnostics);
if (errorsAndWarnings.length) { if (errorsAndWarnings.length) {
consoleError(formatDiagnostics(options, errorsAndWarnings)); consoleError(formatDiagnostics(errorsAndWarnings));
} }
return exitCodeFromResult(allDiagnostics); return exitCodeFromResult(allDiagnostics);
} }
@ -140,7 +139,7 @@ function reportErrorsAndExit(
export function watchMode( export function watchMode(
project: string, options: api.CompilerOptions, consoleError: (s: string) => void) { project: string, options: api.CompilerOptions, consoleError: (s: string) => void) {
return performWatchCompilation(createPerformWatchHost(project, diagnostics => { return performWatchCompilation(createPerformWatchHost(project, diagnostics => {
consoleError(formatDiagnostics(options, diagnostics)); consoleError(formatDiagnostics(diagnostics));
}, options, options => createEmitCallback(options))); }, options, options => createEmitCallback(options)));
} }

View File

@ -131,6 +131,6 @@ export function createCompilerHost(
// Wrapper for formatDiagnostics. // Wrapper for formatDiagnostics.
export type Diagnostics = Array<ts.Diagnostic|Diagnostic>; export type Diagnostics = Array<ts.Diagnostic|Diagnostic>;
export function formatDiagnostics(options: CompilerOptions, diags: Diagnostics): string { export function formatDiagnostics(diags: Diagnostics): string {
return formatDiagnosticsOrig(options, diags); return formatDiagnosticsOrig(diags);
} }

View File

@ -23,13 +23,15 @@ export function filterErrorsAndWarnings(diagnostics: Diagnostics): Diagnostics {
return diagnostics.filter(d => d.category !== ts.DiagnosticCategory.Message); return diagnostics.filter(d => d.category !== ts.DiagnosticCategory.Message);
} }
export function formatDiagnostics(options: api.CompilerOptions, diags: Diagnostics): string { const defaultFormatHost: ts.FormatDiagnosticsHost = {
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
getCanonicalFileName: fileName => fileName,
getNewLine: () => ts.sys.newLine
};
export function formatDiagnostics(
diags: Diagnostics, tsFormatHost: ts.FormatDiagnosticsHost = defaultFormatHost): string {
if (diags && diags.length) { if (diags && diags.length) {
const tsFormatHost: ts.FormatDiagnosticsHost = {
getCurrentDirectory: () => options.basePath || process.cwd(),
getCanonicalFileName: fileName => fileName,
getNewLine: () => ts.sys.newLine
};
return diags return diags
.map(d => { .map(d => {
if (api.isTsDiagnostic(d)) { if (api.isTsDiagnostic(d)) {

View File

@ -44,6 +44,7 @@ describe('ngc transformer command-line', () => {
beforeEach(() => { beforeEach(() => {
errorSpy = jasmine.createSpy('consoleError').and.callFake(console.error); errorSpy = jasmine.createSpy('consoleError').and.callFake(console.error);
basePath = makeTempDir(); basePath = makeTempDir();
process.chdir(basePath);
write = (fileName: string, content: string) => { write = (fileName: string, content: string) => {
const dir = path.dirname(fileName); const dir = path.dirname(fileName);
if (dir != '.') { if (dir != '.') {

View File

@ -112,7 +112,7 @@ export function setup(): TestSupport {
export function expectNoDiagnostics(options: ng.CompilerOptions, diags: ng.Diagnostics) { export function expectNoDiagnostics(options: ng.CompilerOptions, diags: ng.Diagnostics) {
const errorDiags = diags.filter(d => d.category !== ts.DiagnosticCategory.Message); const errorDiags = diags.filter(d => d.category !== ts.DiagnosticCategory.Message);
if (errorDiags.length) { if (errorDiags.length) {
throw new Error(`Expected no diagnostics: ${ng.formatDiagnostics(options, errorDiags)}`); throw new Error(`Expected no diagnostics: ${ng.formatDiagnostics(errorDiags)}`);
} }
} }