Skip to content

Commit

Permalink
Fix non-deterministic builds between project directories (#8869)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcompiles authored Mar 15, 2023
1 parent 2cd398c commit 41ed29f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
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, index) => [a, index]));

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;
}),
);
}
}

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

0 comments on commit 41ed29f

Please sign in to comment.