7708 Commits

Author SHA1 Message Date
Eric Huss
b78dc1043d Add -Z json-target-spec
This adds the `-Z json-target-spec` CLI flag to assist with using custom
`.json` target spec files. `rustc` recently switched so that it requires
`-Z unstable-options` when using custom spec files
(https://github.com/rust-lang/rust/pull/151534). This can make it rather
awkward to use spec files with cargo because it then requires setting
RUSTFLAGS and RUSTDOCFLAGS to pass `-Zunstable-options`. It also ends up
causing some confusing error messages.

Now, using `--target` with `.json` extension files generates an error
that explains you need `-Zjson-target-spec`. The only thing this flag
does is disable that error, and automatically passes
`-Zunstable-options` with the `--target` flag.

This does not 100% cover json target spec files, because they can be
placed in RUST_TARGET_PATH or the sysroot, and `rustc` will
automatically search for them (without the `.json` extension in the
`--target` arg). The user will just need to use RUSTFLAGS/RUSTDOCFLAGS
in that situation (but I expect that to be rare).

The majority of this change is changing `CompileTarget::new` to take a
flag if `-Zjson-target-spec` is enabled, and then threading through all
the places that call it. `CompileTarget::new` is responsible for
generating an error if json is used without the Z flag.
2026-01-24 17:52:57 -08:00
Ed Page
4071ddae4e feat(lint): Add redundant_readme lint
Name and styling was inspired by
[redundant_static_lifetimes](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes)

Fixes #15869
2026-01-23 14:18:02 -06:00
Ed Page
12b79e92b3 test(lints): Add redundant_readme lint 2026-01-23 13:43:04 -06:00
Weihang Lo
5939c2115d tests: add regression test for --artifact-dir on stable (#16541)
### What does this PR try to resolve?

This PR adds a regression test to ensure that `cargo build
--artifact-dir` is rejected on the stable channel unless `-Z
unstable-options` is used.

The behavior is already correct, but this test ensures it does not
regress in the future.

### How to test and review this PR?

Run the Cargo test suite:

```bash
cargo test --test testsuite artifact_dir::artifact_dir_rejected_on_stable
2026-01-23 20:10:38 +00:00
Eric Huss
85d815facc Fix build-std lto test to run on other platforms
The `-C linker-features=-lld` flag is only available on
x86_64-unknown-linux-gnu. On all other platforms, this test was failing.
2026-01-23 11:00:24 -08:00
Eric Huss
b314edf9c8 Increase cache_lock test timeout
We've been seeing occasional failures on CI with these tests timing out.
I'm guessing that the runners are too overloaded and are unable to
complete the test within 1 second. This bumps up the timeout to 10s to
see if that will resolve the problem.
2026-01-22 09:03:58 -08:00
harshithreddy10
e1ab796af0 style: rustfmt 2026-01-22 16:23:32 +05:30
harshithreddy10
3cb99307c6 tests: add regression test for --artifact-dir on stable 2026-01-22 16:10:46 +05:30
Weihang Lo
f0c243f6a0 test(build-std): adjust snapshot (#16539)
### What does this PR try to resolve?

rust-lang/rust#149514 added a build.rs to libtest,
which affected how Cargo print status message
(build.rs was scheduled first because it has one dependent)

### How to test and review this PR?

See
<https://github.com/rust-lang/cargo/actions/runs/21232930264/job/61094884597?pr=16506>

```
thread 'basic' (9963) panicked at tests/build-std/main.rs:179:10:

---- expected: tests/build-std/main.rs:167:27
++++ actual:   stderr
   1    1 | [COMPILING] [..]
   2      - ...
   3      - [COMPILING] test v0.0.0 ([..])
        2 + [COMPILING] rustc-std-workspace-std v1.99.0 (/home/runner/.rustup/toolchains/nightly-[HOST_TARGET]/lib/rustlib/src/rust/library/rustc-std-workspace-std)
        3 + [COMPILING] getopts v0.2.24
   4    4 | [COMPILING] foo v0.0.1 ([ROOT]/foo)
   5    5 | [FINISHED] `test` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
   6    6 | [RUNNING] unittests src/lib.rs (target/[HOST_TARGET]/debug/deps/foo-[HASH])
   7    7 | [RUNNING] unittests src/main.rs (target/[HOST_TARGET]/debug/deps/foo-[HASH])
   8    8 | [RUNNING] tests/smoke.rs (target/[HOST_TARGET]/debug/deps/smoke-[HASH])
   9    9 | [DOCTEST] foo

```
2026-01-22 05:24:45 +00:00
Weihang Lo
5c9f786490 test(build-std): adjust snapshot
rust-lang/rust#149514 added a build.rs to libtest,
which affected how Cargo print status message
(build.rs built first so Cargo printed `Compiling` first)
2026-01-21 22:34:12 -05:00
Eric Huss
7a34e4f957 fix: update resolve_all_features() to filter pkg deps (#16221)
### What does this PR try to resolve?

Before,
[`resolve_all_features()`](13695497cd/src/cargo/ops/cargo_compile/mod.rs (L1001-L1032))
isn't filtering dependencies, as it uses the set returned by
[`Resolve::deps()`](0f14d9d2fa/src/cargo/core/resolver/resolve.rs (L327-L330))
directly which is mostly unfiltered. This PR fixes it by having the set
goes through
[`PackageSet::filter_deps()`](0f14d9d2fa/src/cargo/core/package.rs (L632-L660))'s
filter.

Note that this patch changed `resolve_all_features()`'s function
signature as well as made `PackageSet::filter_deps()` public.

Close #15399

### How to test and review this PR?

As described in #15399, `cargo test --frozen` should not panic (doesn't
try to download unneeded deps) when does the following:

```sh
git clone https://github.com/hickory-dns/hickory-dns
cd hickory-dns
git checkout v0.25.1
mkdir .cargo_home
CARGO_HOME=.cargo_home cargo fetch --locked --target=x86_64-unknown-linux-gnu
CARGO_HOME=.cargo_home cargo test --frozen -p hickory-resolver
```

More discussion on [Zulip
chat](https://rust-lang.zulipchat.com/#narrow/channel/246057-t-cargo/topic/Issue.20.2315399.3A.20.20.60cargo.20test.60.20trys.20to.20fetch.20not.20required.20deps/with/553899270).
2026-01-21 22:06:09 +00:00
Scott Schafer
0f6e232518 fix: show implicit_minimum_version_req emitted source once per package 2026-01-20 22:13:28 -07:00
Scott Schafer
768308307b fix: Move implicit_minimum_version_req emitted source to primary group 2026-01-20 22:13:28 -07:00
Scott Schafer
fb08c99bdf test: Make a implicit_minimum_version_req test ordered 2026-01-20 22:13:28 -07:00
Weihang Lo
56e514ca8a fix: --remap-path-scope stabilized in 1.95-nightly 2026-01-20 22:55:03 -05:00
Ed Page
6a5bef29fc feat(lints): Add non_kebab_case_bin lint
Regarding lint naming:
- I didn't go with `non_kebab_case` because we had received
  encouragement to be more specific than `non_camel_case` when
  discussing this at the All Hands
- I went with `bin` rather than `bin_name` because the thing we are
  ultimately linting is the binary file stem, not any specific field, and if
  other fields end up contributing to the binary name, we should include
  those, like
  [bin.namefilename](https://doc.rust-lang.org/cargo/reference/unstable.html#different-binary-name)
  or any potential [prefixes or suffixes](https://github.com/rust-lang/cargo/issues/1970)

Regarding the lint's look: I tried to make this look as much like rustc
as possible:
```
    Checking cargo v0.95.0 (/home/epage/src/personal/cargo)
warning: function `Is_default_main` should have a snake case name
   --> src/cargo/lints/rules/non_kebab_case_bin.rs:188:4
    |
188 | fn Is_default_main(path: Option<&cargo_util_schemas::manifest::PathValue>) -> bool {
    |    ^^^^^^^^^^^^^^^ help: convert the identifier to snake case (notice the capitalization): `is_default_main`
    |
    = note: `#[warn(non_snake_case)]` (part of `#[warn(nonstandard_style)]`) on by default
```

Fixes #15586
2026-01-20 14:53:46 -06:00
Saplyn Miao
5190ae780f test: add tests for optional dependency resolution check 2026-01-21 03:28:06 +08:00
Ed Page
5cd00ce20e test(lints): Add non_snake_case_bin_name tests 2026-01-16 16:54:22 -06:00
Ed Page
41775f4afb fix(rm): Suggest table flags when none are specified 2026-01-20 12:39:37 -06:00
Ed Page
30ae8bcf4f test(rm): Show no-flag behavior 2026-01-20 12:38:12 -06:00
Ed Page
f7179c49ce fix(rm): Improve lack of -p error message 2026-01-20 10:28:46 -06:00
Weihang Lo
a793cd1d02 fix(patch): clean up patch-related error messages (#16498)
### What does this PR try to resolve?

This is a follow-up for #16407.

This PR is supposed to make patch-related error messages closer to the
desired
[style](https://rustc-dev-guide.rust-lang.org/diagnostics.html#errors-and-lints),
remove unnecessary(?) context wrappings, and fix a few minor issues in
code.

I tried to restructure some of the error messages to include the values
from `.context()` where I thought it would make sense. Obvious
duplications (e.g. repository url of the patch) were removed.

I tried to rephrase some of the error messages to sound closer to the
desired style and to ease future migration to rustc's error reporter.

### How to test and review this PR?

Test changes should demonstrate the intended behavior change.
Suggestions on how to make messages even better are obviously welcome.

### Note
I was thinking about (and tried) moving this portion of error messages
directly to the annotate snippets
[#15944](https://github.com/rust-lang/cargo/issues/15944), but with
several functions returning `Result` it was not straightforward.
Probably needs well-thought refactoring, which is out of scope of this
PR.

r? @weihanglo
2026-01-17 16:56:42 +00:00
omskscream
8bbb6426c9 fix (patch): clean up error messages on patch summary checks - version mismatch 2026-01-17 17:56:12 +02:00
omskscream
375efde98a fix (patch): clean up error messages on patch summary checks - patch not found 2026-01-17 17:56:12 +02:00
omskscream
c1c2c63636 fix (patch): clean up error messages on patch summary checks - multiple resolved candidates 2026-01-17 17:56:12 +02:00
omskscream
418bb33b9c test (patch): add test case where a patch defined using git resolves to several candidates 2026-01-17 17:56:12 +02:00
omskscream
28e1777662 fix (patch): clean up error messages on patch summary checks 2026-01-17 17:56:12 +02:00
omskscream
d8892defea fix (patch): clean up error message on same source patch 2026-01-17 17:56:12 +02:00
omskscream
f7ff0ea112 fix (patch): clean up error message on duplicated patches 2026-01-17 17:56:11 +02:00
Ross Sullivan
67aaf3bf55 fix: move artifact deps to be stored in their build unit dir 2026-01-17 11:14:00 +09:00
Ross Sullivan
4a6ad19e39 test: Added layout test for artifact dependencies 2026-01-17 11:13:15 +09:00
Ed Page
85eff7c802 fix(lockfile): switch to resolver.lockfile-path config (#16510)
### What does this PR try to resolve?

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

* When both CLI and config present, favor config
* Plan to remove `--lockfile-path` in 1.95.0-nightly (if this gets into
1.94.0-nightly)

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

### How to test and review this PR?
2026-01-15 16:18:08 +00:00
Ed Page
102926cf37 Moved build-script bins to deps directory (#16515)
### What does this PR try to resolve?

In https://github.com/rust-lang/cargo/pull/16502#discussion_r2694115028
there was a discussion about merging the `build-script` and `deps` dirs
in the `build-dir`.

Currently:
```
build-dir/<profile>/build/<pkgname>/<hash>/
    deps/ # rustc output
    build-script/ # build script binary
    build-script-execution/ # the output of running a build script
```

Note: For build-scripts `deps` is empty.

This PR:
1. moves the build script binaries from `build-script` to `deps` to
simplify the layout
2. renames `build-script-execution` to `build-script` to simplify the
dir naming and shorten the path length for windows

```
build-dir/<profile>/build/<pkgname>/<hash>/
    deps/ # rustc output including build-script bins
    build-script/ # the output of running a build script
```

cc tracking issue: https://github.com/rust-lang/cargo/issues/15010

### How to test and review this PR?

see the test changes

r? @epage
2026-01-15 16:03:20 +00:00
Weihang Lo
97b28fc373 Invalidate the whole build cache when -Zno-embed-metadata changes (#16513)
I mixed the `-Zno-embed-metadata` value into the general config hash,
because the flag does complicated stuff (pass rustc flag, but also
change how dependencies are passed to rustc).

Fixes: https://github.com/rust-lang/cargo/issues/16503
2026-01-15 14:35:10 +00:00
Ross Sullivan
ff0d6093d2 fix: Moved build-script bins to deps directory 2026-01-15 23:33:20 +09:00
Weihang Lo
b099a005af fix: switch to resolver.lockfile-path config from CLI flag 2026-01-15 09:11:39 -05:00
Weihang Lo
93ea2738da test: prepare for resolver.lockfile-path 2026-01-15 09:08:00 -05:00
Ross Sullivan
8b7871442d fix: Do not create examples dir in build dir with new layout 2026-01-15 21:39:15 +09:00
Ross Sullivan
cc52435261 test: Add assertion for examples in build-dir 2026-01-15 21:38:09 +09:00
Jakub Beránek
1bcb3b6c87 Invalidate the whole build cache when -Zno-embed-metadata changes 2026-01-15 12:57:35 +01:00
Weihang Lo
d559ea31ec Optimize cargo locate-project --workspace (#16423)
### Summary

Optimize cargo locate-project --workspace by adding a fast path that
avoids full workspace loading when possible.

### Problem

Previously, ```cargo locate-project --workspace``` always performed full
workspace loading, which includes:
Parsing all member manifests
Resolving path dependencies
Validating workspace configuration
This is expensive for large workspaces when all we need is the workspace
root path.

### Solution

Introduce a new function
```find_workspace_root_with_membership_check()``` that provides a fast
path by only reading the minimal set of manifests needed to determine
the workspace root.

### When Fast Path is Taken
The fast path succeeds when both conditions are met:

- The package is explicitly listed in ```workspace.members``` (including
glob patterns)
- The package is not excluded via workspace.exclude

Specific scenarios:
- Workspace root manifest: Returns immediately (the manifest is its own
workspace root)
- Explicit package.workspace pointer: Reads only the target workspace
manifest, verifies membership
- No explicit pointer: Walks up parent directories, checking each
Cargo.toml for membership

### When Slow Path is Taken

Falls back to full workspace loading when:

- Package is a member via path dependencies (implicit membership)
- Workspace has default-members defined (requires validation)
- Package is not found in explicit members list
- Any error occurs during fast path (ensures correct error messages)

Fixes #15107
2026-01-14 19:32:09 +00:00
Weihang Lo
0ec8a97198 fix(build-std): std link metadata propagate to user
This reverts https://github.com/rust-lang/cargo/pull/16489
though currently only compiler_builtins has link metadata

`-Zany-build-script-metadata` is not supported yet for build-std
2026-01-12 12:59:28 -05:00
Weihang Lo
316d9fd718 test(build-std): regression test for rust-lang/cargo#16489 2026-01-12 12:59:28 -05:00
Liam
ac71ebaf4c Improve error message for missing dependencies 2026-01-11 19:15:06 -05:00
Mohamed Ali
8a090fc485 refactor(locate_project): streamline workspace root retrieval in exec function 2026-01-11 16:37:55 +02:00
Mohamed Ali
459c03ee38 test(locate-project): Add coverage for workspace root discovery 2026-01-11 16:17:46 +02:00
Weihang Lo
09ea79d151 fix: preserve dep_name for build script metadata
This fix ensures the synthetic RunCustomBuild-RunCustomBuild edges carry
the manifest dep name, so `CARGO_DEP_*` environment variable uses the
correct prefix even with patched sources and renamed dependencies.
2026-01-10 05:41:52 -05:00
Weihang Lo
d30b15c35c test(build-script): regression test rust-lang/cargo#16493
This regressed because we looked up the manifest dependency name
by matching SourceId, which fails when `[patch]` changes the source.

dc03f44ec8/src/cargo/core/compiler/custom_build.rs (L473-L475)
2026-01-10 05:40:24 -05:00
Weihang Lo
dc03f44ec8 Add --id flag to cargo report timings and cargo report rebuilds (#16490)
Resolves #16472

This diff adds the ability to specify a session ID when running `cargo
report timings` and `cargo report rebuilds`. Previously, these commands
always reported on the most recent build session. Now users can inspect
any previous session by passing `--id <SESSION_ID>`.
2026-01-09 22:12:14 +00:00
Tanmay Arya
a34fb1be7c add(flock): display lockfile path when blocking in --vv mode 2026-01-10 02:08:30 +05:30