Skip to content

Commit e6bed4d

Browse files
authored
Support linting breaking changes in manifests & add feature_missing lint (#1007)
Add support for linting of package manifests, allowing us to scan for breaking changes there as well. For example, deleting a feature is a major breaking change. As of this PR, we can detect and report that: ``` --- failure feature_missing: package feature removed or renamed --- Description: A feature has been removed from this package's Cargo.toml. This will break downstream crates which enable that feature. ref: https://doc.rust-lang.org/cargo/reference/semver.html#cargo-feature-remove impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.37.0/src/lints/feature_missing.ron Failed in: feature going_missing in the package's Cargo.toml feature rand in the package's Cargo.toml Summary semver requires new major version: 1 major and 0 minor checks failed ``` Completes the first checkbox of the 2024H2 Rust Project Goal on cargo-semver-checks: rust-lang/rust-project-goals#104 Unblocks the lints specified in #48.
1 parent 45f0664 commit e6bed4d

File tree

17 files changed

+461
-178
lines changed

17 files changed

+461
-178
lines changed

.github/workflows/ci.yml

+26-10
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ jobs:
194194
uses: actions/cache@v4
195195
with:
196196
path: semver/localdata/test_data/
197-
key: test-rustdocs-${{ runner.os }}-${{ steps.toolchain.outputs.cachekey }}-${{ hashFiles('semver/test_crates/**/*.rs') }}
197+
key: test-rustdocs-and-meta-${{ runner.os }}-${{ steps.toolchain.outputs.cachekey }}-${{ hashFiles('semver/test_crates/**/*.rs') }}
198198

199199
- name: Regenerate test data
200200
if: steps.cache-test-rustdocs.outputs.cache-hit != 'true'
@@ -406,11 +406,17 @@ jobs:
406406
407407
# Show what's in the cache.
408408
ls subject-new/target/semver-checks/cache/
409-
EXPECTED_CACHED_RUSTDOC='subject-new/target/semver-checks/cache/libp2p_core-0_37_0-ccce455725cbab73.json'
409+
EXPECTED_PREFIX='subject-new/target/semver-checks/cache/libp2p_core-0_37_0-ccce455725cbab73'
410+
EXPECTED_CACHED_METADATA="${EXPECTED_PREFIX}.metadata.json"
411+
EXPECTED_CACHED_RUSTDOC="${EXPECTED_PREFIX}.json"
410412
411-
# Ensure the previous cached rustdoc file still exists.
413+
# Ensure the previous cached rustdoc and metadata files still exist.
414+
[ -f "$EXPECTED_CACHED_METADATA" ] || { \
415+
echo "could not find libp2p-core metadata cache file"; \
416+
exit 1;
417+
}
412418
[ -f "$EXPECTED_CACHED_RUSTDOC" ] || { \
413-
echo "could not find libp2p-core cache file"; \
419+
echo "could not find libp2p-core rustdoc cache file"; \
414420
exit 1;
415421
}
416422
@@ -527,20 +533,30 @@ jobs:
527533
ls subject/target/semver-checks/cache/
528534
529535
# The previous cached rustdoc should continue to exist.
530-
if [ -f subject/target/semver-checks/cache/serde-1_0_162-5900ebf8bb9b9f8b.json ]; then
531-
exit 0
532-
else
536+
PREVIOUS_PREFIX='subject/target/semver-checks/cache/serde-1_0_162-5900ebf8bb9b9f8b'
537+
PREVIOUS_RUSTDOC="${PREVIOUS_PREFIX}.json"
538+
PREVIOUS_METADATA="${PREVIOUS_PREFIX}.metadata.json"
539+
if [ ! -f "$PREVIOUS_RUSTDOC" ]; then
533540
echo "Older rustdoc JSON not found in cache!"
534541
exit 1
535542
fi
543+
if [ ! -f "$PREVIOUS_METADATA" ]; then
544+
echo "Older metadata file not found in cache!"
545+
exit 1
546+
fi
536547
537548
# There should also be a new cached rustdoc file for the new feature settings.
538-
if [ -f subject/target/semver-checks/cache/serde-1_0_162-6999ae87ca463ab3.json ]; then
539-
exit 0
540-
else
549+
NEW_PREFIX='subject/target/semver-checks/cache/serde-1_0_162-6999ae87ca463ab3'
550+
NEW_RUSTDOC="${NEW_PREFIX}.json"
551+
NEW_METADATA="${NEW_PREFIX}.metadata.json"
552+
if [ ! -f "$NEW_RUSTDOC" ]; then
541553
echo "New rustdoc JSON for new feature combo not found in cache!"
542554
exit 1
543555
fi
556+
if [ ! -f "$NEW_METADATA" ]; then
557+
echo "New metadata file for new feature combo not found in cache!"
558+
exit 1
559+
fi
544560
545561
- name: Cleanup
546562
run: |

CONTRIBUTING.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ To generate this data, please run `./scripts/regenerate_test_rustdocs.sh`.
111111
To use a specific toolchain, like beta or nightly, pass it as
112112
an argument: `./scripts/regenerate_test_rustdocs.sh +nightly`.
113113

114-
## What are those `.snap` or `.snap.new` files generated via `cargo test`
114+
## What are those `.snap` or `.snap.new` files generated via `cargo test`
115115

116116
As part of our overall testing strategy, we use a technique called "snapshot testing."
117117
The tool we use for this ([`insta`](https://insta.rs/docs/)) is user friendly and has mutliple ways to interact with it:
@@ -138,7 +138,7 @@ Reviewing them is possible via these options:
138138
```
139139
3. **Manually**: If you can't (or don't want to) use `cargo-insta`, you can verify the snapshot
140140
file manually. There should be a file called `test_outputs/<some_path>/<lint_name>.snap.new`.
141-
Open it, and verify that its contents match what you expected: all expected data is present, and no unexpected data is included.
141+
Open it, and verify that its contents match what you expected: all expected data is present, and no unexpected data is included.
142142
Once you've checked it, remove the `.new` suffix so that the file's new path
143143
is `test_outputs/<some_path>/<lint_name>.snap`
144144

@@ -172,14 +172,16 @@ We'll use the [`scripts/make_new_lint.sh`](https://github.com/obi1kenobi/cargo-s
172172

173173
Now it's time to fill in these files!
174174
- Define the lint in `src/lints/<lint_name>.ron`.
175-
- Make sure your lint outputs `span_filename` and `span_begin_line` for it
176-
to be a valid lint. The pattern we commonly use is:
175+
- For almost all lints, make sure your lint outputs `span_filename` and `span_begin_line`
176+
in order to be a valid lint. The pattern we commonly use is:
177177
```
178178
span_: span @optional {
179179
filename @output
180180
begin_line @output
181181
}
182182
```
183+
<details><summary>Exception: lints over Cargo.toml information (click to expand)</summary>If you are writing a lint over manifest (<code>Cargo.toml</code>) information such as "a feature was deleted," you won't be able to find span data from the manifest file. To proceed, pick a value unique to each result produced by your lint query and output it as <code>ordering_key</code> instead.</details>
184+
183185
- Demonstrate the semver issue your lint is looking for by adding suitable code in
184186
the `test_crates/<lint_name>/old` and `test_crates/<lint_name>/new` crates.
185187
- Add code to the test crates that aims to catch for false-positives
@@ -269,8 +271,8 @@ Inspect the generated "actual" output in the `.snap.new` file:
269271
If so, ensure the reported code is indeed violating semver and is not being flagged
270272
by any other lint.
271273

272-
If everything looks okay, either run `cargo insta review` (see
273-
the [snapshot instructions](#what-are-those-snap-or-snapnew-files-generated-via-cargo-test)
274+
If everything looks okay, either run `cargo insta review` (see
275+
the [snapshot instructions](#what-are-those-snap-or-snapnew-files-generated-via-cargo-test)
274276
for context) or manually move `test_outputs/query_execution/<lint_name>.snap.new`
275277
to `test_outputs/query_execution/<lint_name>.snap`.
276278
Then re-run `cargo test` and make sure everything passes.
@@ -342,7 +344,7 @@ to update the test outputs.
342344
> It may contain output for other test crates — this is not necessarily an error:
343345
> See the [troubleshooting section](#troubleshooting) for more info.
344346
345-
To update the output, please refer to the section
347+
To update the output, please refer to the section
346348
on [snapshot testing](#what-are-those-snap-or-snapnew-files-generated-via-cargo-test)
347349

348350
Once you've update the test output, run `cargo test` again and the `<lint_name>` test should pass!
@@ -356,40 +358,40 @@ otherwise the test will fail in CI.
356358
### Troubleshooting
357359

358360
- <details><summary>A valid query must output <code>span_filename</code> and/or <code>span_begin_line</code> (click to expand)</summary>
359-
361+
360362
If your lint fails with an error similar to the following:
361363
```
362364
---- query::tests_lints::enum_missing stdout ----
363365
thread 'query::tests_lints::enum_missing' panicked at 'A valid query must output both `span_filename` and `span_begin_line`. See https://github.com/obi1kenobi/cargo-semver-checks/blob/main/CONTRIBUTING.md for how to do this.', src/query.rs:395:26
364366
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
365367
```
366-
368+
367369
It likely means that your lint does not specify the `span_filename` and `span_begin_line` of where the error occurs. To fix this, add the following to the part of query that catches the error:
368370
```
369371
span_: span @optional {
370372
filename @output
371373
begin_line @output
372374
}
373375
```
374-
376+
375377
</details>
376378
- <details><summary>Other lints' tests failed too (click to expand)</summary>
377379

378380
This is not always a problem! In process of testing a lint, it's frequently desirable to include
379381
test code that contains a related semver issue in order to ensure the lint differentiates between
380382
them.
381-
383+
382384
For example, say one is testing a lint for pub field removals from a struct. Its test crate code
383385
may then include removals of the entire struct, in order to make sure that the lint *does not*
384386
report those. But those struct removals *will* get reported by the lint that looks for semver
385387
violations due to struct removal!
386-
388+
387389
So if you added code to a test crate, and it caused other lints to report new findings, consider:
388390
- whether your code indeed contains the reported semver issue;
389391
- whether the same semver issue is being reported only once, and not multiple times
390392
by different lints,
391393
- and whether the new reported lint result points to the correct item and span information.
392-
394+
393395
If the answer to all is yes, then everything is fine! Just edit those other lints'
394396
expected output files to include the new items, and you can get back on track.
395397
</details>

0 commit comments

Comments
 (0)