Ts-loader: Builds fail when using thread-loader

Created on 4 Nov 2020  路  8Comments  路  Source: TypeStrong/ts-loader

Expected Behaviour

Using thread-loader before ts-loader, compilation of a project should succeed.

Actual Behaviour

Compilation fails with the following error:

[webpack-cli] Compilation finished
assets by status 655 bytes [cached] 1 asset
./src/app.ts 39 bytes [built] [code generated] [1 error]

ERROR in ./src/app.ts
Module build failed (from ./node_modules/thread-loader/dist/cjs.js):
Thread Loader (Worker 0)
Invalid value used as weak map key
    at PoolWorker.fromErrorObj (/Users/valeriopipolo/repos/tsloaderleak/node_modules/thread-loader/dist/WorkerPool.js:344:12)
    at /Users/valeriopipolo/repos/tsloaderleak/node_modules/thread-loader/dist/WorkerPool.js:217:29
    at WeakMap.set (<anonymous>)
    at Object.getTypeScriptInstance (/Users/valeriopipolo/repos/tsloaderleak/node_modules/ts-loader/dist/instances.js:37:23)
    at Object.loader (/Users/valeriopipolo/repos/tsloaderleak/node_modules/ts-loader/dist/index.js:16:41)

webpack 5.3.2 compiled with 1 error in 441 ms

Steps to Reproduce the Problem

Use a minimal webpack configuration as follows:

module.exports = {
  devtool: "inline-source-map",
  entry: "./src/app.ts",
  output: {
    filename: "bundle.js",
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: ["thread-loader", "ts-loader"],
      },
    ],
  },
};

Running builds with webpack should fail.
Reproducible with the following dependencies:

    "thread-loader": "3.0.1",
    "ts-loader": "8.0.8",
    "typescript": "4.0.5",
    "webpack": "4.13.0"

Location of a Minimal Repository that Demonstrates the Issue.

https://github.com/valerio/tsloader-build-issue-with-thread-loader

Most helpful comment

@johnnyreilly 8.0.9 work without errors. Thanks a lot!

All 8 comments

When using thread-loader the loader._compiler instance is set to undefined, which makes setting it as a key to a WeakMap fail at runtime: https://github.com/TypeStrong/ts-loader/blob/master/src/instances.ts#L66

A couple ideas for workarounds:

  • use a "marker" object to use as a key in the WeakMap whenever a webpack compiler isn't available
  • keep a separate "fallback" map of instance name -> TS compiler that should be used only when the webpack compiler is undefined

I'll see which one seems nicer and make a PR for it.

Thanks @valerio!

I've updated the version of webpack in the first post to 4.12.2.

With the fix I had in mind compilation will work fine up to that version, but using webpack >= 4.13.0 will fail with a different error:

ERROR in ./src/app.ts
Module build failed (from ./node_modules/thread-loader/dist/cjs.js):
Thread Loader (Worker 0)
Cannot read property 'hooks' of undefined
    at PoolWorker.fromErrorObj (/Users/valeriopipolo/repos/tsloader-build-issue-with-thread-loader/node_modules/thread-loader/dist/WorkerPool.js:344:12)
    at /Users/valeriopipolo/repos/tsloader-build-issue-with-thread-loader/node_modules/thread-loader/dist/WorkerPool.js:217:29
    at mapSeries (/Users/valeriopipolo/repos/tsloader-build-issue-with-thread-loader/node_modules/neo-async/async.js:3625:14)
    at PoolWorker.onWorkerMessage (/Users/valeriopipolo/repos/tsloader-build-issue-with-thread-loader/node_modules/thread-loader/dist/WorkerPool.js:171:34)
    at Object.initializeInstance (/Users/valeriopipolo/repos/ts-loader/dist/instances.js:267:31)
    at successLoader (/Users/valeriopipolo/repos/ts-loader/dist/index.js:26:17)
    at Object.loader (/Users/valeriopipolo/repos/ts-loader/dist/index.js:23:5)

This happens also with lower versions of ts-loader such as 8.0.7, so it seems like a separate issue.

i freezed my version of ts-loader on "8.0.7" instead "^8.0.7" and all work.

v8.0.9 is should be out in the next 20 minutes and will hopefully resolve the issues https://github.com/TypeStrong/ts-loader/releases/tag/v8.0.9

@johnnyreilly 8.0.9 work without errors. Thanks a lot!

Thanks @valerio for fixing and @Krasnopir for testing! 鉂わ笍馃尰

I've updated the version of webpack in the first post to 4.12.2.

With the fix I had in mind compilation will work fine up to that version, but using webpack >= 4.13.0 will fail with a different error:

ERROR in ./src/app.ts
Module build failed (from ./node_modules/thread-loader/dist/cjs.js):
Thread Loader (Worker 0)
Cannot read property 'hooks' of undefined
    at PoolWorker.fromErrorObj (/Users/valeriopipolo/repos/tsloader-build-issue-with-thread-loader/node_modules/thread-loader/dist/WorkerPool.js:344:12)
    at /Users/valeriopipolo/repos/tsloader-build-issue-with-thread-loader/node_modules/thread-loader/dist/WorkerPool.js:217:29
    at mapSeries (/Users/valeriopipolo/repos/tsloader-build-issue-with-thread-loader/node_modules/neo-async/async.js:3625:14)
    at PoolWorker.onWorkerMessage (/Users/valeriopipolo/repos/tsloader-build-issue-with-thread-loader/node_modules/thread-loader/dist/WorkerPool.js:171:34)
    at Object.initializeInstance (/Users/valeriopipolo/repos/ts-loader/dist/instances.js:267:31)
    at successLoader (/Users/valeriopipolo/repos/ts-loader/dist/index.js:26:17)
    at Object.loader (/Users/valeriopipolo/repos/ts-loader/dist/index.js:23:5)

This happens also with lower versions of ts-loader such as 8.0.7, so it seems like a separate issue.

I'm running into this exact same Cannot read property 'hooks' of undefined error with ts-loader v8.0.11, thread-loader v3.0.1, and webpack v4.44.2.

Was this page helpful?
0 / 5 - 0 ratings