From 8293e517c0630b7ab05ff35efa27485f51b96882 Mon Sep 17 00:00:00 2001 From: ShuiRuTian <158983297@qq.com> Date: Thu, 18 Jun 2020 23:31:30 +0800 Subject: [PATCH] fix rename cause import change wrongly (#38462) * use canonical file name when resolve module * renameSync in vfs supports same folder. * Update src/harness/vfsUtil.ts Co-authored-by: Ron Buckton * change tss rather than compiler. * remove useless comment. * use fileName rather than path. Co-authored-by: Song Gao Co-authored-by: Ron Buckton --- src/harness/vfsUtil.ts | 4 +++- src/services/getEditsForFileRename.ts | 6 +++--- ...getEditsForFileRename_nodeModuleDirectoryCase.ts | 13 +++++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 tests/cases/fourslash/getEditsForFileRename_nodeModuleDirectoryCase.ts diff --git a/src/harness/vfsUtil.ts b/src/harness/vfsUtil.ts index 71d95b4d4ab..974c164976c 100644 --- a/src/harness/vfsUtil.ts +++ b/src/harness/vfsUtil.ts @@ -596,7 +596,9 @@ namespace vfs { if (existingNode) { if (isDirectory(node)) { if (!isDirectory(existingNode)) throw createIOError("ENOTDIR"); - if (this._getLinks(existingNode).size > 0) throw createIOError("ENOTEMPTY"); + // 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. + // if not and the directory pointed by the new path is not empty, throw an error. + if (this.stringComparer(oldpath, newpath) !== 0 && this._getLinks(existingNode).size > 0) throw createIOError("ENOTEMPTY"); } else { if (isDirectory(existingNode)) throw createIOError("EISDIR"); diff --git a/src/services/getEditsForFileRename.ts b/src/services/getEditsForFileRename.ts index 03803e6d3b3..e503826dc8c 100644 --- a/src/services/getEditsForFileRename.ts +++ b/src/services/getEditsForFileRename.ts @@ -126,8 +126,8 @@ namespace ts { ): void { const allFiles = program.getSourceFiles(); for (const sourceFile of allFiles) { - const newFromOld = oldToNew(sourceFile.path) as Path; - const newImportFromPath = newFromOld !== undefined ? newFromOld : sourceFile.path; + const newFromOld = oldToNew(sourceFile.fileName); + const newImportFromPath = newFromOld ?? sourceFile.fileName; const newImportFromDirectory = getDirectoryPath(newImportFromPath); const oldFromNew: string | undefined = newToOld(sourceFile.fileName); @@ -157,7 +157,7 @@ namespace ts { // Need an update if the imported file moved, or the importing file moved and was using a relative path. return toImport !== undefined && (toImport.updated || (importingSourceFileMoved && pathIsRelative(importLiteral.text))) - ? moduleSpecifiers.updateModuleSpecifier(program.getCompilerOptions(), newImportFromPath, toImport.newFileName, createModuleSpecifierResolutionHost(program, host), importLiteral.text) + ? moduleSpecifiers.updateModuleSpecifier(program.getCompilerOptions(), getCanonicalFileName(newImportFromPath) as Path, toImport.newFileName, createModuleSpecifierResolutionHost(program, host), importLiteral.text) : undefined; }); } diff --git a/tests/cases/fourslash/getEditsForFileRename_nodeModuleDirectoryCase.ts b/tests/cases/fourslash/getEditsForFileRename_nodeModuleDirectoryCase.ts new file mode 100644 index 00000000000..134e5212ad4 --- /dev/null +++ b/tests/cases/fourslash/getEditsForFileRename_nodeModuleDirectoryCase.ts @@ -0,0 +1,13 @@ +/// + +// @Filename: /a/b/file1.ts +////import { foo } from "foo"; + +// @Filename: /a/node_modules/foo/index.d.ts +////export const foo = 0; + +verify.getEditsForFileRename({ + oldPath: "/a/b", + newPath: "/a/B", + newFileContents: {}, +});