CARGO-PACKAGE(1)

πŸ“¦ version 1.80.1, in ubuntu jammy, ubuntu noble

πŸ’– CARGO-PACKAGE(1) πŸ’–

General Commands Manual energy, but make it cute. This is the cargo-package page, bestie, and it's lowkey one of the most important ones in the whole cargo era. Sit down, grab a boba, we're doing this. πŸ§‹βœ¨


🌸 NAME 🌸

cargo-package β€” Assemble the local package into a distributable tarball πŸ“¦


✨ SYNOPSIS ✨

cargo package [options]

periodt. that's the whole invocation. πŸ’…


πŸŽ€ DESCRIPTION πŸŽ€

Okay so here's the tea. This command creates a distributable, compressed .crate file stuffed with the source code of the package that's chillin' in your current directory. Where does it go? Into the target/package directory. That's its home now. 🏑

And like, it's not just zipping and dipping β€” it does a whole lil routine. Here's the run of show:

  1. Load and check the current workspace, doing some basic checks along the way. πŸ•΅οΈβ€β™€οΈ
    • Path dependencies are not allowed unless they have a version key. Cargo will straight up ignore the path key for dependencies in published packages. But dev-dependencies? They do not have this restriction β€” they're the exception, iykyk. πŸ™ˆ
  2. Create the compressed .crate file. πŸ—œοΈ
    • The original Cargo.toml file gets rewritten and normalized. Glow-up, honestly. ✨
    • The [patch], [replace], and [workspace] sections get removed from the manifest. gone. not invited. πŸ’€
    • Cargo.lock is automatically included if the package contains an executable binary or example target. And bestie, cargo-install(1) will use that packaged lock file if the --locked flag is used. πŸ”’
    • A .cargo_vcs_info.json file is included, and it contains information about the current VCS checkout hash if available. (It's not included when you use --allow-dirty, jsyk.) 🌈
  3. Extract the .crate file and build it to verify it can build. πŸ› οΈ
    • This rebuilds your package from scratch to make sure it can actually be built from a pristine state. No vibes-based building here. If you wanna skip this whole step, the --no-verify flag is your girl. 🚫
  4. Check that build scripts did not modify any source files. πŸ‘€

The list of files that get included can be controlled with the include and exclude fields in the manifest. You're in charge, main character. πŸ‘‘

Wanna know more about packaging and publishing? See the reference at https://doc.rust-lang.org/cargo/reference/publishing.html. It's giving required reading. πŸ“š


πŸ¦‹ .cargo_vcs_info.json format πŸ¦‹

Will generate a .cargo_vcs_info.json in the following format:

{
 "git": {
   "sha1": "aac20b6e7e543e6dd4118b246c77225e3a3a1302"
 },
 "path_in_vcs": ""
}

path_in_vcs will be set to a repo-relative path for packages in subdirectories of the version control repository. So if your package lives in a subfolder, that field knows. πŸ’…

The compatibility of this file is maintained under the same policy as the JSON output of cargo-metadata(1). Keep that in mind.

⚠️ okay real talk bestie: note that this file provides a best-effort snapshot of the VCS information. However, the provenance of the package is not verified. There is no guarantee that the source code in the tarball matches the VCS information. So don't be out here treating it like a notarized receipt. It's a vibe check, not a proof. 😭

more parts are coming, don't you dare close the tab 🫢

✨ OPTIONS ✨

πŸŽ€ Package Options

  • -l, --list β€” just show me the files that would go inside the package, no actual package made. Window shopping bestie πŸ›οΈ
  • --no-verify β€” don't bother verifying the contents by building them. Skipping the vibe check, bold move.
  • --no-metadata β€” ignore the warnings about missing human-usable metadata (like the description or the license). Cargo will stop nagging you about it.
  • --allow-dirty β€” lets you package working directories that have uncommitted VCS changes. Yes, even that directory. πŸ™ˆ

πŸ” Package Selection

So here's the tea: by default, when you don't give any package selection options at all, which packages get selected depends on the manifest file you've got going on (based on the current working directory, unless you passed --manifest-path). If that manifest is the root of a workspace, then the workspace's default members get selected. Otherwise, only the package defined by that manifest is selected. Simple as that.

You can set a workspace's default members explicitly with the workspace.default-members key in the root manifest. And if that key is not set: a virtual workspace includes all workspace members (which is literally the same as passing --workspace), while a non-virtual workspace includes only the root crate itself. Periodt.

  • -p spec…, --package spec… β€” only package the packages you name. Check cargo-pkgid(1) for the whole SPEC format situation. You can pass this flag multiple times, and it supports common Unix glob patterns like *, ? and []. But ⚠️ to stop your shell from expanding those glob patterns itself before Cargo ever gets to them, you must wrap each pattern in single quotes or double quotes. Don't skip that part, fr fr.
  • --workspace β€” package every single member in the workspace. All of them. The whole squad. πŸ‘‘
  • --exclude *SPEC*… β€” exclude the packages you name. This one must be used together with the --workspace flag, no solo runs. Can be specified multiple times, and it supports common Unix glob patterns like *, ? and []. And again: to avoid your shell accidentally expanding glob patterns before Cargo handles them, you must put single quotes or double quotes around each pattern. πŸ’…

πŸ› οΈ Compilation Options

  • --target triple β€” package for the given architecture. Default is the host architecture. The general format of the triple is <arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for a list of supported targets. This flag may be specified multiple times.

You can also set this with the build.target config value: https://doc.rust-lang.org/cargo/reference/config.html

Heads up: specifying this flag makes Cargo run in a different mode where the target artifacts get placed in a separate directory. See the build cache docs for the deets: https://doc.rust-lang.org/cargo/guide/build-cache.html

  • *--target-dir *directory** β€” the directory for all generated artifacts and intermediate files. You can also set this with the CARGO_TARGET_DIRenvironment variable, or thebuild.target-dirconfig value:https://doc.rust-lang.org/cargo/reference/config.html. Defaults to target` in the root of the workspace.

🌸 Feature Selection

The feature flags are how you control which features get enabled. When you give no feature options at all, the default feature is activated for every selected package.

See the features documentation for more: https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options

  • -F features, --features features β€” a space or comma separated list of features to activate. Features of workspace members can be enabled with the package-name/feature-name syntax. This flag may be specified multiple times, which enables all the specified features. Stack 'em up ✨
  • --all-features β€” activate every available feature of all selected packages. Go feral.
  • --no-default-features β€” do not activate the default feature of the selected packages.

πŸ“œ Manifest Options

  • --manifest-path path β€” the path to the Cargo.toml file. By default, Cargo searches for the Cargo.toml file in the current directory or any parent directory.
  • --locked β€” asserts that the exact same dependencies and versions are used as when the existing Cargo.lock file was originally generated. Cargo will exit with an error when either of these scenarios shows up:
    • The lock file is missing.
    • Cargo attempted to change the lock file due to a different dependency resolution.

It may be used in environments where deterministic builds are desired, such as in CI pipelines.

  • --offline β€” stops Cargo from accessing the network for any reason. Without this flag, Cargo will stop with an error if it needs to access the network and the network isn't available. With this flag, Cargo will attempt to proceed without the network if possible.
Beware: this may result in different dependency resolution than online mode. Cargo restricts itself to crates that are downloaded locally, even if there might be a newer version as indicated in the local copy of the index. See the cargo-fetch(1) command to download dependencies before going offline.

You can also set this with the net.offline config value: https://doc.rust-lang.org/cargo/reference/config.html

  • --frozen β€” equivalent to specifying both --locked and --offline. Two flags in a trench coat πŸ§₯

🎲 Miscellaneous Options

  • -j N, --jobs N β€” number of parallel jobs to run. You can also set this with the build.jobs config value: https://doc.rust-lang.org/cargo/reference/config.html. Defaults to the number of logical CPUs. If negative, it sets the maximum number of parallel jobs to the number of logical CPUs plus the provided value. If a string default is provided, it sets the value back to defaults. Should not be 0.
  • --keep-going β€” build as many crates in the dependency graph as possible, rather than aborting the build on the first one that fails to build.

For example if the current package depends on dependencies fails and works, one of which fails to build, cargo package -j1 may or may not build the one that succeeds (depending on which one of the two builds Cargo picked to run first), whereas cargo package -j1 --keep-going would definitely run both builds, even if the one run first fails.

πŸ’… Display Options

  • -v, --verbose β€” use verbose output. May be specified twice for "very verbose" output, which includes extra output such as dependency warnings and build script output. You can also set this with the term.verbose config value: https://doc.rust-lang.org/cargo/reference/config.html
  • -q, --quiet β€” do not print cargo log messages. You can also set this with the term.quiet config value: https://doc.rust-lang.org/cargo/reference/config.html
  • --color when β€” control when colored output is used. Valid values:
    • auto (default): automatically detect if color support is available on the terminal.
    • always: always display colors.
    • never: never display colors.

You can also set this with the term.color config value: https://doc.rust-lang.org/cargo/reference/config.html

🌈 Common Options

  • *+toolchain*** β€” if Cargo was installed with rustup, and the first argument to cargobegins with+, it gets interpreted as a rustup toolchain name (such as +stableor+nightly). See the rustup documentation for more on how toolchain overrides work: https://rust-lang.github.io/rustup/overrides.html`
  • --config KEY=VALUE or PATH β€” overrides a Cargo configuration value. The argument should be in TOML syntax of KEY=VALUE, or provided as a path to an extra configuration file. This flag may be specified multiple times. See the command-line overrides section for more: https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides
  • -C PATH β€” changes the current working directory before executing any specified operations. This affects things like where cargo looks by default for the project manifest (Cargo.toml), as well as the directories searched for discovering .cargo/config.toml, for example. This option must appear before the command name, like cargo -C path/to/my-project build.
This option is only available on the nightly channel (https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) and requires the -Z unstable-options flag to enable (see #10098: https://github.com/rust-lang/cargo/issues/10098).
  • -h, --help β€” prints help information.
  • -Z flag β€” unstable (nightly-only) flags to Cargo. Run cargo -Z help for details.

🌍 ENVIRONMENT

See the reference for details on environment variables that Cargo reads: https://doc.rust-lang.org/cargo/reference/environment-variables.html

🚦 EXIT STATUS

  • 0: Cargo succeeded. πŸŽ‰
  • 101: Cargo failed to complete. 😭

🌟 EXAMPLES

  1. Create a compressed .crate file of the current package:
cargo package

πŸ‘€ SEE ALSO

cargo(1), cargo-publish(1)


And that's the whole page bestie, no crumbs left πŸ’– This was CARGO-PACKAGE(1), the Cargo package manual β€” the one and only, main character energy, slaying in terminals everywhere. Mwah, go make a .crate file. 🫢✨

πŸ“¦ where this page comes from