cargo-call-stack icon indicating copy to clipboard operation
cargo-call-stack copied to clipboard

error: missing field `package`

Open robinmoussu opened this issue 3 years ago • 3 comments

I tried to use cargo-call-stack on a project that uses a workspace, and the only output I got was

error: missing field `package`

The only semi-related information I found on internet was this closed issue on cargo-binutils. It feels that the issue could be fixed by replacing cargo-project by cargo-metadata.

robinmoussu avatar Oct 20 '20 08:10 robinmoussu

I can confirm it's not working if used within a workspace

jiayihu avatar Nov 21 '20 16:11 jiayihu

I can also confirm it's not working on a workspace when you run the cargo command from the workspace root. Additionally if you change the working directory to the workspace member that contains the binary and then try the build works but opening the .elf file fails as the binary appears under <workspace root>/target/... instead of <current directory>/target/....

The error you get when you try from the package directory is:

error: couldn't open ELF file `\\?\<absolute-path-to-workspace-member-directory>\target\thumbv7em-none-eabihf\release\<target-elf>`: The system cannot find the path specified. (os error 3)

I created an example workspace for testing, attached below, and here's the output from various commands:

$ cat rust-toolchain.toml
[toolchain]
channel = "nightly-2022-11-16"
components = [ "rust-src", "rustfmt", "llvm-tools-preview" ]
targets = [ "thumbv7em-none-eabihf", "thumbv7m-none-eabi" ]
$ cargo --version
cargo 1.67.0-nightly (a3dfea71c 2022-11-11)
$ cargo call-stack -V
cargo-call-stack 0.1.13
$ cargo call-stack --bin recursive --target=thumbv7em-none-eabihf -v
error: Parse: missing field `package`
$ cargo call-stack --bin recursive_tail --target=thumbv7em-none-eabihf -v
error: Parse: missing field `package`
$ tree
.
├── Cargo.lock
├── Cargo.toml
├── crates
│   ├── example-bin
│   │   ├── Cargo.toml
│   │   └── src
│   │       ├── bin
│   │       │   ├── recursive
│   │       │   │   └── recursive.rs
│   │       │   └── tail_recursive
│   │       │       └── tail_recursive.rs
│   │       ├── lib.rs
│   │       └── panic
│   │           └── panic_handler.rs
│   └── example-lib
│       ├── Cargo.toml
│       └── src
│           └── lib.rs
└── rust-toolchain.toml

9 directories, 10 files
$ cd crates/example-bin/
$ cargo call-stack --bin recursive --target=thumbv7em-none-eabihf -v
"cargo" "rustc" "--target" "thumbv7em-none-eabihf" "--bin" "recursive" "--release" "-Zbuild-std=core,alloc,compiler_builtins" "--color=always" "--" "--emit=llvm-ir,obj" "-C" "embed-bitcode=yes" "-C" "lto=fat"
warning: profiles for the non root package will be ignored, specify profiles at the workspace root:
package:   D:\Users\Hydra\Documents\dev\playground\rust\example-workspace\crates\example-bin\Cargo.toml
workspace: D:\Users\Hydra\Documents\dev\playground\rust\example-workspace\Cargo.toml
warning: D:\Users\Hydra\Documents\dev\playground\rust\example-workspace\crates\example-bin\Cargo.toml: unused manifest key: bin.0.check
warning: D:\Users\Hydra\Documents\dev\playground\rust\example-workspace\crates\example-bin\Cargo.toml: unused manifest key: bin.1.check
   Compiling core v0.0.0 (D:\Users\Hydra\.rustup\toolchains\nightly-2022-11-16-x86_64-pc-windows-gnu\lib\rustlib\src\rust\library\core)
   Compiling compiler_builtins v0.1.82
   Compiling rustc-std-workspace-core v1.99.0 (D:\Users\Hydra\.rustup\toolchains\nightly-2022-11-16-x86_64-pc-windows-gnu\lib\rustlib\src\rust\library\rustc-std-workspace-core)
   Compiling alloc v0.0.0 (D:\Users\Hydra\.rustup\toolchains\nightly-2022-11-16-x86_64-pc-windows-gnu\lib\rustlib\src\rust\library\alloc)
   Compiling example-lib v0.1.0 (D:\Users\Hydra\Documents\dev\playground\rust\example-workspace\crates\example-lib)
   Compiling example-bin v0.1.0 (D:\Users\Hydra\Documents\dev\playground\rust\example-workspace\crates\example-bin)
    Finished release [optimized + debuginfo] target(s) in 19.37s
error: couldn't open ELF file `\\?\D:\Users\Hydra\Documents\dev\playground\rust\example-workspace\crates\example-bin\target\thumbv7em-none-eabihf\release\recursive`: The system cannot find the path specified. (os error 3)

example-workspace-20221121-1822.zip

hydra avatar Nov 21 '22 17:11 hydra

After looking at the source to cargo-project (also written by @japaric, the author of this tool), it appears there's a workaround.

  1. cd into the crate containing the bin you want to build.
  2. manually specify the normal target directory by adding CARGO_TARGET_DIR="<relative-target-path>" to the cargo call-stack ... command.

For example, while taking my example project above:

$ cd crates/example-bin
$ CARGO_TARGET_DIR="../../target" cargo call-stack --bin tail_recursive --target=thumbv7em-none-eabihf -v
"cargo" "rustc" "--target" "thumbv7em-none-eabihf" "--bin" "tail_recursive" "--release" "-Zbuild-std=core,alloc,compiler_builtins" "--color=always" "--" "--emit=llvm-ir,obj" "-C" "embed-bitcode=yes" "-C" "lto=fat"
warning: profiles for the non root package will be ignored, specify profiles at the workspace root:
package:   D:\Users\Hydra\Documents\dev\playground\rust\example-workspace\crates\example-bin\Cargo.toml
workspace: D:\Users\Hydra\Documents\dev\playground\rust\example-workspace\Cargo.toml
warning: D:\Users\Hydra\Documents\dev\playground\rust\example-workspace\crates\example-bin\Cargo.toml: unused manifest key: bin.0.check
warning: D:\Users\Hydra\Documents\dev\playground\rust\example-workspace\crates\example-bin\Cargo.toml: unused manifest key: bin.1.check
   Compiling core v0.0.0 (D:\Users\Hydra\.rustup\toolchains\nightly-2022-11-16-x86_64-pc-windows-gnu\lib\rustlib\src\rust\library\core)
   Compiling compiler_builtins v0.1.82
   Compiling rustc-std-workspace-core v1.99.0 (D:\Users\Hydra\.rustup\toolchains\nightly-2022-11-16-x86_64-pc-windows-gnu\lib\rustlib\src\rust\library\rustc-std-workspace-core)
   Compiling alloc v0.0.0 (D:\Users\Hydra\.rustup\toolchains\nightly-2022-11-16-x86_64-pc-windows-gnu\lib\rustlib\src\rust\library\alloc)
   Compiling example-lib v0.1.0 (D:\Users\Hydra\Documents\dev\playground\rust\example-workspace\crates\example-lib)
   Compiling example-bin v0.1.0 (D:\Users\Hydra\Documents\dev\playground\rust\example-workspace\crates\example-bin)
    Finished release [optimized + debuginfo] target(s) in 19.40s
digraph {
    node [fontname=monospace shape=box]
    0 [label="_start\nmax >= 16\nlocal = 8"]
    1 [label="example_lib::recurse\nmax >= 8\nlocal = 8"]
    1 -> 1
    0 -> 1

    subgraph cluster_0 {
        style=dashed
        fontname=monospace
        label="SCC0"
        1
    }
}

The issue stems from the fact that the target directory is not retrieved from cargo correctly in cargo-project. Helpfully though cargo-project does read the value from CARGO_TARGET_DIR first, which can be seen here:

https://github.com/japaric/cargo-project/blob/v0.3.0/src/lib.rs#L92

I did have a quick look at what the cargo-bin-utils guys did to support cargo-metadata but as they noted, it's quite an invasive change, this is what they did:

https://github.com/rust-embedded/cargo-binutils/pull/50/files

Suggest the same is eventually done in this tool and that cargo-project is deprecated in favor of a more robust solution (cargo-metadeta) which requires less on-going maintenance as it uses machine-generated output instead of making assumptions about how cargo works.

hydra avatar Nov 21 '22 18:11 hydra