Repository: dprint/dprint (language: rust)

Issue:
Support for prebuilt linux aarch64 musl
I'm attempting to install this this as an `npm` dependency on a node:alpine Docker image, but I get an error:

```
Error downloading dprint zip file. Error: Unknown status code 404 : Not Found
     at ClientRequest.<anonymous> (/app/node_modules/dprint/install_api.js:102:18)
```

I've given that stack trace, it looks like the url it's looking to download the dprint zip file looks something like this:

```
https://github.com/dprint/dprint/releases/download/dprint-aarch64-unknown-linux-musl.zip
```

To reproduce, you can build a docker image with this Dockerfile:

```Dockerfile
FROM node:18-alpine
RUN mkdir -p /app
WORKDIR /app
RUN echo '{"devDependencies":{"dprint":"*"}}' > package.json
RUN yarn install
```

Then run:

```sh
docker build --platform linux/arm64 .
```

I'm running this on an M1 mac, so I'm unsure if running this will work on non-arm devices.

I'm also unsure if this is as easy as just adding it to the list of platforms here https://github.com/dprint/dprint/blob/main/.github/workflows/ci.generate.ts and regenerating that that ci.yaml workflow.


Discussion / hints:
I tried cross compiling it, but haven't been able to get it to work yet in https://github.com/dprint/dprint/pull/665
This is a serious problem against adopting dprint for running in Docker on arm64 Macs :(

Fix patch:
diff --git a/.cargo/config.toml b/.cargo/config.toml
index b72845268..34364c5d1 100644
--- a/.cargo/config.toml
+++ b/.cargo/config.toml
@@ -1,8 +1,3 @@
 # statically link the c runtime https://github.com/rust-lang/rust/issues/100874
 [target.'cfg(all(windows, target_env = "msvc"))']
 rustflags = ["-C", "target-feature=+crt-static"]
-
-# https://github.com/rust-lang/rust/issues/46651#issuecomment-433611633
-[target.aarch64-unknown-linux-musl]
-linker = "aarch64-linux-gnu-gcc"
-rustflags = ["-C", "target-feature=+crt-static", "-C", "link-arg=-lgcc"]
diff --git a/.github/workflows/ci.generate.ts b/.github/workflows/ci.generate.ts
index c44746455..e7a46427c 100644
--- a/.github/workflows/ci.generate.ts
+++ b/.github/workflows/ci.generate.ts
@@ -11,6 +11,8 @@ interface ProfileData {
   os: OperatingSystem;
   target: string;
   runTests?: boolean;
+  /** Build using cross. */
+  cross?: boolean;
 }
 
 const profileDataItems: ProfileData[] = [{
@@ -37,6 +39,7 @@ const profileDataItems: ProfileData[] = [{
 }, {
   os: OperatingSystem.Linux,
   target: "aarch64-unknown-linux-musl",
+  cross: true,
 }];
 const profiles = profileDataItems.map(profile => {
   return {
@@ -80,6 +83,7 @@ const ci = {
             os: profile.os,
             run_tests: (profile.runTests ?? false).toString(),
             target: profile.target,
+            cross: (profile.cross ?? false).toString(),
           })),
         },
       },
@@ -90,7 +94,7 @@ const ci = {
       },
       outputs: Object.fromEntries(
         profiles.map(profile => {
-          const entries = [];
+          const entries: string[][] = [];
           entries.push([
             profile.zipChecksumEnvVarName,
             "${{steps.pre_release_" + profile.target.replaceAll("-", "_") + ".outputs.ZIP_CHECKSUM}}",
@@ -143,13 +147,10 @@ const ci = {
           ].join("\n"),
         },
         {
-          name: "Setup (Linux aarch64-musl)",
-          if: "matrix.config.target == 'aarch64-unknown-linux-musl'",
+          name: "Setup cross",
+          if: "matrix.config.cross == 'true'",
           run: [
-            "sudo apt update",
-            "sudo apt install gcc-aarch64-linux-gnu",
-            "sudo apt install musl musl-dev musl-tools",
-            "rustup target add aarch64-unknown-linux-musl",
+            "cargo install cross --git https://github.com/cross-rs/cross --rev 44011c8854cb2eaac83b173cc323220ccdff18ea",
           ].join("\n"),
         },
         {
@@ -159,7 +160,7 @@ const ci = {
         },
         {
           name: "Build (Debug)",
-          if: "!startsWith(github.ref, 'refs/tags/')",
+          if: "matrix.config.cross != 'true' && !startsWith(github.ref, 'refs/tags/')",
           env: {
             "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER": "aarch64-linux-gnu-gcc",
           },
@@ -169,7 +170,7 @@ const ci = {
         },
         {
           name: "Build (Release)",
-          if: "startsWith(github.ref, 'refs/tags/')",
+          if: "matrix.config.cross != 'true' && startsWith(github.ref, 'refs/tags/')",
           env: {
             "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER": "aarch64-linux-gnu-gcc",
           },
@@ -177,6 +178,20 @@ const ci = {
             "cargo build -p dprint --locked --target ${{matrix.config.target}} --release",
           ].join("\n"),
         },
+        {
+          name: "Build cross (Debug)",
+          if: "matrix.config.cross == 'true' && !startsWith(github.ref, 'refs/tags/')",
+          run: [
+            "cross build -p dprint --locked --target ${{matrix.config.target}}",
+          ].join("\n"),
+        },
+        {
+          name: "Build cross (Release)",
+          if: "matrix.config.cross == 'true' && startsWith(github.ref, 'refs/tags/')",
+          run: [
+            "cross build -p dprint --locked --target ${{matrix.config.target}} --release",
+          ].join("\n"),
+        },
         {
           name: "Test (Debug)",
           if: "matrix.config.run_tests == 'true' && !startsWith(github.ref, 'refs/tags/')",
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 8bd62bb62..495cfdd09 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -23,24 +23,31 @@ jobs:
           - os: macOS-latest
             run_tests: "true"
             target: x86_64-apple-darwin
+            cross: "false"
           - os: macOS-latest
             run_tests: "false"
             target: aarch64-apple-darwin
+            cross: "false"
           - os: windows-latest
             run_tests: "true"
             target: x86_64-pc-windows-msvc
+            cross: "false"
           - os: ubuntu-20.04
             run_tests: "true"
             target: x86_64-unknown-linux-gnu
+            cross: "false"
           - os: ubuntu-20.04
             run_tests: "false"
             target: x86_64-unknown-linux-musl
+            cross: "false"
           - os: ubuntu-20.04
             run_tests: "false"
             target: aarch64-unknown-linux-gnu
+            cross: "false"
           - os: ubuntu-20.04
             run_tests: "false"
             target: aarch64-unknown-linux-musl
+            cross: "true"
     env:
       CARGO_INCREMENTAL: 0
       RUST_BACKTRACE: full
@@ -80,26 +87,28 @@ jobs:
           sudo apt update
           sudo apt install gcc-aarch64-linux-gnu
           rustup target add aarch64-unknown-linux-gnu
-      - name: Setup (Linux aarch64-musl)
-        if: matrix.config.target == 'aarch64-unknown-linux-musl'
-        run: |-
-          sudo apt update
-          sudo apt install gcc-aarch64-linux-gnu
-          sudo apt install musl musl-dev musl-tools
-          rustup target add aarch64-unknown-linux-musl
+      - name: Setup cross
+        if: matrix.config.cross == 'true'
+        run: "cargo install cross --git https://github.com/cross-rs/cross --rev 44011c8854cb2eaac83b173cc323220ccdff18ea"
       - name: Setup (Mac aarch64)
         if: matrix.config.target == 'aarch64-apple-darwin'
         run: rustup target add aarch64-apple-darwin
       - name: Build (Debug)
-        if: "!startsWith(github.ref, 'refs/tags/')"
+        if: "matrix.config.cross != 'true' && !startsWith(github.ref, 'refs/tags/')"
         env:
           CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc
         run: "cargo build -p dprint --locked --target ${{matrix.config.target}}"
       - name: Build (Release)
-        if: "startsWith(github.ref, 'refs/tags/')"
+        if: "matrix.config.cross != 'true' && startsWith(github.ref, 'refs/tags/')"
         env:
           CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc
         run: "cargo build -p dprint --locked --target ${{matrix.config.target}} --release"
+      - name: Build cross (Debug)
+        if: "matrix.config.cross == 'true' && !startsWith(github.ref, 'refs/tags/')"
+        run: "cross build -p dprint --locked --target ${{matrix.config.target}}"
+      - name: Build cross (Release)
+        if: "matrix.config.cross == 'true' && startsWith(github.ref, 'refs/tags/')"
+        run: "cross build -p dprint --locked --target ${{matrix.config.target}} --release"
       - name: Test (Debug)
         if: "matrix.config.run_tests == 'true' && !startsWith(github.ref, 'refs/tags/')"
         run: "cargo test --locked --target ${{matrix.config.target}} --all-features"


Test patch: