Skip to content

getScriptInfoOrConfig: Canonicalize tsconfig path before lookup #26280

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

Merged
merged 10 commits into from
Jan 9, 2019
2 changes: 1 addition & 1 deletion src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,7 @@ namespace ts.server {
const path = toNormalizedPath(uncheckedFileName);
const info = this.getScriptInfoForNormalizedPath(path);
if (info) return info;
const configProject = this.configuredProjects.get(uncheckedFileName);
const configProject = this.configuredProjects.get(this.toCanonicalFileName(path));
Copy link
Member

Choose a reason for hiding this comment

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

This should this.toPath(uncheckedFileName) instead of just canonicalizing file name, (you need to handle current directory as well though in most cases unchecked file is absolute path but toPath is more correct version)

return configProject && configProject.getCompilerOptions().configFile;
}

Expand Down
37 changes: 37 additions & 0 deletions src/testRunner/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10180,6 +10180,43 @@ declare class TestLib {
});
});

describe("tsserverProjectSystem refactors", () => {
it("handles canonicalization of tsconfig path", () => {
const aTs: File = { path: "/Foo/a.ts", content: "const x = 0;" };
const tsconfig: File = { path: "/Foo/tsconfig.json", content: '{ "files": ["./a.ts"] }' };
const session = createSession(createServerHost([aTs, tsconfig]));
openFilesForSession([aTs], session);

const result = executeSessionRequest<protocol.GetEditsForRefactorRequest, protocol.GetEditsForRefactorResponse>(session, protocol.CommandTypes.GetEditsForRefactor, {
file: aTs.path,
startLine: 1,
startOffset: 1,
endLine: 2,
endOffset: aTs.content.length,
refactor: "Move to a new file",
action: "Move to a new file",
});
assert.deepEqual<protocol.RefactorEditInfo | undefined>(result, {
edits: [
{
fileName: aTs.path,
textChanges: [{ start: { line: 1, offset: 1 }, end: { line: 1, offset: aTs.content.length + 1 }, newText: "" }],
},
{
fileName: tsconfig.path,
textChanges: [{ start: { line: 1, offset: 21 }, end: { line: 1, offset: 21 }, newText: ', "./x.ts"' }],
},
{
fileName: "/Foo/x.ts",
textChanges: [{ start: { line: 0, offset: 0 }, end: { line: 0, offset: 0 }, newText: "const x = 0;" }],
},
],
renameFilename: undefined,
renameLocation: undefined,
});
});
});

function makeReferenceItem(file: File, isDefinition: boolean, text: string, lineText: string, options?: SpanFromSubstringOptions): protocol.ReferencesResponseItem {
return {
...protocolFileSpanFromSubstring(file, text, options),
Expand Down