259 Commits

Author SHA1 Message Date
Ed Page
876c9fb802 fix(package): Allow packaging of self-cycles with -Zpackage-workspace 2025-06-03 20:03:02 -05:00
Ed Page
27c39b6597 test(package): Show nightly self-dep behavior 2025-06-03 19:58:18 -05: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
Weihang Lo
314df11c74
Revert "fix(package): detect dirtiness for symlinks to submodule"
This reverts commit 71ea2e5c5fa285e8e0336d51fd03ba4a427154bf.

`Repository::discover` and `Repository::status_file` are too expenstive
to run inside a loop. And `cargo package` are doing a lot of duplicate
works for checking submodule VCS status.

The possible fix might look like

* Let `status_submodules` function returns a path entry set, so
  Cargo can check whether a source file is dirty based on that.
* When listing files in `PathSource`, attach the VCS status of a
  path entry assoicated with. Then subsequent operations can skip
  status check entirely.

The above solutions are not trivial, and the dirtiness check is
informational only based on T-cargo conclusion, so we should be
good just reverting the change now.

Again, the caveat of this is that we can't really detect
dirty symlinks that links into a Git submodule.
2025-04-11 14:04:38 -04:00
Weihang Lo
71ea2e5c5f
fix(package): detect dirtiness for symlinks to submodule
If a there is a symlink into a git repository/submodule,
when checking its git status with the wrong outer repo,
we'll get an NotFound error,
as the object doesn't belong to the outer repository.
This kind of error blocked the entire `cargo package` operation.

This fix additionally discovers the nearest Git repository,
and then checks status with that,
assuming the repo is the parent of the source file of the symlink.
This is a best effort solution, so if the check fails we ignore.
2025-04-10 12:43:10 -04:00
Weihang Lo
d760263afb
fix(package): ignore status check failure
Dirtiness check for symlinks is mostly informational.
And changes in submodule would fail git-status as well (see #15384).
To avoid adding complicated logic to handle that,
for now we ignore the status check failure.
2025-04-10 11:11:51 -04:00
Weihang Lo
79473988e5
test(package): show symlink to submodule failed the check 2025-04-10 11:11:44 -04:00
Sebastian Poeplau
aa1b353d64 Prevent undeclared public network access
Stop a few tests which aren't marked with `public_network_test` from
trying to access crates.io. While doing so, also make sure that other
`cargo_add` and `cargo_info` tests initialize the registry.
2025-04-01 11:41:30 +02:00
Weihang Lo
259128d270
fix: revert the behavior checking lockfile's VCS
Lockfile might be gitignore'd,
So it never shows up in `git status`.

However, `cargo packag` vcs checks actually performs
`git status --untracked --ignored`.
It is a bit confusing that lockfile is ignored but still counts as dirty
from the report of `cargo package`.

There are some more nuances:
We check lockfile's VCS status if the lockfile is ouside the current
package root. That means a non-workspace Cargo package will not have
this VCS check. I don't think it is good that we have diverged behaviors
Hence this revert.

We can always re-evaluate later,
as we've reserved rooms for doing more dirty checks.

https://github.com/rust-lang/cargo/issues/14967#issuecomment-2651329783
2025-03-25 13:15:34 -04:00
Ed Page
6cf8267012
feat(package): add --exclude-lockfile flag (#15234)
### What does this PR try to resolve?

Fixes #15059
Fixes #15159

This provides an escape hatch `--exclude-lockfile`for uncommon workflows
that don't verify (`--no-verify` is passed) the build with their
unpublished packages
In effect, this takes the heuristic removed in #14815 and replaces it
with a flag

When `--exclude-lockfile` is enabled,
`cargo package` will not verify the lock file if present,
nor will it generate a new one if absent.
Cargo.lock will not be included in the resulting tarball.

Together with `--no-verify`,
this flag decouples packaging from checking the registry index.
While this is useful for some non-normal workflows that requires
to assemble packages having unpublished dependencies.
It is recommended to use `-Zpackage-workspace` to package the entire
workspace, instead of opting out lockfile.

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

The first commit was stolen from
<1a104b5444>
(credit to @NoisyCoil!)

The second added two failing cases we observed in #15059.

### Additional information
2025-03-14 15:25:36 +00:00
Weihang Lo
f0907fca16
fix(package): report if the lockfile is dirty
Lockfile might not be under the package root, but its entries
may be outdated and get packaged into the `.crate` tarball.
We take a conservative action that if the lockfile is dirty,
then all workspace members are considered dirty.
2025-03-07 08:45:49 -05:00
Weihang Lo
d9908b14f7
test(package): dirty lockfile isn't considered dirty but should 2025-03-06 23:42:49 -05:00
Weihang Lo
1b94e1b27c
fix(package): report if workspace manifest is dirty
Workspace manifest might not be under the package root, but
workspace inheritance may still affect what is packaged into
the `.crate.` tarball. We take a conservative action that if
the workspace manifest is dirty, then all workspace members
are considered dirty.
2025-03-06 23:42:49 -05:00
Ed Page
8686e24c72
refactor: control byte display precision with std::fmt options (#15246)
### What does this PR try to resolve?

Just found we can simplify `human_readable_bytes` a bit.
This also remove the `bytesize` dependency,
since we already have a hand-rolled function.
(It is a good crate, though)

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

Should have no real functional difference.
`cargo package` starts reporting IEC (KiB) instead of SI (KB).
So do some thresholds of `cargo package`.

Could also switch entirely to `bytesize` if that is preferred.

### Additional information
2025-02-28 14:32:42 +00:00
Weihang Lo
425858c742
refactor: control byte display precision with std::fmt options 2025-02-28 08:56:28 -05:00
Ed Page
730348ada9 fix(package): Ensure we can package directories ending with '.rs'
This might have also broken dependencies whose path ends with `.rs` as
well

This broke in #14961
2025-02-27 14:50:56 -06:00
Ed Page
45da135dba test(package): Show cargo script mistaken identity 2025-02-27 14:46:05 -06:00
NoisyCoil
4e8b337eb3
feat(package): add --exclude-lockfile flag
When `--exclude-lockfile` is enabled,
`cargo package` will not verify the lock file if present,
nor will it generate a new one if absent.
Cargo.lock will not be included in the resulting tarball.

Together with `--no-verify`,
this flag decouples packaging from checking the registry index.
While this is useful for some non-normal workflows that requires
to assemble packages having unpublished dependencies.
It is recommended to use `-Zpackage-workspace` to package the entire
workspace, instead of opting out lockfile.
2025-02-26 12:48:48 -05:00
Weihang Lo
b7002ffd5e
test(package): fail with unpublished cyclic/transitive deps
After dd698ff0483bda065d1f78968e70ee86fb116a72,
`cargo package --no-verify` at least fails in three different cases:

* An unpublished package depending on itself as a dev-dependency (cyclic self-referential dev-dependencies).
  * Can be resolved by removing the `version` field from the affected dev-dependency.
  * `-Zpackage-workspace` doesn't help with it.
* Existing `cargo package` has `--package <pkg>` specifying certain unpublished packages.
  * Can be resolved by specifying all unpublished packages in one `cargo` call.
  * `-Zpackage-workspace` also requires all dependency versions available in the target registry when calling, so doesn't help.
* `cargo package --no-verify` has been used as a kind of “plumbing commands” to create tarballs without considering dependency orders. The use cases include:
  * Preparing tarballs for other package managers.
  * Integrating into custom develop workflows for unpublished/internal crates.
  * Constructing custom/private registries.

This commit shows the former two cases.
2025-02-26 12:45:45 -05:00
Ed Page
e1d61b8080 fix(package): Register workspace member renames in overlay
This was reported at https://github.com/rust-lang/cargo/issues/10948#issuecomment-2674289330

I suspect there is a second bug here because my debugging only showed us
doing this for `val-json` and not `concepts` and only when building `utils`.

For the exact mapping between the publish payload and the index,
see
https://doc.rust-lang.org/cargo/reference/registry-index.html#json-schema,
particularly the note about differences.
2025-02-24 11:27:37 -06:00
Ed Page
4213e921ca test(package): Show behavior with renamed members 2025-02-21 08:39:40 -06:00
Ed Page
19d307170f fix(package): Fix lookups to capitalized workspace member's index entry 2025-02-21 08:25:23 -06:00
Ed Page
258db04176 test(package): Show behavior with capitalized member 2025-02-21 08:25:23 -06:00
Weihang Lo
24dd205d5a
fix(package): deduplicate dirty symlink detection
metdata path fields may point to a dirty symlilnk and cause
duplicate report. This commit combines `dirty_metadata_paths`
and `dirty_symlinks` into one so is able to de-duplicate them.
2024-12-31 16:25:31 -05:00
Weihang Lo
de39f59e26
fix(package): check dirtiness of symlink source files
This adds a special case for checking source files are symlinks
and have been modified when under a VCS control

This is required because those paths may link to a file outside the
current package root, but still under the git workdir, affecting the
final packaged `.crate` file.

This may have potential performance issue. If a package contains
thousands of symlinks, Cargo will fire `git status` for each of them.
2024-12-31 16:25:31 -05:00
Weihang Lo
014e516e74
test(package): symlink dirty also under dirtiness check
This show that a regular file under a symlink directory
is also not tarcked by the current vcs dirtiness check.
2024-12-31 16:25:31 -05:00
Weihang Lo
059fe16085
fix(package): warn if symlinks checked out as plain text files
`cargo package` will warn users when git `core.symlinks` is `false`
and some symlinks were checked out as plain files during packaging.

Git config [`core.symlinks`] defaults to true when unset.
In git-for-windows (and git as well),
the config should be set to false explicitly when the repo was created,
if symlink support wasn't detected [^1].

We assume the config was always set at creation time and never changed.
So, if it is true, we don't bother users with any warning.

[^1]: <f1241afcc7/setup.c (L2394-L2403)>

[`core.symlinks`]: https://git-scm.com/docs/git-config#Documentation/git-config.txt-coresymlinks
2024-12-31 14:19:55 -05:00
Weihang Lo
871b17f59a
test(package): show behavior with core.symlinks=false 2024-12-31 11:52:44 -05:00
Weihang Lo
863e1f40de
fix(package): check dirtiness of path fields in manifest
This adds a special case for `package.{readme,license-file}`
to Git VCS status check.
If they were specified with paths outside the current package root,
but still under git workdir, Cargo checks git status of those files
to determine if they were dirty.

We don't need to take care of other fields with path values because

* `PathSource` only list files under the package root.
  Things like `target.path` works for `cargo build`, but won't be
  included in `.crate` file from `cargo publish`.
* The only exceptions are `package.readme`/`package.license-file`.
  Cargo would copy files over if they are outside package root.
2024-12-24 11:59:21 -05:00
Weihang Lo
a991a7dd98
test(package): show corner cases of vcs dirtiness check
This is a test showing corner cases that dirty files outside
the package being packaging actually made the `.crate` file dirty.
However, `cargo package` and `.cargo_vcs_info.json` didn't capture it.
2024-12-24 11:57:40 -05:00
Sebastian Thiel
af92c4445b
Use snapshots instead of matching on parts of the output
Personally I liked that the test was only dependent on what really matters, the lack of presence of a particular filename. Now the test would fail if Cargo one day adds more (generated) files to the package.

Co-authored-by: Weihang Lo <weihanglo@users.noreply.github.com>
2024-12-24 08:03:16 +01:00
Sebastian Thiel
2c3f8d87c4
fix: assure possibly blocking non-files (like FIFOs) won't be picked up for publishing.
This would otherwise cause the publish to hang.
2024-12-23 08:14:42 +01:00
Weihang Lo
d325acea1d
fix(package): use relpath to cwd for vcs dirtiness report
Address https://github.com/rust-lang/cargo/pull/14968#issuecomment-2555901072
2024-12-19 20:22:21 -05:00
Weihang Lo
9e2b373acb
test(package): relative path to cwd for dirtiness report 2024-12-19 20:18:05 -05:00
Weihang Lo
982eb2de3a
fix(package): show dirty filepaths relative to git workdir 2024-12-19 17:34:38 -05:00
Weihang Lo
ecc8e37f40
test(package): track vcs status on workspace member granularity 2024-12-19 17:31:25 -05:00
Ed Page
ad23d73fae fix(publish): Allow dry-run of a non-bumped workspace
Fixes #14789
2024-11-21 09:48:26 -06:00
Ed Page
55350fc670 test: Switch from 'exec_with_output' to 'run'
This is a follow up to #14846 which changed `run` to return the
`RawOutput`.

Reasons I didn't "update" some code to the new `run` return value
- We were actually using `ProcessBuilder::exec_with_output` and I didn't
  want to disentangle what it would take to switch to `Execs`
- We did processing on the `Result` and I didn't want to check how that
  could be updated
2024-11-21 13:48:27 -06:00
Ed Page
f80fc018a6 test: Ensure we don't rely on unit return values
This will allow changing `Execs::run` return type
2024-11-18 20:52:53 -06:00
Sebastian Dröge
dd698ff048 Always include Cargo.lock in published crates
Originally it was only included for packages that have executables or
examples for `cargo install`, however this causes inconsistencies and
is kind of unexpected nowadays, e.g. with cdylib crates.

Including it always only slightly increases the crate size and allows
for all crates to know a set of dependency versions that were working,
which can make regression tracking easier.

Fixes https://github.com/rust-lang/cargo/issues/13447
2024-11-14 09:06:18 +02:00
Ed Page
af3cfd5abe fix(test): Un-deprecate contains assertions 2024-11-07 15:55:35 -06:00
todd
11f84a964c fix: use Registry::describe_source to fix error message and related test cases
`Registry::describe_source` will be used as default error message, but if the message
come from `Registry::describe_source` is empty, then it uses `SourceId` message
2024-10-25 00:35:30 +08:00
Ed Page
5b84fc99c5 feat(test): Snapshot .crate validation 2024-10-17 06:26:47 +08:00
Ed Page
5e35e271bc feat(toml): Add autolib
PR #5335 added `autobins`, etc for #5330.  Nowhere in there is
discussion of `autolib`.

Cargo script disables support for additional build-targets by disabling
discovery.
Except we don't have a way to disable discovery of `autolib`, leading to #14476.
By adding `autolib`, we can continue in that direction.

This also allows us to bypass inferring of libs on published packages,
like all other build-targets which were handled in #13849.

As this seems fairly low controversy, this insta-stabilizes the field.
In prior versions of Cargo, users will get an "unused manifest key"
warning.
For packags where this is set by `cargo publish`, the warning will be suppressed and things will work as normal.
For `cargo vendor`, the same except there will be some churn in the
vendored source as this field will now be set.
For local development, it should be rare to set `autolib` so the lack of
error by discovering a file when this is set shouldn't be a problem.

Fixes #14476
2024-09-24 11:24:24 -05:00
Weihang Lo
5dfdd59009
feat: make lockfile v4 the default
This commit makes lockfile version 4 the default version when Cargo
tries to write to a lockfile.

The lockfile version 4 has been stabilized since 1.78.0,
and will become default in 1.83.0.
the length of transition period is pretty similar as before.

One caveat is that in other output from Cargo,
e.g., `cargo metatada`, status messages,
`SourceID` will display in the v4 URL encoded format.
This shouldn't affect the majority of Rust users,
as `SourceId` representation should be opaque to them,
unless comparing `SourceId` across different version of toolchains.
2024-09-24 20:43:02 -04:00
Ed Page
a6cf1be655 test: Switch from allow to expect deprecated
This caught a couple of lingering items.
2024-09-23 20:44:06 -05:00
Joe Neeman
4110b844fb Filter package specs while packaging 2024-08-30 10:10:46 +07:00
Joe Neeman
653025fdd5 Add test for package filtering with old edition 2024-08-30 10:10:46 +07:00
Joe Neeman
96d4d6f6cb Allow unpublishable crates to be packaged 2024-08-20 10:41:19 +07:00