Skip to content

Commit

Permalink
Load lib target name from cargo metadata. (#433)
Browse files Browse the repository at this point in the history
* Load lib target name from `cargo metadata`.

* Clean up test cases.

* Reproduce the `perf-event-open-sys2` crash in CI.

* Restore old behavior around bin targets.

* Support proc macro crates too.
  • Loading branch information
obi1kenobi authored Apr 21, 2023
1 parent 962a22e commit 1dda3d4
Show file tree
Hide file tree
Showing 28 changed files with 342 additions and 353 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
- run-on-ref-slice-fork-windows
- run-on-tokio-explicit
- run-on-tokio-implicit
- run-on-perf-event-open-sys2
if: ${{ success() || failure() }} # Run this job even if a dependency has failed.
steps:
- name: Job outcomes
Expand All @@ -58,6 +59,7 @@ jobs:
echo "run-on-ref-slice-fork-windows: ${{ needs.run-on-ref-slice-fork-windows.result }}"
echo "run-on-tokio-explicit: ${{ needs.run-on-tokio-explicit.result }}"
echo "run-on-tokio-implicit: ${{ needs.run-on-tokio-implicit.result }}"
echo "run-on-perf-event-open-sys2: ${{ needs.run-on-perf-event-open-sys2.result }}"
# Fail this required job if any of its dependent jobs have failed.
#
Expand Down Expand Up @@ -96,6 +98,8 @@ jobs:
run: exit 1
- if: ${{ needs.run-on-tokio-implicit.result != 'success' }}
run: exit 1
- if: ${{ needs.run-on-perf-event-open-sys2.result != 'success' }}
run: exit 1

lint:
name: Check lint and rustfmt
Expand Down Expand Up @@ -1319,6 +1323,66 @@ jobs:
key: ${{ runner.os }}-${{ steps.toolchain.outputs.cachekey }}-${{ hashFiles('semver\**\Cargo.lock') }}-${{ github.job }}-rustdoc
path: subject\target\semver-checks\cache

run-on-perf-event-open-sys2:
# cargo-semver-checks failed to find the generated rustdoc JSON
# because the crate used a lib name different from the crate name.
# https://github.com/obi1kenobi/cargo-semver-checks/issues/432
name: 'Semver: perf-event-open-sys2 v5.0.0'
runs-on: ubuntu-latest
needs:
- build-binary
steps:
- name: Checkout cargo-semver-checks
uses: actions/checkout@v3
with:
persist-credentials: false
path: 'semver'

- name: Checkout bevy
uses: actions/checkout@v3
with:
persist-credentials: false
repository: 'phantomical/perf-event'
ref: '47dc882dbfb44b86584540a6e4f2815caaf72c10'
path: 'subject'

- name: Install rust
id: toolchain
uses: dtolnay/rust-toolchain@stable

- name: Restore binary
id: cache-binary
uses: actions/cache/restore@v3
with:
path: bins/
key: bins-${{ runner.os }}-${{ github.run_id }}-${{ github.run_attempt }}
fail-on-cache-miss: true

- name: Restore rustdoc
id: cache
uses: actions/cache/restore@v3
with:
key: ${{ runner.os }}-${{ steps.toolchain.outputs.cachekey }}-${{ hashFiles('semver/**/Cargo.lock') }}-${{ github.job }}-rustdoc
path: subject/target/semver-checks/cache

- name: Restore cargo index and rustdoc target dir
uses: Swatinem/rust-cache@v2
with:
workspaces: |
subject/target/semver-checks/local-perf-event-open-sys2-5_0_0
- name: Run semver-checks
run: |
cd semver
../bins/cargo-semver-checks semver-checks check-release --manifest-path="../subject/perf-event-open-sys/Cargo.toml" --baseline-version 5.0.0 --verbose
- name: Save rustdoc
uses: actions/cache/save@v3
if: steps.cache.outputs.cache-hit != 'true'
with:
key: ${{ runner.os }}-${{ steps.toolchain.outputs.cachekey }}-${{ hashFiles('semver/**/Cargo.lock') }}-${{ github.job }}-rustdoc
path: subject/target/semver-checks/cache

init-release:
name: Run the release workflow
needs:
Expand Down
26 changes: 0 additions & 26 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,6 @@ pub(crate) fn get_package_version(manifest: &Manifest) -> anyhow::Result<&str> {
Ok(version)
}

pub(crate) fn get_lib_target_name(manifest: &Manifest) -> anyhow::Result<String> {
// If there's a [lib] section, return the name it specifies, if any.
if let Some(product) = &manifest.parsed.lib {
if let Some(lib_name) = &product.name {
return Ok(lib_name.clone());
}
}

// Otherwise, assume the crate is a lib crate with the default lib target name:
// the same name as the package but with dashes replaced with underscores.
Ok(get_package_name(manifest)?.replace('-', "_"))
}

pub(crate) fn get_first_bin_target_name(manifest: &Manifest) -> anyhow::Result<String> {
// If there's a [[bin]] section, return the first item's name.
if let Some(product) = manifest.parsed.bin.first() {
if let Some(bin_name) = &product.name {
return Ok(bin_name.clone());
}
}

// Otherwise, assume the crate is a bin crate with the default bin target name:
// the same name as the package but with dashes replaced with underscores.
Ok(get_package_name(manifest)?.replace('-', "_"))
}

pub(crate) fn get_project_dir_from_manifest_path(
manifest_path: &std::path::Path,
) -> anyhow::Result<std::path::PathBuf> {
Expand Down
2 changes: 1 addition & 1 deletion src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ mod tests {
);

let mut actual_paths = actual_results[0]["path"]
.as_vec(|val| val.as_vec(FieldValue::as_str))
.as_vec_with(|val| val.as_vec_with(FieldValue::as_str))
.expect("not a Vec<Vec<&str>>");
actual_paths.sort_unstable();

Expand Down
Loading

0 comments on commit 1dda3d4

Please # to comment.