examples icon indicating copy to clipboard operation
examples copied to clipboard

nestjs example with node-package requires includes/paths that point to local node_modules

Open airtonix opened this issue 1 year ago • 14 comments

  1. If you add @company/browser-package to the nestjs app and run moon run nestjs-app:dev then:
  • the tsconfig updates with a new reference that point to ../../packages/browser-package
  1. If you hard revert all changes and try again with @company/node-package, nothing happens.
  2. If you manually do the changes to mimic what was done in #1 and then try to create some.service.ts that makes use of the add function from @company/node-package then you get an error that this file(or any file in the nest-js package) is not included. This is because the includes directive is not in the nestjs-app tsconfig.json but instead in the tsconfig.build.json where the editor can't see it.
  3. if you make that correction for #3 then you get the next error which is that the node-package files are not in the rootDir examples/apps/nestjs-app. adding rootDir: "./" to the root tsconfig.options.json fixes this.
  • if you're following along incrementally here, then moon run nestjs-app:dev will work
  • remove the build directory now
  1. now that we have rootDir in the root tsconfig.options.json and our node-package properly referenced in the nestjs-app tsconfigs... running moon run nestjs-app:dev fails because the build directory no longer reflects the structure of the nestjs-app src directory, instead it reflect the structure of the monorepo so nest-cli can't find build/src/main (which is now at build/apps/nestjs-app/src/main)
  2. if you roll all the changes back to #2 and instead of using ../../packages/node-package, use node_modules/company/node-package and now everything works.

airtonix avatar Apr 07 '25 22:04 airtonix

ugh... looks like a lot of these issues might be caused by:

  • improper typescript selection in the editor (it defaults to the ide instance of typescript instead of the one in node_modules)
  • which was further compounded by not hard resetting the repo between certain steps so that the moon cache knew it had to make the right changes to the various tsconfigs

airtonix avatar Apr 07 '25 23:04 airtonix

I'm sure there are many issues with this repo, since it's not actively maintained, and more just a proof of concept of how things are organized. The nest app was also contributed to a third-party, so probably wasn't tested very much.

With that said, configuring TS is always such a giant pain.

milesj avatar Apr 07 '25 23:04 milesj

yeah i revisted my steps and at step 2 after removing moon cache and nestjs build dir, everything works.

airtonix avatar Apr 07 '25 23:04 airtonix

perhaps there's some anecdotal evidence here for more nuanced cache busting?

  • in a users project config (somehow?), or
  • in moon core logic when determining how/if/when it should modify tsconfigs?

airtonix avatar Apr 07 '25 23:04 airtonix

Nope, this is still an issue. I've uploaded an example

airtonix avatar Apr 08 '25 23:04 airtonix

Main issue is this https://github.com/airtonix/moon-examples/pull/1/files#r2034166204

airtonix avatar Apr 08 '25 23:04 airtonix

Ok some discovery in https://github.com/airtonix/moon-examples/pull/1

If a moon repo has a nestjs app that never uses anything from the rest of the monorepo, then it can be structured just like it currently is in the moon example repo. (as what nest-cli calls a "single app repo")

However, in a monorepo that's very unlikely to remain the case.

So in the case where we start using other parts of the monorepo, two things have to change:

  1. the nest-cli.json needs to move to the root of the monorepo.
  2. the nest-cli.json now needs to nest the original contents of the apps nest-cli.json within a projects key.

At this point i can run (from the root of the monorepo mind you):

nest start nestjs-app

and it builds and runs.

What I can't seem to do (which I'll probably need to do more research in to moon) is this:

moon nestjs-app:dev

my theory is that Moon is incapable of running tasks for a project from the root of the repo?

airtonix avatar Apr 08 '25 23:04 airtonix

edit... I was able to overcome it with the jankiest of jank...

  dev:
    command: bash -c "cd ../.. && nest start nestjs-app --watch"
    

https://github.com/airtonix/moon-examples/pull/1/commits/f6444dc556f9c3e13931b4ded9074d98c534ea96

airtonix avatar Apr 08 '25 23:04 airtonix

@airtonix Would this option work? https://moonrepo.dev/docs/config/project#runfromworkspaceroot

Also this seems like a limitation of nest js if it can't run in a monorepo?

milesj avatar Apr 09 '25 01:04 milesj

Yep, that also works.

Perhaps for now we add a second nestjs example? one that imports and uses node-package and has a nest-cli.json at the root of the repo?

and also update the nestjs guide to mention these aspects about nestjs

airtonix avatar Apr 09 '25 01:04 airtonix

The nestjs in this repo is probably out of date. I wonder if this is still an issue with a newer versoin.

milesj avatar Apr 09 '25 15:04 milesj

Yeah nothing about the newer verson of nestjs has changed with regards to this problem.

I think the way NX.dev solve this is by providing a webpack bundler that obviates the need to leave the project directory or deal with the fact that such a tsconfig setup would result in the build output directory reflecting the resolved tree caused by rootDir.

In fact I could imagine this would not be an issue unique to nestjs, any other framework that relies on tsc to build the app before it runs would have some variant of this challenge

airtonix avatar Apr 10 '25 02:04 airtonix

My original solution to this problem was to rely on the fact that my package manager created a node_modules directory inside the nestjs-app directory.

This meant i could:

  • avoid the need for rootDir in the root tsconfig
  • not have my apps/nestjs-app/build dir reflect the monorepo structure. so i can now just run nest in the project directory

BUT... to get here requires another kind of jank:

  • my nestjs-app tsconfig needed to point references, paths and includes to node_modules/@company/nodejs-pacakage instead of ../../packages/nodejs-package

airtonix avatar Apr 10 '25 03:04 airtonix

ok so just tried another way to solve this problem.

and it works?

https://github.com/moonrepo/examples/commit/5baacaa6a976d6bbd98e0277c9780cbadd0611d2#diff-1b7017c24b9d5587ec007d51cab6350ad0725b3bd0f45a957f635bb79a0164dbR4-R6

airtonix avatar Apr 10 '25 03:04 airtonix