Skip to content

Commit 8293e51

Browse files
ShuiRuTianSong Gaorbuckton
authored
fix rename cause import change wrongly (microsoft#38462)
* use canonical file name when resolve module * renameSync in vfs supports same folder. * Update src/harness/vfsUtil.ts Co-authored-by: Ron Buckton <ron.buckton@microsoft.com> * change tss rather than compiler. * remove useless comment. * use fileName rather than path. Co-authored-by: Song Gao <song.gao@laserfiche.com> Co-authored-by: Ron Buckton <ron.buckton@microsoft.com>
1 parent f697d26 commit 8293e51

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/harness/vfsUtil.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,9 @@ namespace vfs {
596596
if (existingNode) {
597597
if (isDirectory(node)) {
598598
if (!isDirectory(existingNode)) throw createIOError("ENOTDIR");
599-
if (this._getLinks(existingNode).size > 0) throw createIOError("ENOTEMPTY");
599+
// if both old and new arguments point to the same directory, just pass. So we could rename /src/a/1 to /src/A/1 in Win.
600+
// if not and the directory pointed by the new path is not empty, throw an error.
601+
if (this.stringComparer(oldpath, newpath) !== 0 && this._getLinks(existingNode).size > 0) throw createIOError("ENOTEMPTY");
600602
}
601603
else {
602604
if (isDirectory(existingNode)) throw createIOError("EISDIR");

src/services/getEditsForFileRename.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ namespace ts {
126126
): void {
127127
const allFiles = program.getSourceFiles();
128128
for (const sourceFile of allFiles) {
129-
const newFromOld = oldToNew(sourceFile.path) as Path;
130-
const newImportFromPath = newFromOld !== undefined ? newFromOld : sourceFile.path;
129+
const newFromOld = oldToNew(sourceFile.fileName);
130+
const newImportFromPath = newFromOld ?? sourceFile.fileName;
131131
const newImportFromDirectory = getDirectoryPath(newImportFromPath);
132132

133133
const oldFromNew: string | undefined = newToOld(sourceFile.fileName);
@@ -157,7 +157,7 @@ namespace ts {
157157

158158
// Need an update if the imported file moved, or the importing file moved and was using a relative path.
159159
return toImport !== undefined && (toImport.updated || (importingSourceFileMoved && pathIsRelative(importLiteral.text)))
160-
? moduleSpecifiers.updateModuleSpecifier(program.getCompilerOptions(), newImportFromPath, toImport.newFileName, createModuleSpecifierResolutionHost(program, host), importLiteral.text)
160+
? moduleSpecifiers.updateModuleSpecifier(program.getCompilerOptions(), getCanonicalFileName(newImportFromPath) as Path, toImport.newFileName, createModuleSpecifierResolutionHost(program, host), importLiteral.text)
161161
: undefined;
162162
});
163163
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a/b/file1.ts
4+
////import { foo } from "foo";
5+
6+
// @Filename: /a/node_modules/foo/index.d.ts
7+
////export const foo = 0;
8+
9+
verify.getEditsForFileRename({
10+
oldPath: "/a/b",
11+
newPath: "/a/B",
12+
newFileContents: {},
13+
});

0 commit comments

Comments
 (0)