nrwl / nx

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

@nrwl/js:library fails to cross import/compile util libraries #10800

Closed nhhockeyplayer closed 2 years ago

nhhockeyplayer commented 2 years ago

SUBJECT: @nrwl/js:library fails to cross import/compile util libraries

Current Behavior

Nx cannot locate shared modules in the npm package ecosystem under node_modules My entity library cannot import dto's from dto library I have to manually rig up node_modules file:// protocol installs to get my build to succeed

Expected Behavior

modules not found... this should be automated paths are configured right typescript is coded right project files barrel all in order and fleet

Steps to Reproduce

ask to be collaborator to my repo and I will grant you access Im a private developer

remove all npm packages within package.json

then execute

====== cut here ===== here are the details on how this came about... all angular cache at .angular/cache/** turned off the following command was used to create entity library.. after subsequent same for dto library

meanstack02@admin meanstacknh % node node_modules/@nrwl/cli/bin/nx.js generate @nrwl/js:library --name=dto --directory=shared/core --publishable --buildable --prefix=shared-core-dto --skipFormat --addModuleSpec --parentModule=apps/admin/src/app/app.module.ts --tags='domain:shared,scope:core,platform:web,type:util' --unitTestRunner=jest --importPath=@meanstacknh/shared-core-dto --strict --linter=eslint --standaloneConfig --compilationMode=partial --no-interactive

cross lib import statements not holding water for lines 3 and 4 below

import {getModelForClass, modelOptions, pre, prop} from '@typegoose/typegoose'
import {AbstractPojo} from './pojo/abstract.pojo'
import {AnswerType, IAnswerDTO} from '@meanstacknh/shared-core-dto/i-answer.dto'
import {EQuestion} from '@meanstacknh/shared-core-dto/isomorphic/biz/e-question'

simple build refuses to complete/succeed for entity library that cross references classes of shared dto library fails on location of the module on import statements

nx run-many --all --target build

  or

nx build shared-core-dto   
nx build shared-core-entity

here is the repo just ask to be collaborator https://github.com/nhhockeyplayer/meanstacknh

Failure Logs

> nx run shared-core-entity:build

Compiling TypeScript files for project "shared-core-entity"...
libs/shared/core/entity/src/lib/answer.entity.ts:3:38 - error TS2307: Cannot find module '@meanstacknh/shared-core-dto/i-answer.dto' or its corresponding type declarations.

 —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 >  NX   Ran target build for project shared-core-entity and 1 task(s) it depends on (2s)

         With additional flags:

    ✖    1/2 failed
    ✔    1/2 succeeded [1 read from cache]

   See Nx Cloud run details at https://nx.app/runs/NtLDtWAH9S0

Environment

nx report

 >  NX   Report complete - copy this into the issue template

   Node : 16.15.1
   OS   : darwin x64
   yarn : 1.22.18

   nx : 14.3.1
   @nrwl/angular : 14.3.1
   @nrwl/cypress : 14.3.1
   @nrwl/detox : Not Found
   @nrwl/devkit : 14.3.1
   @nrwl/eslint-plugin-nx : 14.3.1
   @nrwl/express : Not Found
   @nrwl/jest : 14.3.1
   @nrwl/js : 14.3.1
   @nrwl/linter : 14.3.1
   @nrwl/nest : 14.3.1
   @nrwl/next : Not Found
   @nrwl/node : 14.3.1
   @nrwl/nx-cloud : 14.1.0
   @nrwl/nx-plugin : Not Found
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : 14.3.1
   @nrwl/web : Not Found
   @nrwl/workspace : 14.3.1
   typescript : 4.7.3
   ---------------------------------------
   Community plugins:
     @ionic/angular: 6.1.10
     @ngrx/component-store: 13.2.0
     @ngrx/effects: 13.2.0
     @ngrx/entity: 13.2.0
     @ngrx/router-store: 13.2.0
     @ngrx/store: 13.2.0
     @nxtend/ionic-angular: 13.1.0
     @ngrx/schematics: 13.2.0
     @ngrx/store-devtools: 13.2.0
     @nxpm/stack: 4.21.0
meanstack02@admin meanstacknh % ng --version
14.3.1
nhhockeyplayer commented 2 years ago

If the nx team can review this ticket and this thread https://stackoverflow.com/questions/56603223/nrwl-nx-importing-lib-error-ts2307-cannot-find-module-eduboard-interfaces

if its truly a bug should be fixed? yes?

Thanks for your diligence

nhhockeyplayer commented 2 years ago

Some ground on this made and obtained a successful build but not without some divine intervention.

CONTEXT: my monorepo has two utility libraries


- @meanstacknh/shared-core-dto
- @meanstacknh/shared-core-entity

my command line terminal build sequence is as follows

@meanstacknh/shared-core-dto builds first and succeeds

@meanstacknh/shared-core-entity uses interfaces from @meanstacknh/shared-core-dto and fails to build because the project for @meanstacknh/shared-core-entity cannot find the @meanstacknh/shared-core-dto modules via import statements

No pathing variables within tsconfig files were touched anywhere... also .angular/cache was turned back on w/o effect

If a @meanstacknh/shared-core-entity utility lib is using @meanstacknh/shared-core-dto as an npm library they should be handled as such... yes?

which implies Nx should be installing the dependent library into the node_modules (or emulating it in its product logic) in order for cross dependent publishable utility libraries to build compile and succeed

QUESTION: Are cross dependent publishable utility libraries legal in Nx ? Should they be?

What did I do to get the build to succeed?

I added the @meanstacknh/shared-core-dto lib as a peer dependency to the package.json of @meanstacknh/shared-core-entity but this had no effect

{
    "name": "@meanstacknh/shared-core-entity",
    "version": "0.0.1",
    "type": "commonjs",
    "peerDependencies": {
        "@meanstacknh/shared-core-dto": "latest"
    },
    "dependencies": {
        "tslib": "^2.3.0"
    }
}

what really got this to work was I had to manually instrument my monorepo package.json with the following (workaround hack):

Package.json

    "@meanstacknh/shared-core-dto": "file:libs/shared/core/dto/src/lib",
    "@meanstacknh/shared-core-entity": "file:libs/shared/core/entity/src/lib"

Im unsure what Nx is doing internally but the crux of this issue is I cannot build cross dependent libraries without ensuring they are installed in node_modules

Nx is NOT transposing node_modules to reflect utility library compilations for dependent util library import statements to succeed their build

This ticket has been quiet but this one seems to be a fair legit bug which should be high priority as its blocking libraries from being built.

Hoping for some action and feedback (or inspired Nx workaround I am sure I have not gotten this the way Nx needs it to be)

The policy for publishable utility libraries is sketchy at this time.

Thanks for an awesome elite product and your soonest response.

nhhockeyplayer commented 2 years ago

the move schematic is also impacted...

nx g move --project admin-root-shell admin/root/feature-shell

have to manually update package.json with the workaround I performed for this ticket

nhhockeyplayer commented 2 years ago

another observation... tsconfig is not being updated properly after the MOVE schematic (awesome feature btw) is used ...specifically the paths manual intervention required at this time

nhhockeyplayer commented 2 years ago

yikes... well here is an interesting anomaly...

upon looking at my tsconfig.json to see if paths are changed...

paths are changed root name has a double @ sign. Thats why its unable to reference properly as mentioned above. I didn't add that... this is the Nx logic under the hood. Hopefully this will help finding more.

tsconfig.json

"@@meanstacknh/shared-feature-user": [
                "libs/shared/feature-user/src/index.ts"
            ],

ts files impacted too on imports with '@@'

nartc commented 2 years ago

Hi @nhhockeyplayer, thanks for reporting and sorry for taking a long time to get to this issue. Can you add nartc as a collaborator to your project? If not, can you provide a quick reproduce?

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.