diesel-rs / diesel

A safe, extensible ORM and Query Builder for Rust
https://diesel.rs
Apache License 2.0
12.57k stars 1.05k forks source link

Linking error for diesel_cli in Alpine despite libraries being installed #3976

Closed the-dipsy closed 5 months ago

the-dipsy commented 5 months ago

Versions

Feature Flags

Problem Description

Installing diesel_cli with postgres in Alpine fails, because libpgcommon, libpgport, libssl, and libcrypto are not included in the cc command.

I don't know if this problem is caused by diesel_cli itself or some dependency, but it should at least be documented somewhere.

What is the actual output?

    Updating crates.io index
  Installing diesel_cli v2.1.1
    Updating crates.io index
   Compiling proc-macro2 v1.0.79
[...]
   Compiling toml_edit v0.19.15
   Compiling diesel v2.1.5
   Compiling toml v0.7.8
   Compiling migrations_internals v2.1.0
   Compiling migrations_macros v2.1.0
   Compiling serde_regex v1.1.0
   Compiling diesel_migrations v2.1.0
   Compiling diesel_cli v2.1.1
error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/bin:/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/bin/self-contained:/usr/local/cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" VSLANG="1033" "cc" "-m64" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/rcrt1.o" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crti.o" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtbeginS.o" "/tmp/rustcIjmnTk/symbols.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.diesel.50ec808d4e1e3636-cgu.00.rcgu.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.diesel.50ec808d4e1e3636-cgu.01.rcgu.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.diesel.50ec808d4e1e3636-cgu.02.rcgu.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.diesel.50ec808d4e1e3636-cgu.03.rcgu.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.diesel.50ec808d4e1e3636-cgu.04.rcgu.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.diesel.50ec808d4e1e3636-cgu.05.rcgu.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.diesel.50ec808d4e1e3636-cgu.06.rcgu.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.diesel.50ec808d4e1e3636-cgu.07.rcgu.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.diesel.50ec808d4e1e3636-cgu.08.rcgu.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.diesel.50ec808d4e1e3636-cgu.09.rcgu.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.diesel.50ec808d4e1e3636-cgu.10.rcgu.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.diesel.50ec808d4e1e3636-cgu.11.rcgu.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.diesel.50ec808d4e1e3636-cgu.12.rcgu.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.diesel.50ec808d4e1e3636-cgu.13.rcgu.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.diesel.50ec808d4e1e3636-cgu.14.rcgu.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.diesel.50ec808d4e1e3636-cgu.15.rcgu.o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b.4z8r9slgviysewgb.rcgu.o" "-Wl,--as-needed" "-L" "/tmp/cargo-installkLXMcA/release/deps" "-L" "/usr/lib" "-L" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib" "-Wl,-Bstatic" "/tmp/cargo-installkLXMcA/release/deps/libdiffy-5e564561211e10cc.rlib" "/tmp/cargo-installkLXMcA/release/deps/libnu_ansi_term-b7d3d522e607cf87.rlib" "/tmp/cargo-installkLXMcA/release/deps/liboverload-10f7fd7880996a60.rlib" "/tmp/cargo-installkLXMcA/release/deps/liburl-00c52c647114c707.rlib" "/tmp/cargo-installkLXMcA/release/deps/libidna-373fc0b1e7957eee.rlib" "/tmp/cargo-installkLXMcA/release/deps/libunicode_normalization-1bdabd55c2c50447.rlib" "/tmp/cargo-installkLXMcA/release/deps/libtinyvec-33bc818232c96f88.rlib" "/tmp/cargo-installkLXMcA/release/deps/libtinyvec_macros-fbbe34639df0cab2.rlib" "/tmp/cargo-installkLXMcA/release/deps/libunicode_bidi-bbea6925253a10ff.rlib" "/tmp/cargo-installkLXMcA/release/deps/libform_urlencoded-37080d45b68109a0.rlib" "/tmp/cargo-installkLXMcA/release/deps/libpercent_encoding-f351235460ab9d75.rlib" "/tmp/cargo-installkLXMcA/release/deps/libdotenvy-4d43521c5b5f0a14.rlib" "/tmp/cargo-installkLXMcA/release/deps/libchrono-11afb3097fdfea49.rlib" "/tmp/cargo-installkLXMcA/release/deps/libnum_traits-a6bd0c3a7e5ee5b5.rlib" "/tmp/cargo-installkLXMcA/release/deps/libiana_time_zone-78452d5d3671e75b.rlib" "/tmp/cargo-installkLXMcA/release/deps/libheck-6b9488ca4a64be58.rlib" "/tmp/cargo-installkLXMcA/release/deps/libdiesel_table_macro_syntax-628a88e29ce4d136.rlib" "/tmp/cargo-installkLXMcA/release/deps/libsyn-4d863b0ded05edde.rlib" "/tmp/cargo-installkLXMcA/release/deps/libquote-9c3e020f36e1c9bb.rlib" "/tmp/cargo-installkLXMcA/release/deps/libproc_macro2-c9b6a44adfe30003.rlib" "/tmp/cargo-installkLXMcA/release/deps/libunicode_ident-72a946a226bf1c75.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libproc_macro-771f5f282c47560d.rlib" "/tmp/cargo-installkLXMcA/release/deps/libclap_complete-5012d61bc3f7d240.rlib" "/tmp/cargo-installkLXMcA/release/deps/libdiesel_migrations-b9f897c642d60d94.rlib" "/tmp/cargo-installkLXMcA/release/deps/libmigrations_internals-97ee7e5f27a12ce7.rlib" "/tmp/cargo-installkLXMcA/release/deps/libtoml-6d9131d55ee173ca.rlib" "/tmp/cargo-installkLXMcA/release/deps/libtoml_edit-4355af0f8a1aed22.rlib" "/tmp/cargo-installkLXMcA/release/deps/libserde_spanned-f5c66e56dc7576b8.rlib" "/tmp/cargo-installkLXMcA/release/deps/libindexmap-66807ab8984a7cc5.rlib" "/tmp/cargo-installkLXMcA/release/deps/libequivalent-3d0bf949e6f9c23b.rlib" "/tmp/cargo-installkLXMcA/release/deps/libhashbrown-935e08db9b81b1bb.rlib" "/tmp/cargo-installkLXMcA/release/deps/libwinnow-28650c353e3fb371.rlib" "/tmp/cargo-installkLXMcA/release/deps/libtoml_datetime-69c71640b0395afe.rlib" "/tmp/cargo-installkLXMcA/release/deps/libdiesel-c5dc5df1372cab52.rlib" "/tmp/cargo-installkLXMcA/release/deps/libitoa-aba3c429878fe160.rlib" "/tmp/cargo-installkLXMcA/release/deps/libbitflags-0f360508d9fd27a3.rlib" "/tmp/cargo-installkLXMcA/release/deps/libbyteorder-1b818e84aedcd24c.rlib" "/tmp/cargo-installkLXMcA/release/deps/libpq_sys-9efba9c812cd0497.rlib" "-lpq" "/tmp/cargo-installkLXMcA/release/deps/libserde_regex-79803b090beeb8c5.rlib" "/tmp/cargo-installkLXMcA/release/deps/libregex-cf8fbb6cd79bf7b7.rlib" "/tmp/cargo-installkLXMcA/release/deps/libregex_automata-4e1476ac79dc3a3b.rlib" "/tmp/cargo-installkLXMcA/release/deps/libaho_corasick-b5a969608caf3624.rlib" "/tmp/cargo-installkLXMcA/release/deps/libmemchr-9d6eb58a185199f2.rlib" "/tmp/cargo-installkLXMcA/release/deps/libregex_syntax-2225d0e3349e1761.rlib" "/tmp/cargo-installkLXMcA/release/deps/libclap-f76930904b9276e6.rlib" "/tmp/cargo-installkLXMcA/release/deps/libclap_builder-95604e29e17a0b55.rlib" "/tmp/cargo-installkLXMcA/release/deps/libstrsim-ce5e35de57c2e3c8.rlib" "/tmp/cargo-installkLXMcA/release/deps/libanstream-1b6c97fa89b0451a.rlib" "/tmp/cargo-installkLXMcA/release/deps/libanstyle_query-ba73f2a2db153ff0.rlib" "/tmp/cargo-installkLXMcA/release/deps/libcolorchoice-47516f38bca96d04.rlib" "/tmp/cargo-installkLXMcA/release/deps/libanstyle_parse-580fb861b3e7c5f4.rlib" "/tmp/cargo-installkLXMcA/release/deps/libutf8parse-8db9483a53737766.rlib" "/tmp/cargo-installkLXMcA/release/deps/libclap_lex-34b4742b4b039c0c.rlib" "/tmp/cargo-installkLXMcA/release/deps/libanstyle-c9c27cec39c04ceb.rlib" "/tmp/cargo-installkLXMcA/release/deps/libserde-4d548ce2aab7a838.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd-11c02606063fb1b5.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libpanic_unwind-527b1ade0207850b.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libobject-5c6479882291983e.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libmemchr-b30d7598b031e855.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libaddr2line-7bdd9f1650697dee.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libgimli-af6e6b96893335a7.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_demangle-61a774fd0ca56cef.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd_detect-0bdaa2154e535182.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libhashbrown-40daaa64e7b3ff16.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_alloc-51b0c25ea806d020.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libminiz_oxide-503b1c30e4fe986c.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libadler-0dfa15d5b98a6f2c.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libunwind-7e3d215879e752d5.rlib" "-lunwind" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcfg_if-f91de774775af47a.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liblibc-a172c9da7a8c39aa.rlib" "-lc" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liballoc-05ed26095d7bae0a.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_core-940bcdced839ada6.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-ece58c607aadf4ad.rlib" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-cfd03998b63f49b3.rlib" "-Wl,-Bdynamic" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-nostartfiles" "-L" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib" "-L" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained" "-o" "/tmp/cargo-installkLXMcA/release/deps/diesel-c14349dc3829848b" "-Wl,--gc-sections" "-static-pie" "-Wl,-z,relro,-z,now" "-Wl,-O1" "-nodefaultlibs" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtendS.o" "/usr/local/rustup/toolchains/1.75.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtn.o"
  = note: /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/lib/libpq.a(fe-connect.o): in function `defaultNoticeProcessor':
          fe-connect.c:(.text+0x288): undefined reference to `pg_fprintf'
          /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/lib/libpq.a(fe-connect.o): in function `sslVerifyProtocolVersion':
          fe-connect.c:(.text+0xcb1): undefined reference to `pg_strcasecmp'
          /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: fe-connect.c:(.text+0xcc5): undefined reference to `pg_strcasecmp'
          /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: fe-connect.c:(.text+0xcd9): undefined reference to `pg_strcasecmp'
          /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: fe-connect.c:(.text+0xced): undefined reference to `pg_strcasecmp'
          /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/lib/libpq.a(fe-connect.o): in function `emitHostIdentityInfo':
          fe-connect.c:(.text+0xe25): undefined reference to `pg_getnameinfo_all'
[...]
          /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/lib/libpq.a(fe-secure.o): in function `pqsecure_raw_write.part.0':
          fe-secure.c:(.text+0x47c): undefined reference to `pg_snprintf'
          /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: fe-secure.c:(.text+0x549): undefined reference to `pg_strerror_r'
          /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: fe-secure.c:(.text+0x56b): undefined reference to `pg_snprintf'
          /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/lib/libpq.a(fe-auth.o): in function `pg_fe_sendauth':
          fe-auth.c:(.text+0x842): undefined reference to `pg_md5_encrypt'
          /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: fe-auth.c:(.text+0x862): undefined reference to `pg_md5_encrypt'
          /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/lib/libpq.a(fe-auth.o): in function `pg_fe_getusername':
          fe-auth.c:(.text+0xa74): undefined reference to `pg_get_user_name'
          /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/lib/libpq.a(fe-auth.o): in function `PQencryptPassword':
          fe-auth.c:(.text+0xb78): undefined reference to `pg_md5_encrypt'
          /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/lib/libpq.a(fe-auth.o): in function `PQencryptPasswordConn':
          fe-auth.c:(.text+0xc71): undefined reference to `pg_md5_encrypt'
          collect2: error: ld returned 1 exit status

  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

error: could not compile `diesel_cli` (bin "diesel") due to previous error
error: failed to compile `diesel_cli v2.1.1`, intermediate artifacts can be found at `/tmp/cargo-installkLXMcA`.
To reuse those artifacts with a future compilation, set the environment variable `CARGO_TARGET_DIR` to that path.

Steps to reproduce

  1. Start an alpine based container using podman run --rm -it rust:alpine
  2. Install the dependencies using apk add --no-cache musl-dev openssl-libs-static libpq-dev
  3. Try to install diesel_cli using cargo install diesel_cli --no-default-features --features postgres

Interim fix

The issues can be fixed manually by instructing rust to include the missing libraries using the environment variable RUSTFLAGS="-lpgcommon -lpgport -lssl -lcrypto".

weiznich commented 5 months ago

Thanks for opening this bug report. This seems to be a duplicate of https://github.com/sgrif/pq-sys/issues/27, therefore I'm closing this in favor of the other bug.

the-dipsy commented 5 months ago

To quickly elaborate, if I understand correctly, this has been fixed in pg-sys 0.5.0, which the current main branch of diesel already supports and will therefore be available in diesel's next release. Thanks alot and keep up the good work 😊

weiznich commented 5 months ago

That's nearly correct. It doesn't fix statically linking the system provided libraries, as we do not have a good way to detect which libraries are actually needed. It instead provides a "bundled" feature flag that allows to build a defined version of libpq from source and statically links that version. In that way we have the required knowlegde about all the dependencies.

peasee commented 5 months ago

@weiznich do you have any timeline for the next diesel release? I've updated my Cargo.toml to pull master from the git directly which works well, but would love to pin against the released version.

weiznich commented 5 months ago

@peasee As this is a free time project we generally do not give any ETA for releases other than "when it's done". You can help making this happen faster by either contributing fixes for the remaining issues marked as release blocker or by funding the development.