Search code examples
angularbitbucket-pipelinesangular13angular-compilernx.dev

Nx build - Another process, with id xxx, is currently running ngcc


I have a Nx monorepo that contains 2 projects. When I build these projects locally everything works as expected, but when I try to build the same projects on the build server I get the error

Another process, with id 111, is currently running ngcc.
Waiting up to 250s for it to finish.
(If you are sure no ngcc process is running then you should delete the lock-file at /opt/atlassian/pipelines/agent/build/node_modules/.ngcc_lock_file.)

This is probably caused by command nx affected:build --all --parallel --configuration=production. That runs all builds in parallel, where somehow both builds run ngcc, which in turn locks certain files in the node_modules directory. So much is clear to me. But the problem is that I've tried almost every suggested fix, but the warning is still there, slowing down the build significantly and even making it fail in some cases.


Versions

  • Angular: 13.2.x (it gave the same error on v12.x)
  • Nx: 13.8.x
  • Node: v14.17.x
  • Pipeline: Bitbucket pipelines

Adding ngcc to post-install

According to some answers (here, here, here, here and many more) the best workaround is to add ngcc --properties es2015 browser module main or even ngcc --properties es2015 browser module main --create-ivy-entry-points --first-only to the postinstall in the package.json. Since Nx V12 this is automatically added when generating a new monorepo via Nx, but doesn't seem to fix the message on the build server (never had any issues locally)

("postinstall": "node ./decorate-angular-cli.js && ngcc --properties es2015 browser module main",)

Delete the lock-file

I've tried to delete the lock-file before the build begins, because I thought maybe the postinstall would create the lock-file, but somehow doesn't get the time to delete it. But all my attempts show that there is no lock-file before the build starts.

Script:

if [ -f "$BITBUCKET_CLONE_DIR/$NGCC_LOCATION" ]; then rm $BITBUCKET_CLONE_DIR/$NGCC_LOCATION; else echo "no lock-file"; fi

$NGCC_LOCATION="node_modules/.ngcc_lock_file."

Always results in no lock file


Caching

The node_modules are cached in the build server, so that it can be used in the separate pipeline steps. The script is as follows:

pipelines:
  pull-requests:
    '**':
      - step: *install
      - parallel:
          - step: *lint
          - step: *test
          - step: *build

Where every step uses the node cache that is created on the install-step. I cannot verify if the ngcc result is stored in the cache, but since the whole node_modules is cached after the install step and the ngcc result is added to the node_modules I am guessing it is stored.


Since none of the above is fixing the warning, is there something I am missing or is this just the way ngcc works for now?


Solution

  • I believe this problem is addressed in the following GitHub issue:

    And in particular by the following comments:

    Essentially it's necessary to trick the Angular Compiler Webpack Plugin into thinking it's running under Bazel by setting an environment variable (BAZEL_TARGET) for the Node processes running on the build system to read. Following is the relevant code snippet from the plugin's ngcc processor:

    if (process.env.BAZEL_TARGET) {
        return;
    }
    

    This prevents the plugin from running ngcc over the node_modules directory, which creates the lock file, which in turn prevents the parallel processes from executing. Since ngcc is already being run via the package.json postinstall script, there should be no need to run it again via the plugin, although the argument goes that the plugin can't be blamed for not knowing about the postinstall script.

    As is mentioned in the GitHub issue, the solution may be a bit of a hacky workaround but in testing with the versions/tech mentioned in my earlier comment seems to have been working reliably.

    Hope this makes sense.