Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read package/workspace config from Cargo manifest #799

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use trustfall_rustdoc::{load_rustdoc, VersionedCrate};
use rustdoc_cmd::RustdocCommand;
use std::collections::{BTreeMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Instant;

pub use config::GlobalConfig;
Expand Down Expand Up @@ -484,6 +485,11 @@ note: skipped the following crates since they have no library target: {skipped}"
);
}

let workspace_overrides =
manifest::deserialize_lint_table(metadata.workspace_metadata.clone())
.context("[workspace.metadata.cargo-semver-checks] table is incorrect")?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: word choice, here and below as well

Suggested change
.context("[workspace.metadata.cargo-semver-checks] table is incorrect")?
.context("[workspace.metadata.cargo-semver-checks] table is invalid")?

.map(Arc::new);

selected
.iter()
.map(|selected| {
Expand All @@ -506,6 +512,23 @@ note: skipped the following crates since they have no library target: {skipped}"
})?;
Ok((crate_name.clone(), None))
} else {
let package_overrides =
manifest::deserialize_lint_table(selected.metadata.clone())
.with_context(|| {
format!(
"{} [package.metadata.cargo-semver-checks] table is incorrect",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea: do we by any chance know the path to the Cargo.toml file here? If it isn't too difficult, it might be useful to not just say which crate had the invalid table but also give a path to it.

This is because in some cases, projects may contain more than one crate by the same name — for example, see #462. In such cases, knowing the Cargo.toml path would be very helpful to the user.

If it's too hard to do now, we can just open a C-enhancement issue for the idea and tackle it another time.

selected.name
)
})?;

let mut overrides = OverrideStack::new();
if let Some(workspace) = &workspace_overrides {
overrides.push(Arc::clone(workspace));
}
if let Some(package) = package_overrides {
overrides.push(Arc::new(package));
}

let start = std::time::Instant::now();
let (current_crate, baseline_crate) = generate_versioned_crates(
config,
Expand Down
10 changes: 10 additions & 0 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ impl From<OverrideConfig> for QueryOverride {
}
}

/// Helper function to deserialize an optional lint table from a [`serde_json::Value`]
/// into a [`OverrideMap`]. Returns an `Err` if the `cargo-semver-checks` table is present
/// but invalid. Returns `Ok(None)` if the table is not present.
pub(crate) fn deserialize_lint_table(
metadata: serde_json::Value,
) -> anyhow::Result<Option<OverrideMap>> {
let table: Option<LintTable> = serde_json::from_value(metadata)?;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to avoid the .clone() when calling this function by making it accept &Value and then using the trick described in this post: https://users.rust-lang.org/t/serde-from-value-moves-my-value-but-i-want-i-returned-to-me-on-err/69621

Ok(table.and_then(LintTable::into_overrides))
}

#[cfg(test)]
mod tests {
use crate::{manifest::OverrideConfig, QueryOverride};
Expand Down
Loading