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

For duplicate source files of the same package, make one redirect to the other #16274

Merged
19 commits merged into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 13 additions & 13 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ namespace ts {
// Maps from a SourceFile's `.path` to the name of the package it was imported with.
let sourceFileToPackageName = createMap<string>();
// See `sourceFileIsRedirectedTo`.
let isSourceFileTargetOfRedirect = createMap<true>();
let redirectTargetsSet = createMap<true>();

const filesByName = createMap<SourceFile | undefined>();
// stores 'filename -> file association' ignoring case
Expand Down Expand Up @@ -558,7 +558,7 @@ namespace ts {
dropDiagnosticsProducingTypeChecker,
getSourceFileFromReference,
sourceFileToPackageName,
isSourceFileTargetOfRedirect,
redirectTargetsSet,
};

verifyCompilerOptions();
Expand Down Expand Up @@ -797,12 +797,12 @@ namespace ts {
if (oldSourceFile.redirect) {
// We got `newSourceFile` by path, so it is actually for the underlying file.
// This lets us know if the underlying file has changed. If it has we should break the redirect.
if (newSourceFile !== oldSourceFile.redirect.underlying) {
if (newSourceFile !== oldSourceFile.redirect.unredirected) {
// Underlying file has changed. Might not redirect anymore. Must rebuild program.
return oldProgram.structureIsReused = StructureIsReused.Not;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SafeModules isn't conservative enough?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, because we haven't set the dirty bit on the affected projects? Couldn't we? We're watching the files anyway, aren't we?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, we use StructureIsReused.Not in situations where files are added or removed, which would include the case of redirects being broken or created.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per our offline discussion, I agree that this is an appropriate way to handle changes.

}
}
else if (oldProgram.isSourceFileTargetOfRedirect.has(oldSourceFile.path)) {
else if (oldProgram.redirectTargetsSet.has(oldSourceFile.path)) {
// This is similar to the above case. If a redirected-to source file changes, the redirect may be broken.
if (newSourceFile !== oldSourceFile) {
return oldProgram.structureIsReused = StructureIsReused.Not;
Expand Down Expand Up @@ -941,7 +941,7 @@ namespace ts {
resolvedTypeReferenceDirectives = oldProgram.getResolvedTypeReferenceDirectives();

sourceFileToPackageName = oldProgram.sourceFileToPackageName;
isSourceFileTargetOfRedirect = oldProgram.isSourceFileTargetOfRedirect;
redirectTargetsSet = oldProgram.redirectTargetsSet;

return oldProgram.structureIsReused = StructureIsReused.Completely;
}
Expand Down Expand Up @@ -1597,19 +1597,19 @@ namespace ts {
}
}

function createRedirectSourceFile(redirectTo: SourceFile, underlying: SourceFile, fileName: string, path: Path): SourceFile {
const redirect: SourceFile = Object.create(redirectTo);
function createRedirectSourceFile(redirectTarget: SourceFile, unredirected: SourceFile, fileName: string, path: Path): SourceFile {
const redirect: SourceFile = Object.create(redirectTarget);
redirect.fileName = fileName;
redirect.path = path;
redirect.redirect = { redirectTo, underlying };
redirect.redirect = { redirectTarget, unredirected };
Object.defineProperties(redirect, {
id: {
get(this: SourceFile) { return this.redirect.redirectTo.id; },
set(this: SourceFile, value: SourceFile["id"]) { this.redirect.redirectTo.id = value; },
get(this: SourceFile) { return this.redirect.redirectTarget.id; },
set(this: SourceFile, value: SourceFile["id"]) { this.redirect.redirectTarget.id = value; },
},
symbol: {
get(this: SourceFile) { return this.redirect.redirectTo.symbol; },
set(this: SourceFile, value: SourceFile["symbol"]) { this.redirect.redirectTo.symbol = value; },
get(this: SourceFile) { return this.redirect.redirectTarget.symbol; },
set(this: SourceFile, value: SourceFile["symbol"]) { this.redirect.redirectTarget.symbol = value; },
},
});
return redirect;
Expand Down Expand Up @@ -1666,7 +1666,7 @@ namespace ts {
// Some other SourceFile already exists with this package name and version.
// Instead of creating a duplicate, just redirect to the existing one.
const dupFile = createRedirectSourceFile(fileFromPackageId, file, fileName, path);
isSourceFileTargetOfRedirect.set(fileFromPackageId.path, true);
redirectTargetsSet.set(fileFromPackageId.path, true);
filesByName.set(path, dupFile);
sourceFileToPackageName.set(path, packageId.name);
files.push(dupFile);
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2330,12 +2330,12 @@ namespace ts {
*/
/* @internal */ redirect?: {
/** Source file this redirects to. */
readonly redirectTo: SourceFile,
readonly redirectTarget: SourceFile,
/**
* Source file for the duplicate package. This will not be used by the Program,
* but we need to keep this around so we can watch for changes in underlying.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to update the comments that still refer to "underlying".

*/
readonly underlying: SourceFile,
readonly unredirected: SourceFile,
} | undefined;

amdDependencies: AmdDependency[];
Expand Down Expand Up @@ -2511,8 +2511,8 @@ namespace ts {

/** Given a source file, get the name of the package it was imported from. */
/* @internal */ sourceFileToPackageName: Map<string>;
/** True if some other source file redirects to this one. */
/* @internal */ isSourceFileTargetOfRedirect: Map<true>;
/** Set of all source files that some other source file redirects to. */
/* @internal */ redirectTargetsSet: Map<true>;
}

/* @internal */
Expand Down
2 changes: 1 addition & 1 deletion src/harness/unittests/reuseProgramStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace ts {
const files = arrayToMap(texts, t => t.name, t => {
if (oldProgram) {
let oldFile = <SourceFileWithText>oldProgram.getSourceFile(t.name);
if (oldFile && oldFile.redirect) oldFile = oldFile.redirect.underlying;
if (oldFile && oldFile.redirect) oldFile = oldFile.redirect.unredirected;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use braces.

if (oldFile && oldFile.sourceText.getVersion() === t.text.getVersion()) {
return oldFile;
}
Expand Down