Skip to content

Commit 1cc7cc9

Browse files
authored
chore: pass project root to spinner (#11740)
1 parent 8dbe95e commit 1cc7cc9

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

crates/common/src/compile.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl ProjectCompiler {
194194
let quiet = self.quiet.unwrap_or(false);
195195
let bail = self.bail.unwrap_or(true);
196196

197-
let output = with_compilation_reporter(quiet, || {
197+
let output = with_compilation_reporter(quiet, Some(self.project_root.clone()), || {
198198
tracing::debug!("compiling project");
199199

200200
let timer = Instant::now();
@@ -559,13 +559,17 @@ pub fn etherscan_project(metadata: &Metadata, target_path: &Path) -> Result<Proj
559559
}
560560

561561
/// Configures the reporter and runs the given closure.
562-
pub fn with_compilation_reporter<O>(quiet: bool, f: impl FnOnce() -> O) -> O {
562+
pub fn with_compilation_reporter<O>(
563+
quiet: bool,
564+
project_root: Option<PathBuf>,
565+
f: impl FnOnce() -> O,
566+
) -> O {
563567
#[expect(clippy::collapsible_else_if)]
564568
let reporter = if quiet || shell::is_json() {
565569
Report::new(NoReporter::default())
566570
} else {
567571
if std::io::stdout().is_terminal() {
568-
Report::new(SpinnerReporter::spawn())
572+
Report::new(SpinnerReporter::spawn(project_root))
569573
} else {
570574
Report::new(BasicStdoutReporter::default())
571575
}

crates/common/src/term.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use foundry_compilers::{
33
artifacts::remappings::Remapping,
44
report::{self, BasicStdoutReporter, Reporter},
55
};
6-
use foundry_config::find_project_root;
76
use itertools::Itertools;
87
use semver::Version;
98
use std::{
@@ -94,6 +93,8 @@ impl Spinner {
9493
pub struct SpinnerReporter {
9594
/// The sender to the spinner thread.
9695
sender: mpsc::Sender<SpinnerMsg>,
96+
/// The project root path for trimming file paths in verbose output.
97+
project_root: Option<PathBuf>,
9798
}
9899

99100
impl SpinnerReporter {
@@ -102,7 +103,7 @@ impl SpinnerReporter {
102103
/// The spinner's message will be updated via the `reporter` events
103104
///
104105
/// On drop the channel will disconnect and the thread will terminate
105-
pub fn spawn() -> Self {
106+
pub fn spawn(project_root: Option<PathBuf>) -> Self {
106107
let (sender, rx) = mpsc::channel::<SpinnerMsg>();
107108

108109
std::thread::Builder::new()
@@ -130,7 +131,7 @@ impl SpinnerReporter {
130131
})
131132
.expect("failed to spawn thread");
132133

133-
Self { sender }
134+
Self { sender, project_root }
134135
}
135136

136137
fn send_msg(&self, msg: impl Into<String>) {
@@ -157,14 +158,12 @@ impl Reporter for SpinnerReporter {
157158
// Verbose message with dirty files displays first to avoid being overlapped
158159
// by the spinner in .tick() which prints repeatedly over the same line.
159160
if shell::verbosity() >= 5 {
160-
let project_root = find_project_root(None);
161-
162161
self.send_msg(format!(
163162
"Files to compile:\n{}",
164163
dirty_files
165164
.iter()
166165
.map(|path| {
167-
let trimmed_path = if let Ok(project_root) = &project_root {
166+
let trimmed_path = if let Some(project_root) = &self.project_root {
168167
path.strip_prefix(project_root).unwrap_or(path)
169168
} else {
170169
path
@@ -214,9 +213,9 @@ impl Reporter for SpinnerReporter {
214213
/// spinning cursor to display solc progress.
215214
///
216215
/// If no terminal is available this falls back to common `println!` in [`BasicStdoutReporter`].
217-
pub fn with_spinner_reporter<T>(f: impl FnOnce() -> T) -> T {
216+
pub fn with_spinner_reporter<T>(project_root: Option<PathBuf>, f: impl FnOnce() -> T) -> T {
218217
let reporter = if TERM_SETTINGS.indicate_progress {
219-
report::Report::new(SpinnerReporter::spawn())
218+
report::Report::new(SpinnerReporter::spawn(project_root))
220219
} else {
221220
report::Report::new(BasicStdoutReporter::default())
222221
};
@@ -240,7 +239,7 @@ mod tests {
240239

241240
#[test]
242241
fn can_format_properly() {
243-
let r = SpinnerReporter::spawn();
242+
let r = SpinnerReporter::spawn(None);
244243
let remappings: Vec<Remapping> = vec![
245244
"library/=library/src/".parse().unwrap(),
246245
"weird-erc20/=lib/weird-erc20/src/".parse().unwrap(),

crates/forge/src/cmd/flatten.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ impl FlattenArgs {
4545

4646
let target_path = dunce::canonicalize(target_path)?;
4747

48-
let flattener =
49-
with_compilation_reporter(true, || Flattener::new(project.clone(), &target_path));
48+
let flattener = with_compilation_reporter(true, Some(project.root().to_path_buf()), || {
49+
Flattener::new(project.clone(), &target_path)
50+
});
5051

5152
let flattened = match flattener {
5253
Ok(flattener) => Ok(flattener.flatten()),

0 commit comments

Comments
 (0)