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

cargo clippy error when using compile option --emit=obj, and crate-type = ["staticlib"] #13661

Open
cathy-sjh opened this issue Mar 27, 2024 · 1 comment
Labels
C-bug Category: bug S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request.

Comments

@cathy-sjh
Copy link

Problem

I have a simple crate, the crate-type is staticlib, and depends on lazy_static, when add compile option --emit=obj, cargo clippy error.

Steps

rust_project/Cargo.toml:

[package]
name = "mangle1"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lazy_static = "1.4.0"

[lib]
name = "mangle1"
crate-type = ["staticlib"]

rust_project/src/lib.rs:

use lazy_static::lazy_static;

lazy_static! {
    pub static ref G_LAZY_VALUE1: i32 = 1;
}
pub fn test_func() -> i32 {

    *G_LAZY_VALUE1
}

rust_project/.cargo/config.toml

[build]
rustflags = [
    "--emit=obj",
]

Then cargo build is successful.
But cargo clippy fails with the following error:

Checking lazy_static v1.4.0
    Checking mangle1 v0.1.0 (/usr1/xxxx/rust_program/rust_language_test/clippy_test/rust_project)
error: crate `lazy_static` required to be available in rlib format, but was not found in this form

error: could not compile `mangle1` (lib) due to previous error

When the crate-type is a dylib, the cargo clippy has the same error.
Is there a way to make clippy not report an error, but the --emit=obj option and crate-type = ["staticlib"] can't be changed?

Possible Solution(s)

No response

Notes

No response

Version

cargo 1.72.0-nightly (03bc66b55 2023-06-23)
release: 1.72.0-nightly
commit-hash: 03bc66b55c290324bd46eb22e369c8fae1908f91
commit-date: 2023-06-23
host: aarch64-unknown-linux-gnu
libgit2: 1.6.4 (sys:0.17.2 vendored)
libcurl: 8.1.2-DEV (sys:0.4.63+curl-8.1.2 vendored ssl:OpenSSL/1.1.1u)
ssl: OpenSSL 1.1.1u  30 May 2023
os: Linux 2.0 (SP8) [64-bit]
@cathy-sjh cathy-sjh added C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels Mar 27, 2024
@weihanglo
Copy link
Member

weihanglo commented Mar 28, 2024

cargo clippy runs cargo check underneath the hood. cargo check is a specialized command that avoids code generations as possible as it could. If you run cargo check --verbose you see it passes --extern lazy_static=/.../target/debug/deps/liblazy_static-<xxxx>.rmeta to the rustc invocation for building mangle1. .rmeta is not an object, so it doesn't satify the requirement of staticlib: "all upstream dependencies are required to be available in rlib formats.".

You might ask, --emit obj is set and present in the rust invocation, why it cannot be found. The truth is build.rustflags or any RUSTFLAGS variant is complete opaque to Cargo. Cargo doesn't parse any of those flags, so it doesn't know there are object files emitted.

Is there a way to make clippy not report an error, but the --emit=obj option and crate-type = ["staticlib"] can't be changed?

AFAIK The short answer is no, but there are some alternatives.

  • Remove build.rustflags and instead pass it conditionally via environment variable or command line, so that cargo clippy can works without issues.
  • Remove crate-type = ["staticlib"] from Cargo.toml. Instead, only build staticlib when needed via cargo rustc --crate-type staticlib. This approach might avoid unnecessary staticlib being build when in the future you need to build rlib as well.

Is there any reason hindering you from changing those configurations?

@weihanglo weihanglo added S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request. and removed S-triage Status: This issue is waiting on initial triage. labels Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request.
Projects
None yet
Development

No branches or pull requests

2 participants