Skip to content

Commit 807429e

Browse files
committed
Auto merge of #7026 - ehuss:publish-lockfile-stabilize, r=alexcrichton
Stabilize publish-lockfile. This stabilizes the publish-lockfile feature. Specifically: - Makes `Cargo.lock` included by default for packages with executables. - Deprecates the `publish-lockfile` manifest key. It is no longer used. Additional notes: - Fixed issue where if a `Cargo.lock` file didn't exist, `cargo package` would fail the VCS dirty check. - Changed it so that `cargo publish` or `cargo package` will now show manifest warnings. I believe this was an oversight. Closes #5654
2 parents 7be4ca2 + 34307c6 commit 807429e

20 files changed

+270
-218
lines changed

src/bin/cargo/commands/install.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,16 @@ pub fn cli() -> App {
6868
)
6969
.after_help(
7070
"\
71-
This command manages Cargo's local set of installed binary crates. Only packages
72-
which have [[bin]] targets can be installed, and all binaries are installed into
73-
the installation root's `bin` folder. The installation root is determined, in
74-
order of precedence, by `--root`, `$CARGO_INSTALL_ROOT`, the `install.root`
75-
configuration key, and finally the home directory (which is either
76-
`$CARGO_HOME` if set or `$HOME/.cargo` by default).
71+
This command manages Cargo's local set of installed binary crates. Only
72+
packages which have executable [[bin]] or [[example]] targets can be
73+
installed, and all executables are installed into the installation root's
74+
`bin` folder. The installation root is determined, in order of precedence, by
75+
`--root`, `$CARGO_INSTALL_ROOT`, the `install.root` configuration key, and
76+
finally the home directory (which is either `$CARGO_HOME` if set or
77+
`$HOME/.cargo` by default).
7778
7879
There are multiple sources from which a crate can be installed. The default
79-
location is crates.io but the `--git`, `--path`, and `registry` flags can
80+
location is crates.io but the `--git`, `--path`, and `--registry` flags can
8081
change this source. If the source contains more than one package (such as
8182
crates.io or a git repository with multiple crates) the `<crate>` argument is
8283
required to indicate which crate should be installed.

src/cargo/core/features.rs

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ features! {
186186
[stable] rename_dependency: bool,
187187

188188
// Whether a lock file is published with this crate
189+
// This is deprecated, and will likely be removed in a future version.
189190
[unstable] publish_lockfile: bool,
190191

191192
// Overriding profiles for dependencies.

src/cargo/core/manifest.rs

-3
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,6 @@ impl Manifest {
478478
pub fn publish(&self) -> &Option<Vec<String>> {
479479
&self.publish
480480
}
481-
pub fn publish_lockfile(&self) -> bool {
482-
self.publish_lockfile
483-
}
484481
pub fn replace(&self) -> &[(PackageIdSpec, Dependency)] {
485482
&self.replace
486483
}

src/cargo/core/package.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ impl Package {
240240

241241
/// Returns if package should include `Cargo.lock`.
242242
pub fn include_lockfile(&self) -> bool {
243-
self.manifest().publish_lockfile()
244-
&& self.targets().iter().any(|t| t.is_example() || t.is_bin())
243+
self.targets().iter().any(|t| t.is_example() || t.is_bin())
245244
}
246245
}
247246

src/cargo/ops/cargo_package.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ pub struct PackageOpts<'cfg> {
4242
static VCS_INFO_FILE: &'static str = ".cargo_vcs_info.json";
4343

4444
pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Option<FileLock>> {
45-
// Make sure the Cargo.lock is up-to-date and valid.
46-
ops::resolve_ws(ws)?;
45+
if ws.root().join("Cargo.lock").exists() {
46+
// Make sure the Cargo.lock is up-to-date and valid.
47+
ops::resolve_ws(ws)?;
48+
// If Cargo.lock does not exist, it will be generated by `build_lock`
49+
// below, and will be validated during the verification step.
50+
}
4751
let pkg = ws.current()?;
4852
let config = ws.config();
4953

@@ -615,7 +619,7 @@ fn run_verify(ws: &Workspace<'_>, tar: &FileLock, opts: &PackageOpts<'_>) -> Car
615619
};
616620

617621
let exec: Arc<dyn Executor> = Arc::new(DefaultExecutor);
618-
ops::compile_ws(
622+
ops::compile_with_exec(
619623
&ws,
620624
&ops::CompileOptions {
621625
config,

src/cargo/util/toml/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,11 @@ impl TomlManifest {
10341034
let publish_lockfile = match project.publish_lockfile {
10351035
Some(b) => {
10361036
features.require(Feature::publish_lockfile())?;
1037+
warnings.push(
1038+
"The `publish-lockfile` feature is deprecated and currently \
1039+
has no effect. It may be removed in a future version."
1040+
.to_string(),
1041+
);
10371042
b
10381043
}
10391044
None => features.is_enabled(Feature::publish_lockfile()),

src/doc/man/cargo-install.adoc

+19-4
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ cargo-install - Build and install a Rust binary
1717

1818
== DESCRIPTION
1919

20-
This command manages Cargo's local set of installed binary crates. Only packages
21-
which have `\[[bin]]` targets can be installed, and all binaries are installed into
22-
the installation root's `bin` folder.
20+
This command manages Cargo's local set of installed binary crates. Only
21+
packages which have executable `\[[bin]]` or `\[[example]]` targets can be
22+
installed, and all executables are installed into the installation root's
23+
`bin` folder.
2324

2425
include::description-install-root.adoc[]
2526

2627
There are multiple sources from which a crate can be installed. The default
27-
location is crates.io but the `--git`, `--path`, and `registry` flags can
28+
location is crates.io but the `--git`, `--path`, and `--registry` flags can
2829
change this source. If the source contains more than one package (such as
2930
crates.io or a git repository with multiple crates) the _CRATE_ argument is
3031
required to indicate which crate should be installed.
@@ -42,6 +43,20 @@ specified by setting the `CARGO_TARGET_DIR` environment variable to a relative
4243
path. In particular, this can be useful for caching build artifacts on
4344
continuous integration systems.
4445

46+
By default, the `Cargo.lock` file that is included with the package will be
47+
ignored. This means that Cargo will recompute which versions of dependencies
48+
to use, possibly using newer versions that have been released since the
49+
package was published. The `--locked` flag can be used to force Cargo to use
50+
the packaged `Cargo.lock` file if it is available. This may be useful for
51+
ensuring reproducible builds, to use the exact same set of dependencies that
52+
were available when the package was published. It may also be useful if a
53+
newer version of a dependency is published that no longer builds on your
54+
system, or has other problems. The downside to using `--locked` is that you
55+
will not receive any fixes or updates to any dependency. Note that Cargo did
56+
not start publishing `Cargo.lock` files until version 1.37, which means
57+
packages published with prior versions will not have a `Cargo.lock` file
58+
available.
59+
4560
== OPTIONS
4661

4762
=== Install Options

src/doc/man/cargo-package.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ steps:
2525
- The original `Cargo.toml` file is rewritten and normalized.
2626
- `[patch]`, `[replace]`, and `[workspace]` sections are removed from the
2727
manifest.
28+
- `Cargo.lock` is automatically included if the package contains an
29+
executable binary or example target. man:cargo-install[1] will use the
30+
packaged lock file if the `--locked` flag is used.
2831
- A `.cargo_vcs_info.json` file is included that contains information
2932
about the current VCS checkout hash if available (not included with
3033
`--allow-dirty`).

src/doc/man/generated/cargo-install.html

+20-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ <h2 id="cargo_install_synopsis">SYNOPSIS</h2>
1717
<h2 id="cargo_install_description">DESCRIPTION</h2>
1818
<div class="sectionbody">
1919
<div class="paragraph">
20-
<p>This command manages Cargo&#8217;s local set of installed binary crates. Only packages
21-
which have <code>[[bin]]</code> targets can be installed, and all binaries are installed into
22-
the installation root&#8217;s <code>bin</code> folder.</p>
20+
<p>This command manages Cargo&#8217;s local set of installed binary crates. Only
21+
packages which have executable <code>[[bin]]</code> or <code>[[example]]</code> targets can be
22+
installed, and all executables are installed into the installation root&#8217;s
23+
<code>bin</code> folder.</p>
2324
</div>
2425
<div class="paragraph">
2526
<p>The installation root is determined, in order of precedence:</p>
@@ -45,7 +46,7 @@ <h2 id="cargo_install_description">DESCRIPTION</h2>
4546
</div>
4647
<div class="paragraph">
4748
<p>There are multiple sources from which a crate can be installed. The default
48-
location is crates.io but the <code>--git</code>, <code>--path</code>, and <code>registry</code> flags can
49+
location is crates.io but the <code>--git</code>, <code>--path</code>, and <code>--registry</code> flags can
4950
change this source. If the source contains more than one package (such as
5051
crates.io or a git repository with multiple crates) the <em>CRATE</em> argument is
5152
required to indicate which crate should be installed.</p>
@@ -65,6 +66,21 @@ <h2 id="cargo_install_description">DESCRIPTION</h2>
6566
path. In particular, this can be useful for caching build artifacts on
6667
continuous integration systems.</p>
6768
</div>
69+
<div class="paragraph">
70+
<p>By default, the <code>Cargo.lock</code> file that is included with the package will be
71+
ignored. This means that Cargo will recompute which versions of dependencies
72+
to use, possibly using newer versions that have been released since the
73+
package was published. The <code>--locked</code> flag can be used to force Cargo to use
74+
the packaged <code>Cargo.lock</code> file if it is available. This may be useful for
75+
ensuring reproducible builds, to use the exact same set of dependencies that
76+
were available when the package was published. It may also be useful if a
77+
newer version of a dependency is published that no longer builds on your
78+
system, or has other problems. The downside to using <code>--locked</code> is that you
79+
will not receive any fixes or updates to any dependency. Note that Cargo did
80+
not start publishing <code>Cargo.lock</code> files until version 1.37, which means
81+
packages published with prior versions will not have a <code>Cargo.lock</code> file
82+
available.</p>
83+
</div>
6884
</div>
6985
</div>
7086
<div class="sect1">

src/doc/man/generated/cargo-package.html

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ <h2 id="cargo_package_description">DESCRIPTION</h2>
4444
manifest.</p>
4545
</li>
4646
<li>
47+
<p><code>Cargo.lock</code> is automatically included if the package contains an
48+
executable binary or example target. <a href="commands/cargo-install.html">cargo-install(1)</a> will use the
49+
packaged lock file if the <code>--locked</code> flag is used.</p>
50+
</li>
51+
<li>
4752
<p>A <code>.cargo_vcs_info.json</code> file is included that contains information
4853
about the current VCS checkout hash if available (not included with
4954
<code>--allow-dirty</code>).</p>

src/doc/src/reference/publishing.md

+68-41
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,79 @@ limit to the number of versions which can be published, however.
1313
First thing’s first, you’ll need an account on [crates.io] to acquire
1414
an API token. To do so, [visit the home page][crates.io] and log in via a GitHub
1515
account (required for now). After this, visit your [Account
16-
Settings](https://crates.io/me) page and run the `cargo login` command
16+
Settings](https://crates.io/me) page and run the [`cargo login`] command
1717
specified.
1818

1919
```console
2020
$ cargo login abcdefghijklmnopqrstuvwxyz012345
2121
```
2222

2323
This command will inform Cargo of your API token and store it locally in your
24-
`~/.cargo/credentials` (previously it was `~/.cargo/config`). Note that this
25-
token is a **secret** and should not be shared with anyone else. If it leaks for
26-
any reason, you should regenerate it immediately.
24+
`~/.cargo/credentials`. Note that this token is a **secret** and should not be
25+
shared with anyone else. If it leaks for any reason, you should regenerate it
26+
immediately.
2727

2828
### Before publishing a new crate
2929

3030
Keep in mind that crate names on [crates.io] are allocated on a first-come-first-
3131
serve basis. Once a crate name is taken, it cannot be used for another crate.
3232

33+
Check out the [metadata you can
34+
specify](reference/manifest.html#package-metadata) in `Cargo.toml` to ensure
35+
your crate can be discovered more easily! Before publishing, make sure you have
36+
filled out the following fields:
37+
38+
- `authors`
39+
- `license` or `license-file`
40+
- `description`
41+
- `homepage`
42+
- `documentation`
43+
- `repository`
44+
45+
It would also be a good idea to include some `keywords` and `categories`,
46+
though they are not required.
47+
48+
If you are publishing a library, you may also want to consult the [Rust API
49+
Guidelines].
50+
3351
#### Packaging a crate
3452

35-
The next step is to package up your crate into a format that can be uploaded to
36-
[crates.io]. For this we’ll use the `cargo package` subcommand. This will take
37-
our entire crate and package it all up into a `*.crate` file in the
38-
`target/package` directory.
53+
The next step is to package up your crate and upload it to [crates.io]. For
54+
this we’ll use the [`cargo publish`] subcommand. This command performs the following
55+
steps:
56+
57+
1. Perform some verification checks on your package.
58+
2. Compress your source code into a `.crate` file.
59+
3. Extract the `.crate` file into a temporary directory and verify that it
60+
compiles.
61+
4. Upload the `.crate` file to [crates.io].
62+
5. The registry will perform some additional checks on the uploaded package
63+
before adding it.
64+
65+
It is recommended that you first run `cargo publish --dry-run` (or [`cargo
66+
package`] which is equivalent) to ensure there aren't any warnings or errors
67+
before publishing. This will perform the first three steps listed above.
3968

4069
```console
41-
$ cargo package
70+
$ cargo publish --dry-run
4271
```
4372

44-
As an added bonus, the `*.crate` will be verified independently of the current
45-
source tree. After the `*.crate` is created, it’s unpacked into
46-
`target/package` and then built from scratch to ensure that all necessary files
47-
are there for the build to succeed. This behavior can be disabled with the
48-
`--no-verify` flag.
73+
You can inspect the generated `.crate` file in the `target/package` directory.
74+
[crates.io] currently has a 10MB size limit on the `.crate` file. You may want
75+
to check the size of the `.crate` file to ensure you didn't accidentally
76+
package up large assets that are not required to build your package, such as
77+
test data, website documentation, or code generation. You can check which
78+
files are included with the following command:
4979

50-
Now’s a good time to take a look at the `*.crate` file to make sure you didn’t
51-
accidentally package up that 2GB video asset, or large data files used for code
52-
generation, integration tests, or benchmarking. There is currently a 10MB
53-
upload size limit on `*.crate` files. So, if the size of `tests` and `benches`
54-
directories and their dependencies are up to a couple of MBs, you can keep them
55-
in your package; otherwise, better to exclude them.
80+
```console
81+
$ cargo package --list
82+
```
5683

5784
Cargo will automatically ignore files ignored by your version control system
5885
when packaging, but if you want to specify an extra set of files to ignore you
59-
can use the `exclude` key in the manifest:
86+
can use the [`exclude`
87+
key](reference/manifest.html#the-exclude-and-include-fields-optional) in the
88+
manifest:
6089

6190
```toml
6291
[package]
@@ -67,10 +96,8 @@ exclude = [
6796
]
6897
```
6998

70-
The syntax of each element in this array is what
71-
[rust-lang/glob](https://github.com/rust-lang/glob) accepts. If you’d rather
72-
roll with a whitelist instead of a blacklist, Cargo also supports an `include`
73-
key, which if set, overrides the `exclude` key:
99+
If you’d rather explicitly list the files to include, Cargo also supports an
100+
`include` key, which if set, overrides the `exclude` key:
74101

75102
```toml
76103
[package]
@@ -83,28 +110,22 @@ include = [
83110

84111
### Uploading the crate
85112

86-
Now that we’ve got a `*.crate` file ready to go, it can be uploaded to
87-
[crates.io] with the `cargo publish` command. And that’s it, you’ve now published
88-
your first crate!
113+
When you are ready to publish, use the [`cargo publish`] command
114+
to upload to [crates.io]:
89115

90116
```console
91117
$ cargo publish
92118
```
93119

94-
If you’d like to skip the `cargo package` step, the `cargo publish` subcommand
95-
will automatically package up the local crate if a copy isn’t found already.
96-
97-
Be sure to check out the [metadata you can
98-
specify](reference/manifest.html#package-metadata) to ensure your crate can be
99-
discovered more easily!
120+
And that’s it, you’ve now published your first crate!
100121

101122
### Publishing a new version of an existing crate
102123

103-
In order to release a new version, change the `version` value specified in your
104-
`Cargo.toml` manifest. Keep in mind [the semver
105-
rules](reference/manifest.html#the-version-field). Then optionally run `cargo package` if
106-
you want to inspect the `*.crate` file for the new version before publishing,
107-
and run `cargo publish` to upload the new version.
124+
In order to release a new version, change the `version` value specified in
125+
your `Cargo.toml` manifest. Keep in mind [the semver
126+
rules](reference/manifest.html#the-version-field), and consult [RFC 1105] for
127+
what constitutes a semver-breaking change. Then run [`cargo publish`] as
128+
described above to upload the new version.
108129

109130
### Managing a crates.io-based crate
110131

@@ -176,7 +197,7 @@ is likely for you to encounter the following message when working with them:
176197
> It looks like you don’t have permission to query a necessary property from
177198
GitHub to complete this request. You may need to re-authenticate on [crates.io]
178199
to grant permission to read GitHub org memberships. Just go to
179-
https://crates.io/login
200+
<https://crates.io/login>.
180201

181202
This is basically a catch-all for “you tried to query a team, and one of the
182203
five levels of membership access control denied this”. That is not an
@@ -195,7 +216,7 @@ you will get the error above. You may also see this error if you ever try to
195216
publish a crate that you don’t own at all, but otherwise happens to have a team.
196217

197218
If you ever change your mind, or just aren’t sure if [crates.io] has sufficient
198-
permission, you can always go to https://crates.io/login, which will prompt you
219+
permission, you can always go to <https://crates.io/login>, which will prompt you
199220
for permission if [crates.io] doesn’t have all the scopes it would like to.
200221

201222
An additional barrier to querying GitHub is that the organization may be
@@ -218,5 +239,11 @@ the “Grant Access” button next to its name:
218239

219240
![Authentication Access Control](images/auth-level-acl.png)
220241

242+
[RFC 1105]: https://github.com/rust-lang/rfcs/blob/master/text/1105-api-evolution.md
243+
[Rust API Guidelines]: https://rust-lang-nursery.github.io/api-guidelines/
244+
[`cargo login`]: commands/cargo-login.html
245+
[`cargo package`]: commands/cargo-package.html
246+
[`cargo publish`]: commands/cargo-publish.html
221247
[crates.io]: https://crates.io/
222248
[oauth-scopes]: https://developer.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/
249+

0 commit comments

Comments
 (0)