Skip to content

Commit 16ea9af

Browse files
authored
Merge pull request microsoft#28398 from Microsoft/renameInReferencedProject
Fix the issue with file being included in the referencing project on rename when it wasnt included earlier
2 parents 59a8c94 + 1d87250 commit 16ea9af

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

src/services/getEditsForFileRename.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace ts {
1414
const oldToNew = getPathUpdater(oldFileOrDirPath, newFileOrDirPath, getCanonicalFileName, sourceMapper);
1515
const newToOld = getPathUpdater(newFileOrDirPath, oldFileOrDirPath, getCanonicalFileName, sourceMapper);
1616
return textChanges.ChangeTracker.with({ host, formatContext }, changeTracker => {
17-
updateTsconfigFiles(program, changeTracker, oldToNew, newFileOrDirPath, host.getCurrentDirectory(), useCaseSensitiveFileNames);
17+
updateTsconfigFiles(program, changeTracker, oldToNew, oldFileOrDirPath, newFileOrDirPath, host.getCurrentDirectory(), useCaseSensitiveFileNames);
1818
updateImports(program, changeTracker, oldToNew, newToOld, host, getCanonicalFileName);
1919
});
2020
}
@@ -45,7 +45,7 @@ namespace ts {
4545
return combinePathsSafe(getDirectoryPath(a1), rel);
4646
}
4747

48-
function updateTsconfigFiles(program: Program, changeTracker: textChanges.ChangeTracker, oldToNew: PathUpdater, newFileOrDirPath: string, currentDirectory: string, useCaseSensitiveFileNames: boolean): void {
48+
function updateTsconfigFiles(program: Program, changeTracker: textChanges.ChangeTracker, oldToNew: PathUpdater, oldFileOrDirPath: string, newFileOrDirPath: string, currentDirectory: string, useCaseSensitiveFileNames: boolean): void {
4949
const { configFile } = program.getCompilerOptions();
5050
if (!configFile) return;
5151
const configDir = getDirectoryPath(configFile.fileName);
@@ -63,7 +63,8 @@ namespace ts {
6363
const includes = mapDefined(property.initializer.elements, e => isStringLiteral(e) ? e.text : undefined);
6464
const matchers = getFileMatcherPatterns(configDir, /*excludes*/ [], includes, useCaseSensitiveFileNames, currentDirectory);
6565
// If there isn't some include for this, add a new one.
66-
if (!getRegexFromPattern(Debug.assertDefined(matchers.includeFilePattern), useCaseSensitiveFileNames).test(newFileOrDirPath)) {
66+
if (getRegexFromPattern(Debug.assertDefined(matchers.includeFilePattern), useCaseSensitiveFileNames).test(oldFileOrDirPath) &&
67+
!getRegexFromPattern(Debug.assertDefined(matchers.includeFilePattern), useCaseSensitiveFileNames).test(newFileOrDirPath)) {
6768
changeTracker.insertNodeAfter(configFile, last(property.initializer.elements), createStringLiteral(relativePath(newFileOrDirPath)));
6869
}
6970
}

src/testRunner/unittests/tsserverProjectSystem.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10523,6 +10523,42 @@ declare class TestLib {
1052310523
]);
1052410524
verifySingleInferredProject(session);
1052510525
});
10526+
10527+
it("getEditsForFileRename when referencing project doesnt include file and its renamed", () => {
10528+
const aTs: File = { path: "/a/src/a.ts", content: "" };
10529+
const aTsconfig: File = {
10530+
path: "/a/tsconfig.json",
10531+
content: JSON.stringify({
10532+
compilerOptions: {
10533+
composite: true,
10534+
declaration: true,
10535+
declarationMap: true,
10536+
outDir: "./build",
10537+
}
10538+
}),
10539+
};
10540+
const bTs: File = { path: "/b/src/b.ts", content: "" };
10541+
const bTsconfig: File = {
10542+
path: "/b/tsconfig.json",
10543+
content: JSON.stringify({
10544+
compilerOptions: {
10545+
composite: true,
10546+
outDir: "./build",
10547+
},
10548+
include: ["./src"],
10549+
references: [{ path: "../a" }],
10550+
}),
10551+
};
10552+
10553+
const host = createServerHost([aTs, aTsconfig, bTs, bTsconfig]);
10554+
const session = createSession(host);
10555+
openFilesForSession([aTs, bTs], session);
10556+
const response = executeSessionRequest<protocol.GetEditsForFileRenameRequest, protocol.GetEditsForFileRenameResponse>(session, CommandNames.GetEditsForFileRename, {
10557+
oldFilePath: aTs.path,
10558+
newFilePath: "/a/src/a1.ts",
10559+
});
10560+
assert.deepEqual<ReadonlyArray<protocol.FileCodeEdits>>(response, []); // Should not change anything
10561+
});
1052610562
});
1052710563

1052810564
describe("tsserverProjectSystem with tsbuild projects", () => {

0 commit comments

Comments
 (0)