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

Rebuilds fail when app is using WebpackBundleAnalyzer & probably others #1441

Closed
bendemboski opened this issue May 19, 2023 · 3 comments
Closed
Labels
bug Something isn't working has reproduction

Comments

@bendemboski
Copy link
Contributor

Reproduction

$ npx ember-cli new test-app
$ cd test-app/
$ yarn add --dev @embroider/core @embroider/compat @embroider/webpack webpack webpack-bundle-analyzer

edit ember-cli-build.js to look like:

'use strict';

const EmberApp = require('ember-cli/lib/broccoli/ember-app');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');

module.exports = function (defaults) {
  const app = new EmberApp(defaults, {
    // Add options here
  });

  const { Webpack } = require('@embroider/webpack');
  return require('@embroider/compat').compatBuild(app, Webpack, {
    packagerOptions: {
      webpackConfig: {
        plugins: [new BundleAnalyzerPlugin()],
      },
    },
  });
};

run ember serve, then edit a file to cause a rebuild.

Result

The rebuild fails with this error:

ERROR Summary:

  - broccoliBuilderErrorStack: TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Compiler'
    --- property 'root' closes the circle
    at JSON.stringify (<anonymous>)
    at stringify (/private/tmp/test-app/node_modules/fs-extra/node_modules/jsonfile/utils.js:3:20)
    at outputJsonSync (/private/tmp/test-app/node_modules/fs-extra/lib/json/output-json-sync.js:7:15)
    at AppBuilder.addResolverConfig (/private/tmp/test-app/node_modules/@embroider/core/src/app.js:966:35)
    at AppBuilder.build (/private/tmp/test-app/node_modules/@embroider/core/src/app.js:887:10)
    at async WaitForTrees.buildHook (/private/tmp/test-app/node_modules/@embroider/core/src/build-stage.js:33:13)
  - code: [undefined]
  - codeFrame: Converting circular structure to JSON
    --> starting at object with constructor 'Compiler'
    --- property 'root' closes the circle
  - errorMessage: Converting circular structure to JSON
    --> starting at object with constructor 'Compiler'
    --- property 'root' closes the circle
        at WaitForTrees (@embroider/compat/app)
-~- created here: -~-
    at new Plugin (/private/tmp/test-app/node_modules/@embroider/core/node_modules/broccoli-plugin/dist/index.js:47:36)
    at new WaitForTrees (/private/tmp/test-app/node_modules/@embroider/core/src/wait-for-trees.js:30:9)
    at CompatApp.get tree (/private/tmp/test-app/node_modules/@embroider/core/src/build-stage.js:25:16)
    at CompatApp.<anonymous> (/private/tmp/test-app/node_modules/typescript-memoize/dist/memoize-decorator.js:132:52)
    at new PackagerRunner (/private/tmp/test-app/node_modules/@embroider/core/src/to-broccoli-plugin.js:11:26)
    at Object.defaultPipeline (/private/tmp/test-app/node_modules/@embroider/compat/src/default-pipeline.js:50:12)
    at module.exports (/private/tmp/test-app/ember-cli-build.js:12:39)
    at Builder.readBuildFile (/private/tmp/test-app/node_modules/ember-cli/lib/models/builder.js:48:20)
    at async Builder.setupBroccoliBuilder (/private/tmp/test-app/node_modules/ember-cli/lib/models/builder.js:68:17)
    at async Builder.ensureBroccoliBuilder (/private/tmp/test-app/node_modules/ember-cli/lib/models/builder.js:56:7)

Analysis

When the webpack-bundle-analyzer plugin's apply() is called, it caches a copy of the compiler object on itself. That object contains a reference back to the plugin, creating a circular reference. When embroider tries to write out the resolver config on a rebuild, it fails because the config options now contain a circular reference and are not JSON stringifiable.

In general, I think it looks like the webpack config is not guaranteed to be JSON stringifiable, so trying to stringify the whole configuration isn't a safe thing to do. Perhaps CompatAppAdapter.resolverConfig() should not be returning its entire CompatResolverOptions, but instead filtering them down to only the fields needed to implement the module resolver options? Or addResolverConfig() should prune down the object it receives to just the set of fields that match the interface?

@chancancode
Copy link
Contributor

Same issue with extraPublicTrees, it does seem like addResolverConfig is trying to serialize too much

@chancancode
Copy link
Contributor

This appears to be the subset that it should have picked out

@achambers
Copy link

Should be fixed by: #1481

@achambers achambers added has reproduction bug Something isn't working labels Jun 28, 2023
@ef4 ef4 closed this as completed in 21c56b2 Jun 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working has reproduction
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants