Skip to content

Commit 3b607df

Browse files
authored
Merge pull request #30437 from Microsoft/validSourceFile
Add more information when getValidSourceFile cant find the file in question.
2 parents 0f598db + e78f52d commit 3b607df

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/server/session.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -659,16 +659,32 @@ namespace ts.server {
659659
}
660660
}
661661

662-
if (fileRequest && this.logger.hasLevel(LogLevel.verbose)) {
663-
try {
664-
const { file, project } = this.getFileAndProject(fileRequest);
665-
const scriptInfo = project.getScriptInfoForNormalizedPath(file);
666-
if (scriptInfo) {
667-
const text = getSnapshotText(scriptInfo.getSnapshot());
668-
msg += `\n\nFile text of ${fileRequest.file}:${indent(text)}\n`;
662+
if (this.logger.hasLevel(LogLevel.verbose)) {
663+
if (fileRequest) {
664+
try {
665+
const { file, project } = this.getFileAndProject(fileRequest);
666+
const scriptInfo = project.getScriptInfoForNormalizedPath(file);
667+
if (scriptInfo) {
668+
const text = getSnapshotText(scriptInfo.getSnapshot());
669+
msg += `\n\nFile text of ${fileRequest.file}:${indent(text)}\n`;
670+
}
669671
}
672+
catch { } // tslint:disable-line no-empty
673+
}
674+
675+
if (err.message && err.message.indexOf(`Could not find sourceFile:`) !== -1) {
676+
msg += `\n\nProjects::\n`;
677+
let counter = 0;
678+
const addProjectInfo = (project: Project) => {
679+
msg += `\nProject '${project.projectName}' (${ProjectKind[project.projectKind]}) ${counter}\n`;
680+
msg += project.filesToString(/*writeProjectFileNames*/ true);
681+
msg += "\n-----------------------------------------------\n";
682+
counter++;
683+
};
684+
this.projectService.externalProjects.forEach(addProjectInfo);
685+
this.projectService.configuredProjects.forEach(addProjectInfo);
686+
this.projectService.inferredProjects.forEach(addProjectInfo);
670687
}
671-
catch {} // tslint:disable-line no-empty
672688
}
673689

674690
this.logger.msg(msg, Msg.Err);

src/services/services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ namespace ts {
11581158
function getValidSourceFile(fileName: string): SourceFile {
11591159
const sourceFile = program.getSourceFile(fileName);
11601160
if (!sourceFile) {
1161-
throw new Error("Could not find file: '" + fileName + "'.");
1161+
throw new Error(`Could not find sourceFile: '${fileName}' in ${program && JSON.stringify(program.getSourceFiles().map(f => f.fileName))}.`);
11621162
}
11631163
return sourceFile;
11641164
}

src/testRunner/unittests/tsserver/cachingFileSystemInformation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ namespace ts.projectSystem {
139139
assert.isTrue(false, `should not find file '${imported.path}'`);
140140
}
141141
catch (e) {
142-
assert.isTrue(e.message.indexOf(`Could not find file: '${imported.path}'.`) === 0);
142+
assert.isTrue(e.message.indexOf(`Could not find sourceFile: '${imported.path}' in ["${root.path}"].`) === 0, `Actual: ${e.message}`);
143143
}
144144
const f2Lookups = getLocationsForModuleLookup("f2");
145145
callsTrackingHost.verifyCalledOnEachEntryNTimes(CalledMapsWithSingleArg.fileExists, f2Lookups, 1);

0 commit comments

Comments
 (0)