diff --git a/src/services/getEditsForFileRename.ts b/src/services/getEditsForFileRename.ts index e503826dc8c..d2b17739824 100644 --- a/src/services/getEditsForFileRename.ts +++ b/src/services/getEditsForFileRename.ts @@ -151,7 +151,7 @@ namespace ts { const toImport = oldFromNew !== undefined // If we're at the new location (file was already renamed), need to redo module resolution starting from the old location. // TODO:GH#18217 - ? getSourceFileToImportFromResolved(resolveModuleName(importLiteral.text, oldImportFromPath, program.getCompilerOptions(), host as ModuleResolutionHost), + ? getSourceFileToImportFromResolved(importLiteral, resolveModuleName(importLiteral.text, oldImportFromPath, program.getCompilerOptions(), host as ModuleResolutionHost), oldToNew, allFiles) : getSourceFileToImport(importedModuleSymbol, importLiteral, sourceFile, program, host, oldToNew); @@ -193,11 +193,11 @@ namespace ts { const resolved = host.resolveModuleNames ? host.getResolvedModuleWithFailedLookupLocationsFromCache && host.getResolvedModuleWithFailedLookupLocationsFromCache(importLiteral.text, importingSourceFile.fileName) : program.getResolvedModuleWithFailedLookupLocationsFromCache(importLiteral.text, importingSourceFile.fileName); - return getSourceFileToImportFromResolved(resolved, oldToNew, program.getSourceFiles()); + return getSourceFileToImportFromResolved(importLiteral, resolved, oldToNew, program.getSourceFiles()); } } - function getSourceFileToImportFromResolved(resolved: ResolvedModuleWithFailedLookupLocations | undefined, oldToNew: PathUpdater, sourceFiles: readonly SourceFile[]): ToImport | undefined { + function getSourceFileToImportFromResolved(importLiteral: StringLiteralLike, resolved: ResolvedModuleWithFailedLookupLocations | undefined, oldToNew: PathUpdater, sourceFiles: readonly SourceFile[]): ToImport | undefined { // Search through all locations looking for a moved file, and only then test already existing files. // This is because if `a.ts` is compiled to `a.js` and `a.ts` is moved, we don't want to resolve anything to `a.js`, but to `a.ts`'s new location. if (!resolved) return undefined; @@ -210,8 +210,9 @@ namespace ts { // Then failed lookups that are in the list of sources const result = forEach(resolved.failedLookupLocations, tryChangeWithIgnoringPackageJsonExisting) - // Then failed lookups except package.json since we dont want to touch them (only included ts/js files) - || forEach(resolved.failedLookupLocations, tryChangeWithIgnoringPackageJson); + // Then failed lookups except package.json since we dont want to touch them (only included ts/js files). + // At this point, the confidence level of this fix being correct is too low to change bare specifiers or absolute paths. + || pathIsRelative(importLiteral.text) && forEach(resolved.failedLookupLocations, tryChangeWithIgnoringPackageJson); if (result) return result; // If nothing changed, then result is resolved module file thats not updated diff --git a/tests/cases/fourslash/getEditsForFileRename_unresolvableImport.ts b/tests/cases/fourslash/getEditsForFileRename_unresolvableImport.ts new file mode 100644 index 00000000000..141142382df --- /dev/null +++ b/tests/cases/fourslash/getEditsForFileRename_unresolvableImport.ts @@ -0,0 +1,27 @@ +/// + +// @Filename: /tsconfig.json +//// { +//// "compilerOptions": { +//// "allowJs": true, +//// "paths": { +//// "*": ["./next/src/*"], +//// "@app": ["./modules/@app/*"], +//// "@app/*": ["./modules/@app/*"], +//// "@local": ["./modules/@local/*"], +//// "@local/*": ["./modules/@local/*"] +//// } +//// } +//// } + +// @Filename: /modules/@app/something/index.js +//// import "@local/some-other-import"; + +// @Filename: /modules/@local/index.js +//// import "@local/some-other-import"; + +verify.getEditsForFileRename({ + oldPath: "/modules/@app/something", + newPath: "/modules/@app/something-2", + newFileContents: {} +}); diff --git a/tests/cases/fourslash/getEditsForFileRename_unresolvableNodeModule.ts b/tests/cases/fourslash/getEditsForFileRename_unresolvableNodeModule.ts new file mode 100644 index 00000000000..0f77242a9ef --- /dev/null +++ b/tests/cases/fourslash/getEditsForFileRename_unresolvableNodeModule.ts @@ -0,0 +1,16 @@ +/// + +// @allowJs: true +// @checkJs: true + +// @Filename: /modules/@app/something/index.js +//// import "doesnt-exist"; + +// @Filename: /modules/@local/foo.js +//// import "doesnt-exist"; + +verify.getEditsForFileRename({ + oldPath: "/modules/@app/something", + newPath: "/modules/@app/something-2", + newFileContents: {} +});