Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Suggestion] E0432 - could it include suggestions to add "lib" in crate-type? #111243

Open
dannywillems opened this issue May 5, 2023 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-cargo Relevant to the cargo team, which will review and decide on the PR/issue.

Comments

@dannywillems
Copy link

dannywillems commented May 5, 2023

Hi,

I am learning Rust and started a playbook.

I encountered the error E0432 while trying to use a binary in a library I have just created.
I spent a bit of time to understand it was because of a missing "lib" in crate-type. I thought it was automatically added as it was in the lib section.
Could suggestions be added to error messages?

@jyn514 jyn514 added T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. A-diagnostics Area: Messages for errors, warnings, and lints labels May 24, 2023
@jyn514
Copy link
Member

jyn514 commented May 24, 2023

Here are the commands cargo generates for a crate-type = ["cdylib", "staticlib"] crate:

   Compiling example v0.1.0 (/home/jyn/src/example)
     Running `/home/jyn/.local/lib/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc --crate-name example --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=113 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=70605fc8f37b61e6 -C extra-filename=-70605fc8f37b61e6 --out-dir /home/jyn/.local/lib/cargo/target/debug/deps -C incremental=/home/jyn/.local/lib/cargo/target/debug/incremental -L dependency=/home/jyn/.local/lib/cargo/target/debug/deps`
     Running `/home/jyn/.local/lib/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc --crate-name example --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=113 --crate-type cdylib --crate-type staticlib --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=45e74d774f239f04 --out-dir /home/jyn/.local/lib/cargo/target/debug/deps -C incremental=/home/jyn/.local/lib/cargo/target/debug/incremental -L dependency=/home/jyn/.local/lib/cargo/target/debug/deps`

There are two things going on:

  1. Cargo doesn't pass --emit=metadata to rustc when building the library.
  2. Cargo doesn't wait until the library finishes building to start building the binary.

Also, there is a bug in rustc: passing --emit=metadata generates an empty file:

; rustc --crate-name example --crate-type cdylib --crate-type staticlib --emit=dep-info,link,metadata  src/lib.rs 
; file libexample.rmeta 
libexample.rmeta: empty

I guess cargo's behavior could make sense if you were only building the library (e.g. to link into a C program), but I think cargo should not allow having any target in the package other than the library, since it knows just by crate-type = lib being missing that they'll fail to compile.

@rust-lang/cargo could you transfer this to the cargo repository, please? I don't have permissions.

@jyn514
Copy link
Member

jyn514 commented May 24, 2023

Also, there is a bug in rustc: passing --emit=metadata generates an empty file:

ah, this is #67293.

@ehuss
Copy link
Contributor

ehuss commented May 24, 2023

I'm not entirely clear exactly what this issue is asking for. @dannywillems, can you include the exact Cargo.toml that you had, the file structure of your project, and what the error said?

Perhaps you can also include what you were trying to accomplish? I don't know what exactly was meant by "use a binary in a library". If you mean access code from a binary in a library, that isn't how Cargo works. Intra-package dependencies go the other way, where binaries can only access code from a library.

@jyn514
Copy link
Member

jyn514 commented May 24, 2023

@ehuss
Copy link
Contributor

ehuss commented May 24, 2023

Oh, thanks! Sorry I missed that, that's exactly what I need.

So I'm not sure there is a lot cargo can do in this case. It doesn't know anything about the code, or whether or not you intend to access a library from the binary. crate-type = ["cdylib", "staticlib"] is a legitimate configuration, and it is fine to write binaries that don't use the library as a Rust library. The only option I can think of is that rustc could try to detect if there is a static lib, dylib, or zero-length rmeta in the search path, and let you know that it found the non-rust-library types and provide some kind of hint like what is recommended here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-cargo Relevant to the cargo team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants
@ehuss @dannywillems @jyn514 and others