cargo icon indicating copy to clipboard operation
cargo copied to clipboard

Running cargo in a devcontainer gives "No such file or directory (os error 2)"

Open lukejoshuapark opened this issue 1 year ago • 4 comments

Problem

I've encountered this problem a few times when working with Rust/cargo in a devcontainer. The full debug log from cargo is included below

0.010043100s DEBUG cargo::core::workspace: find_members - only me as a member
   0.010150400s DEBUG cargo::util::rustc: adding rustup info to rustc fingerprint
   0.010545100s DEBUG cargo::util::rustc: failed to read rustc info cache: failed to read `/workspaces/rsdc/lv-api/target/.rustc_info.json`
   0.010564900s DEBUG cargo::util::rustc: rustc info cache miss
   0.010567000s DEBUG cargo::util::rustc: running `/root/.rustup/toolchains/1.74.1-x86_64-unknown-linux-gnu/bin/rustc -vV`
   0.021745200s DEBUG cargo::util::rustc: rustc info cache miss
   0.021771200s DEBUG cargo::util::rustc: running `/root/.rustup/toolchains/1.74.1-x86_64-unknown-linux-gnu/bin/rustc - --crate-name ___ --print=file-names --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg`
   0.033355700s DEBUG cargo::util::config::target: Got all targets {}
   0.034553900s DEBUG cargo::ops::resolve: avoid_patch_ids={}
   0.034593500s DEBUG cargo::ops::resolve: attempting to prefer lv-api v0.1.0 (/workspaces/rsdc/lv-api)
   0.034600400s DEBUG cargo::core::registry: load/locked   /workspaces/rsdc/lv-api
   0.034624000s DEBUG cargo::core::resolver: initial activation: lv-api v0.1.0 (/workspaces/rsdc/lv-api)
   0.035662200s DEBUG cargo::ops::resolve: avoid_patch_ids={}
   0.035679100s DEBUG cargo::ops::resolve: attempting to prefer lv-api v0.1.0 (/workspaces/rsdc/lv-api)
   0.035691400s DEBUG cargo::core::registry: load/locked   /workspaces/rsdc/lv-api
   0.035708800s DEBUG cargo::core::resolver: initial activation: lv-api v0.1.0 (/workspaces/rsdc/lv-api)
   0.035765200s DEBUG cargo::core::package: lv-api v0.1.0 (/workspaces/rsdc/lv-api) doesn't need a download
   0.035781100s DEBUG cargo::core::resolver::features: features={
    (
        PackageId {
            name: "lv-api",
            version: "0.1.0",
            source: "/workspaces/rsdc/lv-api",
        },
        NormalOrDev,
    ): {},
}
   0.035830500s DEBUG cargo::util::rustc: adding rustup info to rustc fingerprint
   0.036310000s DEBUG cargo::util::rustc: failed to read rustc info cache: failed to read `/workspaces/rsdc/lv-api/target/.rustc_info.json`
   0.036318700s DEBUG cargo::util::rustc: rustc info cache miss
   0.036319900s DEBUG cargo::util::rustc: running `/root/.rustup/toolchains/1.74.1-x86_64-unknown-linux-gnu/bin/rustc -vV`
   0.046727700s  WARN cargo::util::rustc: failed to update rustc info cache: failed to write `/workspaces/rsdc/lv-api/target/.rustc_info.json`
   0.055625400s  WARN cargo::util::rustc: failed to update rustc info cache: failed to write `/workspaces/rsdc/lv-api/target/.rustc_info.json`
   0.055662500s DEBUG cargo: exit_with_error; err=CliError { error: Some(No such file or directory (os error 2)), exit_code: 101 }
   0.055680700s DEBUG cargo: display_error; err=No such file or directory (os error 2)
error: No such file or directory (os error 2)

Ultimately adding the target directory manually will fix the problem, but then I'll see warning logs like the following when executing cargo build/cargo run etc.

warning: error finalizing incremental compilation session directory `/workspaces/rsdc/lv-api/target/debug/incremental/lv_api-15hp44cqiobch/s-grmht0j19n-1x2ta41-working`: No such file or directory (os error 2)

Steps

  1. Creating a devcontainer for vscode like the following:
{
    "image": "rust:1.74"
}
  1. Re-opening in a devcontainer.
  2. Running cargo build

Gives me

error: No such file or directory (os error 2)

Possible Solution(s)

The only direct solution I've observed is manually creating the target directory, but still observe similar warnings from using cargo.

Notes

No response

Version

cargo 1.74.1 (ecb9851af 2023-10-18)
release: 1.74.1
commit-hash: ecb9851afd3095e988daaa35a48bc7f3cb748e04
commit-date: 2023-10-18
host: x86_64-unknown-linux-gnu
libgit2: 1.7.1 (sys:0.18.0 vendored)
libcurl: 8.4.0-DEV (sys:0.4.68+curl-8.4.0 vendored ssl:OpenSSL/1.1.1u)
ssl: OpenSSL 1.1.1u  30 May 2023
os: Ubuntu 22.4.0 [64-bit]

lukejoshuapark avatar Dec 18 '23 09:12 lukejoshuapark

Could you please provide a minimal reproduction repo? Then we can try it directly.

0xPoe avatar Dec 18 '23 11:12 0xPoe

@hi-rustin Apologies, I should have included that above. Please see here: https://github.com/lukejoshuapark/cargo-oserror2-repro

And screenshot of the repro within the container for me.

Code_zvU9jO4X65

lukejoshuapark avatar Dec 18 '23 21:12 lukejoshuapark

Which host are you running docker on? If it is macOS, this is likely https://github.com/rust-lang/docker-rust/issues/161, which is probably just an issue on the Docker side.

Ideally there should be some context information with the bare error: No such file or directory (os error 2) to indicate what was not found. Unfortunately I can't reproduce that specific error message (I can reproduce several others).

If you want to try to investigate where the error is coming from so that we can add some context to it, you could try the following:

Inside the container, build cargo with some debugging enabled.

  1. git clone https://github.com/rust-lang/cargo.git
  2. cd cargo
  3. cargo build --features anyhow/backtrace
  4. Then inside your workspace directory, run the newly built cargo with RUST_BACKTRACE=1 CARGO_LOG=trace /cargo/target/debug/cargo build (substitute the path to wherever you built cargo, I built it in the root).

ehuss avatar Dec 19 '23 01:12 ehuss

Hey @ehuss, thanks for your response. The host machine is Windows, but I suspect you're right that it is related to the Docker config/setup.

The logs in my original post specify exactly what can't be found:

0.035830500s DEBUG cargo::util::rustc: adding rustup info to rustc fingerprint
   0.036310000s DEBUG cargo::util::rustc: failed to read rustc info cache: failed to read `/workspaces/rsdc/lv-api/target/.rustc_info.json`
   0.036318700s DEBUG cargo::util::rustc: rustc info cache miss
   0.036319900s DEBUG cargo::util::rustc: running `/root/.rustup/toolchains/1.74.1-x86_64-unknown-linux-gnu/bin/rustc -vV`
   0.046727700s  WARN cargo::util::rustc: failed to update rustc info cache: failed to write `/workspaces/rsdc/lv-api/target/.rustc_info.json`
   0.055625400s  WARN cargo::util::rustc: failed to update rustc info cache: failed to write `/workspaces/rsdc/lv-api/target/.rustc_info.json`
   0.055662500s DEBUG cargo: exit_with_error; err=CliError { error: Some(No such file or directory (os error 2)), exit_code: 101 }
   0.055680700s DEBUG cargo: display_error; err=No such file or directory (os error 2)

I don't know enough about this for my opinion to really mean anything, but it seems like cargo can't create new directories due to how the directory from my host machine is mounted into the container.

Hopefully this is enough additional context, but I'll try the custom cargo build tonight when I get home nonetheless. Thanks for your time!

lukejoshuapark avatar Dec 19 '23 01:12 lukejoshuapark