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;
let externs = '/** @externs */\n';
if (diagnostics.length) {
console.error(ng.formatDiagnostics(compilerOpts, diagnostics));
console.error(ng.formatDiagnostics(diagnostics));
} else {
if (bazelOpts.tsickleGenerateExterns) {
externs += tsickle.getGeneratedExterns(tsickleEmitResult.externs);

View File

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

View File

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

View File

@ -23,13 +23,15 @@ export function filterErrorsAndWarnings(diagnostics: Diagnostics): Diagnostics {
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) {
const tsFormatHost: ts.FormatDiagnosticsHost = {
getCurrentDirectory: () => options.basePath || process.cwd(),
getCanonicalFileName: fileName => fileName,
getNewLine: () => ts.sys.newLine
};
return diags
.map(d => {
if (api.isTsDiagnostic(d)) {

View File

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

View File

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