question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Workspace 1 uses different webpack instance than an imported Workspace 2

See original GitHub issue
  • I’d be willing to implement a fix

Describe the bug

We have a weird setup where one package builds subpackages using the subpackages’ webpack configurations. This worked before Yarn 2, and I’m trying to get it to work with Yarn 2. The problem is that the subpackage’s webpack instance is a different instance than the first package’s webpack instance (due to different virtual paths), which errors with The 'compilation' argument must be an instance of Compilation.

I’d expect this to work, since both packages have the same version of webpack specified. But if this is intended behavior, is there a way to get this working? Maybe worth adding to the docs in case others have a similar problem.

To Reproduce

Reproduction
const { promises: { writeFile } } = require('fs')

await packageJson({
  name: 'package2',
  devDependencies: {
    'webpack': '^5',
    // changes virtual path between package1/package2
    'webpack-cli': '*',
  },
}, { cwd: 'package2/' })

await writeFile('package2/webpack.config.js', `
const path = require('path')
const webpack = require('webpack')

module.exports = {
  entry: path.resolve(__dirname, 'index.js'),
  // configure a plugin using this webpack's instance
  plugins: [new webpack.HotModuleReplacementPlugin()],
}
`)

await packageJson({
  name: 'package1',
  devDependencies: {
    'webpack': '^5',
    'package2': 'workspace:*'
  },
}, { cwd: 'package1/' })

await writeFile('package1/buildSubpackage.js', `
const webpack = require('webpack')

const config = require('package2/webpack.config.js')

webpack(config, (err, stats) => {
  if (err) {
    console.error(err);
    process.exit(1);
  }
  console.log(stats.toString());
})
`)

await packageJson({
  name: 'root',
  private: true,
  workspaces: [
    'package1/',
    'package2/',
  ],
})

await yarn('install')
await expect(yarn('workspace', 'package1', 'node', 'buildSubpackage')).rejects.not.toThrow()

Screenshots

If applicable, add screenshots to help explain your problem.

Environment if relevant (please complete the following information):

  • OS: [e.g. OSX, Linux, Windows, …]
  • Node version [e.g. 8.15.0, 10.15.1, …]
  • Yarn version [e.g. 2.4.0, …]

Additional context

Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
yarnbotcommented, Jan 23, 2021

This issue reproduces on master:

Error: expect(received).rejects.not.toThrow()

Error name:    "Error"
Error message: "Command failed: /usr/bin/node /github/workspace/scripts/actions/../run-yarn.js workspace package1 node buildSubpackage
TypeError: The 'compilation' argument must be an instance of Compilation
    at Function.getCompilationHooks (/tmp/tmp-20NBd98YHLyuXd/.yarn/$$virtual/webpack-virtual-ff5fc9106b/0/cache/webpack-npm-5.17.0-e594321923-2840d6291d.zip/node_modules/webpack/lib/NormalModule.js:179:10)
    at /tmp/tmp-20NBd98YHLyuXd/.yarn/$$virtual/webpack-virtual-ff5fc9106b/0/cache/webpack-npm-5.17.0-e594321923-2840d6291d.zip/node_modules/webpack/lib/HotModuleReplacementPlugin.js:705:18
    at Hook.eval [as call] (eval at create (/tmp/tmp-20NBd98YHLyuXd/.yarn/cache/tapable-npm-2.2.0-516ee6aa5b-f8ed725aed.zip/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:100:1)
    at Hook.CALL_DELEGATE [as _call] (/tmp/tmp-20NBd98YHLyuXd/.yarn/cache/tapable-npm-2.2.0-516ee6aa5b-f8ed725aed.zip/node_modules/tapable/lib/Hook.js:14:14)
    at Compiler.newCompilation (/tmp/tmp-20NBd98YHLyuXd/.yarn/$$virtual/webpack-virtual-f9f75363a7/0/cache/webpack-npm-5.17.0-e594321923-2840d6291d.zip/node_modules/webpack/lib/Compiler.js:993:26)
    at /tmp/tmp-20NBd98YHLyuXd/.yarn/$$virtual/webpack-virtual-f9f75363a7/0/cache/webpack-npm-5.17.0-e594321923-2840d6291d.zip/node_modules/webpack/lib/Compiler.js:1035:29
    at Hook.eval [as callAsync] (eval at create (/tmp/tmp-20NBd98YHLyuXd/.yarn/cache/tapable-npm-2.2.0-516ee6aa5b-f8ed725aed.zip/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/tmp/tmp-20NBd98YHLyuXd/.yarn/cache/tapable-npm-2.2.0-516ee6aa5b-f8ed725aed.zip/node_modules/tapable/lib/Hook.js:18:14)
    at Compiler.compile (/tmp/tmp-20NBd98YHLyuXd/.yarn/$$virtual/webpack-virtual-f9f75363a7/0/cache/webpack-npm-5.17.0-e594321923-2840d6291d.zip/node_modules/webpack/lib/Compiler.js:1030:28)
    at /tmp/tmp-20NBd98YHLyuXd/.yarn/$$virtual/webpack-virtual-f9f75363a7/0/cache/webpack-npm-5.17.0-e594321923-2840d6291d.zip/node_modules/webpack/lib/Compiler.js:473:12

"

      at Function.getCompilationHooks (.yarn/$$virtual/webpack-virtual-ff5fc9106b/0/cache/webpack-npm-5.17.0-e594321923-2840d6291d.zip/node_modules/webpack/lib/NormalModule.js:179:10)
      at .yarn/$$virtual/webpack-virtual-ff5fc9106b/0/cache/webpack-npm-5.17.0-e594321923-2840d6291d.zip/node_modules/webpack/lib/HotModuleReplacementPlugin.js:705:18
      at Hook.eval [as call] (eval at create (.yarn/cache/tapable-npm-2.2.0-516ee6aa5b-f8ed725aed.zip/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:100:1)
      at Hook.CALL_DELEGATE [as _call] (.yarn/cache/tapable-npm-2.2.0-516ee6aa5b-f8ed725aed.zip/node_modules/tapable/lib/Hook.js:14:14)
      at Compiler.newCompilation (.yarn/$$virtual/webpack-virtual-f9f75363a7/0/cache/webpack-npm-5.17.0-e594321923-2840d6291d.zip/node_modules/webpack/lib/Compiler.js:993:26)
      at .yarn/$$virtual/webpack-virtual-f9f75363a7/0/cache/webpack-npm-5.17.0-e594321923-2840d6291d.zip/node_modules/webpack/lib/Compiler.js:1035:29
      at Hook.eval [as callAsync] (eval at create (.yarn/cache/tapable-npm-2.2.0-516ee6aa5b-f8ed725aed.zip/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
      at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (.yarn/cache/tapable-npm-2.2.0-516ee6aa5b-f8ed725aed.zip/node_modules/tapable/lib/Hook.js:18:14)
      at Compiler.compile (.yarn/$$virtual/webpack-virtual-f9f75363a7/0/cache/webpack-npm-5.17.0-e594321923-2840d6291d.zip/node_modules/webpack/lib/Compiler.js:1030:28)
      at .yarn/$$virtual/webpack-virtual-f9f75363a7/0/cache/webpack-npm-5.17.0-e594321923-2840d6291d.zip/node_modules/webpack/lib/Compiler.js:473:12
    at Object.toThrow (/github/workspace/.yarn/cache/expect-npm-24.8.0-8c7640c562-0ac41999f0.zip/node_modules/expect/build/index.js:242:20)
    at module.exports (evalmachine.<anonymous>:55:84)
    at async /github/workspace/.yarn/cache/@arcanis-sherlock-npm-1.0.38-d4f5e2dbf3-63f998598d.zip/node_modules/@arcanis/sherlock/lib/executeRepro.js:56:13
    at async executeInTempDirectory (/github/workspace/.yarn/cache/@arcanis-sherlock-npm-1.0.38-d4f5e2dbf3-63f998598d.zip/node_modules/@arcanis/sherlock/lib/executeRepro.js:17:16)
    at async Object.executeRepro (/github/workspace/.yarn/cache/@arcanis-sherlock-npm-1.0.38-d4f5e2dbf3-63f998598d.zip/node_modules/@arcanis/sherlock/lib/executeRepro.js:24:12)
    at async ExecCommand.execute (/github/workspace/.yarn/cache/@arcanis-sherlock-npm-1.0.38-d4f5e2dbf3-63f998598d.zip/node_modules/@arcanis/sherlock/lib/commands/exec.js:25:38)
    at async ExecCommand.validateAndExecute (/github/workspace/.yarn/cache/clipanion-npm-2.0.0-rc.16-b9444aaf89-a57989414f.zip/node_modules/clipanion/lib/advanced/Command.js:161:26)
    at async Cli.run (/github/workspace/.yarn/cache/clipanion-npm-2.0.0-rc.16-b9444aaf89-a57989414f.zip/node_modules/clipanion/lib/advanced/Cli.js:74:24)
    at async Cli.runExit (/github/workspace/.yarn/cache/clipanion-npm-2.0.0-rc.16-b9444aaf89-a57989414f.zip/node_modules/clipanion/lib/advanced/Cli.js:83:28)
0reactions
brandon-leapyearcommented, Jan 23, 2021

Thanks! Got past the error

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack: "there are multiple modules with names that only ...
This is usually a result of a minuscule typo. For instance, if you are importing your modules like this: import Vue from 'vue'....
Read more >
The JavaScript Modules Handbook – Complete Guide to ES ...
To import exported code, use ES module's import statement. For instance, open your module-2.js file and replicate the code below: // module-2.js ...
Read more >
Development - webpack
webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable...
Read more >
Configuring TypeScript for multiple packages in a Lerna + ...
In this article I will be showing you how to leverage the power of Lerna and yarn workspaces in a TypeScript project to...
Read more >
Migration | Yarn - Package Manager
For example here's what webpack-dev-server would reveal: ➤ YN0000: Found 1 package(s) ... Yarn 2 uses a different style of configuration files than...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found