Skip to content

dylib crate fails to compile #138339

@Dotnet-x86

Description

@Dotnet-x86

I tried this code and run: demo.zip

Code

cargo run dev

Engine

lib.rs

pub fn run () {
    println!("Hello from engine");
}

cargo.toml

[package]
name = "engine"
version = "0.1.0"
edition = "2024"

[dependencies]

[lib]
crate-type = ["dylib"]

Editor

main.rs

extern crate engine;
fn main() {
    engine::run();
    println!("Hello from editor");
}

cargo.toml

[package]
name = "editor"
version = "0.1.0"
edition = "2024"

[dependencies]
engine = {path = "../engine"}

workspace.cargo.toml

[workspace]
resolver = "2"
members = [ "editor","engine"]

I expected the dylib crate to work correctly as a dependency, allowing the binary to dynamically link to it and run without issues.

Instead, this happened:

Details

PS C:\Users\{}\Documents\Rust\development> cargo run dev
   Compiling editor v0.1.0 (C:\Users\compl\Documents\Rust\development\editor)
error: cannot satisfy dependencies so `engine` only shows up once
  |
  = help: having upstream crates all available in one format will likely make this go away
  = note: `engine` was unavailable as a static crate, preventing fully static linking     

error: cannot satisfy dependencies so `core` only shows up once
  |
  = help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so `alloc` only shows up once
  |
  = help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so `compiler_builtins` only shows up once
  |
  = help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so `rustc_std_workspace_core` only shows up once
  |
  = help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so `unwind` only shows up once
  |
  = help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so `cfg_if` only shows up once
  |
  = help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so `hashbrown` only shows up once
  |
  = help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so `rustc_std_workspace_alloc` only shows up once
  |
  = help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so `std_detect` only shows up once
  |
  = help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so `rustc_demangle` only shows up once
  |
  = help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so `windows_targets` only shows up once
  |
  = help: having upstream crates all available in one format will likely make this go away

error: cannot satisfy dependencies so `panic_unwind` only shows up once
  |
  = help: having upstream crates all available in one format will likely make this go away

error: could not compile `editor` (bin "editor") due to 13 previous errors

Tools

rustc --version --verbose:

rustc 1.87.0-nightly (9fb94b32d 2025-03-10)
binary: rustc
commit-hash: 9fb94b32df38073bf63d009df77ed10cb1c989d0
commit-date: 2025-03-10
host: x86_64-pc-windows-msvc
release: 1.87.0-nightly
LLVM version: 20.1.0

Activity

added
I-prioritizeIssue: Indicates that prioritization has been requested for this issue.
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Mar 11, 2025
added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
A-cratesArea: Crates and their interactions (like crate loading)
E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issue
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Mar 11, 2025
Noratrieb

Noratrieb commented on Mar 11, 2025

@Noratrieb
Member

Thank you for the report! It would be useful to bisect the regression using cargo-bisect-rustc to make it easier to figure out what happened and ping the relevant people.
@rustbot label E-needs-bisection

moxian

moxian commented on Mar 11, 2025

@moxian
Contributor

cargo-bisect-rustc output attached, but I appear to be too tired to intelligently triage it myself right now:

take2:

searched toolchains nightly-2020-04-23 through nightly-2024-06-07


********************************************************************************
Regression in nightly-2021-03-15
********************************************************************************

fetching https://static.rust-lang.org/dist/2021-03-14/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2021-03-14: 40 B / 40 B [=======================================================] 100.00 % 76.44 KB/s converted 2021-03-14 to acca818928654807ed3bc1ce0e97df118f8716c8
fetching https://static.rust-lang.org/dist/2021-03-15/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2021-03-15: 40 B / 40 B [======================================================] 100.00 % 250.40 KB/s converted 2021-03-15 to d6eaea1c8860adb5302d2fbaad409e36585ab217
looking for regression commit between 2021-03-14 and 2021-03-15
fetching (via remote github) commits from max(acca818928654807ed3bc1ce0e97df118f8716c8, 2021-03-12) to d6eaea1c8860adb5302d2fbaad409e36585ab217
ending github query because we found starting sha: acca818928654807ed3bc1ce0e97df118f8716c8
get_commits_between returning commits, len: 8
  commit[0] 2021-03-13: Auto merge of #83064 - cjgillot:fhash, r=jackh726
  commit[1] 2021-03-14: Auto merge of #83105 - JohnTitor:rollup-tqpm8pb, r=JohnTitor
  commit[2] 2021-03-14: Auto merge of #83028 - GuillaumeGomez:prevent-js-error-if-no-filter, r=Nemo157
  commit[3] 2021-03-14: Auto merge of #83094 - GuillaumeGomez:crates-js-location, r=Nemo157
  commit[4] 2021-03-14: Auto merge of #83044 - kubo39:set-llvm-code-model, r=nikic
  commit[5] 2021-03-14: Auto merge of #83082 - cjgillot:defkey-ii, r=oli-obk
  commit[6] 2021-03-14: Auto merge of #82399 - petrochenkov:modin2, r=Aaron1011
  commit[7] 2021-03-14: Auto merge of #83062 - JohnTitor:improve-reassign-err, r=davidtwco
ERROR: no CI builds available between acca818928654807ed3bc1ce0e97df118f8716c8 and d6eaea1c8860adb5302d2fbaad409e36585ab217 within last 167 days
old incorrect bisection attempt. don't look
searched toolchains nightly-2019-11-05 through nightly-2024-06-07

********************************************************************************
Regression in nightly-2020-04-23
********************************************************************************

fetching https://static.rust-lang.org/dist/2020-04-22/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2020-04-22: 40 B / 40 B [=======================================================] 100.00 % 48.67 KB/s converted 2020-04-22 to 45d050cde277b22a755847338f2acc2c7b834141
fetching https://static.rust-lang.org/dist/2020-04-23/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2020-04-23: 40 B / 40 B [=======================================================] 100.00 % 82.64 KB/s converted 2020-04-23 to b2e36e6c2d229126b59e892c9147fbb68115d292
looking for regression commit between 2020-04-22 and 2020-04-23
fetching (via remote github) commits from max(45d050cde277b22a755847338f2acc2c7b834141, 2020-04-20) to b2e36e6c2d229126b59e892c9147fbb68115d292
ending github query because we found starting sha: 45d050cde277b22a755847338f2acc2c7b834141
get_commits_between returning commits, len: 7
  commit[0] 2020-04-21: Auto merge of #71170 - spastorino:dyn-fnonce-alignment, r=nikomatsakis
  commit[1] 2020-04-22: Auto merge of #71402 - ehuss:update-cargo, r=ehuss
  commit[2] 2020-04-22: Auto merge of #71410 - JohnTitor:rollup-vh6dut5, r=JohnTitor
  commit[3] 2020-04-22: Auto merge of #71323 - nnethercote:bitcode-in-rlib, r=alexcrichton
  commit[4] 2020-04-22: Auto merge of #71424 - Dylan-DPC:rollup-iunh61a, r=Dylan-DPC
  commit[5] 2020-04-22: Auto merge of #71374 - nnethercote:alphabetize-C-and-Z-options, r=petrochenkov
  commit[6] 2020-04-22: Auto merge of #71431 - Dylan-DPC:rollup-rvm6tfy, r=Dylan-DPC
ERROR: no CI builds available between 45d050cde277b22a755847338f2acc2c7b834141 and b2e36e6c2d229126b59e892c9147fbb68115d292 within last 167 days
bjorn3

bjorn3 commented on Mar 11, 2025

@bjorn3
moxian

moxian commented on Mar 11, 2025

@moxian
Dotnet-x86

Dotnet-x86 commented on Mar 11, 2025

@Dotnet-x86
Author

Still running cargo bisect-rustc but haven't found a working version yet.

Image

tgross35

tgross35 commented on Mar 11, 2025

@tgross35
Contributor

Do you know this worked in a previous version? If not then you probably don't have to hunt for a working version, it may never have built correctly. If so, then you should be able to pass that version to cargo-bisect-rustc with --start.

ChrisDenton

ChrisDenton commented on Mar 11, 2025

@ChrisDenton
Member

The issue is you're mixing the static standard library with the dynamic standard library. You always need to use either one or the other.

If you want the dynamic version then add the rustflag -C prefer-dynamic. To do this, create the file .cargo/config.toml and add this to it:

[build]
rustflags = ["-C", "prefer-dynamic"]
ChrisDenton

ChrisDenton commented on Mar 11, 2025

@ChrisDenton
Member

The demo did used to "just work" way back in 1.51 but I don't know why. Quite possibly 1.52 fixed a bug where duplicates weren't being detected.

moxian

moxian commented on Mar 11, 2025

@moxian
Contributor

bisects to #83070 -> rust-lang/cargo#9252

The current logic has a strange behavior where it works as intended if there is only one package in the workspace, but a workspace with multiple packages will always use prefer-dynamic.

Instead of using current_opt, which isn't a good concept to use in a workspace, I switched this to be "primary" (a package selected on the command-line).

The demo does indeed build fine when the package is explicitly specified (cargo build -p editor).
Although... with caveats:

> cargo clean
> cargo build    # doesn't work, as shown in OP
   Compiling engine v0.1.0 (..)
   Compiling editor v0.1.0 (..)
error: cannot satisfy dependencies so `engine` only shows up once
  |
  = help: having upstream crates all available in one format will likely make this go away
  = note: `engine` was unavailable as a static crate, preventing fully static linking
  <...>
error: could not compile `editor` (bin "editor") due to 13 previous errors

> cargo build -p editor    # still doesn't work
   Compiling editor v0.1.0 (H:\work\my\trash\rust-mini\demo\editor)
error: cannot satisfy dependencies so `engine` only shows up once
  |
  = help: having upstream crates all available in one format will likely make this go away
  = note: `engine` was unavailable as a static crate, preventing fully static linking
  <...>
error: could not compile `editor` (bin "editor") due to 13 previous errors


# however if we don't run the "wrong" command, then:
> cargo clean
> cargo build -p editor    # succeeds now
   Compiling engine v0.1.0 (...)
   Compiling editor v0.1.0 (....)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.76s

This statefulness and "build poisoning" is at leastsomewhat surprising, and I see no easy way to get ourselves out of the "broken" state sans a full rebuild.

@rustbot label: -E-needs-bisection +S-has-bisection +T-cargo -T-compiler +A-linkage

added
S-has-bisectionStatus: A bisection has been found for this issue
T-cargoRelevant to the cargo team, which will review and decide on the PR/issue.
A-linkageArea: linking into static, shared libraries and binaries
and removed
E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Mar 11, 2025
Dotnet-x86

Dotnet-x86 commented on Mar 12, 2025

@Dotnet-x86
Author

The issue is you're mixing the static standard library with the dynamic standard library. You always need to use either one or the other.

If you want the dynamic version then add the rustflag -C prefer-dynamic. To do this, create the file .cargo/config.toml and add this to it:

[build]
rustflags = ["-C", "prefer-dynamic"]

Thank you! I tried this, and it works now.

removed
I-prioritizeIssue: Indicates that prioritization has been requested for this issue.
regression-untriagedUntriaged performance or correctness regression.
on Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-cratesArea: Crates and their interactions (like crate loading)A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.S-has-bisectionStatus: A bisection has been found for this issueS-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-cargoRelevant to the cargo team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ChrisDenton@moxian@tgross35@bjorn3@jieyouxu

        Issue actions

          dylib crate fails to compile · Issue #138339 · rust-lang/rust