Skip to content

Commit

Permalink
fix(core): sort projects after updating from context (#28024)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
Whenever a file is created in a project, the daemon will insert the
newly created file at the end of the project file map list. This causes
issues with cache determinism because the newly created file is not
sorted. When doing `nx reset`, the file will then be properly sorted
## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
Whenever a file is inserted into the project map, sort the changed file
map.
## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
Cammisuli authored Sep 23, 2024
1 parent 05f16cf commit 341306a
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions packages/nx/src/native/workspace/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::sync::Arc;
Expand Down Expand Up @@ -278,11 +278,14 @@ impl WorkspaceContext {
"adding {} updated files to project files",
updated_files.len()
);

let mut updated_projects = HashSet::<&str>::new();
for updated_file in updated_files.into_iter() {
let file = updated_file.0;
let hash = updated_file.1;
if let Some(project_files) = find_project_for_path(&file, &project_root_mappings)
.and_then(|project| project_files_map.get_mut(project))
let project = find_project_for_path(&file, &project_root_mappings);
if let Some(project_files) =
project.and_then(|project| project_files_map.get_mut(project))
{
trace!("{file:?} was found in a project");
if let Some(file) = project_files.iter_mut().find(|f| f.file == file) {
Expand All @@ -291,6 +294,7 @@ impl WorkspaceContext {
} else {
trace!("{file:?} was not part of a project, adding to project files");
project_files.push(FileData { file, hash });
updated_projects.insert(project.expect("Project already exists"));
}
} else {
trace!("{file:?} was not found in any project, updating global files");
Expand Down Expand Up @@ -321,6 +325,21 @@ impl WorkspaceContext {
}
}

// sort the updated projects after deletion
// projects that have deleted files were not added to `updated_projects` set because deletion doesnt change the determinism
// but if there were any files deleted from projects, the sort should be faster becaues there potentially could be less files to sort
for updated_project in updated_projects {
trace!(updated_project, "sorting updated project");
if let Some(project_files) = project_files_map.get_mut(updated_project) {
// if the project files are less than 500, then parallel sort has too much overhead to actually be faster
if cfg!(target_arch = "wasm32") || project_files.len() < 500 {
project_files.sort();
} else {
project_files.par_sort();
}
}
}

let non_project_files = global_files
.into_iter()
.map(|(file, hash)| FileData { file, hash })
Expand Down

0 comments on commit 341306a

Please sign in to comment.