Skip to content

Commit

Permalink
Auto merge of #6934 - ehuss:stabilize-offline, r=alexcrichton
Browse files Browse the repository at this point in the history
Stabilize offline mode.

This stabilizes the `--offline` flag as detailed at #5655 (comment). It also adds the `net.offline` config value.

Closes #5655
Closes #4686
  • Loading branch information
bors committed May 13, 2019
2 parents 02f93ea + 309eb87 commit d56af31
Show file tree
Hide file tree
Showing 55 changed files with 803 additions and 97 deletions.
3 changes: 2 additions & 1 deletion src/bin/cargo/cli.rs
Expand Up @@ -32,7 +32,6 @@ Available unstable (nightly-only) flags:
-Z avoid-dev-deps -- Avoid installing dev-dependencies if possible
-Z minimal-versions -- Install minimal dependency versions instead of maximum
-Z no-index-update -- Do not update the registry, avoids a network request for benchmarking
-Z offline -- Offline mode that does not perform network requests
-Z unstable-options -- Allow the usage of unstable options such as --registry
-Z config-profile -- Read profiles from .cargo/config files
-Z install-upgrade -- `cargo install` will upgrade instead of failing
Expand Down Expand Up @@ -168,6 +167,7 @@ fn execute_subcommand(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
&args.value_of("color").map(|s| s.to_string()),
args.is_present("frozen"),
args.is_present("locked"),
args.is_present("offline"),
arg_target_dir,
&args
.values_of_lossy("unstable-features")
Expand Down Expand Up @@ -239,6 +239,7 @@ See 'cargo help <command>' for more information on a specific command.\n",
)
.arg(opt("frozen", "Require Cargo.lock and cache are up to date").global(true))
.arg(opt("locked", "Require Cargo.lock is up to date").global(true))
.arg(opt("offline", "Run without accessing the network").global(true))
.arg(
Arg::with_name("unstable-features")
.help("Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details")
Expand Down
2 changes: 0 additions & 2 deletions src/cargo/core/features.rs
Expand Up @@ -322,7 +322,6 @@ impl Features {
pub struct CliUnstable {
pub print_im_a_teapot: bool,
pub unstable_options: bool,
pub offline: bool,
pub no_index_update: bool,
pub avoid_dev_deps: bool,
pub minimal_versions: bool,
Expand Down Expand Up @@ -367,7 +366,6 @@ impl CliUnstable {
match k {
"print-im-a-teapot" => self.print_im_a_teapot = parse_bool(v)?,
"unstable-options" => self.unstable_options = true,
"offline" => self.offline = true,
"no-index-update" => self.no_index_update = true,
"avoid-dev-deps" => self.avoid_dev_deps = true,
"minimal-versions" => self.minimal_versions = true,
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/core/resolver/errors.rs
Expand Up @@ -299,9 +299,9 @@ pub(super) fn activation_error(
};

if let Some(config) = config {
if config.cli_unstable().offline {
if config.offline() {
msg.push_str(
"\nAs a reminder, you're using offline mode (-Z offline) \
"\nAs a reminder, you're using offline mode (--offline) \
which can sometimes cause surprising resolution failures, \
if this error is too confusing you may wish to retry \
without the offline flag.",
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_generate_lockfile.rs
Expand Up @@ -36,7 +36,7 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
failure::bail!("you can't generate a lockfile for an empty workspace.")
}

if opts.config.cli_unstable().offline {
if opts.config.offline() {
failure::bail!("you can't update in the offline mode");
}

Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/lockfile.rs
Expand Up @@ -47,7 +47,7 @@ pub fn write_pkg_lockfile(ws: &Workspace<'_>, resolve: &Resolve) -> CargoResult<
}

if !ws.config().lock_update_allowed() {
if ws.config().cli_unstable().offline {
if ws.config().offline() {
failure::bail!("can't update in the offline mode");
}

Expand Down
6 changes: 3 additions & 3 deletions src/cargo/sources/git/source.rs
Expand Up @@ -158,9 +158,9 @@ impl<'cfg> Source for GitSource<'cfg> {
let git_path = self.config.assert_package_cache_locked(&git_path);
let db_path = git_path.join("db").join(&self.ident);

if self.config.cli_unstable().offline && !db_path.exists() {
if self.config.offline() && !db_path.exists() {
failure::bail!(
"can't checkout from '{}': you are in the offline mode (-Z offline)",
"can't checkout from '{}': you are in the offline mode (--offline)",
self.remote.url()
);
}
Expand All @@ -172,7 +172,7 @@ impl<'cfg> Source for GitSource<'cfg> {
let actual_rev = self.remote.rev_for(&db_path, &self.reference);
let should_update = actual_rev.is_err() || self.source_id.precise().is_none();

let (db, actual_rev) = if should_update && !self.config.cli_unstable().offline {
let (db, actual_rev) = if should_update && !self.config.offline() {
self.config.shell().status(
"Updating",
format!("git repository `{}`", self.remote.url()),
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/sources/registry/index.rs
Expand Up @@ -363,7 +363,7 @@ impl<'cfg> RegistryIndex<'cfg> {
yanked_whitelist: &HashSet<PackageId>,
f: &mut dyn FnMut(Summary),
) -> CargoResult<()> {
if self.config.cli_unstable().offline
if self.config.offline()
&& self.query_inner_with_online(dep, load, yanked_whitelist, f, false)? != 0
{
return Ok(());
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/sources/registry/remote.rs
Expand Up @@ -182,7 +182,7 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
}

fn update_index(&mut self) -> CargoResult<()> {
if self.config.cli_unstable().offline {
if self.config.offline() {
if self.repo()?.is_empty()? {
// An empty repository is guaranteed to fail, since hitting
// this path means we need at least one crate. This is an
Expand Down
22 changes: 19 additions & 3 deletions src/cargo/util/config.rs
Expand Up @@ -52,10 +52,15 @@ pub struct Config {
rustdoc: LazyCell<PathBuf>,
/// Whether we are printing extra verbose messages
extra_verbose: bool,
/// `frozen` is set if we shouldn't access the network
/// `frozen` is the same as `locked`, but additionally will not access the
/// network to determine if the lock file is out-of-date.
frozen: bool,
/// `locked` is set if we should not update lock files
/// `locked` is set if we should not update lock files. If the lock file
/// is missing, or needs to be updated, an error is produced.
locked: bool,
/// `offline` is set if we should never access the network, but otherwise
/// continue operating if possible.
offline: bool,
/// A global static IPC control mechanism (used for managing parallel builds)
jobserver: Option<jobserver::Client>,
/// Cli flags of the form "-Z something"
Expand Down Expand Up @@ -119,6 +124,7 @@ impl Config {
extra_verbose: false,
frozen: false,
locked: false,
offline: false,
jobserver: unsafe {
if GLOBAL_JOBSERVER.is_null() {
None
Expand Down Expand Up @@ -560,6 +566,7 @@ impl Config {
color: &Option<String>,
frozen: bool,
locked: bool,
offline: bool,
target_dir: &Option<PathBuf>,
unstable_flags: &[String],
) -> CargoResult<()> {
Expand Down Expand Up @@ -604,6 +611,11 @@ impl Config {
self.extra_verbose = extra_verbose;
self.frozen = frozen;
self.locked = locked;
self.offline = offline
|| self
.get::<Option<bool>>("net.offline")
.unwrap_or(None)
.unwrap_or(false);
self.target_dir = cli_target_dir;
self.cli_flags.parse(unstable_flags)?;

Expand All @@ -619,7 +631,11 @@ impl Config {
}

pub fn network_allowed(&self) -> bool {
!self.frozen() && !self.cli_unstable().offline
!self.frozen() && !self.offline()
}

pub fn offline(&self) -> bool {
self.offline
}

pub fn frozen(&self) -> bool {
Expand Down
4 changes: 4 additions & 0 deletions src/doc/man/cargo-fetch.adoc
Expand Up @@ -23,6 +23,10 @@ file before fetching the dependencies.

If `--target` is not specified, then all target dependencies are fetched.

See also the link:https://crates.io/crates/cargo-prefetch[cargo-prefetch]
plugin which adds a command to download popular crates. This may be useful if
you plan to use Cargo without a network with the `--offline` flag.

== OPTIONS

=== Fetch options
Expand Down
4 changes: 4 additions & 0 deletions src/doc/man/cargo-install.adoc
Expand Up @@ -100,6 +100,10 @@ include::options-target-triple.adoc[]
*--debug*::
Build with the `dev` profile instead the `release` profile.

=== Manifest Options

include::options-locked.adoc[]

=== Miscellaneous Options

include::options-jobs.adoc[]
Expand Down
17 changes: 17 additions & 0 deletions src/doc/man/generated/cargo-bench.html
Expand Up @@ -341,6 +341,23 @@ <h3 id="cargo_bench_manifest_options">Manifest Options</h3>
access.</p>
</div>
</dd>
<dt class="hdlist1"><strong>--offline</strong></dt>
<dd>
<p>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.</p>
<div class="paragraph">
<p>Beware that this may result in 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 <a href="commands/cargo-fetch.html">cargo-fetch(1)</a> command to download dependencies before going
offline.</p>
</div>
<div class="paragraph">
<p>May also be specified with the <code>net.offline</code> <a href="reference/config.html">config value</a>.</p>
</div>
</dd>
</dl>
</div>
</div>
Expand Down
17 changes: 17 additions & 0 deletions src/doc/man/generated/cargo-build.html
Expand Up @@ -286,6 +286,23 @@ <h3 id="cargo_build_manifest_options">Manifest Options</h3>
access.</p>
</div>
</dd>
<dt class="hdlist1"><strong>--offline</strong></dt>
<dd>
<p>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.</p>
<div class="paragraph">
<p>Beware that this may result in 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 <a href="commands/cargo-fetch.html">cargo-fetch(1)</a> command to download dependencies before going
offline.</p>
</div>
<div class="paragraph">
<p>May also be specified with the <code>net.offline</code> <a href="reference/config.html">config value</a>.</p>
</div>
</dd>
</dl>
</div>
</div>
Expand Down
17 changes: 17 additions & 0 deletions src/doc/man/generated/cargo-check.html
Expand Up @@ -277,6 +277,23 @@ <h3 id="cargo_check_manifest_options">Manifest Options</h3>
access.</p>
</div>
</dd>
<dt class="hdlist1"><strong>--offline</strong></dt>
<dd>
<p>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.</p>
<div class="paragraph">
<p>Beware that this may result in 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 <a href="commands/cargo-fetch.html">cargo-fetch(1)</a> command to download dependencies before going
offline.</p>
</div>
<div class="paragraph">
<p>May also be specified with the <code>net.offline</code> <a href="reference/config.html">config value</a>.</p>
</div>
</dd>
</dl>
</div>
</div>
Expand Down
17 changes: 17 additions & 0 deletions src/doc/man/generated/cargo-clean.html
Expand Up @@ -141,6 +141,23 @@ <h3 id="cargo_clean_manifest_options">Manifest Options</h3>
access.</p>
</div>
</dd>
<dt class="hdlist1"><strong>--offline</strong></dt>
<dd>
<p>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.</p>
<div class="paragraph">
<p>Beware that this may result in 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 <a href="commands/cargo-fetch.html">cargo-fetch(1)</a> command to download dependencies before going
offline.</p>
</div>
<div class="paragraph">
<p>May also be specified with the <code>net.offline</code> <a href="reference/config.html">config value</a>.</p>
</div>
</dd>
</dl>
</div>
</div>
Expand Down
17 changes: 17 additions & 0 deletions src/doc/man/generated/cargo-doc.html
Expand Up @@ -245,6 +245,23 @@ <h3 id="cargo_doc_manifest_options">Manifest Options</h3>
access.</p>
</div>
</dd>
<dt class="hdlist1"><strong>--offline</strong></dt>
<dd>
<p>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.</p>
<div class="paragraph">
<p>Beware that this may result in 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 <a href="commands/cargo-fetch.html">cargo-fetch(1)</a> command to download dependencies before going
offline.</p>
</div>
<div class="paragraph">
<p>May also be specified with the <code>net.offline</code> <a href="reference/config.html">config value</a>.</p>
</div>
</dd>
</dl>
</div>
</div>
Expand Down
22 changes: 22 additions & 0 deletions src/doc/man/generated/cargo-fetch.html
Expand Up @@ -26,6 +26,11 @@ <h2 id="cargo_fetch_description">DESCRIPTION</h2>
<div class="paragraph">
<p>If <code>--target</code> is not specified, then all target dependencies are fetched.</p>
</div>
<div class="paragraph">
<p>See also the <a href="https://crates.io/crates/cargo-prefetch">cargo-prefetch</a>
plugin which adds a command to download popular crates. This may be useful if
you plan to use Cargo without a network with the <code>--offline</code> flag.</p>
</div>
</div>
</div>
<div class="sect1">
Expand Down Expand Up @@ -113,6 +118,23 @@ <h3 id="cargo_fetch_manifest_options">Manifest Options</h3>
access.</p>
</div>
</dd>
<dt class="hdlist1"><strong>--offline</strong></dt>
<dd>
<p>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.</p>
<div class="paragraph">
<p>Beware that this may result in 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 <a href="commands/cargo-fetch.html">cargo-fetch(1)</a> command to download dependencies before going
offline.</p>
</div>
<div class="paragraph">
<p>May also be specified with the <code>net.offline</code> <a href="reference/config.html">config value</a>.</p>
</div>
</dd>
</dl>
</div>
</div>
Expand Down
17 changes: 17 additions & 0 deletions src/doc/man/generated/cargo-fix.html
Expand Up @@ -348,6 +348,23 @@ <h3 id="cargo_fix_manifest_options">Manifest Options</h3>
access.</p>
</div>
</dd>
<dt class="hdlist1"><strong>--offline</strong></dt>
<dd>
<p>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.</p>
<div class="paragraph">
<p>Beware that this may result in 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 <a href="commands/cargo-fetch.html">cargo-fetch(1)</a> command to download dependencies before going
offline.</p>
</div>
<div class="paragraph">
<p>May also be specified with the <code>net.offline</code> <a href="reference/config.html">config value</a>.</p>
</div>
</dd>
</dl>
</div>
</div>
Expand Down
17 changes: 17 additions & 0 deletions src/doc/man/generated/cargo-generate-lockfile.html
Expand Up @@ -91,6 +91,23 @@ <h3 id="cargo_generate_lockfile_manifest_options">Manifest Options</h3>
access.</p>
</div>
</dd>
<dt class="hdlist1"><strong>--offline</strong></dt>
<dd>
<p>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.</p>
<div class="paragraph">
<p>Beware that this may result in 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 <a href="commands/cargo-fetch.html">cargo-fetch(1)</a> command to download dependencies before going
offline.</p>
</div>
<div class="paragraph">
<p>May also be specified with the <code>net.offline</code> <a href="reference/config.html">config value</a>.</p>
</div>
</dd>
</dl>
</div>
</div>
Expand Down

0 comments on commit d56af31

Please sign in to comment.