-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): optimize daemon output glob matching (#27775)
<!-- 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 - Nonexistant outputs are considered globs - Glob outputs are walked from workspace root, which is slow on large repos [NX Daemon Server] - 2024-09-06T19:44:39.674Z - Done responding to the client outputsHashesMatch [NX Daemon Server] - 2024-09-06T19:44:39.674Z - Handled OUTPUTS_HASHES_MATCH. Handling time: 94. Response time: 0. [NX Daemon Server] - 2024-09-06T19:44:39.775Z - [REQUEST]: Responding to the client. recordOutputsHash [NX Daemon Server] - 2024-09-06T19:44:39.775Z - Done responding to the client recordOutputsHash [NX Daemon Server] - 2024-09-06T19:44:39.775Z - Handled RECORD_OUTPUTS_HASH. Handling time: 100. Response time: 0. [NX Daemon Server] - 2024-09-06T19:44:39.818Z - [REQUEST]: Responding to the client. PROCESS_IN_BACKGROUND [NX Daemon Server] - 2024-09-06T19:44:39.818Z - Done responding to the client PROCESS_IN_BACKGROUND [NX Daemon Server] - 2024-09-06T19:44:39.818Z - Handled PROCESS_IN_BACKGROUND. Handling time: 14. Response time: 0. ## Expected Behavior - Nonexistant outputs are only globs if they should be - Globs are a bit faster [NX Daemon Server] - 2024-09-06T19:43:36.899Z - Handled OUTPUTS_HASHES_MATCH. Handling time: 0. Response time: 0. [NX Daemon Server] - 2024-09-06T19:43:36.900Z - [REQUEST]: Responding to the client. recordOutputsHash [NX Daemon Server] - 2024-09-06T19:43:36.900Z - Done responding to the client recordOutputsHash [NX Daemon Server] - 2024-09-06T19:43:36.900Z - Handled RECORD_OUTPUTS_HASH. Handling time: 0. Response time: 0. [NX Daemon Server] - 2024-09-06T19:43:36.944Z - [REQUEST]: Responding to the client. PROCESS_IN_BACKGROUND [NX Daemon Server] - 2024-09-06T19:43:36.944Z - Done responding to the client PROCESS_IN_BACKGROUND [NX Daemon Server] - 2024-09-06T19:43:36.944Z - Handled PROCESS_IN_BACKGROUND. Handling time: 13. Response time: 0. [NX Daemon Server] - 2024-09-06T19:43:36.949Z - Uploading file artifacts [NX Daemon Server] - 2024-09-06T19:43:36.949Z - Done uploading file artifacts > Note timings are from Nx repo, close enough to be comparable. No real improvement was expected here, mainly checking that things didn't get worse. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes # --------- Co-authored-by: FrozenPandaz <[email protected]>
- Loading branch information
1 parent
e71e2f3
commit ac9da41
Showing
17 changed files
with
314 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod cache; | ||
pub mod expand_outputs; | ||
pub mod file_ops; | ||
pub mod cache; | ||
pub mod validate_outputs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use itertools::Itertools; | ||
use regex::Regex; | ||
|
||
use crate::native::glob::{contains_glob_pattern, glob_transform::partition_glob}; | ||
|
||
const ALLOWED_WORKSPACE_ROOT_OUTPUT_PREFIXES: [&str; 2] = ["!{workspaceRoot}", "{workspaceRoot}"]; | ||
|
||
fn is_missing_prefix(output: &str) -> bool { | ||
let re = Regex::new(r"^!?\{[\s\S]+\}").expect("Output pattern regex should compile"); | ||
|
||
!re.is_match(output) | ||
} | ||
|
||
#[napi] | ||
pub fn validate_outputs(outputs: Vec<String>) -> anyhow::Result<()> { | ||
let outputs_len = outputs.len(); | ||
let mut missing_prefix = Vec::with_capacity(outputs_len); | ||
let mut workspace_globs = Vec::with_capacity(outputs_len); | ||
|
||
for output in outputs.iter() { | ||
if is_missing_prefix(output) { | ||
missing_prefix.push(output); | ||
} else { | ||
for prefix in ALLOWED_WORKSPACE_ROOT_OUTPUT_PREFIXES.iter() { | ||
if let Some(trimmed) = output.strip_prefix(prefix) { | ||
if contains_glob_pattern(&trimmed) { | ||
let (root, _) = partition_glob(&trimmed)?; | ||
if root.is_empty() { | ||
workspace_globs.push(output); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if workspace_globs.is_empty() && missing_prefix.is_empty() { | ||
return Ok(()); | ||
} | ||
|
||
let mut error_message = String::new(); | ||
if !missing_prefix.is_empty() { | ||
error_message.push_str(&format!( | ||
"The following outputs are invalid: \n - {}\n\nRun `nx repair` to fix this.", | ||
missing_prefix.iter().join("\n - ") | ||
)); | ||
} | ||
if !workspace_globs.is_empty() { | ||
error_message.push_str(&format!( | ||
"The following outputs are defined by a glob pattern from the workspace root: \n - {}\n\nThese can be slow, replace them with a more specific pattern.", | ||
workspace_globs.iter().join("\n - ") | ||
)); | ||
} | ||
|
||
Err(anyhow::anyhow!(error_message)) | ||
} | ||
|
||
#[napi] | ||
pub fn get_transformable_outputs(outputs: Vec<String>) -> Vec<String> { | ||
outputs | ||
.into_iter() | ||
.filter(|output| is_missing_prefix(output)) | ||
.collect() | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::is_missing_prefix; | ||
|
||
#[test] | ||
fn test_is_missing_prefix() { | ||
assert!(is_missing_prefix("dist")); | ||
assert!(is_missing_prefix("!dist")); | ||
assert!(!is_missing_prefix("{workspaceRoot}/dist")); | ||
assert!(!is_missing_prefix("!{workspaceRoot}/dist")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.