Skip to content

Commit 924030f

Browse files
committed
Explain if file is imported using "@typescript/lib"
1 parent 3ca7531 commit 924030f

File tree

10 files changed

+111
-71
lines changed

10 files changed

+111
-71
lines changed

Diff for: src/compiler/diagnosticMessages.json

+8
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,14 @@
15241524
"category": "Message",
15251525
"code": 1461
15261526
},
1527+
"Library '{0}' specified in compilerOptions and imported via '{1}'": {
1528+
"category": "Message",
1529+
"code": 1462
1530+
},
1531+
"Library referenced via '{0}' from file '{1}' and imported via '{2}'": {
1532+
"category": "Message",
1533+
"code": 1463
1534+
},
15271535

15281536
"The 'import.meta' meta-property is not allowed in files which will build into CommonJS output.": {
15291537
"category": "Error",

Diff for: src/compiler/program.ts

+28-26
Original file line numberDiff line numberDiff line change
@@ -1052,12 +1052,6 @@ export function loadWithModeAwareCache<Entry, SourceFile, ResolutionCache, Resol
10521052
return resolutions;
10531053
}
10541054

1055-
function getLibFileName(libReference: FileReference) {
1056-
const libName = toFileNameLowerCase(libReference.fileName);
1057-
const libFileName = libMap.get(libName);
1058-
return { libName, libFileName };
1059-
}
1060-
10611055
/** @internal */
10621056
export function forEachResolvedProjectReference<T>(
10631057
resolvedProjectReferences: readonly (ResolvedProjectReference | undefined)[] | undefined,
@@ -1111,6 +1105,28 @@ export function getInferredLibrarayNameResolveFrom(currentDirectory: string, lib
11111105
return combinePaths(currentDirectory, `__lib_node_modules_lookup_${libFileName}__.ts`);
11121106
}
11131107

1108+
/** @internal */
1109+
export function getLibraryNameFromLibFileName(libFileName: string) {
1110+
// Support resolving to lib.dom.d.ts -> @typescript/lib-dom, and
1111+
// lib.dom.iterable.d.ts -> @typescript/lib-dom/iterable
1112+
// lib.es2015.symbol.wellknown.d.ts -> @typescript/lib-es2015/symbol-wellknown
1113+
const components = libFileName.split(".");
1114+
let path = components[1];
1115+
let i = 2;
1116+
while (components[i] && components[i] !== "d") {
1117+
path += (i === 2 ? "/" : "-") + components[i];
1118+
i++;
1119+
}
1120+
return "@typescript/lib-" + path;
1121+
}
1122+
1123+
/** @internal */
1124+
export function getLibFileNameFromLibReference(libReference: FileReference) {
1125+
const libName = toFileNameLowerCase(libReference.fileName);
1126+
const libFileName = libMap.get(libName);
1127+
return { libName, libFileName };
1128+
}
1129+
11141130
interface DiagnosticCache<T extends Diagnostic> {
11151131
perFile?: Map<Path, readonly T[]>;
11161132
allDiagnostics?: readonly T[];
@@ -1228,7 +1244,7 @@ export function isProgramUptoDate(
12281244
return !sourceFileVersionUptoDate(sourceFile) ||
12291245
hasInvalidatedResolutions(sourceFile.path) ||
12301246
some(sourceFile.libReferenceDirectives, libRef => {
1231-
const { libFileName } = getLibFileName(libRef);
1247+
const { libFileName } = getLibFileNameFromLibReference(libRef);
12321248
return !!libFileName && hasInvalidatedLibResolutions(libFileName);
12331249
});
12341250
}
@@ -2471,7 +2487,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
24712487
// Do this resolution if necessary to determine reconstruction of program
24722488
if (structureIsReused !== StructureIsReused.Completely &&
24732489
some(newSourceFile.libReferenceDirectives, libReference => {
2474-
const { libFileName } = getLibFileName(libReference);
2490+
const { libFileName } = getLibFileNameFromLibReference(libReference);
24752491
return !!libFileName &&
24762492
pathForLibFileWorker(libFileName).actual !== oldProgram?.resolvedLibReferences?.get(libFileName)?.actual;
24772493
})) {
@@ -3361,7 +3377,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
33613377
}
33623378

33633379
function getLibFileFromReference(ref: FileReference) {
3364-
const { libFileName } = getLibFileName(ref);
3380+
const { libFileName } = getLibFileNameFromLibReference(ref);
33653381
const actualFileName = libFileName && resolvedLibReferences?.get(libFileName)?.actual;
33663382
return actualFileName !== undefined ? getSourceFile(actualFileName) : undefined;
33673383
}
@@ -3866,20 +3882,6 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
38663882
return result.actual;
38673883
}
38683884

3869-
function getLibraryName(libFileName: string) {
3870-
// Support resolving to lib.dom.d.ts -> @typescript/lib-dom, and
3871-
// lib.dom.iterable.d.ts -> @typescript/lib-dom/iterable
3872-
// lib.es2015.symbol.wellknown.d.ts -> @typescript/lib-es2015/symbol-wellknown
3873-
const components = libFileName.split(".");
3874-
let path = components[1];
3875-
let i = 2;
3876-
while (components[i] && components[i] !== "d") {
3877-
path += (i === 2 ? "/" : "-") + components[i];
3878-
i++;
3879-
}
3880-
return "@typescript/lib-" + path;
3881-
}
3882-
38833885
function pathForLibFileWorker(libFileName: string): LibResolution {
38843886
const existing = resolvedLibProcessing?.get(libFileName);
38853887
if (existing) return existing;
@@ -3888,7 +3890,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
38883890
const oldResolution = oldProgram.resolvedLibReferences?.get(libFileName);
38893891
if (oldResolution) {
38903892
if (oldResolution.resolution && isTraceEnabled(options, host)) {
3891-
const libraryName = getLibraryName(libFileName);
3893+
const libraryName = getLibraryNameFromLibFileName(libFileName);
38923894
const resolveFrom = getInferredLibrarayNameResolveFrom(currentDirectory, libFileName);
38933895
trace(host,
38943896
oldResolution.resolution.resolvedModule ?
@@ -3907,7 +3909,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
39073909
}
39083910
}
39093911

3910-
const libraryName = getLibraryName(libFileName);
3912+
const libraryName = getLibraryNameFromLibFileName(libFileName);
39113913
const resolveFrom = getInferredLibrarayNameResolveFrom(currentDirectory, libFileName);
39123914
const resolution = actualResolveLibrary(libraryName, resolveFrom, options, libFileName);
39133915
const result: LibResolution = {
@@ -3922,7 +3924,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
39223924

39233925
function processLibReferenceDirectives(file: SourceFile) {
39243926
forEach(file.libReferenceDirectives, (libReference, index) => {
3925-
const { libName, libFileName } = getLibFileName(libReference);
3927+
const { libName, libFileName } = getLibFileNameFromLibReference(libReference);
39263928
if (libFileName) {
39273929
// we ignore any 'no-default-lib' reference set on this file.
39283930
processRootFile(pathForLibFile(libFileName), /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ true, { kind: FileIncludeKind.LibReferenceDirective, file: file.path, index, });

Diff for: src/compiler/watch.ts

+33-3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ import {
5656
getDefaultLibFileName,
5757
getDirectoryPath,
5858
getEmitScriptTarget,
59+
getLibFileNameFromLibReference,
60+
getLibraryNameFromLibFileName,
5961
getLineAndCharacterOfPosition,
6062
getNewLineCharacter,
6163
getNormalizedAbsolutePath,
@@ -476,8 +478,18 @@ export function fileIncludeReasonToDiagnostics(program: Program, reason: FileInc
476478
break;
477479
case FileIncludeKind.LibReferenceDirective:
478480
Debug.assert(!referenceLocation.packageId);
479-
message = Diagnostics.Library_referenced_via_0_from_file_1;
480-
break;
481+
const file = Debug.checkDefined(program.getSourceFileByPath(reason.file));
482+
const { libFileName } = getLibFileNameFromLibReference(file.libReferenceDirectives[reason.index]);
483+
const importName = getLibraryImportName(program, libFileName!);
484+
return chainDiagnosticMessages(
485+
/*details*/ undefined,
486+
importName ?
487+
Diagnostics.Library_referenced_via_0_from_file_1_and_imported_via_2 :
488+
Diagnostics.Library_referenced_via_0_from_file_1,
489+
referenceText,
490+
toFileName(referenceLocation.file, fileNameConvertor),
491+
importName!
492+
);
481493
default:
482494
Debug.assertNever(reason);
483495
}
@@ -536,7 +548,18 @@ export function fileIncludeReasonToDiagnostics(program: Program, reason: FileInc
536548
return chainDiagnosticMessages(/*details*/ undefined, ...messageAndArgs);
537549
}
538550
case FileIncludeKind.LibFile: {
539-
if (reason.index !== undefined) return chainDiagnosticMessages(/*details*/ undefined, Diagnostics.Library_0_specified_in_compilerOptions, options.lib![reason.index]);
551+
if (reason.index !== undefined) {
552+
const libFileName = options.lib![reason.index];
553+
const importName = getLibraryImportName(program, libFileName);
554+
return chainDiagnosticMessages(
555+
/*details*/ undefined,
556+
importName ?
557+
Diagnostics.Library_0_specified_in_compilerOptions_and_imported_via_1 :
558+
Diagnostics.Library_0_specified_in_compilerOptions,
559+
libFileName,
560+
importName!
561+
);
562+
}
540563
const target = forEachEntry(targetOptionDeclaration.type, (value, key) => value === getEmitScriptTarget(options) ? key : undefined);
541564
const messageAndArgs: DiagnosticAndArguments = target ? [Diagnostics.Default_library_for_target_0, target] : [Diagnostics.Default_library];
542565
return chainDiagnosticMessages(/*details*/ undefined, ...messageAndArgs);
@@ -546,6 +569,13 @@ export function fileIncludeReasonToDiagnostics(program: Program, reason: FileInc
546569
}
547570
}
548571

572+
function getLibraryImportName(program: Program, libFileName: string) {
573+
const resolved = program.resolvedLibReferences!.get(libFileName);
574+
return resolved?.resolution.resolvedModule?.resolvedFileName === resolved?.actual ?
575+
getLibraryNameFromLibFileName(libFileName) :
576+
undefined;
577+
}
578+
549579
function toFileName(file: SourceFile | string, fileNameConvertor?: (fileName: string) => string) {
550580
const fileName = isString(file) ? file : file.fileName;
551581
return fileNameConvertor ? fileNameConvertor(fileName) : fileName;

Diff for: tests/baselines/reference/tsbuild/moduleResolution/library-file-resolution-with-redirection.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ File '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts' exists - u
175175
Resolving real path for '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts', result '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts'.
176176
======== Module name '@typescript/lib-dom' was successfully resolved to '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts'. ========
177177
node_modules/@typescript/lib-es5/index.d.ts
178-
Library 'lib.es5.d.ts' specified in compilerOptions
178+
Library 'lib.es5.d.ts' specified in compilerOptions and imported via '@typescript/lib-es5'
179179
node_modules/@typescript/lib-dom/index.d.ts
180-
Library 'lib.dom.d.ts' specified in compilerOptions
180+
Library 'lib.dom.d.ts' specified in compilerOptions and imported via '@typescript/lib-dom'
181181
project1/core.d.ts
182182
Matched by default include pattern '**/*'
183183
project1/file.ts
@@ -200,9 +200,9 @@ Resolution for module '@typescript/lib-es5' was found in cache from location '/h
200200
Resolution for module '@typescript/lib-dom' was found in cache from location '/home/src/projects'.
201201
======== Module name '@typescript/lib-dom' was successfully resolved to '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts'. ========
202202
node_modules/@typescript/lib-es5/index.d.ts
203-
Library 'lib.es5.d.ts' specified in compilerOptions
203+
Library 'lib.es5.d.ts' specified in compilerOptions and imported via '@typescript/lib-es5'
204204
node_modules/@typescript/lib-dom/index.d.ts
205-
Library 'lib.dom.d.ts' specified in compilerOptions
205+
Library 'lib.dom.d.ts' specified in compilerOptions and imported via '@typescript/lib-dom'
206206
project2/index.ts
207207
Matched by default include pattern '**/*'
208208
project2/utils.d.ts
@@ -218,9 +218,9 @@ Resolution for module '@typescript/lib-es5' was found in cache from location '/h
218218
Resolution for module '@typescript/lib-dom' was found in cache from location '/home/src/projects'.
219219
======== Module name '@typescript/lib-dom' was successfully resolved to '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts'. ========
220220
node_modules/@typescript/lib-es5/index.d.ts
221-
Library 'lib.es5.d.ts' specified in compilerOptions
221+
Library 'lib.es5.d.ts' specified in compilerOptions and imported via '@typescript/lib-es5'
222222
node_modules/@typescript/lib-dom/index.d.ts
223-
Library 'lib.dom.d.ts' specified in compilerOptions
223+
Library 'lib.dom.d.ts' specified in compilerOptions and imported via '@typescript/lib-dom'
224224
project3/index.ts
225225
Matched by default include pattern '**/*'
226226
project3/utils.d.ts
@@ -245,9 +245,9 @@ Resolving real path for '/home/src/projects/node_modules/@typescript/lib-esnext/
245245
Resolution for module '@typescript/lib-dom' was found in cache from location '/home/src/projects'.
246246
======== Module name '@typescript/lib-dom' was successfully resolved to '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts'. ========
247247
node_modules/@typescript/lib-esnext/index.d.ts
248-
Library 'lib.esnext.d.ts' specified in compilerOptions
248+
Library 'lib.esnext.d.ts' specified in compilerOptions and imported via '@typescript/lib-esnext'
249249
node_modules/@typescript/lib-dom/index.d.ts
250-
Library 'lib.dom.d.ts' specified in compilerOptions
250+
Library 'lib.dom.d.ts' specified in compilerOptions and imported via '@typescript/lib-dom'
251251
project4/index.ts
252252
Matched by default include pattern '**/*'
253253
project4/utils.d.ts

Diff for: tests/baselines/reference/tsbuildWatch/moduleResolution/library-file-resolution-with-redirection.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ File '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts' exists - u
161161
Resolving real path for '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts', result '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts'.
162162
======== Module name '@typescript/lib-dom' was successfully resolved to '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts'. ========
163163
node_modules/@typescript/lib-es5/index.d.ts
164-
Library 'lib.es5.d.ts' specified in compilerOptions
164+
Library 'lib.es5.d.ts' specified in compilerOptions and imported via '@typescript/lib-es5'
165165
node_modules/@typescript/lib-dom/index.d.ts
166-
Library 'lib.dom.d.ts' specified in compilerOptions
166+
Library 'lib.dom.d.ts' specified in compilerOptions and imported via '@typescript/lib-dom'
167167
project1/core.d.ts
168168
Matched by default include pattern '**/*'
169169
project1/file.ts
@@ -186,9 +186,9 @@ Resolution for module '@typescript/lib-es5' was found in cache from location '/h
186186
Resolution for module '@typescript/lib-dom' was found in cache from location '/home/src/projects'.
187187
======== Module name '@typescript/lib-dom' was successfully resolved to '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts'. ========
188188
node_modules/@typescript/lib-es5/index.d.ts
189-
Library 'lib.es5.d.ts' specified in compilerOptions
189+
Library 'lib.es5.d.ts' specified in compilerOptions and imported via '@typescript/lib-es5'
190190
node_modules/@typescript/lib-dom/index.d.ts
191-
Library 'lib.dom.d.ts' specified in compilerOptions
191+
Library 'lib.dom.d.ts' specified in compilerOptions and imported via '@typescript/lib-dom'
192192
project2/index.ts
193193
Matched by default include pattern '**/*'
194194
project2/utils.d.ts
@@ -204,9 +204,9 @@ Resolution for module '@typescript/lib-es5' was found in cache from location '/h
204204
Resolution for module '@typescript/lib-dom' was found in cache from location '/home/src/projects'.
205205
======== Module name '@typescript/lib-dom' was successfully resolved to '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts'. ========
206206
node_modules/@typescript/lib-es5/index.d.ts
207-
Library 'lib.es5.d.ts' specified in compilerOptions
207+
Library 'lib.es5.d.ts' specified in compilerOptions and imported via '@typescript/lib-es5'
208208
node_modules/@typescript/lib-dom/index.d.ts
209-
Library 'lib.dom.d.ts' specified in compilerOptions
209+
Library 'lib.dom.d.ts' specified in compilerOptions and imported via '@typescript/lib-dom'
210210
project3/index.ts
211211
Matched by default include pattern '**/*'
212212
project3/utils.d.ts
@@ -231,9 +231,9 @@ Resolving real path for '/home/src/projects/node_modules/@typescript/lib-esnext/
231231
Resolution for module '@typescript/lib-dom' was found in cache from location '/home/src/projects'.
232232
======== Module name '@typescript/lib-dom' was successfully resolved to '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts'. ========
233233
node_modules/@typescript/lib-esnext/index.d.ts
234-
Library 'lib.esnext.d.ts' specified in compilerOptions
234+
Library 'lib.esnext.d.ts' specified in compilerOptions and imported via '@typescript/lib-esnext'
235235
node_modules/@typescript/lib-dom/index.d.ts
236-
Library 'lib.dom.d.ts' specified in compilerOptions
236+
Library 'lib.dom.d.ts' specified in compilerOptions and imported via '@typescript/lib-dom'
237237
project4/index.ts
238238
Matched by default include pattern '**/*'
239239
project4/utils.d.ts

Diff for: tests/baselines/reference/tsc/moduleResolution/library-file-resolution-with-redirection.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ File '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts' exists - u
165165
Resolving real path for '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts', result '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts'.
166166
======== Module name '@typescript/lib-dom' was successfully resolved to '/home/src/projects/node_modules/@typescript/lib-dom/index.d.ts'. ========
167167
node_modules/@typescript/lib-es5/index.d.ts
168-
Library 'lib.es5.d.ts' specified in compilerOptions
168+
Library 'lib.es5.d.ts' specified in compilerOptions and imported via '@typescript/lib-es5'
169169
node_modules/@typescript/lib-dom/index.d.ts
170-
Library 'lib.dom.d.ts' specified in compilerOptions
170+
Library 'lib.dom.d.ts' specified in compilerOptions and imported via '@typescript/lib-dom'
171171
project1/core.d.ts
172172
Matched by default include pattern '**/*'
173173
project1/file.ts

0 commit comments

Comments
 (0)