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

Fix non-deterministic builds between project directories #8869

Merged
merged 13 commits into from
Mar 15, 2023
20 changes: 20 additions & 0 deletions packages/bundlers/default/src/DefaultBundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,8 @@ function createIdealGraph(
}
}

let modifiedSourceBundles = new Set();

// Step Remove Shared Bundles: Remove shared bundles from bundle groups that hit the parallel request limit.
for (let bundleGroupId of bundleGraph.getNodeIdsConnectedFrom(rootNodeId)) {
// Find shared bundles in this bundle group.
Expand Down Expand Up @@ -1081,6 +1083,7 @@ function createIdealGraph(
for (let sourceBundleId of sourceBundles) {
let sourceBundle = nullthrows(bundleGraph.getNode(sourceBundleId));
invariant(sourceBundle !== 'root');
modifiedSourceBundles.add(sourceBundle);
bundleToRemove.sourceBundles.delete(sourceBundleId);
for (let asset of bundleToRemove.assets) {
sourceBundle.assets.add(asset);
Expand Down Expand Up @@ -1119,6 +1122,23 @@ function createIdealGraph(
}
}
}

// Fix asset order in source bundles as they are likely now incorrect after shared bundle deletion
if (modifiedSourceBundles.size > 0) {
let assetOrderMap = new Map(assets.map(a => [a, assets.indexOf(a)]));
mattcompiles marked this conversation as resolved.
Show resolved Hide resolved

for (let bundle of modifiedSourceBundles) {
bundle.assets = new Set(
[...bundle.assets].sort((a, b) => {
let aIndex = nullthrows(assetOrderMap.get(a));
let bIndex = nullthrows(assetOrderMap.get(b));

return aIndex - bIndex;
}),
);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Wonder if you could use BitSet for bundle.assets here? Then when you do bundle.assets.add it would just be setting a bit corresponding to the position within assets. When we take the ideal graph and convert it back to the legacy graph we could iterate in asset order (as BitSet#values() does).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Funny you mention this because I considered that approach as well but was worried this iteration cost might not be worth it. You've inspired me to try it though. Will report back.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@devongovett Just realised it'd be hard to use the BitSet here as we need to create bundles before we know the full list of assets as we add them during the traverse.

Copy link
Member

Choose a reason for hiding this comment

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

You could create the bitset earlier maybe

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could, but I'd have to add a second full asset traverse to collect the list of all assets before starting the main traverse. Unless there's some other cheap way to get the list of all assets?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After migrating all asset sets to BitSets I actually saw a drop in perf. This is due to the values() extraction being too expensive. I think there's potential to use BitSet more widely in the bundler but it would require a larger rethink to optimise around its benefits.

tl;dr let's just sort for now.


function deleteBundle(bundleRoot: BundleRoot) {
bundleGraph.removeNode(nullthrows(bundles.get(bundleRoot.id)));
bundleRoots.delete(bundleRoot);
Expand Down
26 changes: 16 additions & 10 deletions packages/transformers/js/core/src/dependency_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ impl<'a> DependencyCollector<'a> {
}
_ => Some(format!(
"{:x}",
hash!(format!("{}:{}:{}", self.config.filename, specifier, kind))
hash!(format!(
"{}:{}:{}",
self.get_project_relative_filename(),
specifier,
kind
))
)),
};

Expand Down Expand Up @@ -1245,21 +1250,22 @@ impl<'a> DependencyCollector<'a> {
}
}

fn get_import_meta_url(&mut self) -> ast::Expr {
use ast::*;

// Get a relative path from the project root.
let filename = if let Some(relative) =
pathdiff::diff_paths(&self.config.filename, &self.config.project_root)
{
fn get_project_relative_filename(&self) -> String {
if let Some(relative) = pathdiff::diff_paths(&self.config.filename, &self.config.project_root) {
relative.to_slash_lossy()
} else if let Some(filename) = Path::new(&self.config.filename).file_name() {
String::from(filename.to_string_lossy())
} else {
String::from("unknown.js")
};
}
}

fn get_import_meta_url(&mut self) -> ast::Expr {
use ast::*;

Expr::Lit(Lit::Str(format!("file:///{}", filename).into()))
Expr::Lit(Lit::Str(
format!("file:///{}", self.get_project_relative_filename()).into(),
))
}

fn get_import_meta(&mut self) -> ast::Expr {
Expand Down