Skip to content

Commit

Permalink
dirty_reason.rs: put everything behind verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
dnbln committed Nov 25, 2022
1 parent ef581f3 commit 1966436
Show file tree
Hide file tree
Showing 19 changed files with 51 additions and 134 deletions.
1 change: 1 addition & 0 deletions crates/cargo-test-support/src/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ fn replace_dirty_msvc_impl(s: &str, is_msvc: bool) -> String {
fn replace_dirty_msvc(s: &str) -> String {
replace_dirty_msvc_impl(s, cfg!(target_env = "msvc"))
}

/// Normalizes text for both actual and expected strings on Windows.
fn normalize_windows(text: &str, cwd: Option<&Path>) -> String {
// Let's not deal with / vs \ (windows...)
Expand Down
50 changes: 25 additions & 25 deletions src/cargo/core/compiler/fingerprint/dirty_reason.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::*;
use crate::core::{Shell, Verbosity};
use crate::core::Shell;

use crate::Config;
use dissimilar::Chunk;
use itertools::Itertools;
use std::fmt;
Expand Down Expand Up @@ -76,8 +77,8 @@ pub enum DirtyReason {
}

impl DirtyReason {
pub fn presentation(&self) -> DirtyReasonPrettyPresentation<'_> {
DirtyReasonPrettyPresentation(self)
pub fn presentation<'a>(&'a self, unit: &'a Unit, config: &'a Config) -> DirtyReasonPrettyPresentation<'a> {
DirtyReasonPrettyPresentation(self, unit, config)
}
}

Expand Down Expand Up @@ -266,7 +267,7 @@ impl fmt::Display for DirtyReason {

impl std::error::Error for DirtyReason {}

pub struct DirtyReasonPrettyPresentation<'a>(&'a DirtyReason);
pub struct DirtyReasonPrettyPresentation<'a>(&'a DirtyReason, &'a Unit, &'a Config);

pub fn diffed(a: &str, b: &str) -> (String, String) {
let mut s1 = String::new();
Expand Down Expand Up @@ -415,7 +416,14 @@ impl<'a> DirtyReasonPrettyPresentation<'a> {
}
}

pub fn present_to(&self, s: &mut Shell, unit: &Unit, root: &Path) -> CargoResult<()> {
pub fn present_to(&self, s: &mut Shell) -> CargoResult<()> {
let unit = self.1;
let ws_roots = self.2.ws_roots.borrow();
let root: &Path = ws_roots
.values()
.next()
.map_or_else(|| self.2.cwd(), |it| it.inheritable().ws_root().as_ref());

match &self.0 {
DirtyReason::RustcChanged => s.dirty_because(unit, "the toolchain changed"),
DirtyReason::FeaturesChanged { old, new } => {
Expand Down Expand Up @@ -449,11 +457,9 @@ impl<'a> DirtyReasonPrettyPresentation<'a> {
}
DirtyReason::LocalLengthsChanged => {
s.dirty_because(unit, "the local lengths changed")?;
s.verbose(|s| {
s.note(
s.note(
"This could happen because of added/removed `cargo:rerun-if` instructions in the build script",
)
})?;
)?;

Ok(())
}
Expand Down Expand Up @@ -537,20 +543,14 @@ impl<'a> DirtyReasonPrettyPresentation<'a> {
..
} => {
let file = stale.strip_prefix(root).unwrap_or(&stale);
match s.verbosity() {
Verbosity::Verbose => s.dirty_because(
unit,
format!(
"the file `{}` has changed({})",
file.display(),
Self::after(*reference_mtime, *stale_mtime, "last build"),
),
),
_ => s.dirty_because(
unit,
format!("the file `{}` has changed", file.display()),
s.dirty_because(
unit,
format!(
"the file `{}` has changed ({})",
file.display(),
Self::after(*reference_mtime, *stale_mtime, "last build"),
),
}
)
}
StaleItem::ChangedEnv {
var,
Expand All @@ -571,14 +571,14 @@ impl<'a> DirtyReasonPrettyPresentation<'a> {
max_mtime,
..
} => {
let message = match (dep_mtime, s.verbosity()) {
(Some(dep_mtime), Verbosity::Verbose) => {
let message = match dep_mtime {
Some(dep_mtime) => {
format!(
"the dependency {name} was rebuilt ({})",
Self::after(*max_mtime, *dep_mtime, "last build"),
)
}
(None, _) | (Some(_), _) => {
None => {
format!("the dependency {name} was rebuilt")
}
};
Expand Down
15 changes: 4 additions & 11 deletions src/cargo/core/compiler/job_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ use crate::core::compiler::future_incompat::{
self, FutureBreakageItem, FutureIncompatReportPackage,
};
use crate::core::resolver::ResolveBehavior;
use crate::core::{PackageId, Shell, TargetKind, WorkspaceRootConfig};
use crate::core::{PackageId, Shell, TargetKind};
use crate::util::diagnostic_server::{self, DiagnosticPrinter};
use crate::util::errors::AlreadyPrintedError;
use crate::util::machine_message::{self, Message as _};
Expand Down Expand Up @@ -1370,16 +1370,9 @@ impl<'cfg> DrainState<'cfg> {
self.compiled.insert(unit.pkg.package_id());

if let Some(reason) = dirty_reason {
// FIXME: multiple workspace roots???
let roots = config.ws_roots.borrow();
let ws_root = roots.values().next().map_or(
config.cwd(), // FIXME: use a correct root when ws_roots is empty
|root: &WorkspaceRootConfig| root.inheritable().ws_root(),
);

reason
.presentation()
.present_to(&mut config.shell(), unit, ws_root)?;
config
.shell()
.verbose(|shell| reason.presentation(unit, config).present_to(shell))?;
}

if unit.mode.is_check() {
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2321,7 +2321,7 @@ fn calc_bin_artifact_fingerprint() {
.masquerade_as_nightly_cargo(&["bindeps"])
.with_stderr(
"\
[DIRTY] bar v0.5.0 ([CWD]/bar): the file `bar/src/main.rs` has changed([..])
[DIRTY] bar v0.5.0 ([CWD]/bar): the file `bar/src/main.rs` has changed ([..])
[COMPILING] bar v0.5.0 ([CWD]/bar)
[RUNNING] `rustc --crate-name bar [..]`
[DIRTY] foo v0.1.0 ([CWD]): the dependency bar was rebuilt
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2829,7 +2829,6 @@ fn rebuild_preserves_out_dir() {
foo.cargo("build")
.with_stderr(
"\
[DIRTY] foo v0.0.0 ([CWD]): the precalculated components changed
[COMPILING] foo v0.0.0 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
Expand Down
34 changes: 11 additions & 23 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,6 @@ fn testing_and_such() {
p.cargo("run")
.with_stderr(
"\
[DIRTY] foo v0.5.0 ([CWD]): the precalculated components changed
[COMPILING] foo v0.5.0 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `target/debug/foo[EXE]`
Expand Down Expand Up @@ -1724,7 +1723,7 @@ fn out_dir_is_preserved() {
p.cargo("build -v")
.with_stderr(
"\
[DIRTY] foo [..]: the file `build.rs` has changed([..])
[DIRTY] foo [..]: the file `build.rs` has changed ([..])
[COMPILING] foo [..]
[RUNNING] `rustc --crate-name build_script_build [..]
[RUNNING] `[..]/build-script-build`
Expand Down Expand Up @@ -3328,7 +3327,7 @@ fn rebuild_only_on_explicit_paths() {
p.cargo("build -v")
.with_stderr(
"\
[DIRTY] foo v0.5.0 ([..]): the file `foo` has changed([..])
[DIRTY] foo v0.5.0 ([..]): the file `foo` has changed ([..])
[COMPILING] foo v0.5.0 ([..])
[RUNNING] `[..]/build-script-build`
[RUNNING] `rustc [..] src/lib.rs [..]`
Expand Down Expand Up @@ -3367,7 +3366,7 @@ fn rebuild_only_on_explicit_paths() {
p.cargo("build -v")
.with_stderr(
"\
[DIRTY] foo v0.5.0 ([..]): the file `foo` has changed([..])
[DIRTY] foo v0.5.0 ([..]): the file `foo` has changed ([..])
[COMPILING] foo v0.5.0 ([..])
[RUNNING] `[..]/build-script-build`
[RUNNING] `rustc [..] src/lib.rs [..]`
Expand Down Expand Up @@ -4710,18 +4709,7 @@ fn rerun_if_directory() {
)
.build();

let dirty = |reason: &str| {
p.cargo("check")
.with_stderr(format!(
"\
[DIRTY] foo [..]: {reason}
[COMPILING] foo [..]
[FINISHED] [..]",
))
.run();
};

let dirty_first = || {
let dirty = || {
p.cargo("check")
.with_stderr(format!(
"\
Expand All @@ -4736,18 +4724,18 @@ fn rerun_if_directory() {
};

// Start with a missing directory.
dirty_first();
dirty();
// Because the directory doesn't exist, it will trigger a rebuild every time.
// https://github.com/rust-lang/cargo/issues/6003
dirty("the file `somedir` is missing");
dirty();

if is_coarse_mtime() {
sleep_ms(1000);
}

// Empty directory.
fs::create_dir(p.root().join("somedir")).unwrap();
dirty("the file `somedir` has changed");
dirty();
fresh();

if is_coarse_mtime() {
Expand All @@ -4757,7 +4745,7 @@ fn rerun_if_directory() {
// Add a file.
p.change_file("somedir/foo", "");
p.change_file("somedir/bar", "");
dirty("the file `somedir` has changed");
dirty();
fresh();

if is_coarse_mtime() {
Expand All @@ -4766,7 +4754,7 @@ fn rerun_if_directory() {

// Add a symlink.
p.symlink("foo", "somedir/link");
dirty("the file `somedir` has changed");
dirty();
fresh();

if is_coarse_mtime() {
Expand All @@ -4776,7 +4764,7 @@ fn rerun_if_directory() {
// Move the symlink.
fs::remove_file(p.root().join("somedir/link")).unwrap();
p.symlink("bar", "somedir/link");
dirty("the file `somedir` has changed");
dirty();
fresh();

if is_coarse_mtime() {
Expand All @@ -4785,7 +4773,7 @@ fn rerun_if_directory() {

// Remove a file.
fs::remove_file(p.root().join("somedir/foo")).unwrap();
dirty("the file `somedir` has changed");
dirty();
fresh();
}

Expand Down
5 changes: 0 additions & 5 deletions tests/testsuite/build_script_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ fn rerun_if_env_changes() {
.env("FOO", "bar")
.with_stderr(
"\
[DIRTY] foo v0.0.1 ([..]): the env variable FOO changed
[COMPILING] foo v0.0.1 ([..])
[FINISHED] [..]
",
Expand All @@ -40,7 +39,6 @@ fn rerun_if_env_changes() {
.env("FOO", "baz")
.with_stderr(
"\
[DIRTY] foo v0.0.1 ([..]): the env variable FOO changed
[COMPILING] foo v0.0.1 ([..])
[FINISHED] [..]
",
Expand All @@ -53,7 +51,6 @@ fn rerun_if_env_changes() {
p.cargo("build")
.with_stderr(
"\
[DIRTY] foo v0.0.1 ([..]): the env variable FOO changed
[COMPILING] foo v0.0.1 ([..])
[FINISHED] [..]
",
Expand Down Expand Up @@ -89,7 +86,6 @@ fn rerun_if_env_or_file_changes() {
.env("FOO", "bar")
.with_stderr(
"\
[DIRTY] foo v0.0.1 ([..]): the env variable FOO changed
[COMPILING] foo v0.0.1 ([..])
[FINISHED] [..]
",
Expand All @@ -105,7 +101,6 @@ fn rerun_if_env_or_file_changes() {
.env("FOO", "bar")
.with_stderr(
"\
[DIRTY] foo v0.0.1 ([..]): the file `foo` has changed
[COMPILING] foo v0.0.1 ([..])
[FINISHED] [..]
",
Expand Down
2 changes: 0 additions & 2 deletions tests/testsuite/build_script_extra_link_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ The package foo v0.0.1 ([ROOT]/foo) does not have a bin target.
.with_status(101)
.with_stderr(
"\
[DIRTY] foo [..]: the file `build.rs` has changed
[COMPILING] foo [..]
error: invalid instruction `cargo:rustc-link-arg-bin` from build script of `foo v0.0.1 ([ROOT]/foo)`
The package foo v0.0.1 ([ROOT]/foo) does not have a bin target with the name `abc`.
Expand All @@ -157,7 +156,6 @@ The package foo v0.0.1 ([ROOT]/foo) does not have a bin target with the name `ab
.with_status(101)
.with_stderr(
"\
[DIRTY] foo [..]: the file `build.rs` has changed
[COMPILING] foo [..]
error: invalid instruction `cargo:rustc-link-arg-bin=abc` from build script of `foo v0.0.1 ([ROOT]/foo)`
The instruction should have the form cargo:rustc-link-arg-bin=BIN=ARG
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/cache_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ fn clears_cache_after_fix() {
.with_stdout("")
.with_stderr(
"\
[DIRTY] foo [..]: the file `src/lib.rs` has changed
[CHECKING] foo [..]
[FINISHED] [..]
",
Expand Down
4 changes: 0 additions & 4 deletions tests/testsuite/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,7 @@ fn crates_io_then_directory() {
p.cargo("build")
.with_stderr(
"\
[DIRTY] bar v0.1.0: the path to the source changed
[COMPILING] bar v0.1.0
[DIRTY] foo v0.1.0 ([CWD]): the list of dependencies changed
[COMPILING] foo v0.1.0 ([CWD])
[FINISHED] [..]
",
Expand Down Expand Up @@ -603,9 +601,7 @@ fn git_lock_file_doesnt_change() {
p.cargo("build")
.with_stderr(
"\
[DIRTY] git v0.5.0 ([..]): the path to the source changed
[COMPILING] [..]
[DIRTY] foo v0.0.1 ([..]): the list of dependencies changed
[COMPILING] [..]
[FINISHED] [..]
",
Expand Down
2 changes: 0 additions & 2 deletions tests/testsuite/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ fn no_feature_doesnt_build() {
.with_stderr(
"\
[COMPILING] bar v0.0.1 ([CWD]/bar)
[DIRTY-MSVC] foo v0.0.1 ([CWD]): the list of features changed
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
Expand Down Expand Up @@ -542,7 +541,6 @@ fn default_feature_pulled_in() {
p.cargo("build --no-default-features")
.with_stderr(
"\
[DIRTY-MSVC] foo v0.0.1 ([CWD]): the list of features changed
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
Expand Down
2 changes: 0 additions & 2 deletions tests/testsuite/features_namespaced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ fn namespaced_same_name() {
[DOWNLOADING] crates ...
[DOWNLOADED] baz v0.1.0 [..]
[COMPILING] baz v0.1.0
[DIRTY-MSVC] foo v0.0.1 ([..]): the list of features changed
[COMPILING] foo v0.0.1 [..]
[FINISHED] [..]
[RUNNING] [..]
Expand Down Expand Up @@ -391,7 +390,6 @@ fn no_implicit_feature() {
[DOWNLOADED] lazy_static v1.0.0 [..]
[COMPILING] regex v1.0.0
[COMPILING] lazy_static v1.0.0
[DIRTY-MSVC] foo v0.1.0 ([..]): the list of features changed
[COMPILING] foo v0.1.0 [..]
[FINISHED] [..]
[RUNNING] `target/debug/foo[EXE]`
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,6 @@ fn doesnt_rebuild_dependencies() {
.with_stdout("")
.with_stderr(
"\
[DIRTY] foo v0.1.0 ([..]): forced
[CHECKING] foo v0.1.0 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
Expand Down
Loading

0 comments on commit 1966436

Please sign in to comment.