nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.58k stars 2.36k forks source link

nx graph file output html RangeError: Invalid string length #20734

Open Nicolas-Menettrier opened 10 months ago

Nicolas-Menettrier commented 10 months ago

Current Behavior

When I try to execute the command yarn nx graph --file=./index.html I got this error :

RangeError: Invalid string length
    at JSON.stringify (<anonymous>)
    at buildEnvironmentJs (/node_modules/nx/src/command-line/graph/graph.js:74:70)
    at Object.generateGraph (/node_modules/nx/src/command-line/graph/graph.js:223:35)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.handler (/node_modules/nx/src/command-line/graph/command-object.js:20:30)

Expected Behavior

The command should generate the html output. It works with json files.

GitHub Repo

No response

Steps to Reproduce

  1. yarn nx graph --file=./index.html

Nx Report

>  NX   Report complete - copy this into the issue template

   Node   : 20.9.0
   OS     : darwin-arm64
   yarn   : 1.22.19

   nx                 : 17.2.3
   @nx/js             : 17.2.3
   @nrwl/js           : 14.8.9
   @nx/jest           : 17.2.3
   @nrwl/jest         : 14.8.9
   @nx/linter         : 17.2.3
   @nx/eslint         : 17.2.3
   @nx/workspace      : 17.2.3
   @nrwl/workspace    : 14.8.9
   @nx/cypress        : 17.2.3
   @nrwl/cypress      : 14.8.9
   @nx/devkit         : 17.2.3
   @nrwl/devkit       : 14.8.9
   @nx/eslint-plugin  : 17.2.3
   @nx/express        : 17.2.3
   @nx/nest           : 17.2.3
   @nx/next           : 17.2.3
   @nx/node           : 17.2.3
   @nx/plugin         : 17.2.3
   @nx/react          : 17.2.3
   @nx/rollup         : 17.2.3
   @nx/storybook      : 17.2.3
   @nrwl/tao          : 17.2.3
   @nx/web            : 17.2.3
   @nx/webpack        : 17.2.3
   @nrwl/webpack      : 14.8.9
   typescript         : 5.2.2
   ---------------------------------------
   Community plugins:
   @jscutlery/semver      : 2.26.0
   @nrwl/remix            : 14.7.0
   @nx-plus/docusaurus    : 15.0.0-rc.0
   @nx-tools/nx-container : 4.0.2
   ---------------------------------------
   Local workspace plugins:
         @tozzi/tools/i18n
         @tozzi/workspace-plugin
   ---------------------------------------
   The following packages should match the installed version of nx
     - @nrwl/js@14.8.9
     - @nrwl/jest@14.8.9
     - @nrwl/workspace@14.8.9
     - @nrwl/cypress@14.8.9
     - @nrwl/devkit@14.8.9
     - @nrwl/webpack@14.8.9

   To fix this, run `nx migrate nx@17.2.3`

Failure Logs

RangeError: Invalid string length
    at JSON.stringify (<anonymous>)
    at buildEnvironmentJs (/node_modules/nx/src/command-line/graph/graph.js:74:70)
    at Object.generateGraph (/node_modules/nx/src/command-line/graph/graph.js:223:35)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.handler (/node_modules/nx/src/command-line/graph/command-object.js:20:30)

Package Manager Version

No response

Operating System

Additional Information

No response

jaysoo commented 10 months ago

A repro would help a lot in our investigation. The error could be due to JSON.stringify not being able to handle a large payload, but I need to confirm that is indeed the case since I haven't run into this limit yet on some pretty large monorepos.

I also notice old @nrwl/ scoped packages, you can safely remove them.

Nicolas-Menettrier commented 10 months ago

Sadly I can't share a reproduction for you, but it's quite large monorepo with 103 projects inside

jaysoo commented 10 months ago

Does running nx graph without options work?

Nicolas-Menettrier commented 10 months ago

Yes nx graph is working fine. If I do nx graph --file=./index.json it works as well, it's really specific to the html output

jaysoo commented 10 months ago

Interesting, and --file=output.json works as well, just HTML is broken?

Nicolas-Menettrier commented 10 months ago

Yes --file=output.json is working fine, just the HTML output seems broken. The json is generated in 0.80s, the HTML takes minutes and then crash.

SombreroElGringo commented 10 months ago

Hi @jaysoo I have the same problem, in my case too my monorepo have more than 100 projects, I can easily generate the json output, but when I try to generate the html output it fails and timeout with the same error message.

If we share with you the json output, would it allow you to try the thing in your side?

RangeError: Invalid string length
    at JSON.stringify (<anonymous>)
    at buildEnvironmentJs (/node_modules/nx/src/command-line/graph/graph.js:74:70)
    at Object.generateGraph (/node_modules/nx/src/command-line/graph/graph.js:223:35)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.handler (/node_modules/nx/src/command-line/graph/command-object.js:20:30)
Nicolas-Menettrier commented 10 months ago

The JSON is too big for json stringify, even when using an "optimized" version it still fail because the generated string is too big :

function JSONstringifyOptimized(obj) {
    if (Array.isArray(obj)) {
        return '[' +
            obj.map( (value) => {
                return JSONstringifyOptimized(value);
            }).join(', ') +
            ']';
    } else if (obj && typeof obj === 'object') {
        return Object.keys(obj).length === 0
            ? '{}'
            : '{' +
            Object.keys(obj).map( (key) => {
                return '"key"' + ': ' +
                    JSONstringifyOptimized(obj[key]);
            }).join(', ') +
            '}';
    }
    return obj;
}

function buildEnvironmentJs(exclude, watchMode, localMode, depGraphClientResponse, taskGraphClientResponse, expandedTaskInputsReponse, sourceMapsResponse) {
    let environmentJs = `window.exclude = ${JSONstringifyOptimized(exclude)};
  window.watch = ${!!watchMode};
  window.environment = 'release';
  window.localMode = '${localMode}';

  window.appConfig = {
    showDebugger: false,
    showExperimentalFeatures: false,
    workspaces: [
      {
        id: 'local',
        label: 'local',
        projectGraphUrl: 'project-graph.json',
        taskGraphUrl: 'task-graph.json',
        taskInputsUrl: 'task-inputs.json',
        sourceMapsUrl: 'source-maps.json'
      }
    ],
    defaultWorkspaceId: 'local',
  };
  `;
    if (localMode === 'build') {
        environmentJs += `window.projectGraphResponse = ${JSONstringifyOptimized(depGraphClientResponse)};
    `;
        environmentJs += `window.taskGraphResponse = ${JSONstringifyOptimized(taskGraphClientResponse)};
    `;
        environmentJs += `window.expandedTaskInputsResponse = ${JSONstringifyOptimized(expandedTaskInputsReponse)};`;
        environmentJs += `window.sourceMapsResponse = ${JSONstringifyOptimized(sourceMapsResponse)};`;
    }
    else {
        environmentJs += `window.projectGraphResponse = null;`;
        environmentJs += `window.taskGraphResponse = null;`;
        environmentJs += `window.expandedTaskInputsResponse = null;`;
        environmentJs += `window.sourceMapsResponse = null;`;
    }
    return environmentJs;
}
Nicolas-Menettrier commented 10 months ago

@jaysoo It might be caused by this commit https://github.com/nrwl/nx/commit/75cc561e9d28368c8f827ed62ba9f8f08fabff67 , now there is the source map displayed and it might be too big if you have a lot of project.

MaxKless commented 5 months ago

Hey!

I've tried reproducing the issue but interestingly haven't managed yet. For the record I've created a repo with over 550 projects & 70 targets each. The environment.js that is generated was more than 230MB large.

Could you reproduce the error again and tell me exactly on which call is causing the issue? Is it the expandedTaskInputsReponse or the sourceMapsResponse? Then I can maybe repro it better myself.

Nicolas-Menettrier commented 5 months ago

Hey! Just checked and it fail at the JSON.stringify of the expandedTaskInputsReponse

titan-ron commented 2 months ago

Same issue for us too

Monrepo of +300 projects & 5-10 tasks per each & some 4 env for build & serve

It's taking around 15 minutes to generate the object at createExpandedTaskInputResponse(), then fail on the JSON.strigify() for the window.expandedTaskInputsResponse, with the same error:

RangeError: Invalid string length
    at JSON.stringify (<anonymous>)
    at buildEnvironmentJs (**/node_modules/nx/src/command-line/graph/graph.js:74:70)
    at Object.generateGraph (**/node_modules/nx/src/command-line/graph/graph.js:223:35)
    at async Object.handler (**/node_modules/nx/src/command-line/graph/command-object.js:20:30)

Only when using --file=*.html local host and JSON are work fine