CargoSense / dart_sass

Install and run Dart Sass using Elixir.
https://hex.pm/packages/dart_sass
MIT License
102 stars 23 forks source link

mix sass on alpine fails with exit code 2 #13

Closed ouven closed 2 years ago

ouven commented 2 years ago

Hi, thank you for this piece of work!

I tried to use the mix task in my build pipeline (upon hexpm/elixir:1.12.3-erlang-24.1.1-alpine-3.14.0). Unfortunatly it fails because of some lib bindings.

/build # mix sass default
** (Mix) `mix sass default` exited with 2
/build # ./_build/sass apps/web/assets/css/app.scss 
sh: ./_build/sass: not found
/build # ldd
musl libc (x86_64)
Version 1.2.2
Dynamic Program Loader
Usage: /lib/ld-musl-x86_64.so.1 [options] [--] pathname
/build # ldd ./_build/sass 
        /lib64/ld-linux-x86-64.so.2 (0x7f752da95000)
        libdl.so.2 => /lib64/ld-linux-x86-64.so.2 (0x7f752da95000)
        libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7f752da95000)
        libm.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7f752da95000)
        libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7f752da95000)
Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by ./_build/sass)
Error relocating ./_build/sass: __sbrk: symbol not found
Error relocating ./_build/sass: __isinf: symbol not found
/build # 
mcrumm commented 2 years ago

Unfortunately dart-sass will not run on Alpine Linux because it requires glibc.

You may be able to install a sass binary via apk and copy/symlink it to _build/sass, but there isn't anything more we can do on this end. Thanks!

philipgiuliani commented 2 years ago

I had the same problem and after some research I could get it working by adding this alpine package: https://github.com/sgerrand/alpine-pkg-glibc#installing

...

# add glibc in order to use dart-sass
RUN wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub
RUN wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.34-r0/glibc-2.34-r0.apk
RUN apk add glibc-2.34-r0.apk

# build assets
COPY assets assets
COPY config config
RUN mix assets.deploy

...

And... It works 🥳

mcrumm commented 2 years ago

@philipgiuliani Awesome, thanks for the report! Given the popularity around Alpine for CI, I think it would be great to call this out in the README. Would you like to send a PR? :)

ouven commented 2 years ago

@philipgiuliani -> Hero

I still had some issues with the linker, which I had to solve by calling ldconfig. See the contents (the start) of my Dockerfile, that make it work:

FROM hexpm/elixir:1.12.3-erlang-24.1.1-alpine-3.14.0 as build

# add glibc in order to use dart-sass
ENV GLIBC_REPO=https://github.com/sgerrand/alpine-pkg-glibc
ENV GLIBC_VERSION=2.34-r0

RUN apk add --update --no-cache npm make g++ git ca-certificates
RUN update-ca-certificates --fresh

RUN apk --update add libstdc++ curl ca-certificates && \
    for pkg in glibc-${GLIBC_VERSION} glibc-bin-${GLIBC_VERSION}; \
    do curl -sSL ${GLIBC_REPO}/releases/download/${GLIBC_VERSION}/${pkg}.apk -o /tmp/${pkg}.apk; done && \
    apk add --allow-untrusted /tmp/*.apk && \
    rm -v /tmp/*.apk

RUN mix local.hex --force
RUN mix local.rebar --force

# ...
philipgiuliani commented 2 years ago

Interesting, which error did you get? So we can document this in #14 . I only had the error ../../runtime/bin/eventhandler_linux.cc: 412: error: Failed to start event handler thread 1 when building on my CI (not on my Mac) which could be resolved by downgrading glibc to 2.33 (see details in #14)

Doerge commented 2 years ago

Ran into this as well, and unfortunately didn't find this discussion, because the ticket is closed. I should have looked better!

Since my Docker build is multi stage, I decided to just install npm and use the Javascript version of DartSass instead:

- RUN apk add --no-cache build-base
+ RUN apk add --no-cache build-base npm

+ # Install Dart Sass
+ RUN npm install -g sass

[...]

+ # build Sass -> CSS
+ RUN cd assets && \
+     sass --no-source-map --style=compressed css/app.scss ../priv/static/assets/app.css

# build assets
RUN mix assets.deploy
[...]

where mix.exs assets.deploy is:

  defp aliases do
    [...]
     "assets.deploy": [
       "esbuild default --minify",
       "phx.digest"
     ]
  end

Pros:

I think this is good enough for me.

ouven commented 2 years ago

Interesting, which error did you get? So we can document this in #14 . I only had the error ../../runtime/bin/eventhandler_linux.cc: 412: error: Failed to start event handler thread 1 when building on my CI (not on my Mac) which could be resolved by downgrading glibc to 2.33 (see details in #14)

Sorry, I was not able to reproduce 🤔