Skip to content

When resolving type reference directive use baseUrl and other resolution settings #39646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37470,7 +37470,7 @@ namespace ts {
if (fileToDirective.has(file.path)) return;
fileToDirective.set(file.path, key);
for (const { fileName } of file.referencedFiles) {
const resolvedFile = resolveTripleslashReference(fileName, file.originalFileName);
const resolvedFile = resolveTripleslashReference(fileName, file.fileName);
const referencedFile = host.getSourceFile(resolvedFile);
if (referencedFile) {
addReferencedFilesToTypeDirective(referencedFile, key);
Expand Down
33 changes: 21 additions & 12 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,18 +365,27 @@ namespace ts {
const initialLocationForSecondaryLookup = containingFile && getDirectoryPath(containingFile);

if (initialLocationForSecondaryLookup !== undefined) {
// check secondary locations
if (traceEnabled) {
trace(host, Diagnostics.Looking_up_in_node_modules_folder_initial_location_0, initialLocationForSecondaryLookup);
}
let result: Resolved | undefined;
if (!isExternalModuleNameRelative(typeReferenceDirectiveName)) {
const searchResult = loadModuleFromNearestNodeModulesDirectory(Extensions.DtsOnly, typeReferenceDirectiveName, initialLocationForSecondaryLookup, moduleResolutionState, /*cache*/ undefined, /*redirectedReference*/ undefined);
result = searchResult && searchResult.value;
}
else {
const { path: candidate } = normalizePathAndParts(combinePaths(initialLocationForSecondaryLookup, typeReferenceDirectiveName));
result = nodeLoadModuleByRelativeName(Extensions.DtsOnly, candidate, /*onlyRecordFailures*/ false, moduleResolutionState, /*considerPackageJson*/ true);
// Try using baseUrl resolution
let result = tryLoadModuleUsingOptionalResolutionSettings(
Extensions.DtsOnly,
typeReferenceDirectiveName,
initialLocationForSecondaryLookup,
(extensions, candidate, onlyRecordFailures, state) => nodeLoadModuleByRelativeName(extensions, candidate, onlyRecordFailures, state, /*considerPackageJson*/ true),
moduleResolutionState
);
if (!result) {
// check secondary locations
if (traceEnabled) {
trace(host, Diagnostics.Looking_up_in_node_modules_folder_initial_location_0, initialLocationForSecondaryLookup);
}
if (!isExternalModuleNameRelative(typeReferenceDirectiveName)) {
const searchResult = loadModuleFromNearestNodeModulesDirectory(Extensions.DtsOnly, typeReferenceDirectiveName, initialLocationForSecondaryLookup, moduleResolutionState, /*cache*/ undefined, /*redirectedReference*/ undefined);
result = searchResult && searchResult.value;
}
else {
const { path: candidate } = normalizePathAndParts(combinePaths(initialLocationForSecondaryLookup, typeReferenceDirectiveName));
result = nodeLoadModuleByRelativeName(Extensions.DtsOnly, candidate, /*onlyRecordFailures*/ false, moduleResolutionState, /*considerPackageJson*/ true);
}
}
const resolvedFile = resolvedTypeScriptOnly(result);
if (!resolvedFile && traceEnabled) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2667,7 +2667,7 @@ namespace ts {
return;
}
forEach(file.referencedFiles, (ref, index) => {
const referencedFileName = resolveTripleslashReference(ref.fileName, file.originalFileName);
const referencedFileName = resolveTripleslashReference(ref.fileName, file.fileName);
processSourceFile(
referencedFileName,
isDefaultLib,
Expand Down
1 change: 1 addition & 0 deletions src/testRunner/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"unittests/tsbuild/amdModulesWithOut.ts",
"unittests/tsbuild/configFileErrors.ts",
"unittests/tsbuild/containerOnlyReferenced.ts",
"unittests/tsbuild/declarationEmit.ts",
"unittests/tsbuild/demo.ts",
"unittests/tsbuild/emitDeclarationOnly.ts",
"unittests/tsbuild/emptyFiles.ts",
Expand Down
93 changes: 93 additions & 0 deletions src/testRunner/unittests/tsbuild/declarationEmit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
namespace ts {
describe("unittests:: tsbuild:: declarationEmit", () => {
function getFiles(): vfs.FileSet {
return {
"/src/solution/tsconfig.base.json": JSON.stringify({
compilerOptions: {
rootDir: "./",
outDir: "lib"
}
}),
"/src/solution/tsconfig.json": JSON.stringify({
compilerOptions: { composite: true },
references: [{ path: "./src" }],
include: []
}),
"/src/solution/src/tsconfig.json": JSON.stringify({
compilerOptions: { composite: true },
references: [{ path: "./subProject" }, { path: "./subProject2" }],
include: []
}),
"/src/solution/src/subProject/tsconfig.json": JSON.stringify({
extends: "../../tsconfig.base.json",
compilerOptions: { composite: true },
references: [{ path: "../common" }],
include: ["./index.ts"]
}),
"/src/solution/src/subProject/index.ts": Utils.dedent`
import { Nominal } from '../common/nominal';
export type MyNominal = Nominal<string, 'MyNominal'>;`,
"/src/solution/src/subProject2/tsconfig.json": JSON.stringify({
extends: "../../tsconfig.base.json",
compilerOptions: { composite: true },
references: [{ path: "../subProject" }],
include: ["./index.ts"]
}),
"/src/solution/src/subProject2/index.ts": Utils.dedent`
import { MyNominal } from '../subProject/index';
const variable = {
key: 'value' as MyNominal,
};
export function getVar(): keyof typeof variable {
return 'key';
}`,
"/src/solution/src/common/tsconfig.json": JSON.stringify({
extends: "../../tsconfig.base.json",
compilerOptions: { composite: true },
include: ["./nominal.ts"]
}),
"/src/solution/src/common/nominal.ts": Utils.dedent`
/// <reference path="./types.d.ts" />
export declare type Nominal<T, Name extends string> = MyNominal<T, Name>;`,
"/src/solution/src/common/types.d.ts": Utils.dedent`
declare type MyNominal<T, Name extends string> = T & {
specialKey: Name;
};`,
};
}
verifyTsc({
scenario: "declarationEmit",
subScenario: "when declaration file is referenced through triple slash",
fs: () => loadProjectFromFiles(getFiles()),
commandLineArgs: ["--b", "/src/solution/tsconfig.json", "--verbose"]
});

verifyTsc({
scenario: "declarationEmit",
subScenario: "when declaration file is referenced through triple slash and uses baseUrl",
fs: () => loadProjectFromFiles({
...getFiles(),
"/src/solution/src/common/tsconfig.json": JSON.stringify({
extends: "../../tsconfig.base.json",
compilerOptions: { composite: true, baseUrl: "./" },
include: ["./nominal.ts"]
}),
}),
commandLineArgs: ["--b", "/src/solution/tsconfig.json", "--verbose"]
});

verifyTsc({
scenario: "declarationEmit",
subScenario: "when declaration file is referenced through triple slash but uses no references",
fs: () => loadProjectFromFiles({
...getFiles(),
"/src/solution/tsconfig.json": JSON.stringify({
extends: "./tsconfig.base.json",
compilerOptions: { composite: true },
include: ["./src/**/*.ts"]
}),
}),
commandLineArgs: ["--b", "/src/solution/tsconfig.json", "--verbose"]
});
});
}
Loading