diff --git a/packages/nx/src/native/workspace/context.rs b/packages/nx/src/native/workspace/context.rs index 1a6412793fc48..75d35000b9d9f 100644 --- a/packages/nx/src/native/workspace/context.rs +++ b/packages/nx/src/native/workspace/context.rs @@ -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; @@ -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) { @@ -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"); @@ -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 })