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

Fix false-positives from target-specific optional features #375

Merged
merged 5 commits into from
Feb 21, 2023
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
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
- run-on-rust-libp2p
- run-on-libp2p-dcutr-relay
- run-on-libp2p-gossipsub-request-response
- run-on-libp2p-uds
- run-on-core-graphics
- run-on-bevy-core
- run-on-bevy-gltf
Expand Down Expand Up @@ -235,6 +236,43 @@ jobs:
- name: Run semver-checks on libp2p-request-response
run: cargo run semver-checks check-release --manifest-path="subject/protocols/request-response/Cargo.toml"

run-on-libp2p-uds:
# Run cargo-semver-checks on a crate that has dependencies in a custom target
# /~https://github.com/obi1kenobi/cargo-semver-checks/issues/369
name: Run cargo-semver-checks on libp2p-uds 0.37.0
runs-on: ubuntu-latest
steps:
- name: Checkout cargo-semver-checks
uses: actions/checkout@v3
with:
persist-credentials: false

- name: Checkout rust-libp2p
uses: actions/checkout@v3
with:
persist-credentials: false
repository: 'libp2p/rust-libp2p'
ref: '5b4eab7bafe6cba8fea6c806027ea680662b7cc6'
path: 'subject'

- name: Install rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
override: true

# rust-libp2p requires protobuf-compiler.
- name: Install protobuf-compiler
run: sudo apt install protobuf-compiler

- uses: Swatinem/rust-cache@v2
with:
key: libp2p-gossipsub-request-response

- name: Run semver-checks on libp2p-uds
run: cargo run semver-checks check-release --manifest-path="subject/transports/uds/Cargo.toml"

run-on-core-graphics:
# Run cargo-semver-checks on a crate with no semver violations,
# to make sure there are no false-positives.
Expand Down
22 changes: 15 additions & 7 deletions src/rustdoc_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,21 @@ impl<'a> CrateSource<'a> {
.filter_map(|dep| dep.is_optional().then_some(dep.name()))
.map(|x| x.to_string())
.collect(),
Self::ManifestPath { manifest } => manifest
.parsed
.dependencies
.iter()
.filter_map(|(name, dep)| dep.optional().then_some(name))
.map(|x| x.to_string())
.collect(),
Self::ManifestPath { manifest } => {
let mut dependencies = manifest.parsed.dependencies.clone();
for target in manifest.parsed.target.values() {
// Fixes /~https://github.com/obi1kenobi/cargo-semver-checks/issues/369
// This part is not relevant to `Self::Registry`, because
// it doesn't have a `target` field and doesn't differentiate dependencies
// between different targets.
dependencies.extend(target.dependencies.clone().into_iter());
}
dependencies
.iter()
.filter_map(|(name, dep)| dep.optional().then_some(name))
.map(|x| x.to_string())
.collect()
}
};

let feature_defns: Vec<&String> = match self {
Expand Down
8 changes: 8 additions & 0 deletions test_crates/target-specific-dependencies/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
publish = false
name = "target-specific-dependencies"
version = "0.1.0"
edition = "2021"

[target.none.dependencies]
async-std = { version = "1.6.2", optional = true }
7 changes: 7 additions & 0 deletions test_crates/target-specific-dependencies/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
// This crate checks whether the tool correctly detects
// implicit features defined by target-specific dependencies.
// /~https://github.com/obi1kenobi/cargo-semver-checks/issues/369
#[cfg(not(feature = "async-std"))]
panic!("the tool should have built the project with this flag");
}
13 changes: 13 additions & 0 deletions tests/detects_target_dependencies.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use assert_cmd::Command;

#[test]
fn detects_target_dependencies() {
// This test checks whether the tool correctly detects
// implicit features defined by target-specific dependencies.
// /~https://github.com/obi1kenobi/cargo-semver-checks/issues/369
let mut cmd = Command::cargo_bin("cargo-semver-checks").unwrap();
cmd.current_dir("test_crates/target-specific-dependencies")
.args(["semver-checks", "check-release", "--baseline-root=."])
.assert()
.success();
}