Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message when scriptInfo is missing in mapTextChangeToCodeEdit #28258

Merged
4 commits merged into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,13 @@ namespace ts.server {
private doEnsureDefaultProjectForFile(fileName: NormalizedPath): Project {
this.ensureProjectStructuresUptoDate();
const scriptInfo = this.getScriptInfoForNormalizedPath(fileName);
return scriptInfo ? scriptInfo.getDefaultProject() : Errors.ThrowNoProject();
if (scriptInfo) {
return scriptInfo.getDefaultProject();
}
else {
this.logger.msg(`Cannot find ${fileName} in ${JSON.stringify(this.allScriptInfoFileNamesForDebug)}`, Msg.Err);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be this.allScriptInfoFileNamesForDebug()

return Errors.ThrowNoProject();
}
}

getScriptInfoEnsuringProjectsUptoDate(uncheckedFileName: string) {
Expand Down Expand Up @@ -1966,6 +1972,15 @@ namespace ts.server {
return configProject && configProject.getCompilerOptions().configFile;
}

/* @internal */
allScriptInfoFileNamesForDebug(): ReadonlyArray<{ readonly path: string, readonly fileName: string }> {
const out: { readonly path: string, readonly fileName: string }[] = [];
this.filenameToScriptInfo.forEach((scriptInfo, path) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be use arrayFrom ?

out.push({ path, fileName: scriptInfo.fileName });
});
return out;
}

/**
* Returns the projects that contain script info through SymLink
* Note that this does not return projects in info.containingProjects
Expand Down
30 changes: 20 additions & 10 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1157,19 +1157,27 @@ namespace ts.server {
this.projectService.getScriptInfoEnsuringProjectsUptoDate(args.file) :
this.projectService.getScriptInfo(args.file);
if (!scriptInfo) {
return ignoreNoProjectError ? emptyArray : Errors.ThrowNoProject();
if (ignoreNoProjectError) return emptyArray;
this.logErrorForScriptInfoNotFound(args.file);
return Errors.ThrowNoProject();
}
projects = scriptInfo.containingProjects;
symLinkedProjects = this.projectService.getSymlinkedProjects(scriptInfo);
}
// filter handles case when 'projects' is undefined
projects = filter(projects, p => p.languageServiceEnabled && !p.isOrphan());
if (!ignoreNoProjectError && (!projects || !projects.length) && !symLinkedProjects) {
this.logErrorForScriptInfoNotFound(args.file);
return Errors.ThrowNoProject();
}
return symLinkedProjects ? { projects: projects!, symLinkedProjects } : projects!; // TODO: GH#18217
}

private logErrorForScriptInfoNotFound(fileName: string): void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to editorServices and use it in all locations?

this.logger.msg(`Could not find file ${JSON.stringify(fileName)}.\n` +
`All files are: ${JSON.stringify(this.projectService.allScriptInfoFileNamesForDebug())}`, Msg.Err);
}

private getDefaultProject(args: protocol.FileRequestArgs) {
if (args.projectFileName) {
const project = this.getProject(args.projectFileName);
Expand Down Expand Up @@ -1908,8 +1916,17 @@ namespace ts.server {
return textChanges.map(change => this.mapTextChangeToCodeEdit(change));
}

private mapTextChangeToCodeEdit(change: FileTextChanges): protocol.FileCodeEdits {
return mapTextChangesToCodeEdits(change, this.projectService.getScriptInfoOrConfig(change.fileName));
private mapTextChangeToCodeEdit(textChanges: FileTextChanges): protocol.FileCodeEdits {
const scriptInfo = this.projectService.getScriptInfoOrConfig(textChanges.fileName);
if (!!textChanges.isNewFile === !!scriptInfo) {
if (!scriptInfo) { // and !isNewFile
this.logErrorForScriptInfoNotFound(textChanges.fileName);
}
Debug.fail("Expected isNewFile for (only) new files. " + JSON.stringify({ isNewFile: !!textChanges.isNewFile, hasScriptInfo: !!scriptInfo }));
}
return scriptInfo
? { fileName: textChanges.fileName, textChanges: textChanges.textChanges.map(textChange => convertTextChangeToCodeEdit(textChange, scriptInfo)) }
: convertNewFileTextChangeToCodeEdit(textChanges);
}
sheetalkamat marked this conversation as resolved.
Show resolved Hide resolved

private convertTextChangeToCodeEdit(change: TextChange, scriptInfo: ScriptInfo): protocol.CodeEdit {
Expand Down Expand Up @@ -2431,13 +2448,6 @@ namespace ts.server {
return { file: fileName, start: scriptInfo.positionToLineOffset(textSpan.start), end: scriptInfo.positionToLineOffset(textSpanEnd(textSpan)) };
}

function mapTextChangesToCodeEdits(textChanges: FileTextChanges, scriptInfo: ScriptInfoOrConfig | undefined): protocol.FileCodeEdits {
Debug.assert(!!textChanges.isNewFile === !scriptInfo, "Expected isNewFile for (only) new files", () => JSON.stringify({ isNewFile: !!textChanges.isNewFile, hasScriptInfo: !!scriptInfo }));
return scriptInfo
? { fileName: textChanges.fileName, textChanges: textChanges.textChanges.map(textChange => convertTextChangeToCodeEdit(textChange, scriptInfo)) }
: convertNewFileTextChangeToCodeEdit(textChanges);
}

function convertTextChangeToCodeEdit(change: TextChange, scriptInfo: ScriptInfoOrConfig): protocol.CodeEdit {
return { start: positionToLineOffset(scriptInfo, change.span.start), end: positionToLineOffset(scriptInfo, textSpanEnd(change.span)), newText: change.newText };
}
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8826,6 +8826,7 @@ declare namespace ts.server {
private getProjectInfoWorker;
private getRenameInfo;
private getProjects;
private logErrorForScriptInfoNotFound;
private getDefaultProject;
private getRenameLocations;
private mapRenameInfo;
Expand Down