Skip to content

Commit 394f51a

Browse files
authored
Fix implied formats, file watching, new source file creating during edits (#50098)
* Add test where module resolution cache is not local and hence doesnt report errors in watch mode * Ensure module resolution cache is passed through in watch mode * Remove unnecessary setting of impliedFormat which should anyways be done as part of create source file * Add test for packge.json changing and modifying implied format * Distinguish between package.json watch and affecting file location watch * Pass in failed lookup and affected file locations for source file's implied format Also stop creating options if we already have them * Add diagnostic for explaining file's implied format if based on package.json * Watch implied format dependencies for modules and schedule update on change * For program if implied node format doesnt match create new source file. Handle implied node format in document registry Fixes #50086 * Modify tests to show package.json being watched irrespective of folder its in * Check file path if it can be watched before watching package.json file * Because we are watching package.json files and failed lookups its safe to invalidate package json entries instead of clearing them out everytime program is created * Remove todos * Fix the incorrect merge * Pickup PackageJsonInfo renames from #50088 * Rename
1 parent 427d436 commit 394f51a

File tree

59 files changed

+2927
-249
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2927
-249
lines changed

src/compiler/diagnosticMessages.json

+16
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,22 @@
14641464
"category": "Message",
14651465
"code": 1457
14661466
},
1467+
"File is ECMAScript module because '{0}' has field \"type\" with value \"module\"": {
1468+
"category": "Message",
1469+
"code": 1458
1470+
},
1471+
"File is CommonJS module because '{0}' has field \"type\" whose value is not \"module\"": {
1472+
"category": "Message",
1473+
"code": 1459
1474+
},
1475+
"File is CommonJS module because '{0}' does not have field \"type\"": {
1476+
"category": "Message",
1477+
"code": 1460
1478+
},
1479+
"File is CommonJS module because 'package.json' was not found": {
1480+
"category": "Message",
1481+
"code": 1461
1482+
},
14671483

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

src/compiler/moduleNameResolver.ts

+25-9
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ namespace ts {
543543
}
544544

545545
export interface TypeReferenceDirectiveResolutionCache extends PerDirectoryResolutionCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>, PackageJsonInfoCache {
546+
/*@internal*/ clearAllExceptPackageJsonInfoCache(): void;
546547
}
547548

548549
export interface ModeAwareCache<T> {
@@ -570,6 +571,7 @@ namespace ts {
570571

571572
export interface ModuleResolutionCache extends PerDirectoryResolutionCache<ResolvedModuleWithFailedLookupLocations>, NonRelativeModuleNameResolutionCache, PackageJsonInfoCache {
572573
getPackageJsonInfoCache(): PackageJsonInfoCache;
574+
/*@internal*/ clearAllExceptPackageJsonInfoCache(): void;
573575
}
574576

575577
/**
@@ -584,6 +586,7 @@ namespace ts {
584586
/*@internal*/ getPackageJsonInfo(packageJsonPath: string): PackageJsonInfo | boolean | undefined;
585587
/*@internal*/ setPackageJsonInfo(packageJsonPath: string, info: PackageJsonInfo | boolean): void;
586588
/*@internal*/ entries(): [Path, PackageJsonInfo | boolean][];
589+
/*@internal*/ getInternalMap(): ESMap<Path, PackageJsonInfo | boolean> | undefined;
587590
clear(): void;
588591
}
589592

@@ -649,7 +652,7 @@ namespace ts {
649652

650653
function createPackageJsonInfoCache(currentDirectory: string, getCanonicalFileName: (s: string) => string): PackageJsonInfoCache {
651654
let cache: ESMap<Path, PackageJsonInfo | boolean> | undefined;
652-
return { getPackageJsonInfo, setPackageJsonInfo, clear, entries };
655+
return { getPackageJsonInfo, setPackageJsonInfo, clear, entries, getInternalMap };
653656
function getPackageJsonInfo(packageJsonPath: string) {
654657
return cache?.get(toPath(packageJsonPath, currentDirectory, getCanonicalFileName));
655658
}
@@ -663,6 +666,9 @@ namespace ts {
663666
const iter = cache?.entries();
664667
return iter ? arrayFrom(iter) : [];
665668
}
669+
function getInternalMap() {
670+
return cache;
671+
}
666672
}
667673

668674
function getOrCreateCache<T>(cacheWithRedirects: CacheWithRedirects<T>, redirectedReference: ResolvedProjectReference | undefined, key: string, create: () => T): T {
@@ -797,25 +803,30 @@ namespace ts {
797803
directoryToModuleNameMap?: CacheWithRedirects<ModeAwareCache<ResolvedModuleWithFailedLookupLocations>>,
798804
moduleNameToDirectoryMap?: CacheWithRedirects<PerModuleNameCache>,
799805
): ModuleResolutionCache {
800-
const preDirectoryResolutionCache = createPerDirectoryResolutionCache(currentDirectory, getCanonicalFileName, directoryToModuleNameMap ||= createCacheWithRedirects(options));
806+
const perDirectoryResolutionCache = createPerDirectoryResolutionCache(currentDirectory, getCanonicalFileName, directoryToModuleNameMap ||= createCacheWithRedirects(options));
801807
moduleNameToDirectoryMap ||= createCacheWithRedirects(options);
802808
const packageJsonInfoCache = createPackageJsonInfoCache(currentDirectory, getCanonicalFileName);
803809

804810
return {
805811
...packageJsonInfoCache,
806-
...preDirectoryResolutionCache,
812+
...perDirectoryResolutionCache,
807813
getOrCreateCacheForModuleName,
808814
clear,
809815
update,
810816
getPackageJsonInfoCache: () => packageJsonInfoCache,
817+
clearAllExceptPackageJsonInfoCache,
811818
};
812819

813820
function clear() {
814-
preDirectoryResolutionCache.clear();
815-
moduleNameToDirectoryMap!.clear();
821+
clearAllExceptPackageJsonInfoCache();
816822
packageJsonInfoCache.clear();
817823
}
818824

825+
function clearAllExceptPackageJsonInfoCache() {
826+
perDirectoryResolutionCache.clear();
827+
moduleNameToDirectoryMap!.clear();
828+
}
829+
819830
function update(options: CompilerOptions) {
820831
updateRedirectsMap(options, directoryToModuleNameMap!, moduleNameToDirectoryMap);
821832
}
@@ -919,19 +930,24 @@ namespace ts {
919930
packageJsonInfoCache?: PackageJsonInfoCache | undefined,
920931
directoryToModuleNameMap?: CacheWithRedirects<ModeAwareCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>>,
921932
): TypeReferenceDirectiveResolutionCache {
922-
const preDirectoryResolutionCache = createPerDirectoryResolutionCache(currentDirectory, getCanonicalFileName, directoryToModuleNameMap ||= createCacheWithRedirects(options));
933+
const perDirectoryResolutionCache = createPerDirectoryResolutionCache(currentDirectory, getCanonicalFileName, directoryToModuleNameMap ||= createCacheWithRedirects(options));
923934
packageJsonInfoCache ||= createPackageJsonInfoCache(currentDirectory, getCanonicalFileName);
924935

925936
return {
926937
...packageJsonInfoCache,
927-
...preDirectoryResolutionCache,
938+
...perDirectoryResolutionCache,
928939
clear,
940+
clearAllExceptPackageJsonInfoCache,
929941
};
930942

931943
function clear() {
932-
preDirectoryResolutionCache.clear();
944+
clearAllExceptPackageJsonInfoCache();
933945
packageJsonInfoCache!.clear();
934946
}
947+
948+
function clearAllExceptPackageJsonInfoCache() {
949+
perDirectoryResolutionCache.clear();
950+
}
935951
}
936952

937953
export function resolveModuleNameFromCache(moduleName: string, containingFile: string, cache: ModuleResolutionCache, mode?: ModuleKind.CommonJS | ModuleKind.ESNext): ResolvedModuleWithFailedLookupLocations | undefined {
@@ -1786,7 +1802,7 @@ namespace ts {
17861802
}
17871803

17881804
/*@internal*/
1789-
interface PackageJsonInfo {
1805+
export interface PackageJsonInfo {
17901806
packageDirectory: string;
17911807
packageJsonContent: PackageJsonPathFields;
17921808
versionPaths: VersionPaths | undefined;

src/compiler/parser.ts

+2
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,8 @@ namespace ts {
746746
* check specified by `isFileProbablyExternalModule` will be used to set the field.
747747
*/
748748
setExternalModuleIndicator?: (file: SourceFile) => void;
749+
/*@internal*/ packageJsonLocations?: readonly string[];
750+
/*@internal*/ packageJsonScope?: PackageJsonInfo;
749751
}
750752

751753
function setExternalModuleIndicator(sourceFile: SourceFile) {

0 commit comments

Comments
 (0)