Skip to content

Commit

Permalink
Ensure metadata of self-referential crates can be read. (#1035)
Browse files Browse the repository at this point in the history
Also ensure that if reading metadata fails, we don't crash and instead
just avoid checking manifest-related lints.
  • Loading branch information
obi1kenobi authored Dec 10, 2024
1 parent 2b67091 commit fe144fc
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 85 deletions.
134 changes: 55 additions & 79 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ exclude = [".github/", "brand/", "scripts/", "test_crates/", "test_outputs/", "t
trustfall = "0.8.0"
# `cargo_metadata` is used at the API boundary of `trustfall_rustdoc`,
# so ensure the version we use for `cargo_metadata` here matches what `trustfall_rustdoc` uses too.
trustfall_rustdoc = { version = "0.18.1", default-features = false, features = ["v32", "v33", "v35", "v36", "v37", "rayon", "rustc-hash"] }
cargo_metadata = "0.18.1"
trustfall_rustdoc = { version = "0.19.0", default-features = false, features = ["v32", "v33", "v35", "v36", "v37", "rayon", "rustc-hash"] }
cargo_metadata = "0.19.1"
# End of dependency block

clap = { version = "4.5.21", features = ["derive", "cargo"] }
Expand Down
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,15 @@ impl Scope {
/// semver-check rlib, dylib, and staticlib targets as well.
fn is_lib_like_checkable_target(target: &cargo_metadata::Target) -> bool {
target.is_lib()
|| target
.kind
.iter()
.any(|kind| matches!(kind.as_str(), "rlib" | "dylib" | "staticlib"))
|| target.kind.iter().any(|kind| {
matches!(
kind,
cargo_metadata::TargetKind::RLib { .. }
| cargo_metadata::TargetKind::DyLib { .. }
| cargo_metadata::TargetKind::CDyLib { .. }
| cargo_metadata::TargetKind::StaticLib { .. }
)
})
}

impl Check {
Expand Down
23 changes: 23 additions & 0 deletions src/snapshot_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,29 @@ fn multiple_ambiguous_package_name_definitions() {
);
}

/// Ensure that linting self-referential packages (usually used for the "SemVer trick")
/// works properly and doesn't suffer any issues due to the self-referential metadata.
///
/// More info on the "SemVer trick": https://github.com/dtolnay/semver-trick
///
/// This test currently triggers the `struct_missing` lint as a false-positive,
/// due to the cross-crate re-export. This should be fixed when we start supporting
/// cross-crate items.
#[test]
fn semver_trick_self_referential() {
assert_integration_test(
"semver_trick_self_referential",
&[
"cargo",
"semver-checks",
"--baseline-root",
"test_crates/semver_trick_self_referential/old/",
"--manifest-path",
"test_crates/semver_trick_self_referential/new/",
],
);
}

/// Helper function which lists all files in the directory recursively.
///
/// # Arguments
Expand Down
7 changes: 7 additions & 0 deletions test_crates/semver_trick_self_referential/future/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "semver_trick_self_referential"
version = "0.2.0"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub struct Example;
10 changes: 10 additions & 0 deletions test_crates/semver_trick_self_referential/new/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
publish = false
name = "semver_trick_self_referential"
version = "0.1.1"
edition = "2021"

[dependencies]
# The "SemVer trick": reference a future major version of the package, and re-export its types
# within the same *minor* version to offer a smoother upgrade process between major versions.
self_ref = { package = "semver_trick_self_referential", path = "../future", version = "0.2.0" }
1 change: 1 addition & 0 deletions test_crates/semver_trick_self_referential/new/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub use self_ref::Example;
7 changes: 7 additions & 0 deletions test_crates/semver_trick_self_referential/old/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "semver_trick_self_referential"
version = "0.1.0"
edition = "2021"

[dependencies]
1 change: 1 addition & 0 deletions test_crates/semver_trick_self_referential/old/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub struct Example;
14 changes: 14 additions & 0 deletions test_outputs/query_execution/struct_missing.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/query.rs
expression: "&query_execution_results"
snapshot_kind: text
---
{
"./test_crates/move_item_and_reexport/": [
Expand Down Expand Up @@ -73,6 +74,19 @@ expression: "&query_execution_results"
"visibility_limit": String("public"),
},
],
"./test_crates/semver_trick_self_referential/": [
{
"name": String("Example"),
"path": List([
String("semver_trick_self_referential"),
String("Example"),
]),
"span_begin_line": Uint64(1),
"span_filename": String("src/lib.rs"),
"struct_type": String("unit"),
"visibility_limit": String("public"),
},
],
"./test_crates/struct_missing/": [
{
"name": String("WillBeRemovedStruct"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
source: src/snapshot_tests.rs
expression: check
snapshot_kind: text
---
Check(
scope: Scope(
mode: DenyList(PackageSelection(
selection: DefaultMembers,
excluded_packages: [],
)),
),
current: Rustdoc(
source: Root("test_crates/semver_trick_self_referential/new/"),
),
baseline: Rustdoc(
source: Root("test_crates/semver_trick_self_referential/old/"),
),
release_type: None,
current_feature_config: FeatureConfig(
features_group: Heuristic,
extra_features: [],
is_baseline: false,
),
baseline_feature_config: FeatureConfig(
features_group: Heuristic,
extra_features: [],
is_baseline: true,
),
build_target: None,
witness_generation: WitnessGeneration(
show_hints: false,
witness_directory: None,
),
)
Loading

0 comments on commit fe144fc

Please sign in to comment.