Skip to content

Commit a2e456c

Browse files
committed
Fix self-import resolution in declaration files during build mode
1 parent 18efc1b commit a2e456c

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
contains,
1616
containsPath,
1717
createCompilerDiagnostic,
18+
createGetCanonicalFileName,
1819
Debug,
1920
deduplicate,
2021
Diagnostic,
@@ -1474,6 +1475,69 @@ export function resolveModuleName(moduleName: string, containingFile: string, co
14741475
}
14751476
}
14761477

1478+
if (result.resolvedModule && isDeclarationFileName(containingFile)) {
1479+
const { extension, resolvedFileName } = result.resolvedModule;
1480+
if (extension === Extension.Ts || extension === Extension.Tsx || extension === Extension.Mts || extension === Extension.Cts) {
1481+
let projectDir: string | undefined;
1482+
let options: CompilerOptions | undefined;
1483+
1484+
if (redirectedReference) {
1485+
projectDir = getDirectoryPath(redirectedReference.sourceFile.fileName);
1486+
options = redirectedReference.commandLine.options;
1487+
} else {
1488+
projectDir = getDirectoryPath(containingFile);
1489+
options = compilerOptions;
1490+
}
1491+
1492+
let currentDir = projectDir;
1493+
let packageJsonInfo: PackageJsonInfoCacheEntry | undefined;
1494+
while (currentDir) {
1495+
const packageJsonPath = combinePaths(currentDir, "package.json");
1496+
packageJsonInfo = cache?.getPackageJsonInfo(packageJsonPath);
1497+
if (isPackageJsonInfo(packageJsonInfo)) break;
1498+
const parent = getDirectoryPath(currentDir);
1499+
if (parent === currentDir) break;
1500+
currentDir = parent;
1501+
}
1502+
1503+
if (isPackageJsonInfo(packageJsonInfo) && packageJsonInfo.contents.packageJsonContent.name === moduleName) {
1504+
if (options) {
1505+
let outputDts: string | undefined;
1506+
if (options.outFile) {
1507+
outputDts = changeAnyExtension(options.outFile, ".d.ts");
1508+
}
1509+
else if (options.outDir) {
1510+
const getCanonicalFileName = createGetCanonicalFileName(
1511+
typeof host.useCaseSensitiveFileNames === "function"
1512+
? host.useCaseSensitiveFileNames()
1513+
: host.useCaseSensitiveFileNames ?? true
1514+
);
1515+
1516+
let rootDir = options.rootDir;
1517+
if (!rootDir && redirectedReference) {
1518+
rootDir = getCommonSourceDirectory(
1519+
options,
1520+
() => redirectedReference.commandLine.fileNames,
1521+
host.getCurrentDirectory ? host.getCurrentDirectory() : "",
1522+
getCanonicalFileName
1523+
);
1524+
}
1525+
1526+
if (rootDir) {
1527+
const relativePath = getRelativePathFromDirectory(rootDir, resolvedFileName, getCanonicalFileName);
1528+
outputDts = combinePaths(options.outDir, changeAnyExtension(relativePath, ".d.ts"));
1529+
}
1530+
}
1531+
1532+
if (outputDts) {
1533+
result.resolvedModule.resolvedFileName = outputDts;
1534+
result.resolvedModule.extension = Extension.Dts;
1535+
}
1536+
}
1537+
}
1538+
}
1539+
}
1540+
14771541
return result;
14781542
}
14791543

0 commit comments

Comments
 (0)