Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builds fail when using thread-loader #1206

Closed
valerio opened this issue Nov 4, 2020 · 8 comments
Closed

Builds fail when using thread-loader #1206

valerio opened this issue Nov 4, 2020 · 8 comments

Comments

@valerio
Copy link
Contributor

valerio commented Nov 4, 2020

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

@valerio
Copy link
Contributor Author

valerio commented Nov 4, 2020

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.

@johnnyreilly
Copy link
Member

Thanks @valerio!

@valerio
Copy link
Contributor Author

valerio commented Nov 4, 2020

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.

@Krasnopir
Copy link

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

@johnnyreilly
Copy link
Member

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

@Krasnopir
Copy link

@johnnyreilly 8.0.9 work without errors. Thanks a lot!

@johnnyreilly
Copy link
Member

Thanks @valerio for fixing and @Krasnopir for testing! ❤️🌻

@caseyjhol
Copy link

caseyjhol commented Jan 21, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants