CARGO-PACKAGE(1)

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

πŸŽ€ CARGO-PACKAGE β€” bestie edition πŸŽ€

from the General Commands Manual collection, it's giving manual page energy


πŸ’– NAME πŸ’–

cargo-package β€” assemble the local package into a distributable tarball


✨ SYNOPSIS ✨

cargo package [options]

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so. this command takes the source code of the package sitting in your current directory and turns it into a distributable, compressed .crate file. that finished file lands in the target/package directory. no cap.

here's the whole glow-up routine it runs through, step by step:

  1. load & vibe-check the workspace β€” it loads the current workspace and runs some basic checks on it.
    • path dependencies are not allowed unless they have a version key. Cargo straight up ignores the path key for dependencies in published packages. dev-dependencies do not have this restriction, they're chill like that.
  2. make the compressed .crate file.
    • the original Cargo.toml file is rewritten and normalized.
    • [patch], [replace], and [workspace] sections get removed from the manifest. bye bestie πŸ’…
    • Cargo.lock is always included. when it's missing, a new lock file will be generated β€” unless the --exclude-lockfile flag is used. also, cargo-install(1) will use the packaged lock file if the --locked flag is used.
    • a .cargo_vcs_info.json file is included, and it contains info about the current VCS checkout hash if available, plus a flag saying whether the worktree is dirty.
    • symlinks get flattened to their target files. flat. like my emotional range during finals.
    • files and directories are included or excluded based on the rules mentioned in the [include] and [exclude] fields β€” https://doc.rust-lang.org/cargo/reference/manifest.html#the-exclude-and-include-fields
  3. extract the .crate file and build it to verify it can build.
    • this rebuilds your package from scratch to make sure it can be built from a pristine state. the --no-verify flag can be used to skip this step entirely.
  4. check that build scripts did not modify any source files. periodt.

the list of files included can be controlled with the include and exclude fields in the manifest.

see the reference https://doc.rust-lang.org/cargo/reference/publishing.html for more details about packaging and publishing. 🫢


🌸 .cargo_vcs_info.json format 🌸

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

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

dirty indicates that the Git worktree was dirty when the package was built. πŸ’€

path_in_vcs will be set to a repo-relative path for packages in subdirectories of the version control repository.

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

note bestie: 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. it's giving "trust me bro" and we are simply reporting it. πŸ™ˆ

πŸ‘‘ OPTIONS πŸ‘‘

πŸ“ Package Options πŸ“

  • -l, --list β€” print the files included in a package without actually making one. window shopping fr.
  • --no-verify β€” don't verify the contents by building them. skipping the vibe check, bold of you.
  • --no-metadata β€” ignore warnings about a lack of human-usable metadata (like the description or the license).
  • --allow-dirty β€” allow working directories with uncommitted VCS changes to be packaged. yes queen, ship it messy.
  • --exclude-lockfile β€” don't include the lock file when packaging.
this flag is not for general use. some tools may expect a lock file to be present (e.g. cargo install --locked). consider other options before using this. 🚨
  • --index index β€” the URL of the registry index to use.
  • --registry registry β€” name of the registry to package for; see cargo publish --help for more details about configuration of registry names. the packages will not be published to this registry, but if we are packaging multiple inter-dependent crates, lock-files will be generated under the assumption that dependencies will be published to this registry.
  • --message-format fmt β€” specifies the output message format. currently, it only works with --list and affects the file listing format. this is unstable and requires -Zunstable-options. valid output formats:
    • human (default): display in a file-per-line format.
    • json: emit machine-readable JSON information about each package. one package per JSON line (newline delimited JSON).
{
  /* The Package ID Spec of the package. */
  "id": "path+file:///home/foo#0.0.0",
  /* Files of this package */
  "files" {
    /* Relative path in the archive file. */
    "Cargo.toml.orig": {
      /* Where the file is from.
         - "generate" for file being generated during packaging
         - "copy" for file being copied from another location.
      */
      "kind": "copy",
      /* For the "copy" kind,
         it is an absolute path to the actual file content.
         For the "generate" kind,
         it is the original file the generated one is based on.
      */
      "path": "/home/foo/Cargo.toml"
    },
    "Cargo.toml": {
      "kind": "generate",
      "path": "/home/foo/Cargo.toml"
    },
    "src/main.rs": {
      "kind": "copy",
      "path": "/home/foo/src/main.rs"
    }
  }
}

🌈 Package Selection 🌈

by default, when no package selection options are given, the packages selected depend on the selected manifest file (based on the current working directory if --manifest-path is not given). if the manifest is the root of a workspace then the workspace's default members are selected, otherwise only the package defined by the manifest will be selected.

the default members of a workspace can be set explicitly with the workspace.default-members key in the root manifest. if this is not set, a virtual workspace will include all workspace members (equivalent to passing --workspace), and a non-virtual workspace will include only the root crate itself.

  • -p *spec*…, --package *spec*… β€” package only the specified packages. see cargo-pkgid(1) for the SPEC format. this flag may be specified multiple times and supports common Unix glob patterns like *, ? and []. however, to avoid your shell accidentally expanding glob patterns before Cargo handles them, you must use single quotes or double quotes around each pattern. yes really, quote it.
  • --workspace β€” package all members in the workspace. the whole squad. πŸ‘―
  • --exclude *SPEC*… β€” exclude the specified packages. must be used in conjunction with the --workspace flag. this flag may be specified multiple times and supports common Unix glob patterns like *, ? and []. however, to avoid your shell accidentally expanding glob patterns before Cargo handles them, you must use single quotes or double quotes around each pattern.

⭐ Compilation Options ⭐

  • --target triple β€” package for the given architecture. the 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.

this may also be specified with the build.target config value β€” https://doc.rust-lang.org/cargo/reference/config.html

note that specifying this flag makes Cargo run in a different mode where the target artifacts are placed in a separate directory. see the build cache documentation for more details β€” https://doc.rust-lang.org/cargo/reference/build-cache.html

  • --target-dir directory β€” directory for all generated artifacts and intermediate files. may also be specified with the CARGO_TARGET_DIR environment variable, or the build.target-dir config value β€” https://doc.rust-lang.org/cargo/reference/config.html. defaults to target in the root of the workspace.

🧚 Feature Selection 🧚

the feature flags let you control which features are enabled. when no feature options are given, the default feature is activated for every selected package.

see the features documentation for more details β€” https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options

  • -F features, --features features β€” space or comma separated list of features to activate. features of workspace members may be enabled with package-name/feature-name syntax. this flag may be specified multiple times, which enables all specified features. stacking them like accessories. πŸ’…
  • --all-features β€” activate all available features of all selected packages. go big or go home.
  • --no-default-features β€” do not activate the default feature of the selected packages. minimalism era.

πŸ¦„ Manifest Options πŸ¦„

  • --manifest-path path β€” path to the Cargo.toml file. by default, Cargo searches for the Cargo.toml file in the current directory or any parent directory. it will find you. 🫢
  • --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 the following scenarios arises:
    • 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. very main character of you.

  • --offline β€” prevents 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 is not available. with this flag, Cargo will attempt to proceed without the network if possible.

beware that this may result in a different dependency resolution than online mode. Cargo will restrict 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.

may also be specified with the net.offline config value β€” https://doc.rust-lang.org/cargo/reference/config.html

  • --frozen β€” equivalent to specifying both --locked and --offline. two icons in one. ✨

  • --lockfile-path PATH β€” moves where the lockfile lives, bestie. Instead of the default spot at <workspace_root>/Cargo.lock, it goes to PATH. And PATH must end with Cargo.lock, okay? Like --lockfile-path /tmp/temporary-lockfile/Cargo.lock. Heads up tho: the moment you pass --lockfile-path, Cargo acts like the lockfile at the default path doesn't exist πŸ’€. It'll either use the lockfile sitting at PATH, or write a fresh new one into PATH if there isn't one already. The whole point? Running most commands in read-only directories, writing the lockfile into the PATH you gave it. ✨
This one's nightly only β€” see https://doc.rust-lang.org/book/appendix-07-nightly-rust.html β€” and you need the -Z unstable-options flag turned on to unlock it (peep issue #14421: https://github.com/rust-lang/cargo/issues/14421). πŸ™ˆ

πŸŽ€ Miscellaneous Options πŸŽ€

  • -j N, --jobs N β€” how many parallel jobs to run at once. You can also set this with the build.jobs config value at https://doc.rust-lang.org/cargo/reference/config.html. Defaults to the number of logical CPUs. If it's negative, it sets the max parallel jobs to the number of logical CPUs plus whatever value you gave. If a string default is provided, it resets the value back to defaults. And it should not be 0, okay? 🚫
  • --keep-going β€” build as many crates in the dependency graph as possible instead of bailing the whole build the second one fails. It's giving perseverance, honestly. πŸ’ͺ

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


✨ Display Options ✨

  • -v, --verbose β€” verbose output, bestie. Say it twice for "very verbose" output, which also throws in extra stuff like dependency warnings and build script output. You can also set this with the term.verbose config value at https://doc.rust-lang.org/cargo/reference/config.html. πŸ‘€
  • -q, --quiet β€” do not print cargo log messages. Silence, iconic. May also be specified with the term.quiet config value at https://doc.rust-lang.org/cargo/reference/config.html. 🀫
  • --color when β€” controls when colored output shows up. Valid values are:
    • auto (default): automatically detect if color support is available on the terminal. πŸ–₯️
    • always: always display colors. 🌈
    • never: never display colors. πŸ’€

May also be specified with the term.color config value at https://doc.rust-lang.org/cargo/reference/config.html.


πŸ’« Common Options πŸ’«

  • +toolchain β€” if Cargo was installed with rustup, and the first argument to cargo begins with +, it gets interpreted as a rustup toolchain name (like +stable or +nightly). See the rustup documentation at https://rust-lang.github.io/rustup/overrides.html for more info on how toolchain overrides work. 🧚
  • --config KEY=VALUE or PATH β€” overrides a Cargo configuration value. The argument should be in TOML syntax of KEY=VALUE, or given as a path to an extra configuration file. This flag can be specified multiple times, no cap. See the command-line overrides section at https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides for more. πŸ’…
  • -C PATH β€” changes the current working directory before executing any specified operations. This affects stuff like where cargo looks by default for the project manifest (Cargo.toml), plus the directories it searches to discover .cargo/config.toml, for example. This option must appear before the command name, like cargo -C path/to/my-project build. Order matters, periodt. πŸ‘‘
This one is also nightly only β€” https://doc.rust-lang.org/book/appendix-07-nightly-rust.html β€” and needs the -Z unstable-options flag to enable (see issue #10098: https://github.com/rust-lang/cargo/issues/10098). 😭
  • -h, --help β€” prints help information. Our fave. 🫢
  • -Z flag β€” unstable (nightly-only) flags for Cargo. Run cargo -Z help for details. πŸ”₯

🌍 ENVIRONMENT 🌍

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


πŸšͺ 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 πŸ¦‹


And that's a wrap on the CARGO-PACKAGE(1) page, bestie! πŸ’– This one's from the Cargo source/version, and the date on it is the Cargo man page date, so go peep the real footer if you need the exact stamp. Now go package something iconic. πŸŽ€βœ¨

πŸ“¦ where this page comes from