Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .ignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ src/etc/man
!src/doc/src/commands/index.md
!src/doc/src/commands/manifest-commands.md
!src/doc/src/commands/package-commands.md
!src/doc/src/commands/publishg-commands.md
!src/doc/src/commands/publishing-commands.md

# Snapshots of HTML reports and log files are just too large
tests/testsuite/**/*.jsonl
tests/testsuite/**/*.html
35 changes: 35 additions & 0 deletions crates/cargo-test-support/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,41 @@ pub fn cargo_home() -> PathBuf {
home().join(".cargo")
}

/// Path to the current test's `$CARGO_LOG`
///
/// ex: `$CARGO_TARGET_TMPDIR/cit/t0/home/.cargo/log`
pub fn log_dir() -> PathBuf {
cargo_home().join("log")
}

/// Path to the current test's `$CARGO_LOG` file
///
/// ex: `$CARGO_TARGET_TMPDIR/cit/t0/home/.cargo/log/<id>.jsonl`
///
/// This also asserts the number of log files is exactly the same as `idx + 1`.
pub fn log_file(idx: usize) -> PathBuf {
let log_dir = log_dir();

let entries = std::fs::read_dir(&log_dir).unwrap();
let mut log_files: Vec<_> = entries
.filter_map(Result::ok)
.filter(|e| e.path().extension().and_then(|s| s.to_str()) == Some("jsonl"))
.collect();

// Sort them to get chronological order
log_files.sort_unstable_by(|a, b| a.file_name().to_str().cmp(&b.file_name().to_str()));

assert_eq!(
idx + 1,
log_files.len(),
"unexpected number of log files: {}, expected {}",
log_files.len(),
idx + 1
);

log_files[idx].path()
}

/// Common path and file operations
pub trait CargoPathExt {
fn to_url(&self) -> url::Url;
Expand Down
32 changes: 31 additions & 1 deletion src/bin/cargo/commands/report.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::command_prelude::*;
use cargo::core::compiler::future_incompat::{OnDiskReports, REPORT_PREAMBLE};

use cargo::CargoResult;
use cargo::core::compiler::future_incompat::OnDiskReports;
use cargo::core::compiler::future_incompat::REPORT_PREAMBLE;
use cargo::drop_println;
use cargo::ops;

pub fn cli() -> Command {
subcommand("report")
Expand All @@ -23,11 +27,28 @@ pub fn cli() -> Command {
)
.arg_package("Package to display a report for"),
)
.subcommand(
subcommand("timings")
.about("Reports the build timings of previous builds (unstable)")
.arg(flag("open", "Opens the timing report in a browser")),
)
}

pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
match args.subcommand() {
Some(("future-incompatibilities", args)) => report_future_incompatibilities(gctx, args),
Some(("timings", args)) => {
gctx.cli_unstable().fail_if_stable_command(
gctx,
"report timings",
15844,
"build-analysis",
gctx.cli_unstable().build_analysis,
)?;
let opts = timings_opts(gctx, args)?;
ops::report_timings(gctx, opts)?;
Ok(())
}
Some((cmd, _)) => {
unreachable!("unexpected command {}", cmd)
}
Expand All @@ -49,3 +70,12 @@ fn report_future_incompatibilities(gctx: &GlobalContext, args: &ArgMatches) -> C
drop(gctx.shell().print_ansi_stdout(report.as_bytes()));
Ok(())
}

fn timings_opts<'a>(
gctx: &'a GlobalContext,
args: &ArgMatches,
) -> CargoResult<ops::ReportTimingsOptions<'a>> {
let open_result = args.get_flag("open");

Ok(ops::ReportTimingsOptions { open_result, gctx })
}
45 changes: 3 additions & 42 deletions src/cargo/ops/cargo_doc.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use crate::core::Workspace;
use crate::core::compiler::{Compilation, CompileKind};
use crate::core::{Shell, Workspace, shell::Verbosity};
use crate::core::shell::Verbosity;
use crate::ops;
use crate::util;
use crate::util::CargoResult;
use crate::util::context::{GlobalContext, PathAndArgs};

use anyhow::{Error, bail};
use cargo_util::ProcessBuilder;

use std::ffi::OsString;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::str::FromStr;

/// Format of rustdoc [`--output-format`][1].
Expand Down Expand Up @@ -75,14 +73,7 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
let path = path_by_output_format(&compilation, &kind, &name, &options.output_format);

if path.exists() {
let config_browser = {
let cfg: Option<PathAndArgs> = ws.gctx().get("doc.browser")?;
cfg.map(|path_args| (path_args.path.resolve_program(ws.gctx()), path_args.args))
};
let mut shell = ws.gctx().shell();
let link = shell.err_file_hyperlink(&path);
shell.status("Opening", format!("{link}{}{link:#}", path.display()))?;
open_docs(&path, &mut shell, config_browser, ws.gctx())?;
util::open::open(&path, ws.gctx())?;
}
} else if ws.gctx().shell().verbosity() == Verbosity::Verbose {
for name in &compilation.root_crate_names {
Expand Down Expand Up @@ -216,33 +207,3 @@ fn path_by_output_format(
.join("index.html")
}
}

fn open_docs(
path: &Path,
shell: &mut Shell,
config_browser: Option<(PathBuf, Vec<String>)>,
gctx: &GlobalContext,
) -> CargoResult<()> {
let browser =
config_browser.or_else(|| Some((PathBuf::from(gctx.get_env_os("BROWSER")?), Vec::new())));

match browser {
Some((browser, initial_args)) => {
if let Err(e) = Command::new(&browser).args(initial_args).arg(path).status() {
shell.warn(format!(
"Couldn't open docs with {}: {}",
browser.to_string_lossy(),
e
))?;
}
}
None => {
if let Err(e) = opener::open(&path) {
let e = e.into();
crate::display_warning_with_error("couldn't open docs", &e, shell);
}
}
};

Ok(())
}
1 change: 1 addition & 0 deletions src/cargo/ops/cargo_report/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod timings;
Loading