Skip to content

Unable to find crate proc_macro on musl target #40174

Closed

Description

@golddranks

Using procedural derive macros on musl seems to work just fine for the most of the cases, but there is one pitfall; in some cases like this dtolnay/proc-macro-hack#6 build fails because not only the crate that implements the macro (crate marked with proc-macro = true), but also another, normal crate links to the proc_macro crate. "Normal" crates are unable to find proc_macro when built on musl target:

Compiling proc-macro-hack v0.3.0
error[E0463]: can't find crate for `proc_macro`
 --> /root/.cargo/registry/src/github.life-white.uk-1ecc6299db9ec823/proc-macro-hack-0.3.0/src/lib.rs:1:1
  |
1 | extern crate proc_macro;
  | ^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate

error: aborting due to previous error

error: Could not compile `proc-macro-hack`.

Activity

  1. alexcrichton commented on Mar 1, 2017

    @alexcrichton
    Member

    Yes this is currently intentional. The proc_macro crate is only a dynamic library which the musl target currently doesn't support. I unfortunately don't think this will be easy to fix...

  2. golddranks commented on Mar 1, 2017

    @golddranks
    ContributorAuthor

    I understand why the proc macro crates need to be dynamic: they are loaded and run dynamically by the compiler. However what I don't understand is why the proc_macro crate isn't allowed to be linked both dynamically and statically? I'd imagine someone may want to use the types defined in proc_macro in some helper crate, outside the dynamically linked macro crate, as is the case here.

  3. golddranks commented on Mar 1, 2017

    @golddranks
    ContributorAuthor

    Is there some technical hurdle that makes it hard to do, or is it just not done because it's such a minor case?

  4. golddranks commented on Mar 1, 2017

    @golddranks
    ContributorAuthor

    The simplest test for this seems to be:

    test.rs with just single line:

    extern crate proc_macro;
    

    build with (succeeds):

    rustc test.rs --crate-type lib
    

    and with (fails):

    rustc test.rs --crate-type lib --target=x86_64-unknown-linux-musl
    

    When RUST_LOG=info is enabled, the library probes can be seen.

  5. golddranks commented on Mar 1, 2017

    @golddranks
    ContributorAuthor

    Okay, I did some testing.

    I git cloned the Rust source, edited away the crate-type = ["dylib"] annotation in libproc_macro, librustc_data_structures, librustc_errors, libsyntax and libsyntax_pos. (Transitive deps of libproc_macro)

    After that, I built a rlib of libproc_macro with cargo build --target=x86_64-unknown-linux-musl. (This is on the newest nightly.)

    After that, I copied the build artefacts from ~/rust/src/target/x86_64-unknown-linux-musl/debug/deps/ to ~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib.

    After that, I try to compile the things that failed before with musl target → it works.

    So, it seems that there isn't any real technical hurdles. (or if there is, I haven't encountered them yet)

    @alexcrichton, what do you think of distributing rlibs of aforementioned libraries with the musl target?

  6. golddranks commented on Mar 1, 2017

    @golddranks
    ContributorAuthor

    Btw. I did this work on a Linux host. Tried to do the same on macOS (with musl target of course), but on macOS the linking fails with ld: unknown option: --as-needed. The linker is passed a flag --as-needed which seems to be supported on GNU linker only.

  7. golddranks commented on Mar 1, 2017

    @golddranks
    ContributorAuthor

    Ah, okay, the --as-needed behaviour is explained here: #34282

  8. alexcrichton commented on Mar 2, 2017

    @alexcrichton
    Member

    Yes there are technical hurdles in terms of build system of doing that. This is also rarely what you want because there's no reason to have a full Rust parser linked into a binary (when it's not used).

  9. golddranks commented on Mar 2, 2017

    @golddranks
    ContributorAuthor

    I see, that's a convincing argument.

  10. drozdziak1 commented on Jan 10, 2018

    @drozdziak1

    Hello,
    sorry to dig up a closed issue, but I'm experiencing the same problem during an OpenWrt cross build (i686-unknown-linux-musl is the Rust target and VirtualBox is the OpenWrt one). My compiler is rustc 1.25.0-nightly (61452e506 2018-01-09). I've got a couple questions regarding this:

    • Is the root cause known? What is it?
    • Is there a solution out there?
    • If not, where can I go to help solve this?
  11. added a commit that references this issue on Nov 15, 2018
  12. lisbakke commented on Nov 21, 2018

    @lisbakke

    Hello! I've got this issue, as well -- deps relying on proc-macro2 which do not use default-features = false so proc-macro crate is not found because my target does not have it.

    I'm currently compiling rustc for my target and it seems easiest to create proc-macro lib when I'm building. Is there any documentation on how to do this?

  13. mmastrac commented on Oct 6, 2019

    @mmastrac

    For those following various links to this issue, it is important to note that you can compile code using proc_macro to statically-linked musl binaries, BUT you need to cross-compile from a platform that does support dynamic linking.

    cargo build --target x86_64-unknown-linux-musl should be sufficient, but you'll need to run that on something like Ubuntu, Fedora, or another full system.

  14. 20 remaining items

  15. added a commit that references this issue on Mar 23, 2020
  16. Ekleog commented on Apr 16, 2020

    @Ekleog

    There are likely much better solutions, but… I've just come across https://github.com/dtolnay/watt ; and… would integrating this into rustc as a secondary backend for use only on platforms where regular proc macros aren't supported be a solution? It wouldn't support all the use cases I think, due to regular proc macros being allowed to eg. interact with the FS, but it should already support a number of use cases. And maybe, with time, the API could be extended so that proc macros could all be compiled with wasm, and the current dylib approach could be dropped, thus making porting rustc to a new architecture a lot easier?

  17. mati865 commented on Apr 16, 2020

    @mati865
    Member

    @Ekleog is it still an issue after #69519?

  18. Ekleog commented on Apr 16, 2020

    @Ekleog

    @mati865 I'm not sure, I've pinged here following a message from IRC that said this happened just today -- don't know for sure the version of the compiler used, so I've just pinged them

  19. breard-r commented on Apr 24, 2020

    @breard-r

    @mati865 If it's part of the brand new Rust 1.43, then yes, it is still an issue.

    On Alpine, the heavily patched rust package (1.39) can compile without any trouble. However, on the very same machine, any version of Rust (including 1.39 and 1.43) installed via rustup (and therefore without those patches) is subject to this issue.

  20. 12101111 commented on Apr 24, 2020

    @12101111
    Contributor

    @breard-r Rust 1.43 don't include this patch and you can try 1.44 beta from rustup.

  21. breard-r commented on Apr 24, 2020

    @breard-r

    @12101111 Thank you! I've just installed 1.44.0-beta via rustup and it builds! 😃

  22. jonas-schievink commented on Apr 24, 2020

    @jonas-schievink
    Contributor

    Great! Closing as fixed then.

  23. jyn514 commented on May 23, 2020

    @jyn514
    Member

    This is still broken for me using rustc 1.44.0-beta.3 (0f0d70055 2020-05-11).

      = note: /usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find crti.o: No such file or directory
    
    Full linker arguments
    /rcc # cargo build
       Compiling thiserror-impl v1.0.19
    error: linking with `cc` failed: exit code: 1
      |
      = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--eh-frame-hdr" "-m64" "-L" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.0.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.1.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.10.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.11.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.12.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.13.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.14.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.15.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.2.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.3.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.4.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.5.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.6.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.7.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.8.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.9.rcgu.o" "-o" "/rcc/target/debug/deps/libthiserror_impl-c05c536330366b8e.so" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.1x6px3h632f37ynj.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.2thl14zymh2c935g.rcgu.o" "-Wl,--gc-sections" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/rcc/target/debug/deps" "-L" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib" "-Wl,-Bstatic" "/rcc/target/debug/deps/libsyn-fe2290862b758c2c.rlib" "/rcc/target/debug/deps/libquote-afe91c35027afeba.rlib" "/rcc/target/debug/deps/libproc_macro2-0913bab1357133a2.rlib" "/rcc/target/debug/deps/libunicode_xid-8b9fbfbf3102211f.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libproc_macro-84e8be3f2684e30a.rlib" "-Wl,--start-group" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd-5ee1781929f9655d.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libpanic_unwind-e2ec5d2aca499f1c.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libhashbrown-81740b8a57dba39b.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_alloc-eb99f2d2958ad0d9.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libbacktrace-84349410b417ab27.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libbacktrace_sys-b3c09d3262227e0b.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_demangle-c58a107a6842bb60.rlib" "/tmp/rustc3EmKiY/libunwind-33abd88ac2c901d5.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcfg_if-c210a18d812061d6.rlib" "/tmp/rustc3EmKiY/liblibc-2b136be5748c5959.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liballoc-150a1e69c064728f.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_core-e390c28232836237.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-1cdf993f577c22fa.rlib" "-Wl,--end-group" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-9211daa04cc43fa3.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lc" "-shared"
      = note: /usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find crti.o: No such file or directory
              collect2: error: ld returned 1 exit status
              
    
    error: aborting due to previous error
    
    error: could not compile `thiserror-impl`.
    
  24. 12101111 commented on May 23, 2020

    @12101111
    Contributor

    This is still broken for me using rustc 1.44.0-beta.3 (0f0d70055 2020-05-11).

      = note: /usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find crti.o: No such file or directory
    

    Full linker arguments

    /rcc # cargo build
       Compiling thiserror-impl v1.0.19
    error: linking with `cc` failed: exit code: 1
      |
      = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--eh-frame-hdr" "-m64" "-L" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.0.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.1.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.10.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.11.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.12.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.13.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.14.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.15.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.2.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.3.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.4.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.5.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.6.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.7.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.8.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.thiserror_impl.7sf2w6hc-cgu.9.rcgu.o" "-o" "/rcc/target/debug/deps/libthiserror_impl-c05c536330366b8e.so" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.1x6px3h632f37ynj.rcgu.o" "/rcc/target/debug/deps/thiserror_impl-c05c536330366b8e.2thl14zymh2c935g.rcgu.o" "-Wl,--gc-sections" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/rcc/target/debug/deps" "-L" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib" "-Wl,-Bstatic" "/rcc/target/debug/deps/libsyn-fe2290862b758c2c.rlib" "/rcc/target/debug/deps/libquote-afe91c35027afeba.rlib" "/rcc/target/debug/deps/libproc_macro2-0913bab1357133a2.rlib" "/rcc/target/debug/deps/libunicode_xid-8b9fbfbf3102211f.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libproc_macro-84e8be3f2684e30a.rlib" "-Wl,--start-group" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd-5ee1781929f9655d.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libpanic_unwind-e2ec5d2aca499f1c.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libhashbrown-81740b8a57dba39b.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_alloc-eb99f2d2958ad0d9.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libbacktrace-84349410b417ab27.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libbacktrace_sys-b3c09d3262227e0b.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_demangle-c58a107a6842bb60.rlib" "/tmp/rustc3EmKiY/libunwind-33abd88ac2c901d5.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcfg_if-c210a18d812061d6.rlib" "/tmp/rustc3EmKiY/liblibc-2b136be5748c5959.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liballoc-150a1e69c064728f.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_core-e390c28232836237.rlib" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-1cdf993f577c22fa.rlib" "-Wl,--end-group" "/usr/local/rustup/toolchains/beta-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-9211daa04cc43fa3.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lc" "-shared"
      = note: /usr/lib/gcc/x86_64-alpine-linux-musl/9.2.0/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find crti.o: No such file or directory
              collect2: error: ld returned 1 exit status
              
    
    error: aborting due to previous error
    
    error: could not compile `thiserror-impl`.
    

    It seems you don't install musl-dev

  25. jyn514 commented on May 23, 2020

    @jyn514
    Member

    That fixed it, thank you!

  26. WhyNotHugo commented on Nov 24, 2021

    @WhyNotHugo

    If you're bumping into this in 2021, it's likely that you need to run apk add --no-cache musl-dev.

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-linkageArea: linking into static, shared libraries and binariesA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.O-muslTarget: The musl libcT-compilerRelevant to the compiler 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

      Issue actions