Skip to content
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

Ensure equal FileIncludeReasons are not duplicitively added #58352

Closed
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import {
fileExtensionIsOneOf,
FileIncludeKind,
FileIncludeReason,
fileIncludeReasonIsEqual,
fileIncludeReasonToDiagnostics,
FilePreprocessingDiagnostics,
FilePreprocessingDiagnosticsKind,
Expand Down Expand Up @@ -3797,7 +3798,11 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
}

function addFileIncludeReason(file: SourceFile | undefined, reason: FileIncludeReason) {
if (file) fileReasons.add(file.path, reason);
if (file) {
const existing = fileReasons.get(file.path);
if (some(existing, r => fileIncludeReasonIsEqual(r, reason))) return;
fileReasons.add(file.path, reason);
}
}

function addFileToFilesByName(file: SourceFile | undefined, path: Path, fileName: string, redirectedPath: Path | undefined) {
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4598,6 +4598,8 @@ export interface AutomaticTypeDirectiveFile {
packageId: PackageId | undefined;
}

// Note: if updating FileIncludeReason, ensure fileIncludeReasonIsEqual is also updated.

/** @internal */
export type FileIncludeReason =
| RootFile
Expand Down
37 changes: 37 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
AssignmentDeclarationKind,
AssignmentExpression,
AssignmentOperatorToken,
AutomaticTypeDirectiveFile,
BarBarEqualsToken,
BinaryExpression,
binarySearch,
Expand Down Expand Up @@ -136,6 +137,8 @@ import {
FileExtensionInfo,
fileExtensionIs,
fileExtensionIsOneOf,
FileIncludeKind,
FileIncludeReason,
FileWatcher,
filter,
find,
Expand Down Expand Up @@ -399,6 +402,7 @@ import {
lastOrUndefined,
LateVisibilityPaintedStatement,
length,
LibFile,
LiteralImportTypeNode,
LiteralLikeElementAccessExpression,
LiteralLikeNode,
Expand Down Expand Up @@ -466,6 +470,7 @@ import {
PrintHandlers,
PrivateIdentifier,
ProjectReference,
ProjectReferenceFile,
PrologueDirective,
PropertyAccessEntityNameExpression,
PropertyAccessExpression,
Expand All @@ -481,6 +486,7 @@ import {
QuestionQuestionEqualsToken,
ReadonlyCollection,
ReadonlyTextRange,
ReferencedFile,
removeTrailingDirectorySeparator,
RequireOrImportCall,
RequireVariableStatement,
Expand All @@ -492,6 +498,7 @@ import {
returnFalse,
ReturnStatement,
returnUndefined,
RootFile,
SatisfiesExpression,
ScriptKind,
ScriptTarget,
Expand Down Expand Up @@ -852,6 +859,36 @@ export function typeDirectiveIsEqualTo(oldResolution: ResolvedTypeReferenceDirec
oldResolution.resolvedTypeReferenceDirective.originalPath === newResolution.resolvedTypeReferenceDirective.originalPath;
}

/** @internal */
export function fileIncludeReasonIsEqual(a: FileIncludeReason, b: FileIncludeReason): boolean {
if (a === b) return true;
if (a.kind !== b.kind) return false;

switch (a.kind) {
case FileIncludeKind.RootFile:
Debug.type<RootFile>(b);
return a.index === b.index;
case FileIncludeKind.LibFile:
Debug.type<LibFile>(b);
return a.index === b.index;
case FileIncludeKind.SourceFromProjectReference:
case FileIncludeKind.OutputFromProjectReference:
Debug.type<ProjectReferenceFile>(b);
return a.index === b.index;
case FileIncludeKind.Import:
case FileIncludeKind.ReferenceFile:
case FileIncludeKind.TypeReferenceDirective:
case FileIncludeKind.LibReferenceDirective:
Debug.type<ReferencedFile>(b);
return a.file === b.file && a.index === b.index;
case FileIncludeKind.AutomaticTypeDirectiveFile:
Debug.type<AutomaticTypeDirectiveFile>(b);
return a.typeReference === b.typeReference && packageIdIsEqual(a.packageId, b.packageId);
default:
return Debug.assertNever(a);
}
}

/** @internal */
export function hasChangesInResolutions<K, V>(
names: readonly K[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ Info seq [hh:mm:ss:mss] Files (4)
../../../../a/lib/lib.d.ts
Default library for target 'es5'
node_modules/bar/foo.d.ts
Imported via "./foo" from file 'node_modules/bar/index.d.ts'
Imported via "./foo" from file 'node_modules/bar/index.d.ts'
Root file specified for compilation
node_modules/bar/index.d.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ Info seq [hh:mm:ss:mss] Files (4)
../../../../a/lib/lib.d.ts
Default library for target 'es5'
node_modules/bar/foo.d.ts
Imported via "./foo" from file 'node_modules/bar/index.d.ts'
Imported via "./foo" from file 'node_modules/bar/index.d.ts'
Root file specified for compilation
node_modules/bar/index.d.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ Info seq [hh:mm:ss:mss] Files (4)
../../../../a/lib/lib.d.ts
Default library for target 'es5'
node_modules/bar/foo.d.ts
Imported via "./foo" from file 'node_modules/bar/index.d.ts'
Imported via "./foo" from file 'node_modules/bar/index.d.ts'
Root file specified for compilation
node_modules/bar/index.d.ts
Expand Down