swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
31.11k stars 1.22k forks source link

Retrieve musl path dynamically instead of hardcoded #9655

Open jevillard opened 4 days ago

jevillard commented 4 days ago

Describe the bug

I have installed Nix on an Alpine docker image and I want to run my unit tests with swc from the nix shell nix develop -c pnpm test:cov

Input code

No response

Config

{
  "$schema": "http://json.schemastore.org/swcrc",
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "decorators": true
    },
    "experimental": {
      "plugins": [["swc_mut_cjs_exports", {}]]
    },
    "transform": {
      "decoratorMetadata": true
    }
  },
  "module": {
    "type": "commonjs"
  }
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.7.26&code=H4sIAAAAAAAAA9PXV%2FBUSMnPUy9RyM7LL1coz0gsUSjJVygoLVHIzFMoyUhVKMhJrEwvyi%2FNSwFJFKUWFOWnlCancgEAvo2EiDsAAAA%3D&config=H4sIAAAAAAAAA1WPQQ7CIBBF95yCzNqtLryDhyA4bVAYCEOjpOndCy1Uu2P%2Bf8ODWUgJL9Zwl3M5liGoyBiPuSScKalvSSDlgKyjCQkuvU1cq0FZxi1a9gaSiiOmuoV8bThY7xk73jJnyAz5X6i9CxGZz2BFFY0WzzrRlOD8c9rK9pH62F1%2Fgx%2FUZcfFYPjRN2GiN%2FkPgVhWEupbFxkBAAA%3D

SWC Info output

No response

Expected behavior

That my unit tests work in a Nix environment installed on an Alpine docker image.

Actual behavior

Error: Failed to load native binding
    at Object.<anonymous> (/habu/node_modules/.pnpm/@swc+core@1.7.26_@swc+helpers@0.5.13/node_modules/@swc/core/binding.js:333:11)
    at Module._compile (node:internal/modules/cjs/loader:1469:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)
    at Module.load (node:internal/modules/cjs/loader:1288:32)
    at Module._load (node:internal/modules/cjs/loader:1104:12)
    at Module.require (node:internal/modules/cjs/loader:1311:19)
    at require (node:internal/modules/helpers:179:18)
    at Object.<anonymous> (/habu/node_modules/.pnpm/@swc+core@1.7.26_@swc+helpers@0.5.13/node_modules/@swc/core/index.js:49:17)
    at Module._compile (node:internal/modules/cjs/loader:1469:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)

Version

1.7.26

Additional context

I think we can change the code of this function isMuslFromFilesystem by

// actual
const isMuslFromFilesystem = () => {
  try {
    return readFileSync('/usr/bin/ldd', 'utf-8').includes('musl')
  } catch {
    return null
  }
}

// new
const isMuslFromFilesystem = () => {
  try {
    const lddPath = execSync('which ldd').toString().trim();
    return readFileSync(lddPath, 'utf-8').includes('musl')
  } catch (error) {
    return null;
  }
}
kdy1 commented 3 days ago

cc @Brooooooklyn

Brooooooklyn commented 20 hours ago

@jevillard we have fallback logic if musl is null here: https://github.com/swc-project/swc/blob/main/packages/core/binding.js#L14-L18

jevillard commented 20 hours ago

Yes but in my case it does not work because i have a dockerfile with an alpine image, and inside this alpine I have nix without musl. So the first function, isMuslFromFilesystem, doesn't return null, but on the other hand inside my nix store I don't have musl and so when I run my pnpm test with swc it doesn't work. My hook for the moment is mv /usr/bin/ldd /usr/bin/ldd.old.