CARGO-PACKAGE(1)

๐Ÿ’– cargo-package(1) ๐Ÿ’–

okay bestie, so this is a page from the General Commands Manual, and it's literally all about cargo-package. buckle up, it's giving โœจbuild systemsโœจ but make it cute.


๐ŸŒธ NAME ๐ŸŒธ

cargo-package โ€” Assemble the local package into a distributable tarball


โœจ SYNOPSIS โœจ

cargo package [options]

๐ŸŽ€ DESCRIPTION ๐ŸŽ€

so this command makes a distributable, compressed .crate file out of the source code of the package that's chillin' in your current directory. the file it spits out gets stored in the target/package directory. and here's the whole glow-up process, step by step:

  1. It loads and checks the current workspace, doing some basic checks along the way.
    • Path dependencies are not allowed unless they have a version key. Cargo will ignore the path key for dependencies in published packages. dev-dependencies do not have this restriction. (yes, that's the rule, no cap.)
  2. It creates the compressed .crate file.
    • The original Cargo.toml file is rewritten and normalized.
    • The [patch], [replace], and [workspace] sections get removed from the manifest. bye ๐Ÿ‘‹
    • Cargo.lock is always included. When it's missing, a new lock file will be generated unless the --exclude-lockfile flag is used. cargo-install(1) will use the packaged lock file if the --locked flag is used.
    • A .cargo_vcs_info.json file is included that contains information about the current VCS checkout hash if available, plus a flag for whether the worktree is dirty.
    • Symlinks are flattened to their target files. (flattened!! iconic behaviour honestly)
    • Files and directories are included or excluded based on rules mentioned in the [include] and [exclude] fields <https://doc.rust-lang.org/cargo/reference/manifest.html#the-exclude-and-include-fields>.
  3. It extracts the .crate file and builds it to verify it can build.
    • This rebuilds your package from scratch to make sure it can be built from a pristine state. If you wanna skip this whole step, the --no-verify flag is right there for you ๐Ÿซถ
  4. It checks that build scripts did not modify any source files.

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 ๐Ÿฆ‹

It'll 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 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. ๐Ÿ’€ (yes, seriously, don't @ me)

๐Ÿ’… OPTIONS ๐Ÿ’…

๐Ÿ“ Package Options ๐Ÿ“

  • -l, --list โ€” Print files included in a package without making one.
  • --no-verify โ€” Don't verify the contents by building them.
  • --no-metadata โ€” Ignore warnings about a lack of human-usable metadata (such as the description or the license).
  • --allow-dirty โ€” Allow working directories with uncommitted VCS changes to be packaged.
  • --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.
  • --workspace โ€” Package all members in the workspace.
  • --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 specified target architecture. Flag may be specified multiple times. The default is the host architecture. The general format of the triple is <arch><sub>-<vendor>-<sys>-<abi>.

Possible values:

  • Any supported target in rustc --print target-list.
  • "host-tuple", which will internally be substituted by the host's target. This can be particularly useful if you're cross-compiling some crates, and don't want to specify your host's machine as a target (for instance, an xtask in a shared project that may be worked on by many hosts).
  • A path to a custom target specification. See Custom Target Lookup Path <https://doc.rust-lang.org/rustc/targets/custom.html#custom-target-lookup-path> for more information.

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 <https://doc.rust-lang.org/cargo/reference/build-cache.html> documentation for more details.
  • --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 <https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options> for more details.

  • -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.
  • --all-features โ€” Activate all available features of all selected packages.
  • --no-default-features โ€” Do not activate the default feature of the selected packages.

โญ 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.
  • --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. ๐Ÿ‘‘

  • --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 bestie โ€” this can legitimately give you different dependency resolution than online mode does. Cargo is gonna restrict itself to crates that are downloaded locally, even if a newer version exists in your local copy of the index. So if you wanna go offline and still have everything you need, run the cargo-fetch(1) command first to download your dependencies. ๐Ÿ’…

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

  • --frozen โ€” literally the same as passing both --locked and --offline at once. Two-for-one, no cap. ๐ŸงŠ

๐ŸŒธ Miscellaneous Options ๐ŸŒธ

  • -j N, --jobs N โ€” how many parallel jobs to run. 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 number of parallel jobs to the number of logical CPUs plus the value you gave it. If a string default is provided, it sets the value back to defaults. And it should not be 0, okay?? Not 0. ๐Ÿ™ˆ
  • --keep-going โ€” build as many crates in the dependency graph as possible instead of aborting the whole build the second one of them fails. So iconic of it.

Example time โœจ: say the current package depends on two dependencies โ€” one fails and one works. cargo package -j1 may or may not build the one that succeeds (it depends on which of the two builds Cargo picked to run first), whereas cargo package -j1 --keep-going would definitely run both builds, even if the one it ran first fails. It's giving "we finish what we start." ๐Ÿ‘‘

โœจ Display Options โœจ

  • -v, --verbose โ€” use verbose output. You can specify it twice for "very verbose" output, which includes extra stuff like dependency warnings and build script output. May also be specified with the term.verbose config value at https://doc.rust-lang.org/cargo/reference/config.html.
  • -q, --quiet โ€” do not print cargo log messages. May also be specified with the term.quiet config value at `https://doc.rust-lang.org/cargo/reference/config.html. Shhh. ๐Ÿคซ
  • --color when โ€” controls when colored output gets used. Valid values are:
    • auto (the 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, go off. See the command-line overrides section at https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides for more info.
  • -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), and also the directories it searches when discovering .cargo/config.toml. Important: 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 at https://doc.rust-lang.org/book/appendix-07-nightly-rust.html and it requires the -Z unstable-options flag to enable (see #10098 at https://github.com/rust-lang/cargo/issues/10098). Nightly girlies only, sorry. ๐ŸŒ™
  • -h, --help โ€” prints help information. Your bestie when you forget literally everything.
  • -Z flag โ€” unstable (nightly-only) flags to 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 environment variables that Cargo reads. Yes, it reads them. Yes, there are a lot. ๐Ÿซถ


โญ EXIT STATUS โญ

  • 0: Cargo succeeded. Slay. ๐ŸŽ‰
  • 101: Cargo failed to complete. Not slay. ๐Ÿ˜ญ

๐ŸŽ€ EXAMPLES ๐ŸŽ€

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

๐Ÿฆ‹ SEE ALSO ๐Ÿฆ‹

cargo(1), cargo-publish(1)


That's the whole page, bestie โ€” CARGO-PACKAGE(1) from the Cargo manual, and yes it's giving official-docs energy but make it cute. ๐Ÿ’– Go package something iconic. โœจ๐Ÿ‘‘

๐Ÿ“ฆ where this page comes from