swc-project / swc

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

Crypto library causing pack to fail #3406

Open pixellos opened 2 years ago

pixellos commented 2 years ago

Describe the bug

Between "@swc/core-win32-x64-msvc" 1.2.121 and any version never than 1.2.122 there is a bug - any usage of "uuid": "8.3.2" library causes error

Input code

{
  "name": "myname",
  "version": "1.0.0",
  "main": "dist/main.js",
  "source": true,
  "description": "",
  "scripts": {
    "build-local": "npx spack && npx swc dist-es6/role-mapper.js dist/main.js --config-file .swcrc.local -d dist",

  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@swc/cli": "0.1.55",
    "@swc/core": "1.2.122",
    "@types/uuid": "8.3.3",
    "cross-env": "7.0.3",
    "typescript": "^4.5.5"
  },
  "dependencies": {
    "uuid": "8.3.2"
  },
  "overrides": {
    "@swc/core-android-arm64": "1.2.122",
    "@swc/core-darwin-arm64": "1.2.122",
    "@swc/core-darwin-x64": "1.2.122",
    "@swc/core-freebsd-x64": "1.2.122",
    "@swc/core-linux-arm-gnueabihf": "1.2.122",
    "@swc/core-linux-arm64-gnu": "1.2.122",
    "@swc/core-linux-arm64-musl": "1.2.122",
    "@swc/core-linux-x64-gnu": "1.2.122",
    "@swc/core-linux-x64-musl": "1.2.122",
    "@swc/core-win32-arm64-msvc": "1.2.122",
    "@swc/core-win32-ia32-msvc": "1.2.122",
    "@swc/core-win32-x64-msvc": "1.2.122"
  },
  "browserlists": [
    "ie11"
  ]
}
--- TsConfig
{
    "compilerOptions": {
      "noImplicitAny": false,
      "noEmitOnError": true,
      "removeComments": false,
      "sourceMap": true,
      "target": "es5",
      "outDir": "dist",
      "typeRoots": ["types"],
    },

    "include": [
        "src/**/*"
    ]
  }

----  app code
import { validate } from "uuid";
validate("test");

Config

{}

Playground link

No response

Expected behavior

Compilation should succeed

Actual behavior

node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

[Error: load_transformed failed

Caused by:
    0: failed to analyze module
    1: failed to resolve crypto from \\?\project\node_modules\uuid\dist\esm-node\rng.js
    2: index not found] {
  code: 'GenericFailure'
}

Version

1.2.135

Additional context

No response

RiESAEX commented 2 years ago

I guess you need target: "node" in your spack.config.js

pixellos commented 2 years ago

Added as You asked + RUST_BACKTRACE="full" as it panicked


thread '<unnamed>' panicked at 'rename_bundles called with non-path module', D:\a\swc\swc\crates\swc_bundler\src\bundler\finalize.rs:404:22
stack backtrace:
   0:     0x7fff1ed9beda - rust_eh_personality
   1:     0x7fff1e9e1e1a - <unknown>
   2:     0x7fff1ed91cb8 - rust_eh_personality
   3:     0x7fff1ed9d599 - rust_eh_personality
   4:     0x7fff1ed9e569 - rust_eh_personality
   5:     0x7fff1ed9dfb3 - rust_eh_personality
   6:     0x7fff1ed9df49 - rust_eh_personality
   7:     0x7fff1ed9df04 - rust_eh_personality
   8:     0x7fff20929ce0 - wasmer_vm_f64_nearest
   9:     0x7fff1e65ee90 - <unknown>
  10:     0x7fff1e6311ff - <unknown>
  11:     0x7fff1e5b77bd - <unknown>
  12:     0x7ff67ddc311e - uv_queue_work
  13:     0x7ff67ddaf36d - uv_poll_stop
  14:     0x7ff67ebc0c60 - v8::internal::compiler::RepresentationChanger::Uint32OverflowOperatorFor
  15:     0x7ffffa847c24 - BaseThreadInitThunk
  16:     0x7ffffb94d721 - RtlUserThreadStart
node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

[Error: panic detected] { code: 'GenericFailure' }
pixellos commented 2 years ago

I think it can be related to this file https://github.com/uuidjs/uuid/blob/main/src/native.js Its trying to import node native crypto, which is not package nor file.

xmsz commented 2 years ago

any solution?

pixellos commented 2 years ago

For me only thing that worked was overrides section in package.json with older versions

last-Programmer commented 1 year ago

Could you please tell me where did you override?

I m also facing the same issue

0: failed to analyze module 1: failed to resolve crypto from \***\node_modules\uuid\dist\esm-node\rng.js 2: not processed by tsc resolver because base module is in node_modules 3: Impossible to get the node_modules path] {

with webpack i had this

genericConfig.resolve.fallback = {
    "crypto": require.resolve("crypto-browserify"),
    "stream": require.resolve("stream-browserify")
};

how do i do this with swc?

Thanks

pixellos commented 1 year ago

The pacakge.json has overrides section

  "overrides": {
    "@swc/core-android-arm64": "1.2.122",
    "@swc/core-darwin-arm64": "1.2.122",
    "@swc/core-darwin-x64": "1.2.122",
    "@swc/core-freebsd-x64": "1.2.122",
    "@swc/core-linux-arm-gnueabihf": "1.2.122",
    "@swc/core-linux-arm64-gnu": "1.2.122",
    "@swc/core-linux-arm64-musl": "1.2.122",
    "@swc/core-linux-x64-gnu": "1.2.122",
    "@swc/core-linux-x64-musl": "1.2.122",
    "@swc/core-win32-arm64-msvc": "1.2.122",
    "@swc/core-win32-ia32-msvc": "1.2.122",
    "@swc/core-win32-x64-msvc": "1.2.122"
  },

just put 1.2.121 instead, @last-Programmer