7637 Commits

Author SHA1 Message Date
Ross Sullivan
a919c32c25 feat: any build scripts can now use cargo::metadata=KEY=VALUE 2025-12-31 10:37:22 +09:00
Ross Sullivan
a69a2831df test: Added test to verify build scripts cannot pass env vars without links 2025-12-31 10:27:43 +09:00
Ross Sullivan
042c613f22 feat: Added any-build-script-metadata unstable feature 2025-12-31 10:21:38 +09:00
Ed Page
b54051b150
fix(log): add dependencies field to UnitRegistered (#16448)
### What does this PR try to resolve?

Track unit dependency graph in `LogMessage::UnitRegistered`.

This gives the unit-graph information before running the build, which
will help enable root cause analysis for cascading rebuilds.

Part of <https://github.com/rust-lang/cargo/issues/15844>

### How to test and review this PR?
2025-12-30 20:35:52 +00:00
Weihang Lo
4c91fda452
fix(log): add dependencies field to UnitRegistered
Track unit dependency graph in `LogMessage::UnitRegistered`.

This gives the unit-graph information before running the build,
which will help enable root cause analysis for cascading rebuilds.
2025-12-30 14:16:33 -05:00
Ross Sullivan
4235aa24f8
feat(locking): Added build unit level locking 2025-12-30 18:08:07 +09:00
Ross Sullivan
fab4ae6c36
feat(unstable): Added -Zfine-grain-locking unstable feature 2025-12-30 17:58:37 +09:00
Ed Page
577ab6f454
feat(resolver): List features when no close match (#16445)
### What does this PR try to resolve?

Fixes #9722

When a user requests a non-existent feature, cargo suggests a similar
feature if one exists via edit distance. However, if no feature name is
close enough to suggest, the error message provides no help.

This PR adds a fallback that lists all available features so the user
can see their options when no close match exists.

**Before:**
```shell
package foo depends on bar with feature serde but bar does not have that feature.
```

**After:**
```shell
package foo depends on bar with feature serde but bar does not have that feature.
 available features: cookies, json, tls
```

### How to test and review this PR?

This PR follows the atomic commit pattern:
1. First commit adds a test documenting the current behavior
2. Second commit implements the fix and updates the test expectation

Run the new test:
```shell
cargo +nightly test --test testsuite -- features::dependency_activates_feature_with_no_close_match
```
2025-12-29 22:13:35 +00:00
Glen Baker
add723f066 feat(resolver): List features when no close match
When a requested feature does not exist and there is no similar
feature name to suggest, list all available features.

Implements the "closest else all" pattern - show the closest match
if one exists, otherwise list all available features as a fallback.

Fixes #9722
2025-12-29 13:39:47 -08:00
Glen Baker
601bdefde8 test(resolver): Missing feature with no close match
Add test case documenting current behavior when a requested feature
has no close match. Currently no suggestion is shown.
2025-12-29 13:39:25 -08:00
Weihang Lo
04bd5bba5d
feat(report): new command cargo report sessions (#16428)
### What does this PR try to resolve?

A new report command `cargo report sessions` is added.
The motivation of this is to provide an access to the build session ID,
so that user can retrieve reports for specific session.
For example, `cargo report timings --id <my-older-build>`.

The command name `sessions` was chosen because

* It is a thing that Cargo "reports to user",
  feeling more reasonable than `cargo report list-runs`
  or `cargo rebuild recent-builds`.
* Discussed with `@Muscraft`,
  and we feel `SessionId` will be a better term in `RunId`.
  While whether a `cargo` invocation is a session is debatable,
  sessions the term is widely used for logging and metrics.
* This is unstable. We can always change its name later.

Design decision:
- Doesn't show total number of sessions to not be a performance burden
when we add filtering, see
https://github.com/rust-lang/cargo/pull/16428#discussion_r2649281077

Part of https://github.com/rust-lang/cargo/issues/15844

Fixes https://github.com/rust-lang/cargo/issues/16401

### How to test and review this PR?

See tests for the report output.
It is a simple text-based report. JSON schema is not included.

```console
$ cargo report sessions -Zbuild-analysis
Session IDs for `/projects/cargo` (most recent first):

 - 20251218T220836847Z-0e981be2243ad433
 - 20251218T220801494Z-0e981be2243ad433
 - 20251218T220704295Z-0e981be2243ad433

... and more (use --limit N to see more)
```

Unresolved questions
- without a workspace, do we error? if not, should we have a flag for
forcing of global listing?
(https://github.com/rust-lang/cargo/pull/16428#discussion_r2648201273,
https://github.com/rust-lang/cargo/pull/16428#discussion_r2648202658)
2025-12-29 20:32:16 +00:00
Weihang Lo
e0a956b46c
feat (patch): Display where the patch was defined in patch-related error messages (#16407)
### What does this PR try to resolve?

Closes #13952

This PR adds location information of where each 'patch' was defined to
the dependency override algorithm. Currently there're three possible
sources: manifest `Cargo.toml`, one of the configuration files (listed
[here](https://doc.rust-lang.org/cargo/reference/config.html#hierarchical-structure))
or CLI option `--config 'patch.crates-io.rand.path="rand"'`
([source](https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html#the-patch-section)).

Preserving location of where the patch was defined should simplify
troubleshooting of incorrect or incompatible dependency override
configuration.

This PR does not change:
- the way of toml files parsing
- the way of working with dependency locks
- the way of patch overlaps resolving (end of
`cargo::core::Workspace::root_patch` function)
- related error messages happening in a different phases of the build,
e.g. toml parsing

### How to test and review this PR?

I suppose test changes should demonstrate the intended behavior change.
Few earlier commits do not contain test changes because they do not
change visible behavior of cargo but allow to proceed with the ones that
do.
2025-12-29 20:31:26 +00:00
Weihang Lo
5b77b3c970
fix(report): change diagnostic term from log to session 2025-12-29 15:00:28 -05:00
Weihang Lo
3d50c1d0ff
feat(report): new command cargo report sessions
A new report command `cargo report sessions` is added.
The motivation of this is to provide an access to the build session ID,
so that user can retrieve reports for specific session.
For example, `cargo report timings --id <my-older-build>`.

The command name `sessions` was chosen because

* It is a thing that Cargo "reports to user",
  feeling more reasonable than `cargo report list-runs`
  or `cargo rebuild recent-builds`.
* Discussed with `@Muscraft`,
  and we feel `SessionId` will be a better term in `RunId`.
  While whether a `cargo` invocation is a session is debatable,
  sessions the term is widely used for logging and metrics.
* This is unstable. We can always change its name later.

Part of rust-lang/cargo#15844
2025-12-29 15:00:28 -05:00
Weihang Lo
e29a300c9e
test(report): cargo report sessions 2025-12-29 15:00:27 -05:00
Ed Page
09d9caea0f test(build-rs): Reduce from 'build' to 'check' where possible
Remaining `build`s:
- linker is potentially involved
- testing related to dev-dependencies
  (didn't look to see if `check --benches` could replace `bench`)

Similarly, some `run`s` were removed so long as compilation verified the
same details.

Inspired by #16436 which is adding more tests which don't need `build`
but copied the existing ones.
2025-12-29 12:55:27 -06:00
Weihang Lo
b6306caea2
feat(toml): TOML 1.1 parse support (#16415)
### What does this PR try to resolve?

Support
- multi-line inline tables
- trailing commas on inline tables
- `\e` string escape character
- `\xHH` string escape character
- Optional seconds in times (sets to `0`)

Anyone using these features will raise their development MSRV. The
published `Cargo.toml` file will still be compatible with old TOML
parsers and so old Cargos. If `toml_datetime` gains the ability to track
whether seconds were optional (will be a breaking change), then that
might change though that would only apply to `package.metadata` as we
don't make use of times in Cargo.

### How to test and review this PR?
2025-12-28 23:36:16 +00:00
Weihang Lo
6e24cb6c74
feat(report): support --manifest-path in cargo report timing 2025-12-27 12:31:35 -05:00
Weihang Lo
a161b21419
test(report): no --manifest-path flag in cargo report timings 2025-12-27 12:31:33 -05:00
Weihang Lo
6f72ed4595
test(report): change to type-check 2025-12-27 12:27:50 -05:00
Weihang Lo
cf622c4be3
fix(vendor): recursively filter git files in subdirectories (#16439)
### What does this PR try to resolve?

Fixes #16148
Fixes #13607

Right now cargo vendor only filters .gitattributes, .gitignore, and .git
at the top level of each package. If these files exist in
subdirectories, they don't get filtered. This causes problems when the
vendored code gets checked into a git repo - git processes those files
and modifies them, which breaks checksums.

I ran into this when trying to vendor libssh2-sys. The .gitattributes
file in libgit2/src/util/ was getting its line endings changed by git,
causing checksum failures.

### How should we test and review this PR?

This follows the atomic commit pattern:
- First commit adds a test showing the current buggy behavior (test
passes)
- Second commit implements the fix and updates test assertions (test
still passes)

The fix uses components() to recursively check for .git directories
(since they can appear at any path level like deep/nested/.git/config),
while using file_name() for .gitattributes and .gitignore files.

### Additional context

This should also help with #13607 since it filters .gitignore and .git
recursively too.

If people actually need git functionality in their vendored deps, they
can add rules to their own .gitattributes at the root level.
2025-12-27 14:28:13 +00:00
Vishal Pokala
4c6f5a61ab fix(vendor): recursively filter git files in subdirectories
Fixes #14712
2025-12-27 13:04:27 +00:00
Vishal Pokala
5400ea7000 test(vendor): add test showing git files aren't filtered recursively 2025-12-26 16:15:21 +00:00
Weihang Lo
c7f10b7358
fix(vendor): unpack from local-registry cache path
In rust-lang/cargo#15514, Cargo starts direct-extraction from `.crate`
tarball in `$CARGO_HOME/registry/cache` when vendoring. This is fine with
remote registrires but not for local registries, as local registries'
tarball are stored along with index.

This fix abstracts `cache_path` to `RegistryData` trait, so every
registry source type can define its own locationa of tarball cache.

Note that for local-registry when accessing either index or cache, the
assumption is that file lock of the index and cache directory need to
be externally synchronized. Cargo doesn't take care of it.
2025-12-26 10:59:58 -05:00
Weihang Lo
a2b970f9a1
test(vendor): show local-registry vendor regression
This is a minimal reproducible example of rust-lang/cargo#16412
2025-12-26 10:59:14 -05:00
omskscream
75bfdfd758 feat (patch): add patch location to the error message when there are multiple patch definitions resolving to the same version 2025-12-20 00:48:01 +02:00
omskscream
373f626716 feat (patch): add patch location to the error message when patch source is the same as dependency source 2025-12-20 00:48:00 +02:00
omskscream
68046919e0 feat (patch): add patch location to the error message when there's no matches for a patch definition in the specified location 2025-12-20 00:48:00 +02:00
omskscream
efd8d45491 feat (patch): add patch location to the error message when there's a version mismatch 2025-12-20 00:48:00 +02:00
omskscream
afe63af77f feat (patch): add patch location to the error message when too many matches found for this patch definition 2025-12-20 00:48:00 +02:00
Weihang Lo
3861f60f6b
fix(sbom): Don't set CARGO_SBOM_PATH when empty (like stable) (#16419)
### What does this PR try to resolve?

Noticed `CARGO_SBOM_PATH` was being set when playing around with
artifact-deps.

I could have gated the env by whether the nightly feature was enabled
but figured it would be better to not set if it there aren't SBOMs.

### How to test and review this PR?
2025-12-19 21:53:33 +00:00
Ed Page
9755978749 fix(sbom): Don't set CARGO_SBOM_PATH when empty (like stable)
Noticed `CARGO_SBOM_PATH` was being set when playing around with
artifact-deps.

I could have gated the env by whether the nightly feature was enabled
but figured it would be better to not set if it there aren't SBOMs.
2025-12-19 15:23:04 -06:00
Ed Page
2b7dac9dcb test(sbom): Show CARGO_SBOM_PATH behavior 2025-12-19 11:37:18 -06:00
Ed Page
2dd8d64f6d test(sbom): Switch to snapshots 2025-12-19 11:37:12 -06:00
Weihang Lo
40613e1e58
fix(report): track requested units
Done by having `requested: true` field in user-requested units

Instead of calling it "root units", here we use "requested units",
which represent the actual build targets that users requested.

This is for reserving future extension that requested units may not
always be the "root units" of the unit graph. We might have some
synthetic units like rustdoc-merge, lipo universal binary for macOS,
post-processing, or any user-defined super root units.
The requested units sounds more future-proof than root_units,
2025-12-18 16:32:45 -05:00
Weihang Lo
2ebf882c68
fix(report): show the requested target platforms
we additionally log this info in each `unit-registered` event
2025-12-18 16:32:44 -05:00
Weihang Lo
9b344b0c09
test: remove HTML snapshot test for cargo report timings
This caused a headache that

* You need to remember to generate timings report, otherwise the HTML
  get stale.
* The source diff of both the log file and the HTML report are basically
  unreviewable, and the timestamp and elapsed time change every time.

We'd like to find a better way to help review timing HTML instead of this.
2025-12-18 16:30:25 -05:00
Ed Page
e9daaffc5e feat(toml): TOML 1.1 parse support 2025-12-18 07:38:37 -06:00
Ed Page
d8fc14a058
fix(log): emit fingeprint log also for no-rebuild unit (#16408)
### What does this PR try to resolve?

This makes the log message more complete.
Also consumers can check whether a crate is really "no rebuild needed"

It also open a door if we want to log the current fingerprint value.

Part of <https://github.com/rust-lang/cargo/issues/15844>.

### How to test and review this PR?

The test diff should show the behavior change
2025-12-17 21:22:34 +00:00
omskscream
be108bfe1d test (patch): add test case for patch that is defined in custom config file provided via cli 2025-12-17 22:53:06 +02:00
omskscream
e4151d5f06 test (patch): add test case for when several patch entries defined in different places resolve to same version 2025-12-17 22:43:25 +02:00
omskscream
8f79210002 test (patch): add test case for patch that is defined via env variables and is ignored 2025-12-17 22:43:25 +02:00
Weihang Lo
32de338894
fix(log): emit fingeprint log also for no-rebuild unit
This makes the log message more complete.
Also consumers can check whether a crate is really "no rebuild needed"

It also open a door if we want to log the current fingerprint value.
2025-12-17 09:52:20 -05:00
Weihang Lo
371a9beeb7
test(log): also enable build-analysis for first builds 2025-12-16 23:48:03 -05:00
Weihang Lo
0bb662ba66
fix(log): migrate rebuild message to unit index
Consumer can reference to unit-registered event for details
2025-12-16 20:44:02 -05:00
omskscream
53c0cbc6c9 test (patch): add test case for when patch is configured using CLI config options 2025-12-16 21:59:42 +02:00
Scott Schafer
3ed083194c
test: Use a larger default term width 2025-12-15 16:59:09 -07:00
Weihang Lo
e10429934c
fix(new): Improve quality of package name error messages (#16398)
### What does this PR try to resolve?

While triaging #16396, I noticed that the error messages don't match the
rustc style which we are aiming for, so I tweaked the messages and went
ahead and fixed #16396.

Fixes #16396

### How to test and review this PR?

Since `different-binary-name` doesn't have a `-Z` variant, I held off on
suggesting it so we didn't suggest using an unstable feature.
2025-12-15 17:43:54 +00:00
Ed Page
a7cdba1d8f
feat(log): add more log events (#16390)
### What does this PR try to resolve?

* `resolution-started`
* `resolution-finished`
* `unit-graph-started`
* `unit-graph-finished`
* `unit-registered`

The first four events are simple log events with elapsed time.

The `unit-registered` event contains unit metadata and the index, so
that `unit-started` doesn't need to have those. As a result, the timing
replay now requires `unit-registered` events to derive those data.

The log and HTML snapshot also changed to `rustfix`, which is a bit
smaller in size.

Part of <https://github.com/rust-lang/cargo/issues/15844>

### How to test and review this PR?
2025-12-15 15:39:58 +00:00
Ed Page
214f69e806 fix(new): Only suggest bin.name for valid crate names
Validate crate names are a superset of valid package names which also
try to work for `--extern`.

Fixes #16396
2025-12-15 09:02:00 -06:00