nrwl / nx

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

V2 format pulls every app/project defined in angular.json in the docker build process #9860

Closed manju-reddys closed 2 years ago

manju-reddys commented 2 years ago

Current Behavior

Running a build in the docker by just loading relevant apps code, will fail to complete with error, app2 project.json not found error.

Expected Behavior

Should only load the targeted app project.json in the build process and ignore rest in the angular.json

Steps to Reproduce

  1. Create 2 apps and 1 or more libs.
  2. change angular.json to NX version 2 format - it will generate apps/app*/project.json.
  3. Create a Dockerfile to copy the content of app1 and dockerignore to ignore app2 relevant.
  4. In the docker run the build process like nx build app1 --prod.
  5. After all deps installed, you would see the similar error logs as in the Failure logs.

From the stacktrace: readWorkspaceConfig (/opt/armada/node_modules/nx/src/project-graph/file-utils.js:99:21 looks like it's trying to load all the projects defined in angular.json

Failure Logs

0.565 yarn run v1.22.15
#22 0.597 $ yarn armada.server.build && yarn armada.client.build
#22 0.775 $ nx run armadaserver:serve:production --prod
#22 1.117 nx run [project][:target][:configuration] [_..]
#22 1.117
#22 1.117 Run a target for a project
#22 1.117     (e.g., nx run myapp:serve:production).
#22 1.117
#22 1.117     You can also use the infix notation to run a target:
#22 1.117     (e.g., nx serve myapp --configuration=production)
#22 1.117
#22 1.117     You can skip the use of Nx cache by using the --skip-nx-cache option.
#22 1.117
#22 1.117 Options:
#22 1.117       --version        Show version number  [boolean]
#22 1.117       --prod           Use the production configuration  [boolean] [default: false]
#22 1.117   -c, --configuration  Target configuration  [string]
#22 1.117       --project        Target project  [string]
#22 1.118
#22 1.120 Error: ENOENT: no such file or directory, open '/opt/armada/apps/armadasync/project.json'
#22 1.120     at Object.openSync (node:fs:585:3)
#22 1.120     at readFileSync (node:fs:453:35)
#22 1.120     at readJsonFile (/opt/armada/node_modules/nx/src/utils/fileutils.js:21:43)
#22 1.120     at /opt/armada/node_modules/nx/src/config/workspaces.js:280:61
#22 1.120     at Array.forEach (<anonymous>)
#22 1.120     at inlineProjectConfigurations (/opt/armada/node_modules/nx/src/config/workspaces.js:277:38)
#22 1.120     at resolveNewFormatWithInlineProjects (/opt/armada/node_modules/nx/src/config/workspaces.js:273:24)
#22 1.120     at Workspaces.readFromWorkspaceJson (/opt/armada/node_modules/nx/src/config/workspaces.js:176:16)
#22 1.120     at Workspaces.readWorkspaceConfiguration (/opt/armada/node_modules/nx/src/config/workspaces.js:57:20)
#22 1.120     at readWorkspaceConfig (/opt/armada/node_modules/nx/src/project-graph/file-utils.js:99:21) {
#22 1.120   errno: -2,
#22 1.120   syscall: 'open',
#22 1.120   code: 'ENOENT',
#22 1.120   path: '/opt/armada/apps/armadasync/project.json'
#22 1.120 }
#22 1.136 error Command failed with exit code 1.

Environment

   Node : 16.14.2
   OS   : darwin x64
   yarn : 1.22.18

   nx : 13.10.2
   @nrwl/angular : 13.10.2
   @nrwl/cypress : 13.10.2
   @nrwl/detox : Not Found
   @nrwl/devkit : 13.10.2
   @nrwl/eslint-plugin-nx : 13.10.2
   @nrwl/express : Not Found
   @nrwl/jest : 13.10.2
   @nrwl/js : 13.10.2
   @nrwl/linter : 13.10.2
   @nrwl/nest : 13.10.2
   @nrwl/next : Not Found
   @nrwl/node : 13.10.2
   @nrwl/nx-cloud : Not Found
   @nrwl/nx-plugin : Not Found
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : 13.10.2
   @nrwl/web : Not Found
   @nrwl/workspace : 13.10.2
   typescript : 4.6.3
   rxjs : 7.5.5
   ---------------------------------------
   Community plugins:
     @rx-angular/cdk: 1.0.0-beta.2
     @rx-angular/template: 1.0.0-beta.32
     @storybook/angular: 6.4.19
AgentEnder commented 2 years ago

This isn't really a bug but is just an implementation detail behind the CLI bootstrapping process. When Nx starts up, it builds the dependency graph (or reads it from the daemon).

To do this, it needs the workspace configuration. The complete configuration consists of workspace.json, with all project.json files inlined. Alternatively, if workspace.json doesn't exist, the entire configuration is represented as an in-memory object based on all projects located during project inference (think npm workspaces projects).

To change this would be a reasonably major refactoring in CLI initialization/graph construction. Is there a reason behind needing the behavior? Perhaps there is a better way to accomplish your goal.

manju-reddys commented 2 years ago

@AgentEnder We have quite a few applications and private node modules that some applications depends on (not all the apps). Coping every apps, its related private modules and build, is huge deal breaker in terms of time, resource and unpredictable/breaking issues of apps.

So to minimize the complexity and challenges in the CI build, deploy process, we copy only the app's that are building for example checkout client app and its related server apps + private modules, and remaining apps won't be copied into the docker container for building and packaging, which finally deployed to k8s cluster.

Temp. solution as recommended by you in the slack support, we are cleaning up the angular.json in the docker before we run the build+package.

AgentEnder commented 2 years ago

You could try removing the workspace.json/angular.json file entirely, in which case Nx builds up the configuration from the project.json files it can find. There's a minor perf hit, so you may decide it's not worth it. We plan to address the perf hit, but you can see if it's a significant enough issue for your use case.

electrichead commented 2 years ago

@AgentEnder how would it know the name of the project? I tried this but a command like

yarn nx run myproject:mytarget

says it was unable to find project "myproject". Is there a key in "project.json" that would specify the name?

AgentEnder commented 2 years ago

Project name is inferred based on path in the workspace, or can be manually specified by adding a "name" field in project.json

electrichead commented 2 years ago

Confirm that "name" field in the project.json works. Thanks @AgentEnder !

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. If we missed this issue please reply to keep it active. Thanks for being a part of the Nx community! 🙏

github-actions[bot] commented 1 year ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.