Skip to content

Commit b6121e4

Browse files
Replace disk path operations to use fileNames instead of path (microsoft#56476)
Co-authored-by: Andrew Branch <andrewbranch@users.noreply.github.com>
1 parent c32ad95 commit b6121e4

File tree

106 files changed

+969
-652
lines changed

Some content is hidden

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

106 files changed

+969
-652
lines changed

Diff for: src/compiler/commandLineParser.ts

+26-14
Original file line numberDiff line numberDiff line change
@@ -3771,7 +3771,7 @@ function specToDiagnostic(spec: CompilerOptionsValue, disallowTrailingRecursion?
37713771
/**
37723772
* Gets directories in a set of include patterns that should be watched for changes.
37733773
*/
3774-
function getWildcardDirectories({ validatedIncludeSpecs: include, validatedExcludeSpecs: exclude }: ConfigFileSpecs, path: string, useCaseSensitiveFileNames: boolean): MapLike<WatchDirectoryFlags> {
3774+
function getWildcardDirectories({ validatedIncludeSpecs: include, validatedExcludeSpecs: exclude }: ConfigFileSpecs, basePath: string, useCaseSensitiveFileNames: boolean): MapLike<WatchDirectoryFlags> {
37753775
// We watch a directory recursively if it contains a wildcard anywhere in a directory segment
37763776
// of the pattern:
37773777
//
@@ -3784,23 +3784,26 @@ function getWildcardDirectories({ validatedIncludeSpecs: include, validatedExclu
37843784
//
37853785
// /a/b/* - Watch /a/b directly to catch any new file
37863786
// /a/b/a?z - Watch /a/b directly to catch any new file matching a?z
3787-
const rawExcludeRegex = getRegularExpressionForWildcard(exclude, path, "exclude");
3787+
const rawExcludeRegex = getRegularExpressionForWildcard(exclude, basePath, "exclude");
37883788
const excludeRegex = rawExcludeRegex && new RegExp(rawExcludeRegex, useCaseSensitiveFileNames ? "" : "i");
37893789
const wildcardDirectories: MapLike<WatchDirectoryFlags> = {};
3790+
const wildCardKeyToPath = new Map<CanonicalKey, string>();
37903791
if (include !== undefined) {
3791-
const recursiveKeys: string[] = [];
3792+
const recursiveKeys: CanonicalKey[] = [];
37923793
for (const file of include) {
3793-
const spec = normalizePath(combinePaths(path, file));
3794+
const spec = normalizePath(combinePaths(basePath, file));
37943795
if (excludeRegex && excludeRegex.test(spec)) {
37953796
continue;
37963797
}
37973798

37983799
const match = getWildcardDirectoryFromSpec(spec, useCaseSensitiveFileNames);
37993800
if (match) {
3800-
const { key, flags } = match;
3801-
const existingFlags = wildcardDirectories[key];
3801+
const { key, path, flags } = match;
3802+
const existingPath = wildCardKeyToPath.get(key);
3803+
const existingFlags = existingPath !== undefined ? wildcardDirectories[existingPath] : undefined;
38023804
if (existingFlags === undefined || existingFlags < flags) {
3803-
wildcardDirectories[key] = flags;
3805+
wildcardDirectories[existingPath !== undefined ? existingPath : path] = flags;
3806+
if (existingPath === undefined) wildCardKeyToPath.set(key, path);
38043807
if (flags === WatchDirectoryFlags.Recursive) {
38053808
recursiveKeys.push(key);
38063809
}
@@ -3809,11 +3812,12 @@ function getWildcardDirectories({ validatedIncludeSpecs: include, validatedExclu
38093812
}
38103813

38113814
// Remove any subpaths under an existing recursively watched directory.
3812-
for (const key in wildcardDirectories) {
3813-
if (hasProperty(wildcardDirectories, key)) {
3815+
for (const path in wildcardDirectories) {
3816+
if (hasProperty(wildcardDirectories, path)) {
38143817
for (const recursiveKey of recursiveKeys) {
3815-
if (key !== recursiveKey && containsPath(recursiveKey, key, path, !useCaseSensitiveFileNames)) {
3816-
delete wildcardDirectories[key];
3818+
const key = toCanonicalKey(path, useCaseSensitiveFileNames);
3819+
if (key !== recursiveKey && containsPath(recursiveKey, key, basePath, !useCaseSensitiveFileNames)) {
3820+
delete wildcardDirectories[path];
38173821
}
38183822
}
38193823
}
@@ -3823,7 +3827,12 @@ function getWildcardDirectories({ validatedIncludeSpecs: include, validatedExclu
38233827
return wildcardDirectories;
38243828
}
38253829

3826-
function getWildcardDirectoryFromSpec(spec: string, useCaseSensitiveFileNames: boolean): { key: string; flags: WatchDirectoryFlags; } | undefined {
3830+
type CanonicalKey = string & { __canonicalKey: never; };
3831+
function toCanonicalKey(path: string, useCaseSensitiveFileNames: boolean): CanonicalKey {
3832+
return (useCaseSensitiveFileNames ? path : toFileNameLowerCase(path)) as CanonicalKey;
3833+
}
3834+
3835+
function getWildcardDirectoryFromSpec(spec: string, useCaseSensitiveFileNames: boolean): { key: CanonicalKey; path: string; flags: WatchDirectoryFlags; } | undefined {
38273836
const match = wildcardDirectoryPattern.exec(spec);
38283837
if (match) {
38293838
// We check this with a few `indexOf` calls because 3 `indexOf`/`lastIndexOf` calls is
@@ -3834,15 +3843,18 @@ function getWildcardDirectoryFromSpec(spec: string, useCaseSensitiveFileNames: b
38343843
const starWildcardIndex = spec.indexOf("*");
38353844
const lastDirectorySeperatorIndex = spec.lastIndexOf(directorySeparator);
38363845
return {
3837-
key: useCaseSensitiveFileNames ? match[0] : toFileNameLowerCase(match[0]),
3846+
key: toCanonicalKey(match[0], useCaseSensitiveFileNames),
3847+
path: match[0],
38383848
flags: (questionWildcardIndex !== -1 && questionWildcardIndex < lastDirectorySeperatorIndex)
38393849
|| (starWildcardIndex !== -1 && starWildcardIndex < lastDirectorySeperatorIndex)
38403850
? WatchDirectoryFlags.Recursive : WatchDirectoryFlags.None,
38413851
};
38423852
}
38433853
if (isImplicitGlob(spec.substring(spec.lastIndexOf(directorySeparator) + 1))) {
3854+
const path = removeTrailingDirectorySeparator(spec);
38443855
return {
3845-
key: removeTrailingDirectorySeparator(useCaseSensitiveFileNames ? spec : toFileNameLowerCase(spec)),
3856+
key: toCanonicalKey(path, useCaseSensitiveFileNames),
3857+
path,
38463858
flags: WatchDirectoryFlags.Recursive,
38473859
};
38483860
}

Diff for: src/compiler/moduleNameResolver.ts

+23-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
append,
33
appendIfUnique,
4-
arrayFrom,
54
arrayIsEqualTo,
65
changeAnyExtension,
76
CharacterCodes,
@@ -901,11 +900,24 @@ export interface NonRelativeModuleNameResolutionCache extends NonRelativeNameRes
901900
getOrCreateCacheForModuleName(nonRelativeModuleName: string, mode: ResolutionMode, redirectedReference?: ResolvedProjectReference): PerModuleNameCache;
902901
}
903902

903+
/** @internal */
904+
export interface MissingPackageJsonInfo {
905+
packageDirectory: string;
906+
directoryExists: boolean;
907+
}
908+
909+
/** @internal */
910+
export type PackageJsonInfoCacheEntry = PackageJsonInfo | MissingPackageJsonInfo;
911+
912+
/** @internal */
913+
export function isPackageJsonInfo(entry: PackageJsonInfoCacheEntry | undefined): entry is PackageJsonInfo {
914+
return !!(entry as PackageJsonInfo | undefined)?.contents;
915+
}
916+
904917
export interface PackageJsonInfoCache {
905-
/** @internal */ getPackageJsonInfo(packageJsonPath: string): PackageJsonInfo | boolean | undefined;
906-
/** @internal */ setPackageJsonInfo(packageJsonPath: string, info: PackageJsonInfo | boolean): void;
907-
/** @internal */ entries(): [Path, PackageJsonInfo | boolean][];
908-
/** @internal */ getInternalMap(): Map<Path, PackageJsonInfo | boolean> | undefined;
918+
/** @internal */ getPackageJsonInfo(packageJsonPath: string): PackageJsonInfoCacheEntry | undefined;
919+
/** @internal */ setPackageJsonInfo(packageJsonPath: string, info: PackageJsonInfoCacheEntry): void;
920+
/** @internal */ getInternalMap(): Map<Path, PackageJsonInfoCacheEntry> | undefined;
909921
clear(): void;
910922
/** @internal */ isReadonly?: boolean;
911923
}
@@ -1021,21 +1033,17 @@ export function createCacheWithRedirects<K, V>(ownOptions: CompilerOptions | und
10211033
}
10221034

10231035
function createPackageJsonInfoCache(currentDirectory: string, getCanonicalFileName: (s: string) => string): PackageJsonInfoCache {
1024-
let cache: Map<Path, PackageJsonInfo | boolean> | undefined;
1025-
return { getPackageJsonInfo, setPackageJsonInfo, clear, entries, getInternalMap };
1036+
let cache: Map<Path, PackageJsonInfoCacheEntry> | undefined;
1037+
return { getPackageJsonInfo, setPackageJsonInfo, clear, getInternalMap };
10261038
function getPackageJsonInfo(packageJsonPath: string) {
10271039
return cache?.get(toPath(packageJsonPath, currentDirectory, getCanonicalFileName));
10281040
}
1029-
function setPackageJsonInfo(packageJsonPath: string, info: PackageJsonInfo | boolean) {
1041+
function setPackageJsonInfo(packageJsonPath: string, info: PackageJsonInfoCacheEntry) {
10301042
(cache ||= new Map()).set(toPath(packageJsonPath, currentDirectory, getCanonicalFileName), info);
10311043
}
10321044
function clear() {
10331045
cache = undefined;
10341046
}
1035-
function entries() {
1036-
const iter = cache?.entries();
1037-
return iter ? arrayFrom(iter) : [];
1038-
}
10391047
function getInternalMap() {
10401048
return cache;
10411049
}
@@ -2391,15 +2399,15 @@ export function getPackageJsonInfo(packageDirectory: string, onlyRecordFailures:
23912399

23922400
const existing = state.packageJsonInfoCache?.getPackageJsonInfo(packageJsonPath);
23932401
if (existing !== undefined) {
2394-
if (typeof existing !== "boolean") {
2402+
if (isPackageJsonInfo(existing)) {
23952403
if (traceEnabled) trace(host, Diagnostics.File_0_exists_according_to_earlier_cached_lookups, packageJsonPath);
23962404
state.affectingLocations?.push(packageJsonPath);
23972405
return existing.packageDirectory === packageDirectory ?
23982406
existing :
23992407
{ packageDirectory, contents: existing.contents };
24002408
}
24012409
else {
2402-
if (existing && traceEnabled) trace(host, Diagnostics.File_0_does_not_exist_according_to_earlier_cached_lookups, packageJsonPath);
2410+
if (existing.directoryExists && traceEnabled) trace(host, Diagnostics.File_0_does_not_exist_according_to_earlier_cached_lookups, packageJsonPath);
24032411
state.failedLookupLocations?.push(packageJsonPath);
24042412
return undefined;
24052413
}
@@ -2419,7 +2427,7 @@ export function getPackageJsonInfo(packageDirectory: string, onlyRecordFailures:
24192427
if (directoryExists && traceEnabled) {
24202428
trace(host, Diagnostics.File_0_does_not_exist, packageJsonPath);
24212429
}
2422-
if (state.packageJsonInfoCache && !state.packageJsonInfoCache.isReadonly) state.packageJsonInfoCache.setPackageJsonInfo(packageJsonPath, directoryExists);
2430+
if (state.packageJsonInfoCache && !state.packageJsonInfoCache.isReadonly) state.packageJsonInfoCache.setPackageJsonInfo(packageJsonPath, { packageDirectory, directoryExists });
24232431
// record package json as one of failed lookup locations - in the future if this file will appear it will invalidate resolution results
24242432
state.failedLookupLocations?.push(packageJsonPath);
24252433
}

0 commit comments

Comments
 (0)