|
| 1 | +//! The `cargo report timings` command. |
| 2 | +
|
| 3 | +use std::ffi::OsStr; |
| 4 | +use std::fs::File; |
| 5 | +use std::path::PathBuf; |
| 6 | + |
| 7 | +use annotate_snippets::Level; |
| 8 | +use anyhow::Context as _; |
| 9 | + |
| 10 | +use crate::AlreadyPrintedError; |
| 11 | +use crate::CargoResult; |
| 12 | +use crate::GlobalContext; |
| 13 | +use crate::core::Workspace; |
| 14 | +use crate::util::BuildLogger; |
| 15 | +use crate::util::important_paths::find_root_manifest_for_wd; |
| 16 | +use crate::util::logger::RunId; |
| 17 | + |
| 18 | +pub struct ReportTimingsOptions<'gctx> { |
| 19 | + /// Whether to attempt to open the browser after the report is generated |
| 20 | + pub open_result: bool, |
| 21 | + pub gctx: &'gctx GlobalContext, |
| 22 | +} |
| 23 | + |
| 24 | +pub fn report_timings(gctx: &GlobalContext, _opts: ReportTimingsOptions<'_>) -> CargoResult<()> { |
| 25 | + let ws = find_root_manifest_for_wd(gctx.cwd()) |
| 26 | + .ok() |
| 27 | + .and_then(|manifest_path| Workspace::new(&manifest_path, gctx).ok()); |
| 28 | + let Some((log, _run_id)) = select_log_file(gctx, ws.as_ref())? else { |
| 29 | + let title_extra = if let Some(ws) = ws { |
| 30 | + format!(" for workspace at `{}`", ws.root().display()) |
| 31 | + } else { |
| 32 | + String::new() |
| 33 | + }; |
| 34 | + let title = format!("no build log files found{title_extra}"); |
| 35 | + let note = "run command with `-Z build-analysis` to generate log files"; |
| 36 | + let report = [Level::ERROR |
| 37 | + .primary_title(title) |
| 38 | + .element(Level::NOTE.message(note))]; |
| 39 | + gctx.shell().print_report(&report, false)?; |
| 40 | + return Err(AlreadyPrintedError::new(anyhow::anyhow!("")).into()); |
| 41 | + }; |
| 42 | + |
| 43 | + let _f = File::open(&log) |
| 44 | + .with_context(|| format!("failed to analyze log at `{}`", log.display()))?; |
| 45 | + |
| 46 | + Ok(()) |
| 47 | +} |
| 48 | + |
| 49 | +/// Selects the appropriate log file. |
| 50 | +/// |
| 51 | +/// Currently look at the newest log file for the workspace. |
| 52 | +/// If not in a workspace, pick the newest log file in the log directory. |
| 53 | +fn select_log_file( |
| 54 | + gctx: &GlobalContext, |
| 55 | + ws: Option<&Workspace<'_>>, |
| 56 | +) -> CargoResult<Option<(PathBuf, RunId)>> { |
| 57 | + let log_dir = gctx.home().join("log"); |
| 58 | + let log_dir = log_dir.as_path_unlocked(); |
| 59 | + |
| 60 | + if !log_dir.exists() { |
| 61 | + return Ok(None); |
| 62 | + } |
| 63 | + |
| 64 | + // Gets the latest log files in the log directory |
| 65 | + let mut walk = walkdir::WalkDir::new(log_dir) |
| 66 | + .follow_links(true) |
| 67 | + .sort_by(|a, b| a.file_name().cmp(b.file_name()).reverse()) |
| 68 | + .min_depth(1) |
| 69 | + .max_depth(1) |
| 70 | + .into_iter() |
| 71 | + .filter_map(|entry| { |
| 72 | + let entry = entry.ok()?; |
| 73 | + let path = entry.path(); |
| 74 | + |
| 75 | + // We only accept JSONL/NDJSON files. |
| 76 | + if !entry.file_type().is_file() { |
| 77 | + return None; |
| 78 | + } |
| 79 | + if entry.path().extension() != Some(OsStr::new("jsonl")) { |
| 80 | + return None; |
| 81 | + } |
| 82 | + |
| 83 | + // ...and the file name must follow RunId format |
| 84 | + let run_id = path.file_stem()?.to_str()?.parse::<RunId>().ok()?; |
| 85 | + Some((entry, run_id)) |
| 86 | + }); |
| 87 | + |
| 88 | + let item = if let Some(ws) = ws { |
| 89 | + // If we are under a workspace, find only that workspace's log files. |
| 90 | + let ws_run_id = BuildLogger::generate_run_id(ws); |
| 91 | + walk.skip_while(|(_, run_id)| !run_id.same_workspace(&ws_run_id)) |
| 92 | + .next() |
| 93 | + } else { |
| 94 | + walk.next() |
| 95 | + }; |
| 96 | + |
| 97 | + Ok(item.map(|(entry, run_id)| (entry.into_path(), run_id))) |
| 98 | +} |
0 commit comments