Skip to content

Commit 2c64447

Browse files
committed
Merge pull request microsoft#5514 from Microsoft/relativePathInErrorReporting
Use relative file name when reporting errors
2 parents 7f9a816 + 884dff0 commit 2c64447

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

src/compiler/tsc.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,17 @@ namespace ts {
8989
return <string>diagnostic.messageText;
9090
}
9191

92+
function getRelativeFileName(fileName: string, host: CompilerHost): string {
93+
return host ? convertToRelativePath(fileName, host.getCurrentDirectory(), fileName => host.getCanonicalFileName(fileName)) : fileName;
94+
}
95+
9296
function reportDiagnosticSimply(diagnostic: Diagnostic, host: CompilerHost): void {
9397
let output = "";
9498

9599
if (diagnostic.file) {
96100
const { line, character } = getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
97-
const relativeFileName = host
98-
? convertToRelativePath(diagnostic.file.fileName, host.getCurrentDirectory(), fileName => host.getCanonicalFileName(fileName))
99-
: diagnostic.file.fileName;
100-
101-
output += `${ diagnostic.file.fileName }(${ line + 1 },${ character + 1 }): `;
101+
const relativeFileName = getRelativeFileName(diagnostic.file.fileName, host);
102+
output += `${ relativeFileName }(${ line + 1 },${ character + 1 }): `;
102103
}
103104

104105
let category = DiagnosticCategory[diagnostic.category].toLowerCase();
@@ -107,7 +108,7 @@ namespace ts {
107108
sys.write(output);
108109
}
109110

110-
111+
111112
const redForegroundEscapeSequence = "\u001b[91m";
112113
const yellowForegroundEscapeSequence = "\u001b[93m";
113114
const blueForegroundEscapeSequence = "\u001b[93m";
@@ -133,6 +134,7 @@ namespace ts {
133134
let { line: firstLine, character: firstLineChar } = getLineAndCharacterOfPosition(file, start);
134135
let { line: lastLine, character: lastLineChar } = getLineAndCharacterOfPosition(file, start + length);
135136
const lastLineInFile = getLineAndCharacterOfPosition(file, file.text.length).line;
137+
const relativeFileName = getRelativeFileName(file.fileName, host);
136138

137139
let hasMoreThanFiveLines = (lastLine - firstLine) >= 4;
138140
let gutterWidth = (lastLine + 1 + "").length;
@@ -183,7 +185,7 @@ namespace ts {
183185
}
184186

185187
output += sys.newLine;
186-
output += `${ file.fileName }(${ firstLine + 1 },${ firstLineChar + 1 }): `;
188+
output += `${ relativeFileName }(${ firstLine + 1 },${ firstLineChar + 1 }): `;
187189
}
188190

189191
const categoryColor = categoryFormatMap[diagnostic.category];

0 commit comments

Comments
 (0)