Skip to content

cargo metadata --offline requires Internet access #9273

@0xngold

Description

@0xngold

Problem

Actual behavior: running cargo metadata --offline in a dependency of the primary crate tries to access the Internet. If the host has no access, this fails with a network error.
Expected behavior: cargo metadata --offline does not attempt to access the Internet & returns a limited set of fields that can be calculated offline.

Weird complicating factor: if the crate in question is vendored as a path dependency, cargo metadata works without the --offline switch.

Error example

error: proc macro panicked
 --> C:\Users\some_user\.cargo\registry\src\-b20c50870c528e96\windows-0.3.1\build.rs:2:5
2 |     windows_macros::build!();
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^

  = help: message: Cargo metadata \\?\C:\src\.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin\cargo.exe did not contain `workspace_root` key. Got stdout . Got stderr     
          Updating crates.io index
          warning: spurious network error (2 tries remaining): failed to send request: The server name or address could not be resolved
          ; class=Os (2)
          warning: spurious network error (1 tries remaining): failed to send request: The server name or address could not be resolved
          ; class=Os (2)
          error: failed to get `const-sha1` as a dependency of package `windows v0.3.1 (C:\Users\some_user\.cargo\registry\src\-b20c50870c528e96\windows-0.3.1)`

          Caused by:
            failed to load source for dependency `const-sha1`

          Caused by:
            Unable to update registry `https://github.com/rust-lang/crates.io-index`

          Caused by:
            failed to fetch `https://github.com/rust-lang/crates.io-index`

          Caused by:
            failed to send request: The server name or address could not be resolved
            ; class=Os (2)

          .
   Ran from "C:\\Users\\some_user\\.cargo\\registry\\src\\-b20c50870c528e96\\windows-0.3.1"

error: aborting due to previous error

Steps

  1. Create a binary crate C that depends on windows-rs.
  2. Use cargo local-registry to create a local registry for C's dependencies.
  3. Try to build C with the local registry replacing crates.io, and no Internet access. The build should fail with a cryptic error from the windows-rs build. To reproduce the error message I have above, modifying this line inside the local registry's copy of windows_winmd crate. There is almost certainly a simpler repro, but I suspect an exact repro won't be strictly necessary to address this bug.

Possible Solution(s)

When we make windows-rs a path dependency, it allows cargo metadata to run in that crate without errors while offline. Whatever this code path is, can do the same thing in the local registry case? My suspicion is that C's lockfile somehow applies to the cargo metadata command when windows-rs is a path dependency.

Notes

Output of cargo version:

> cargo version
cargo 1.48.0 (65cbdd2dc 2020-10-14)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Error and warning messages generated by Cargo itself.A-offlineArea: offline modeC-bugCategory: bugCommand-metadataS-triageStatus: This issue is waiting on initial triage.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions