7087 Commits

Author SHA1 Message Date
Weihang Lo
090dcfe9af
fix(publish): better message when nothing to publish
This doesn't need to be an hard error because missing
publish for the entire worspace should be a fairly visible.
However, this is open for future to configure via Cargo lint system.
2025-05-14 01:22:30 -04:00
Weihang Lo
4f7494c4ec
feat(publish): workspace publish skips publish=false
This changes how cargo-publish works with the unstable
`-Zpackage-workspace` feature.

Before this, when publishing the entire workspace,
like `cargo publish --workspace`,
if there is a package with `package.pulibsh=false,
it'll fail the entire publish process.

After this, when `--workspace` is passed,
or when publishing the virtual workspace,
the intent is more like “publish all publishable in this workspace”,
so skip `publish=false` packages and proceed to publish others.

The new overall behavior looks like this:

- `cargo publish` (inside a `package.publish = false` package): error
- `cargo publish -p publishable -p unpublishable`: error
- `cargo publish --workspace`: skips `package.publish = false

See https://github.com/rust-lang/cargo/issues/15006#issuecomment-2847660911
2025-05-14 01:22:29 -04:00
Weihang Lo
2421496c92
test(publish): prepare tests for skipping unpublishable
For `-Zpackage-workspace`.
2025-05-14 01:08:22 -04:00
Eric Huss
a154422cec
feat(network): use Retry-After header for HTTP 429 responses (#15463)
### What does this PR try to resolve?

Cargo registries that return HTTP 429 when the service is overloaded
expect the client to retry the request automatically after a delay.
Cargo currently does not retry for HTTP 429.

### What changed?
* Adds HTTP 429 (too many requests) as a spurious HTTP error to enable
retries.
* Parse the
[Retry-After](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Retry-After)
HTTP header to determine how long to wait before a retry.

In this implementation, the maximum delay is limited to Cargo's existing
limit of 10 seconds. We could consider increasing that limit for this
case, since the server is explicitly requesting the delay.
2025-05-07 21:22:52 +00:00
Arlo Siemsen
b72d6476ae feat(network): use Retry-After header for HTTP 429 responses 2025-05-07 15:44:35 -05:00
Ed Page
cf7b053c0f
Make cargo script ignore workspaces (#15496)
### What does this PR try to resolve?

Cargo script considers itself part of the workspace if the script is
located in a sub-directory of a workspace (presumably since
https://github.com/rust-lang/cargo/pull/15168). This becomes an issue
when using a custom registry that is defined in the `.cargo/config.toml`
within the workspace. Cargo script does not take that file into account
and fails with ``registry index was not found in any configuration:
`test-reg` ``.

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

This PR adds a regression test and makes cargo script ignore the
surrounding workspace.
The test ~~will fail without the fix in the second commit and~~ can be
used to reproduce the issue.

### Additional information

The issue started occurring with `nightly-2025-02-16`.
Related to https://github.com/rust-lang/cargo/issues/12207.

<!--
Thanks for submitting a pull request 🎉! Here are some tips for you:

* If this is your first contribution, read "Cargo Contribution Guide"
first:
  https://doc.crates.io/contrib/
* Run `cargo fmt --all` to format your code changes.
* Small commits and pull requests are always preferable and easy to
review.
* If your idea is large and needs feedback from the community, read how:
  https://doc.crates.io/contrib/process/#working-on-large-features
* Cargo takes care of compatibility. Read our design principles:
  https://doc.crates.io/contrib/design.html
* When changing help text of cargo commands, follow the steps to
generate docs:

https://github.com/rust-lang/cargo/tree/master/src/doc#building-the-man-pages
* If your PR is not finished, set it as "draft" PR or add "WIP" in its
title.
* It's ok to use the CI resources to test your PR, but please don't
abuse them.

Explain the motivation behind this change.
A clear overview along with an in-depth explanation are helpful.

You can use `Fixes #<issue number>` to associate this PR to an existing
issue.

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

Demonstrate how you test this change and guide reviewers through your
PR.
With a smooth review process, a pull request usually gets reviewed
quicker.

If you don't know how to write and run your tests, please read the
guide:
https://doc.crates.io/contrib/tests

### Additional information

Other information you want to mention in this PR, such as prior arts,
future extensions, an unresolved problem, or a TODO list.
-->
2025-05-07 14:23:29 +00:00
arctic-alpaca
2b247ffd4e
make cargo script ignore surrounding workspace 2025-05-07 07:48:08 +02:00
arctic-alpaca
03a2a29aed
add test for cargo script workspace handling 2025-05-07 07:43:44 +02:00
Ed Page
f83e11acfe fix(rustc): Don't panic on unknown bins
This takes the most surgical, direct route to addressing the problem.
Alternatively, we could look into why `cargo rustc` and `cargo check`
are different.

Fixes #15493
2025-05-06 11:13:30 -05:00
Ed Page
02ec8d48e5 test(rustc): Show the bug 2025-05-06 11:05:10 -05:00
Weihang Lo
33d7b0aa92
test: Remove unused nightly requirements (#15498)
### What does this PR try to resolve?

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

### Additional information
2025-05-06 18:26:39 +00:00
Ed Page
b81c676ebc test: Remove unused nightly requirements 2025-05-06 12:52:20 -05:00
Eric Huss
7bdd872cec
Add support for -Zembed-metadata (#15378)
### What does this PR try to resolve?

This PR adds Cargo integration for the new unstable `-Zembed-metadata`
rustc flag, which was implemented in
https://github.com/rust-lang/rust/pull/137535 ([tracking
issue](https://github.com/rust-lang/rust/issues/139165)). The new
behavior has to be enabled explicitly using a new unstable CLI flag
`-Zno-embed-metadata`.

The `-Zembed-metadata=no` rustc flag can reduce disk usage of compiled
artifacts, and also the size of Rust dynamic library artifacts shipped
to users. However, it is not enough to just pass this flag through
`RUSTFLAGS`; it needs to be integrated within Cargo, because it
interacts with how the `--emit` flag is passed to rustc, and also how
`--extern` args are passed to the final linked artifact build by Cargo.
Furthermore, using the flag for all crates in a crate graph compiled by
Cargo would be suboptimal (this will all be described below).

When you pass `-Zembed-metadata=no` to rustc, it will not store Rust
metadata into the compiled artifact. This is important when compiling
libs/rlibs/dylibs, since it reduces their size on disk. However, this
also means that everytime we use this flag, we have to make sure that we
also:
- Include `metadata` in the `--emit` flag to generate a `.rmeta` file,
otherwise no metadata would be generated whatsoever, which would mean
that the artifact wouldn't be usable as a dependency.
- Pass also `--extern <dep>=<path>.rmeta` when compiling the final
linkable artifact. Before, Cargo would only pass `--extern
<dep>=<path>.[rlib|so|dll]`. Since with `-Zembed-metadata=no`, the
metadata is only in the `.rmeta` file and not in the rlib/dylib, this is
needed to help rustc find out where the metadata lies.
- Note: this essentially doubles the cmdline length when compiling the
final linked artifact. Not sure if that is a concern.

The two points above is what this PR implements, and why this rustc flag
needs Cargo integration.

The `-Zembed-metadata` flag is only passed to libs, rlibs and dylibs. It
does not seem to make sense for other crate types. The one situation
where it might make sense are proc macros, but according to @bjorn3 (who
initially came up with the idea for `-Zembed-metadata`, it isn't really
worth it).

Here is a table that summarizes the changes in passed flags and
generated files on disk for rlibs and dylibs:

| **Crate type** | **Flags** | **Generated files** | **Disk usage** |
|--|--|--|--|
| Rlib/Lib (before) | `--emit=dep-info,metadata,link` | `.rlib` (with
metadata), `.rmeta` (for pipelining) | - |
| Rlib/Lib (after) | `--emit=dep-info,metadata,link -Zembed-metadata=no`
| `.rlib` (without metadata), `.rmeta` (for metadata/pipelining) |
Reduced (metadata no longer duplicated) |
| Dylib (before) | `--emit=dep-info,link` | `[.so\|.dll]` (with
metadata) | - |
| Dylib (after) | `--emit=dep-info,metadata,link -Zembed-metadata=no` |
`[.so\|.dll]` (without metadata), `.rmeta` | Unchanged, but split
between two files |

Behavior for other target kinds/crate types should be unchanged.

From the table above, we can see two benefits of using
`-Zembed-metadata=no`:
- For rlibs/dylibs, we no longer store their metadata twice in the
target directory, thus reducing target directory size.
- For dylibs, we store esssentially the same amount of data on disk, but
the benefit is that the metadata is now in a separate .rmeta file. This
means that you can ship the dylib (`.so`/`.dll`) to users without also
shipping the metadata. This would slightly reduce e.g. the
[size](https://github.com/rust-lang/rust/pull/120855#issuecomment-1937018169)
of the shipped rustc toolchains (note that the size reduction here is
after the toolchain has been already heavily compressed).

Note that if this behavior ever becomes the default, it should be
possible to simplify the code quite a bit, and essentially merge the
`requires_upstream_objects` and `benefits_from_split_metadata`
functions.

I did a very simple initial benchmark to evaluate the space savings on
cargo itself and
[hyperqueue](https://github.com/It4innovations/hyperqueue) (a mid-size
crate from my work) using `cargo build` and `cargo build --release` with
and without `-Zembed-metadata=no`:

![image](https://github.com/user-attachments/assets/a26994a2-156f-4863-a823-1042ebe03bf0)

For debug/incremental builds, the effect is smaller, as the artifact
disk usage is dwarfed by incremental artifacts and debuginfo. But for
(non-incremental) release builds, the disk savings (and also performed
I/O operations) are significantly reduced.

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

I wrote two basic tests. The second one tests a situation where a crate
depends on a dylib dependency, which is quite rare, but the behavior of
this has actually changed in this PR (see comparison table above).
Testing this on various real-world projects (or even trying to enable it
by default across the whole Cargo suite?) might be beneficial.

## Unresolved questions

### Is this a breaking change?
With this new behavior, dylibs and rlibs will no longer contain
metadata. If they are compiled with Cargo, that shouldn't matter, but
other build systems might have to adapt.

### Should this become the default?
I think that in terms of disk size usage and performed I/O operations,
it is a pure win. It should either generate less disk data (for rlibs)
or the ~same amount of data for dylibs (the data will be a bit larger,
because the dylib will still contain a metadata stub header, but that's
like 50 bytes and doesn't scale with the size of the dylib, so it's
negligible).

So I think that eventually, we should just do this by default in Cargo,
unless some concerns are found. I suppose that before stabilizing we
should also benchmark the effect on compilation performance.
2025-05-06 15:53:21 +00:00
Jakub Beránek
8250a90878
Add -Zno-embed-metadata unstable flag 2025-05-05 12:07:34 +02:00
Weihang Lo
6cba807e2c
Improved error message for versions prefixed with v (#15484)
### What does this PR try to resolve?

- Added an error message when version in `CRATE[@<VER>]` or `--version
<VER>` starts with 'v' for `install`, `add`, `yank` and `update
--precise <VER>`
- Check if version is valid in `cargo yank`

Fixes #12331

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

Added tests for each subcommand
2025-05-04 13:44:15 +00:00
Pyrode
2ba61b9dfc update: Added error message for when v is prefixed with version 2025-05-03 22:35:44 +05:30
Pyrode
ef3e28d371 test(update): Added test for prefixed v in version 2025-05-03 22:33:00 +05:30
Pyrode
d07439b7fd yank: Check for bad version & added error message for prefixed v in version 2025-05-03 22:31:08 +05:30
Pyrode
cd27a11da6 test(yank): Added test cases for bad version & prefixed v in version 2025-05-03 22:26:15 +05:30
Pyrode
9ae361a8fe add: Added error message for when v is prefixed with version 2025-05-03 22:25:12 +05:30
Pyrode
ac86dea366 test(add): Added test case for prefixed v in version 2025-05-03 22:24:52 +05:30
Pyrode
7e09951039 install: Added error message for when v is prefixed with version 2025-05-03 22:20:38 +05:30
Pyrode
681597f866 test(install): Added test case for prefixed v in version 2025-05-03 22:19:22 +05:30
whosehang
0e282f77ce chore: fix some typos in comment
Signed-off-by: whosehang <whosehang@outlook.com>
2025-05-03 23:20:53 +08:00
Ed Page
8b5c351550
fix: default to all targets when using --edition and --edition-idioms in cargo fix (#15192)
### What does this PR try to resolve?

Close https://github.com/rust-lang/cargo/issues/13527

As we discussed, `cargo fix` should use the default target selection
with `cargo check`.

In this PR, I modified `cargo fix` to no longer use all targets by
default. For `cargo fix --edition` and `cargo fix --edition-idioms`, it
will retain the old behavior and select all targets.

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

Unit tests

### Additional information
2025-05-02 14:53:33 +00:00
Ed Page
64e3cd1ac5
feat(add): suggest similarly named features (#15438)
### What does this PR try to resolve?
Fixes #15436

### How should we test and review this PR?
There are 3 tests for each test case:
- there are no feature suggestions
- there's only one feature suggestion (most common)
- there are several feature suggestions
2025-05-01 14:56:49 +00:00
Farid Huliiev
ba494bce13
feat: suggest similarly named features 2025-05-01 16:12:36 +02:00
Farid Huliiev
e29810c327
feat: add tests for similarly named features 2025-05-01 16:02:15 +02:00
Joe Neeman
faf7329c4f Only ignore dev-dependencies in dependency order if they have no version 2025-05-01 13:41:00 +07:00
Joe Neeman
efb300775a Add a failing test 2025-05-01 13:18:40 +07:00
Farid Huliiev
49e2f84edd
fix: output a blank line between items 2025-04-30 19:56:31 +02:00
Rustin170506
a6eb2bd334 test: use the correct stats code and do not use --all-targets
Signed-off-by: Rustin170506 <techregister@pm.me>
2025-04-28 23:45:05 +08:00
Ed Page
538c1639ad
feat(add/install): check if given crate argument would be valid with inserted @ symbol (#15441)
Suggest to user to use a crate name with an inserted @ before the first
invalid package name character

Fixes #15318

<!--
Thanks for submitting a pull request 🎉! Here are some tips for you:

* If this is your first contribution, read "Cargo Contribution Guide"
first:
  https://doc.crates.io/contrib/
* Run `cargo fmt --all` to format your code changes.
* Small commits and pull requests are always preferable and easy to
review.
* If your idea is large and needs feedback from the community, read how:
  https://doc.crates.io/contrib/process/#working-on-large-features
* Cargo takes care of compatibility. Read our design principles:
  https://doc.crates.io/contrib/design.html
* When changing help text of cargo commands, follow the steps to
generate docs:

https://github.com/rust-lang/cargo/tree/master/src/doc#building-the-man-pages
* If your PR is not finished, set it as "draft" PR or add "WIP" in its
title.
* It's ok to use the CI resources to test your PR, but please don't
abuse them.

### What does this PR try to resolve?

Explain the motivation behind this change.
A clear overview along with an in-depth explanation are helpful.

You can use `Fixes #<issue number>` to associate this PR to an existing
issue.

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

Demonstrate how you test this change and guide reviewers through your
PR.
With a smooth review process, a pull request usually gets reviewed
quicker.

If you don't know how to write and run your tests, please read the
guide:
https://doc.crates.io/contrib/tests

### Additional information

Other information you want to mention in this PR, such as prior arts,
future extensions, an unresolved problem, or a TODO list.
-->
2025-04-28 15:02:41 +00:00
Zhang Wen
776ea25e55 fix the odd info '1 tries remaining' 2025-04-28 13:29:27 +08:00
Weihang Lo
12555be407
Stabilize automatic garbage collection. (#14287)
This proposes to stabilize automatic garbage collection of Cargo's
global cache data in the cargo home directory.

### What is being stabilized?

This PR stabilizes automatic garbage collection, which is triggered at
most once per day by default. This automatic gc will delete old, unused
files in cargo's home directory.

It will delete files that need to be downloaded from the network after 3
months, and files that can be generated without network access after 1
month. These thresholds are intended to balance the intent of reducing
cargo's disk usage versus deleting too often forcing cargo to do extra
work when files are missing.

Tracking of the last-use data is stored in a sqlite database in the
cargo home directory. Cargo updates timestamps in that database whenever
it accesses a file in the cache. This part is already stabilized.

This PR also stabilizes the `gc.auto.frequency` configuration option.
The primary use case for when a user may want to set that is to set it
to "never" to disable gc should the need arise to avoid it.

When gc is initiated, and there are files to delete, there will be a
progress bar while it is deleting them. The progress bar will disappear
when it finishes. If the user runs with `-v` verbose option, then cargo
will also display which files it deletes.

If there is an error while cleaning, cargo will only display a warning,
and otherwise continue.

### What is not being stabilized?

The manual garbage collection option (via `cargo clean gc`) is not
proposed to be stabilized at this time. That still needs some design
work. This is tracked in
https://github.com/rust-lang/cargo/issues/13060.

Additionally, there are several low-level config options currently
implemented which define the thresholds for when it will delete files. I
think these options are probably too low-level and specific. This is
tracked in https://github.com/rust-lang/cargo/issues/13061.

Garbage collection of build artifacts is not yet implemented, and
tracked in https://github.com/rust-lang/cargo/issues/13136.

### Background

This feature is tracked in
https://github.com/rust-lang/cargo/issues/12633 and was implemented in a
variety of PRs, primarily https://github.com/rust-lang/cargo/pull/12634.

The tests for this feature are located in
https://github.com/rust-lang/cargo/blob/master/tests/testsuite/global_cache_tracker.rs.

Cargo started tracking the last-use data on stable via
https://github.com/rust-lang/cargo/pull/13492 in 1.78 which was released
2024-05-02. This PR is proposing to stabilize automatic deletion in 1.82
which will be released in 2024-10-17.

### Risks

Users who frequently use versions of Rust older than 1.78 will not have
the last-use data tracking updated. If they infrequently use 1.78 or
newer, and use the same cache files, then the last-use tracking will
only be updated by the newer versions. If that time frame is more than 1
month (or 3 months for downloaded data), then cargo will delete files
that the older versions are still using. This means the next time they
run the older version, it will have to re-download or re-extract the
files.

The effects of deleting cache data in environments where cargo's cache
is modified by external tools is not fully known. For example, CI
caching systems may save and restore cargo's cache. Similarly, things
like Docker images that try to save the cache in a layer, or mount the
cache in a read-only filesystem may have undesirable interactions.

The once-a-day performance hit might be noticeable to some people. I've
been using this for several months, and almost never notice it. However,
slower systems, or situations where there is a lot of data to delete
might take a while (on the order of seconds hopefully).
2025-04-27 09:38:21 +00:00
dawe
0df8d68cf9
feat(add): check if given crate argument would be valid with inserted @ symbol, suggest fixed argument 2025-04-27 01:23:51 +02:00
dawe
98326ac0f5
feat(install): check if given crate argument would be valid with inserted @ symbol, suggest fixed argument 2025-04-27 01:23:48 +02:00
dawe
ab0d6a7bd2
test(add): add test to show current behaviour of cargo add when @ symbol is missing for the version 2025-04-27 01:23:26 +02:00
dawe
77f54cc658
test(add): add test to show current behaviour of cargo install when @ symbol is missing for the version 2025-04-26 22:01:38 +02:00
Weihang Lo
ee85adae45
Update doctest xcompile flags (#15455)
This updates the flags used for doctest xcompile to match the upstream
changes in https://github.com/rust-lang/rust/pull/137096 which renamed
and stabilized the flags.

This cannot be merged until after nightly is published tonight.
2025-04-26 13:35:28 +00:00
Eric Huss
f10630f2bf Update doctest xcompile flags
This updates the flags used for doctest xcompile to match the upstream
changes in https://github.com/rust-lang/rust/pull/137096 which renamed
and stabilized the flags.
2025-04-26 06:07:27 -07:00
Ed Page
fb3906d3ab
fix: Suggest similar looking feature names when feature is missing (#15454)
### What does this PR try to resolve?

I recently depended on a package with `preserve-order` rather than
`preserve_order` and the error message didn't help me with the problem
so I figure I'd fix that. I also found other improvements along the way

- Suggest an alternative feature when a feature includes a missing
feature
- Suggest an alternative feature when a dependency includes a missing
feature
- Lower case error messages
- Re-frame prescriptive information as help
- Change plural "features" error messages to singular "feature" as they
can only ever have one (knowing an the `MissingFeature` string only has
one feature in it was important for doing a `closest` match on the
feature).

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

### Additional information
2025-04-25 19:45:13 +00:00
Ed Page
08ed8de4d1 fix(resolver): Suggest similar looking features 2025-04-25 09:21:09 -05:00
Ed Page
f616aaf21a fix(resolver): Make features singular; we only track one 2025-04-24 20:47:03 -05:00
Ed Page
3fd3656479 fix(resolver): Have errors follow rustc's casing convention 2025-04-24 20:40:54 -05:00
Ed Page
93c734b395 fix(features): When feature activates a typo, suggest alt 2025-04-24 16:36:30 -05:00
Ed Page
a38d44c2b0 test(features): Show typoed feature behavior 2025-04-24 16:33:15 -05:00
Ed Page
a1c464457b test(features): Clarify test names 2025-04-24 16:23:54 -05:00
Axel PASCON
55952a3c61 fully fixes #15445
changes summary :
	- change the `pkg_id` field of `struct SerializedUnit<'a>` to be `PackageIdSpec` instead of `PackageId`
	- change the unit-graph testcases to match the changes

(cleaning previous commits so every commit passes CI checks, as required)
2025-04-23 13:19:45 +02:00
Ed Page
69eeb6ad94
Implement RFC3695: Allow boolean literals as cfg predicates (#14649)
### What does this PR try to resolve?

This PR implements https://github.com/rust-lang/rfcs/pull/3695: allow
boolean literals as cfg predicates, i.e. `cfg(true)` and `cfg(false)`.

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

The PR should be reviewed commit by commit and tested by looking at the
tests and using `[target.'cfg(<true/false>)']` in
`Cargo.toml`/`.cargo/config.toml`.

### Additional information

I had to bump `cargo-platform` to `0.3.0` has we are changing `CfgExpr`
enum in a semver incompatible change.

We currently have (thks to
https://github.com/rust-lang/cargo/pull/14671) a forward compatibility
warning against `cfg(true/false)` as identifiers instead of keywords.

~~I choose a use a `Cargo.toml` feature (for the manifest) as well as a
unstable CLI flag for the `.cargo/config.toml` part.~~

~~Given the very small (two occurrences on Github Search) for
[`cfg(true)`](https://github.com/search?q=lang%3Atoml+%2F%28%3F-i%29%5C%5Btarget%5C.%5B%27%22%5Dcfg.*true%2F&type=code)
and
[`cfg(false)`](https://github.com/search?q=lang%3Atoml+%2F%28%3F-i%29%5C%5Btarget%5C.%5B%27%22%5Dcfg.*false%2F&type=code),
I choose to gate the feature under a error and not a warning.~~
2025-04-22 17:54:52 +00:00