chore(core): format rust code (#30954)

## Current Behavior
Rust formatting isn't checked in CI

## Expected Behavior
Rust formatting is checked in CI

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
This commit is contained in:
Craigory Coppola 2025-05-01 08:49:50 -04:00 committed by GitHub
parent d9cb931116
commit 981dd94671
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
23 changed files with 143 additions and 117 deletions

View File

@ -94,7 +94,7 @@ jobs:
pnpm nx run-many -t check-imports check-commit check-lock-files check-codeowners --parallel=1 --no-dte &
pids+=($!)
pnpm nx affected --targets=lint,test,build,e2e,e2e-ci &
pnpm nx affected --targets=lint,test,build,e2e,e2e-ci,format-native &
pids+=($!)
for pid in "${pids[@]}"; do

View File

@ -2,3 +2,4 @@ pnpm check-lock-files
pnpm check-commit
pnpm documentation
pnpm pretty-quick --check
pnpm nx format-native nx

View File

@ -139,6 +139,19 @@
"parallel": false
}
},
"test-native": {}
"test-native": {},
"format-native": {
"command": "cargo fmt",
"cache": true,
"options": {
"cwd": "{projectRoot}/src/native",
"args": ["--all", "--check"]
},
"configurations": {
"fix": {
"args": ["--all"]
}
}
}
}
}

View File

@ -1,9 +1,9 @@
use super::contains_glob_pattern;
use crate::native::glob::glob_group::GlobGroup;
use crate::native::glob::glob_parser::parse_glob;
use itertools::Either::{Left, Right};
use itertools::Itertools;
use std::collections::HashSet;
use itertools::Either::{Left, Right};
use super::contains_glob_pattern;
#[derive(Debug)]
enum GlobType {
@ -120,15 +120,13 @@ pub fn partition_glob(glob: &str) -> anyhow::Result<(String, Vec<String>)> {
let (leading_dir_segments, pattern_segments): (Vec<String>, _) = groups
.into_iter()
.filter(|group| !group.is_empty())
.partition_map(|group| {
match &group[0] {
GlobGroup::NonSpecial(value) if !contains_glob_pattern(&value) && !has_patterns => {
Left(value.to_string())
}
_ => {
has_patterns = true;
Right(group)
}
.partition_map(|group| match &group[0] {
GlobGroup::NonSpecial(value) if !contains_glob_pattern(&value) && !has_patterns => {
Left(value.to_string())
}
_ => {
has_patterns = true;
Right(group)
}
});
@ -267,7 +265,8 @@ mod test {
#[test]
fn should_partition_glob_with_leading_dirs() {
let (leading_dirs, globs) = super::partition_glob("dist/app/**/!(README|LICENSE).(js|ts)").unwrap();
let (leading_dirs, globs) =
super::partition_glob("dist/app/**/!(README|LICENSE).(js|ts)").unwrap();
assert_eq!(leading_dirs, "dist/app");
assert_eq!(globs, ["!**/{README,LICENSE}.{js,ts}", "**/*.{js,ts}",]);
}

View File

@ -9,12 +9,16 @@ pub fn hash(content: &[u8]) -> String {
#[napi]
pub fn hash_array(input: Vec<Option<String>>) -> String {
let joined = input.iter().filter_map(|s| {
if s.is_none() {
trace!("Encountered None value in hash_array input: {:?}", input);
}
s.as_deref()
}).collect::<Vec<_>>().join(",");
let joined = input
.iter()
.filter_map(|s| {
if s.is_none() {
trace!("Encountered None value in hash_array input: {:?}", input);
}
s.as_deref()
})
.collect::<Vec<_>>()
.join(",");
let content = joined.as_bytes();
hash(content)
}
@ -41,7 +45,7 @@ pub fn hash_file_path<P: AsRef<Path>>(path: P) -> Option<String> {
#[cfg(test)]
mod tests {
use crate::native::hasher::{hash_file, hash_array};
use crate::native::hasher::{hash_array, hash_file};
use assert_fs::prelude::*;
use assert_fs::TempDir;

View File

@ -8,4 +8,3 @@ use crate::native::project_graph::types::ProjectGraph;
pub fn transfer_project_graph(project_graph: ProjectGraph) -> External<ProjectGraph> {
External::new(project_graph)
}

View File

@ -32,14 +32,8 @@ impl RustPseudoTerminal {
quiet: Option<bool>,
tty: Option<bool>,
) -> napi::Result<ChildProcess> {
self.pseudo_terminal.run_command(
command,
command_dir,
js_env,
exec_argv,
quiet,
tty,
)
self.pseudo_terminal
.run_command(command, command_dir, js_env, exec_argv, quiet, tty)
}
/// This allows us to run a pseudoterminal with a fake node ipc channel

View File

@ -33,14 +33,8 @@ impl RustPseudoTerminal {
quiet: Option<bool>,
tty: Option<bool>,
) -> napi::Result<ChildProcess> {
self.pseudo_terminal.run_command(
command,
command_dir,
js_env,
exec_argv,
quiet,
tty,
)
self.pseudo_terminal
.run_command(command, command_dir, js_env, exec_argv, quiet, tty)
}
/// This allows us to run a pseudoterminal with a fake node ipc channel

View File

@ -117,7 +117,9 @@ impl PseudoTerminal {
let mut content = String::from_utf8_lossy(&buf[0..len]).to_string();
if content.contains("\x1B[6n") {
trace!("Prevented terminal escape sequence ESC[6n from being printed.");
trace!(
"Prevented terminal escape sequence ESC[6n from being printed."
);
content = content.replace("\x1B[6n", "");
}

View File

@ -4,8 +4,8 @@ mod hash_project_config;
mod hash_project_files;
mod hash_runtime;
mod hash_task_output;
mod hash_workspace_files;
mod hash_tsconfig;
mod hash_workspace_files;
pub use hash_env::*;
pub use hash_external::*;
@ -13,5 +13,5 @@ pub use hash_project_config::*;
pub use hash_project_files::*;
pub use hash_runtime::*;
pub use hash_task_output::*;
pub use hash_tsconfig::*;
pub use hash_workspace_files::*;
pub use hash_tsconfig::*;

View File

@ -158,12 +158,15 @@ mod tests {
let hash_result = hash_project_files(proj_name, proj_root, file_sets, &file_map).unwrap();
assert_eq!(
hash_result,
hash(&[
file_data1.hash.as_bytes(),
file_data1.file.as_bytes(),
file_data3.hash.as_bytes(),
file_data3.file.as_bytes()
].concat())
hash(
&[
file_data1.hash.as_bytes(),
file_data1.file.as_bytes(),
file_data3.hash.as_bytes(),
file_data3.file.as_bytes()
]
.concat()
)
);
}
@ -205,12 +208,15 @@ mod tests {
let hash_result = hash_project_files(proj_name, proj_root, file_sets, &file_map).unwrap();
assert_eq!(
hash_result,
hash(&[
file_data1.hash.as_bytes(),
file_data1.file.as_bytes(),
file_data3.hash.as_bytes(),
file_data3.file.as_bytes(),
].concat())
hash(
&[
file_data1.hash.as_bytes(),
file_data1.file.as_bytes(),
file_data3.hash.as_bytes(),
file_data3.file.as_bytes(),
]
.concat()
)
);
}
}

View File

@ -1,9 +1,9 @@
use std::path::Path;
use crate::native::cache::expand_outputs::get_files_for_outputs;
use crate::native::glob::build_glob_set;
use crate::native::hasher::{hash_array, hash_file};
use anyhow::*;
use rayon::prelude::*;
use std::path::Path;
use tracing::trace;
pub fn hash_task_output(workspace_root: &str, glob: &str, outputs: &[String]) -> Result<String> {
@ -14,7 +14,15 @@ pub fn hash_task_output(workspace_root: &str, glob: &str, outputs: &[String]) ->
let hashes = output_files
.into_par_iter()
.filter(|file| glob.is_match(file))
.filter_map(|file| hash_file(Path::new(workspace_root).join(file).to_str().expect("path contains invalid utf-8").to_owned()))
.filter_map(|file| {
hash_file(
Path::new(workspace_root)
.join(file)
.to_str()
.expect("path contains invalid utf-8")
.to_owned(),
)
})
.collect::<Vec<_>>();
Ok(hash_array(hashes.into_iter().map(Some).collect()))
}

View File

@ -9,6 +9,6 @@ mod utils;
#[cfg(not(target_arch = "wasm32"))]
pub mod details;
#[cfg(not(target_arch = "wasm32"))]
pub mod task_history;
#[cfg(not(target_arch = "wasm32"))]
pub mod running_tasks_service;
#[cfg(not(target_arch = "wasm32"))]
pub mod task_history;

View File

@ -99,10 +99,12 @@ impl CountdownPopup {
pub fn render(&mut self, f: &mut Frame<'_>, area: Rect) {
// Add a safety check to prevent rendering outside buffer bounds (this can happen if the user resizes the window a lot before it stabilizes it seems)
if area.height == 0 || area.width == 0 ||
area.x >= f.area().width ||
area.y >= f.area().height {
return; // Area is out of bounds, don't try to render
if area.height == 0
|| area.width == 0
|| area.x >= f.area().width
|| area.y >= f.area().height
{
return; // Area is out of bounds, don't try to render
}
// Ensure area is entirely within frame bounds
@ -112,7 +114,7 @@ impl CountdownPopup {
width: area.width.min(f.area().width.saturating_sub(area.x)),
height: area.height.min(f.area().height.saturating_sub(area.y)),
};
let popup_height = 9;
let popup_width = 70;

View File

@ -27,10 +27,12 @@ impl HelpText {
pub fn render(&self, f: &mut Frame<'_>, area: Rect) {
// Add a safety check to prevent rendering outside buffer bounds (this can happen if the user resizes the window a lot before it stabilizes it seems)
if area.height == 0 || area.width == 0 ||
area.x >= f.area().width ||
area.y >= f.area().height {
return; // Area is out of bounds, don't try to render
if area.height == 0
|| area.width == 0
|| area.x >= f.area().width
|| area.y >= f.area().height
{
return; // Area is out of bounds, don't try to render
}
// Ensure area is entirely within frame bounds

View File

@ -195,7 +195,7 @@ impl LayoutManager {
terminal_panes: Vec::new(),
};
}
match self.task_list_visibility {
TaskListVisibility::Hidden => self.calculate_layout_hidden_task_list(area),
TaskListVisibility::Visible => self.calculate_layout_visible_task_list(area),
@ -252,19 +252,15 @@ impl LayoutManager {
}
// Prevent divide-by-zero
let task_list_height = if area.height < 3 {
1
} else {
area.height / 3
};
let task_list_height = if area.height < 3 { 1 } else { area.height / 3 };
// Apply padding only if there's enough space
let padding_height = if area.height > task_list_height + self.vertical_padding {
self.vertical_padding
} else {
0
};
// Ensure terminal pane has at least 1 row
let available_height = area.height.saturating_sub(task_list_height);
let terminal_pane_height = available_height.saturating_sub(padding_height);
@ -311,19 +307,15 @@ impl LayoutManager {
}
// Prevent divide-by-zero
let task_list_width = if area.width < 3 {
1
} else {
area.width / 3
};
let task_list_width = if area.width < 3 { 1 } else { area.width / 3 };
// Apply padding only if there's enough space
let padding_width = if area.width > task_list_width + self.horizontal_padding {
self.horizontal_padding
} else {
0
};
// Ensure terminal pane has at least 1 column
let available_width = area.width.saturating_sub(task_list_width);
let terminal_pane_width = available_width.saturating_sub(padding_width);
@ -440,7 +432,10 @@ mod tests {
fn test_default_properties() {
let layout_manager = LayoutManager::new(5);
assert_eq!(layout_manager.get_pane_arrangement(), PaneArrangement::None);
assert_eq!(layout_manager.get_task_list_visibility(), TaskListVisibility::Visible);
assert_eq!(
layout_manager.get_task_list_visibility(),
TaskListVisibility::Visible
);
assert_eq!(layout_manager.get_task_count(), 5);
}
@ -689,43 +684,43 @@ mod tests {
fn test_padding_between_components() {
let mut layout_manager = LayoutManager::new(5);
let area = create_test_area(100, 60);
// Test with default horizontal padding (2)
layout_manager.set_mode(LayoutMode::Horizontal);
layout_manager.set_task_list_visibility(TaskListVisibility::Visible);
layout_manager.set_pane_arrangement(PaneArrangement::Single);
let layout = layout_manager.calculate_layout(area);
let task_list = layout.task_list.unwrap();
let terminal_pane = layout.terminal_panes[0];
// The gap between task list and terminal pane should equal the horizontal padding
assert_eq!(terminal_pane.x - (task_list.x + task_list.width), 2);
// Test with increased horizontal padding
layout_manager.set_horizontal_padding(3);
let layout = layout_manager.calculate_layout(area);
let task_list = layout.task_list.unwrap();
let terminal_pane = layout.terminal_panes[0];
// The gap should now be 3
assert_eq!(terminal_pane.x - (task_list.x + task_list.width), 3);
// Test with vertical layout and default vertical padding (1)
layout_manager.set_mode(LayoutMode::Vertical);
let layout = layout_manager.calculate_layout(area);
let task_list = layout.task_list.unwrap();
let terminal_pane = layout.terminal_panes[0];
// The gap should be vertical and equal to the vertical padding
assert_eq!(terminal_pane.y - (task_list.y + task_list.height), 1);
// Test with increased vertical padding
layout_manager.set_vertical_padding(2);
let layout = layout_manager.calculate_layout(area);
let task_list = layout.task_list.unwrap();
let terminal_pane = layout.terminal_panes[0];
// The gap should now be 2
assert_eq!(terminal_pane.y - (task_list.y + task_list.height), 2);
}

View File

@ -297,7 +297,7 @@ impl<'a> StatefulWidget for TerminalPane<'a> {
width: area.width.min(buf.area().width.saturating_sub(area.x)),
height: area.height.min(buf.area().height.saturating_sub(area.y)),
};
if safe_area.width > 0 && safe_area.height > 0 {
// Only attempt to render if we have a valid area
let text = "...";
@ -308,7 +308,7 @@ impl<'a> StatefulWidget for TerminalPane<'a> {
}
return;
}
// Ensure the area doesn't extend beyond buffer boundaries
let safe_area = Rect {
x: area.x,
@ -316,7 +316,7 @@ impl<'a> StatefulWidget for TerminalPane<'a> {
width: area.width.min(buf.area().width.saturating_sub(area.x)),
height: area.height.min(buf.area().height.saturating_sub(area.y)),
};
let base_style = self.get_base_style(state.task_status);
let border_style = if state.is_focused {
base_style

View File

@ -2,7 +2,7 @@ use parking_lot::{Condvar, Mutex, MutexGuard};
pub struct NxMutex<T>(Mutex<T>);
impl <T> NxMutex<T> {
impl<T> NxMutex<T> {
pub fn new(value: T) -> Self {
Self(Mutex::new(value))
}
@ -18,9 +18,13 @@ impl NxCondvar {
Self(Condvar::new())
}
pub fn wait<'a, T, F>(&'a self, mut guard: MutexGuard<'a, T>, condition: F) -> anyhow::Result<MutexGuard<'a, T>>
where
F: Fn(&MutexGuard<'a, T>) -> bool
pub fn wait<'a, T, F>(
&'a self,
mut guard: MutexGuard<'a, T>,
condition: F,
) -> anyhow::Result<MutexGuard<'a, T>>
where
F: Fn(&MutexGuard<'a, T>) -> bool,
{
if condition(&guard) {
self.0.wait(&mut guard);

View File

@ -2,7 +2,7 @@ use std::sync::{Condvar, LockResult, Mutex, MutexGuard};
pub struct NxMutex<T>(Mutex<T>);
impl <T> NxMutex<T> {
impl<T> NxMutex<T> {
pub fn new(value: T) -> Self {
Self(Mutex::new(value))
}
@ -18,9 +18,13 @@ impl NxCondvar {
Self(Condvar::new())
}
pub fn wait<'a, T, F>(&self, mutex_guard: MutexGuard<'a, T>, condition: F) -> LockResult<MutexGuard<'a, T>>
pub fn wait<'a, T, F>(
&self,
mutex_guard: MutexGuard<'a, T>,
condition: F,
) -> LockResult<MutexGuard<'a, T>>
where
F: Fn(&mut T) -> bool
F: Fn(&mut T) -> bool,
{
self.0.wait_while(mutex_guard, condition)
}

View File

@ -16,4 +16,4 @@ pub fn is_ci() -> bool {
|| env::var("BUILD_ID").is_ok()
|| env::var("BUILD_BUILDID").is_ok()
|| env::var("TEAMCITY_VERSION").is_ok()
}
}

View File

@ -1,4 +1,4 @@
use crate::native::{utils::normalize_trait::Normalize, types::FileData};
use crate::native::{types::FileData, utils::normalize_trait::Normalize};
use std::path::{Path, PathBuf};
impl Normalize for Path {
@ -65,13 +65,9 @@ mod test {
FileData {
file: "foo-other/not-child".into(),
hash: "123".into(),
}
},
];
let child_files = get_child_files(&directory, files);
assert_eq!(child_files, [
"foo/bar",
"foo/baz",
"foo/child/bar",
]);
assert_eq!(child_files, ["foo/bar", "foo/baz", "foo/child/bar",]);
}
}
}

View File

@ -50,14 +50,13 @@ where
!ignore_glob_set.is_match(path.as_ref())
})
.filter_map(move |entry| {
entry
.ok()
.and_then(|e|
e.path()
.strip_prefix(&base_dir).ok()
entry.ok().and_then(|e| {
e.path()
.strip_prefix(&base_dir)
.ok()
.filter(|p| !p.to_string_lossy().is_empty())
.map(|p| p.to_owned())
)
})
})
}

View File

@ -61,7 +61,11 @@ fn hash_files(files: Vec<NxFile>) -> Vec<(String, NxFileHashed)> {
})
.collect::<Vec<_>>()
} else {
trace!("hashing workspace files in {} chunks of {}", num_parallelism, chunks);
trace!(
"hashing workspace files in {} chunks of {}",
num_parallelism,
chunks
);
files
.par_chunks(chunks)
.flat_map_iter(|chunks| {