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

Mark previously deferred assets as dirty for symbol prop #9369

Merged
merged 5 commits into from
Nov 12, 2023
Merged
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: 7 additions & 0 deletions packages/core/core/src/requests/AssetGraphRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,13 @@ export class AssetGraphBuilder {
if (!previouslyDeferred && childNode.deferred) {
this.assetGraph.markParentsWithHasDeferred(childNodeId);
} else if (previouslyDeferred && !childNode.deferred) {
// Mark Asset and Dependency as dirty for symbol propagation as it was
// previously deferred and it's used symbols may have changed
this.changedAssetsPropagation.add(node.id);
node.usedSymbolsDownDirty = true;
this.changedAssetsPropagation.add(childNode.id);
childNode.usedSymbolsDownDirty = true;

this.assetGraph.unmarkParentsWithHasDeferred(childNodeId);
}

Expand Down
84 changes: 83 additions & 1 deletion packages/core/integration-tests/test/lazy-compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,23 @@ import {
assertBundles,
removeDistDirectory,
run,
fsFixture,
overlayFS,
} from '@parcel/test-utils';

const findBundle = (bundleGraph, nameRegex) => {
return bundleGraph.getBundles().find(b => nameRegex.test(b.name));
const result = bundleGraph.getBundles().find(b => nameRegex.test(b.name));

if (!result) {
throw new Error(
`Couldn't find bundle matching '${nameRegex}'. Available bundles are ${bundleGraph
?.getBundles()
.map(b => b.name)
.join(', ')}`,
);
}

return result;
};

const distDirIncludes = async matches => {
Expand Down Expand Up @@ -261,4 +274,73 @@ describe('lazy compile', function () {
]);
subscription.unsubscribe();
});

it('should lazy compile properly when an assets needs to be re-visited in symbol propagation', async () => {
await fsFixture(overlayFS, __dirname)`
lazy-compile-import-symbols
index.js:
import { shared, async } from './main';
export default Promise.all([shared(), async()]);

main.js:
export function shared() {
return import('./shared');
}
export function async() {
return import('./async');
}

async.js:
// Trigger more deps so a full 'propagateSymbolsUp' pass is executed
Copy link
Member

Choose a reason for hiding this comment

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

Do you know why this only happens if the full pass runs? Feels odd to me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are other circumstances that can cause this to occur outside of a full pass, however this was the easiest way to reflect in a test. The reason is fails though is because the full pass only applies to propagateSymbolsUp. However, in this case there are nodes that are not re-visited by propagateSymbolsDown and contain stale data.

import './a';
import './b';
import './c';
import { other } from './shared';
export default other;

shared.js:
export const main = 'main';
export const other = 'other';

a.js:
sideEffectNoop();
b.js:
sideEffectNoop();
c.js:
sideEffectNoop();
`;

const b = await bundler(
path.join(__dirname, 'lazy-compile-import-symbols/index.js'),
{
shouldBuildLazily: true,
mode: 'development',
shouldContentHash: false,
inputFS: overlayFS,
},
);

await removeDistDirectory();

const subscription = await b.watch();
let result = await getNextBuild(b);
result = await result.requestBundle(
findBundle(result.bundleGraph, /^index\.js/),
);
result = await result.requestBundle(
findBundle(result.bundleGraph, /^async\./),
);

let output = await run(result.bundleGraph);
assert.deepEqual(await output.default, [
{
main: 'main',
other: 'other',
},
{
default: 'other',
},
]);
subscription.unsubscribe();
});
});