-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
return Errors.ThrowNoProject(); | ||
} | ||
} | ||
|
||
getScriptInfoEnsuringProjectsUptoDate(uncheckedFileName: string) { | ||
|
@@ -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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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 { | ||
|
@@ -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 }; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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()