swc icon indicating copy to clipboard operation
swc copied to clipboard

@swc/core threw an error when attempting to validate swc compiler options.

Open sebastianrath opened this issue 2 years ago • 65 comments

Describe the bug

Hi there,

I am using @swc/core to speedup the execution of my test suite with avajs/ava.

System info: Ubuntu 20.04.1 (VM via parallels 18 on a Mac Mini Intel) node 16.15.0

During npm run test I receive the following error, and I am not sure how to resolve the issue.

/home/st/actions-runner/_work/snow/snow/node_modules/ts-node/src/transpilers/swc.ts:262
      throw new Error(
            ^
Error: @swc/core threw an error when attempting to validate swc compiler options.
You may be using an old version of swc which does not support the options used by ts-node.
Try upgrading to the latest version of swc.
Error message from swc:
Bindings not found
    at createVariant (/home/st/actions-runner/_work/snow/snow/node_modules/ts-node/src/transpilers/swc.ts:262:13)
    at createSwcOptions (/home/st/actions-runner/_work/snow/snow/node_modules/ts-node/src/transpilers/swc.ts:211:25)
    at create (/home/st/actions-runner/_work/snow/snow/node_modules/ts-node/src/transpilers/swc.ts:56:41)
    at createTranspiler (/home/st/actions-runner/_work/snow/snow/node_modules/ts-node/src/index.ts:784:16)
    at createTranspileOnlyGetOutputFunction (/home/st/actions-runner/_work/snow/snow/node_modules/ts-node/src/index.ts:1341:28)
    at createFromPreloadedConfig (/home/st/actions-runner/_work/snow/snow/node_modules/ts-node/src/index.ts:1404:34)
    at create (/home/st/actions-runner/_work/snow/snow/node_modules/ts-node/src/index.ts:624:10)
    at register (/home/st/actions-runner/_work/snow/snow/node_modules/ts-node/src/index.ts:591:15)
    at Object.registerAndCreateEsmHooks (/home/st/actions-runner/_work/snow/snow/node_modules/ts-node/src/esm.ts:116:34)
    at file:///home/st/actions-runner/_work/snow/snow/node_modules/ts-node/esm.mjs:8:7
  ✖ test/1.sys.test.ts exited with a non-zero exit code: 1

Input code

No response

Config

Snippet of tsconfig.json config:

  "ts-node": {
    "swc": true
  },

Playground link

No response

Expected behavior

Successful execution of npm run test.

Actual behavior

No response

Version

v1.2.241

Additional context

No response

sebastianrath avatar Aug 24 '22 14:08 sebastianrath

Which os/cpu are you using?

kdy1 avatar Aug 24 '22 14:08 kdy1

Also, did you use something like --skip-optional or --ignore-scripts?

kdy1 avatar Aug 24 '22 14:08 kdy1

Thanks for the quick reply! I added more information under System Info. No --skip-optional nor --ignore-scripts is used.

Also for completeness, here is the definition of npm run test in package.json:

"test": "nyc --reporter=html --reporter=lcov --reporter=text ava"

sebastianrath avatar Aug 24 '22 14:08 sebastianrath

Quick update, the same project/commit successfully built on the same machine (also Parallels) under Windows. Updating to swc/[email protected]works now also under Linux. Could the error come back or did the latest version fix something that could explain the issue on Linux?

sebastianrath avatar Aug 24 '22 15:08 sebastianrath

Did you share the project directory? You should install on target platform unless you are overriding npm config

kdy1 avatar Aug 24 '22 15:08 kdy1

I was able to reproduce this issue with my Apollo Federation Gateway running in a Node docker container. I can provide the Dockerfile.

FROM node:17.3
WORKDIR /app
ENV NODE_ENV=dev
ENV ZIPKIN=http://localhost:9411
EXPOSE 4000
COPY . .
CMD ["yarn", "run", "dev"]

Also important to note is that I'm not experiencing this issue when I run this directly on my machine. macOS 12.5.1, Apple Silicon M1 node 17.3.1

encountered this error on docker run my-image:1.0

$ docker run my-image:1.0
yarn run v1.22.17

$ ts-node src/index.ts
/app/node_modules/ts-node/src/transpilers/swc.ts:262
      throw new Error(
            ^
Error: @swc/core threw an error when attempting to validate swc compiler options.
You may be using an old version of swc which does not support the options used by ts-node.
Try upgrading to the latest version of swc.
Error message from swc:
Bindings not found
    at createVariant (/app/node_modules/ts-node/src/transpilers/swc.ts:262:13)
    at createSwcOptions (/app/node_modules/ts-node/src/transpilers/swc.ts:211:25)
    at create (/app/node_modules/ts-node/src/transpilers/swc.ts:56:41)
    at createTranspiler (/app/node_modules/ts-node/src/index.ts:784:16)
    at createTranspileOnlyGetOutputFunction (/app/node_modules/ts-node/src/index.ts:1341:28)
    at createFromPreloadedConfig (/app/node_modules/ts-node/src/index.ts:1404:34)
    at phase4 (/app/node_modules/ts-node/src/bin.ts:543:44)
    at bootstrap (/app/node_modules/ts-node/src/bin.ts:95:10)
    at main (/app/node_modules/ts-node/src/bin.ts:55:10)
    at Object.<anonymous> (/app/node_modules/ts-node/src/bin.ts:800:3)
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.

fwiw I'm running this

    "@swc/core": "^1.2.245",
    "@swc/helpers": "^0.4.8",

ejkg avatar Aug 31 '22 20:08 ejkg

I have the same problem, I can't start my project at all with Docker/TS-Node/SWC

Bluzzi avatar Sep 05 '22 08:09 Bluzzi

Can you try to reinstall swc in your docker?

RiESAEX avatar Sep 05 '22 09:09 RiESAEX

That's what I am doing

Dockerfile:

FROM node:16.16-alpine

WORKDIR /app

COPY package*.json ./
RUN npm install
COPY . .

CMD npm run start

.dockerignore:

node_modules

Bluzzi avatar Sep 05 '22 11:09 Bluzzi

This issue has been automatically closed because it received no activity for a month and had no reproduction to investigate. If you think this was closed by accident, please leave a comment. If you are running into a similar issue, please open a new issue with a reproduction. Thank you.

swc-bot avatar Sep 11 '22 23:09 swc-bot

This issue is still valid

Bluzzi avatar Sep 12 '22 09:09 Bluzzi

I'm also running into this issue after updating from node 14.17.1 to node 16.16.0 on a MBP 16 M1 Pro:

/Users/.../node_modules/@swc/core/index.js:250
        throw new Error("Bindings not found");
        ^

Error: Bindings not found
    at Compiler.transformSync (/Users/.../node_modules/@swc/core/index.js:250:15)
    at transformSync (/Users/.../node_modules/@swc/core/index.js:348:21)
    at transformSync (/Users/.../node_modules/@swc-node/core/index.ts:69:26)
    at compile (/Users/.../node_modules/@swc-node/register/register.ts:44:40)
    at exts (/Users/.../node_modules/@swc-node/register/register.ts:55:38)
    at Module._compile (/Users/.../node_modules/pirates/lib/index.js:130:29)
    at Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Object.newLoader [as .ts] (/Users/.../node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)

btecu avatar Sep 15 '22 20:09 btecu

This issue has been automatically closed because it received no activity for a month and had no reproduction to investigate. If you think this was closed by accident, please leave a comment. If you are running into a similar issue, please open a new issue with a reproduction. Thank you.

swc-bot avatar Sep 22 '22 23:09 swc-bot

The issue is still valid.

btecu avatar Sep 22 '22 23:09 btecu

Not sure why bot aggressively marks this one 🤔

kwonoj avatar Sep 22 '22 23:09 kwonoj

@btecu @Bluzzi My co-worker found a fix for this, inside .yarnrc.yml. Both of us are using M1 Pro MacBook Pros and his worked without this addition, but for some reason mine had the error throw new Error("Bindings not found");

The solution was to put - arm64 under cpu. Full output of .yarnrc.yml.

nodeLinker: node-modules

plugins:
  - path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
    spec: "@yarnpkg/plugin-workspace-tools"

yarnPath: .yarn/releases/yarn-3.2.3.cjs

# This setting ensures we always install the Linux binaries when running `yarn install`. This is needed for running
# swc natively in Docker from a Mac host.
supportedArchitectures:
  os:
    - current
    - linux
  cpu:
    - current
    - x64
    - arm64
  libc:
    - current
    - glibc

I don't know if it'll solve your guy's problems, but it did solve mine!

jgujgu avatar Oct 03 '22 15:10 jgujgu

Thanks @jgujgu. Is there anything for npm?

btecu avatar Oct 03 '22 18:10 btecu

@btecu I really wouldn't know, sorry...you really just have to make sure the correct binaries are installed. I imagine npm would have a similar config or way to declare it.

jgujgu avatar Oct 06 '22 15:10 jgujgu

Getting this error on an M1 Mac using node 19, but not on 18.

gspetrou avatar Nov 04 '22 22:11 gspetrou

I'm getting this issue with the Node 16 docker image - the yarn fix didn't solve it.

jcampbell05 avatar Nov 10 '22 12:11 jcampbell05

I've ran into similar issue with a bit uncommon setup, but maybe this will help shed some light.

I have a working setup on local node 18, using yarn (on intel mac). I wanted to create a sample Dockerfile using npm, so I ran npm i locally to generate package-lock.json. Then docker build ran up to the build point where it failed on Fallback bindings does not support filesystem access.. When installing @swc/core and @swc-cli in the build step in the container again, it worked well.

Then I removed node_modules and package-lock.json on the host and ran npm i again to get a new package-lock.json. And with this new lock the whole build ran successfully.

old-package-lock.json.txt package-lock.json.txt

When compared, there are few @swc packages that didn't get "locked" with existing node_modules.

So, maybe the "work-around" is to rm -rf node_modules && rm package-lock.json && npm i on the host?

Also, on host I have cli, core, core-darwin-x64 in node_modules/@swc. When I ran ls in the broken-build image, I got cli, core, wasm with the old-package-lock.json. With the new, working lock I have core-linux-x64-gnu and core-linux-x64-musl in there instead. Docker image is based on node:18-slim.

wodCZ avatar Nov 10 '22 15:11 wodCZ

My issue was because I was mounting a volume in such a way the node_modules would be mounted to the host which could cause issues.

By adding node_modules in it's own volume in the docker-compose spec I can tell docker to preserve the node_modules folder from the underlying image and now it works.

jcampbell05 avatar Nov 10 '22 15:11 jcampbell05

Hi there!

I also had this issue and was able to resolve it. below are my "story".

Most beneficial (give me some food to think) to me were messages from @jgujgu and @wodCZ.

I was migrating from yarn to npm, and I wanted to have a dependency tree as similar as possible. So I did not delete node_modules folder and just run npm i. This caused package-lock.json file to be not fully valid, where swc package for my arch was missing (I figured out that later). So I did a couple of test setups, and they were OK. At some point, I decided to check "moving parts" of the swc software in the file system and found that arm64 file/package was missing.


Summary: if you got a subject error message -- make sure you have both arch package and arch binary in your FS

Advice to developers: maybe you should add/adjust a check if arch binary exists and make sure the error message for that is crystal clear

ZuBB avatar Nov 25 '22 10:11 ZuBB

using node v16.18.1 also met this problem, on MBP 13 M1, problem performed like @btecu

Error: Bindings not found at Compiler.transformSync (/Users/buxiongyu/Desktop/zeus/node_modules/.pnpm/@[email protected]/node_modules/@swc/core/index.js:250:15) at Object.transformSync (/Users/buxiongyu/Desktop/zeus/node_modules/.pnpm/@[email protected]/node_modules/@swc/core/index.js:348:21) at compile (/Users/buxiongyu/Desktop/zeus/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@swc/register/lib/node.js:76:22) at compileHook (/Users/buxiongyu/Desktop/zeus/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@swc/register/lib/node.js:91:16) at Module._compile (/Users/buxiongyu/Desktop/zeus/node_modules/.pnpm/[email protected]/node_modules/pirates/lib/index.js:130:29) at Module._extensions..js (node:internal/modules/cjs/loader:1209:10) at Object.newLoader [as .js] (/Users/buxiongyu/Desktop/zeus/node_modules/.pnpm/[email protected]/node_modules/pirates/lib/index.js:141:7) at Module.load (node:internal/modules/cjs/loader:1033:32) at Function.Module._load (node:internal/modules/cjs/loader:868:12) at Module.require (node:internal/modules/cjs/loader:1057:19)

i'm just wondering if it is cross platform problem of npm/pnpm ?

Trojan0523 avatar Nov 28 '22 11:11 Trojan0523

Same issue on

   MacBook Pro (Retina, 15-inch, Mid 2014)
   OS   : darwin x64
   Node : 16.15.0
   npm  : 8.10.0

Experience this while using @nrwl/nx npm package.

DzmVasileusky avatar Dec 16 '22 16:12 DzmVasileusky

What have helped me

  1. Create new clean project
  2. manually install @swc/core of needed version there
  3. manually copypaste core-darwin-x64 to node_modules/@swc

DzmVasileusky avatar Dec 16 '22 17:12 DzmVasileusky

What helped me in my npm monorepo:

  1. npm uninstall all swc related packages
  2. remove package-lock.json
  3. remove all node_modules directories
  4. npm install
  5. npm install the swc related packages again

nephix avatar Jan 29 '23 14:01 nephix

Hey buddy, I have encountered the same issue as you and have solved it, I would like to share my experience with you to see if it can help. Just a few days ago I was able to install Taro-cli without any issues, then after reinstalling the system, my project encountered the same error as yours. I searched for solutions in the Taro repository and the SWC repository, but there was no ready answer. However, they gave some directions to troubleshoot.So I tried switching the node version, package manager version and source, and Taro/cli version, but all failed. Finally, I debugged the source code with breakpoints, combined with my own experience, and found that the problem was related to the rust runtime environment, which needed to be installed. The specific installation method can be referred to the link below. https://blog.csdn.net/u011149152/article/details/123518465

roftLin avatar Feb 02 '23 05:02 roftLin

I think there is a bug in the postinstall.sh script in the swc module. Somewhere, it "trash" the @swc/wasm module instead of adding it. the script however is discarded if @swc/wasm is added in the dependencies. What fixed it for me (in my dockers in CI), is to add the package in the dependencies. But looking at the comments, i think there is multiple issues listed here, so it may not fix for everyone.

littleski avatar Feb 16 '23 11:02 littleski

@littleski I ran into the same problem and adding @swc/wasm solved it for me too...so yes, a bug in the post install maybe?

arxpoetica avatar Mar 06 '23 17:03 arxpoetica