Skip to content

Libraries can't import stuff from one another (official solution needed) #12453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chrillewoodz opened this issue Oct 3, 2018 · 4 comments
Closed

Comments

@chrillewoodz
Copy link

chrillewoodz commented Oct 3, 2018

Bug Report or Feature Request (mark with an x)

- [x] bug report/question
- [ ] feature request

Command (mark with an x)

- [ ] new
- [x] build
- [ ] serve
- [ ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc

Versions

Node: v8.12.0
npm: 6.4.1
ng:

@angular-devkit/architect 0.8.4
@angular-devkit/build-angular 0.8.4
@angular-devkit/build-ng-packagr 0.8.4
@angular-devkit/build-optimizer 0.8.4
@angular-devkit/build-webpack 0.8.4
@angular-devkit/core 0.8.4
@angular-devkit/schematics 0.8.4
@angular/cli 6.2.4
@ngtools/json-schema 1.1.0
@ngtools/webpack 6.2.4
@schematics/angular 0.8.4
@schematics/update 0.8.4
ng-packagr 4.2.0
rxjs 6.2.2
typescript 2.9.2
webpack 4.20.2

Repro steps

  1. ng new my-app
  2. ng generate library @internal/core
  3. ng generate library @internal/types
  4. Expose something from EACH library
  5. Import something from @internal/core into @internal/types and vice versa.
  6. ng build @internal/types or ng build @internal/core

The log given by the failure

BUILD ERROR
projects/internal/types/src/lib/interfaces/click-outside-instance.interface.ts(1,37): error TS2307: Cannot find module '@internal/core'.

Error: projects/internal/types/src/lib/interfaces/click-outside-instance.interface.ts(1,37): error TS2307: Cannot find module '@internal/core'.

    at Object.<anonymous> (/Users/cardhock/web-projects/@internal/packages/node_modules/ng-packagr/lib/ngc/compile-source-files.js:40:68)
    at Generator.next (<anonymous>)
    at /Users/cardhock/web-projects/@internal/packages/node_modules/ng-packagr/lib/ngc/compile-source-files.js:7:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/cardhock/web-projects/@internal/packages/node_modules/ng-packagr/lib/ngc/compile-source-files.js:3:12)
    at Object.compileSourceFiles (/Users/cardhock/web-projects/@internal/packages/node_modules/ng-packagr/lib/ngc/compile-source-files.js:17:12)
    at Object.<anonymous> (/Users/cardhock/web-projects/@internal/packages/node_modules/ng-packagr/lib/ng-v5/entry-point/ts/compile-ngc.transform.js:31:32)
    at Generator.next (<anonymous>)
    at /Users/cardhock/web-projects/@internal/packages/node_modules/ng-packagr/lib/ng-v5/entry-point/ts/compile-ngc.transform.js:7:71
    at new Promise (<anonymous>)

Desired functionality

It should automatically build both projects without manually having to add a bunch of hacks.

Mention any other details that might be useful

I've gone through every issue regarding this issue but nothing helps, I keep getting the same error.

#10444
#10615
#10780

Are some of the issues I've looked at.

They talk about using paths in compilerOptions in each lib's tsconfig.lib.json file, I've done this:

@internal/core:

  "compilerOptions": {
    "paths": {
      "@internal/types": [
        "dist/types", // mostly for the IDE
        "../../dist/types", // for the app
        "../../../dist/types" // for another lib
      ]
    }
  }

@internal/types:

  "compilerOptions": {
    "paths": {
      "@internal/core": [
        "dist/core", // mostly for the IDE
        "../../dist/core", // for the app
        "../../../dist/core" // for another lib
      ]
    }
  }

But when I build.. it's like I've done nothing at all.

I'm not sure if this happens mostly because none of the libraries have been built so when you're trying to import something it's not actually there, if so this sounds like a bug to me. If it finds a dependency that's not yet been built it should build it.

Can the official solution please be added somewhere on how you get scoped libraries to work with one another, preferably in the story? https://github.com/angular/angular-cli/wiki/stories-create-library

I'm completely stuck on this and I don't know what to do unless I get proper help here. Please help.

@alan-agius4
Copy link
Collaborator

alan-agius4 commented Oct 3, 2018

Import something from @internal/core into @internal/types and vice versa.

This will cause a circular dependency which will cause the error. As you will have something like:
@internal/core <> @internal/types. You need to have a one way dependency example @internal/core > @internal/types.

Apart from that, there shouldn't be the need to add any mappings in the tsconfig as this is done already automatically in the root tsconfig when creating a new library.

    "paths": {
      "@internal/core": [
        "dist/internal/core"
      ],
      "@internal/core/*": [
        "dist/internal/core/*"
      ],
      "@internal/types": [
        "dist/internal/types"
      ],
      "@internal/types/*": [
        "dist/internal/types/*"
      ]
    }

@ngbot ngbot bot added this to the needsTriage milestone Oct 3, 2018
@chrillewoodz
Copy link
Author

@alan-agius4 That would explain why core can depend on types but as soon as I make types depend on core too then core throws error for types (can't find it) when building types.

Is this really correct the desired behaviour? If types need a directive from core to create a correct interface, and core then uses this interface along with other interfaces in types, how do I achieve this? Libs depending on one another isn't something new, I've never had a scenario where this wasn't the case.

@alan-agius4
Copy link
Collaborator

At the moment circular dependencies are not supported. And is not something that we are planning to do anytime soon.

We encourage building application and libraries following the acyclic and not circular dependencies principe.

What I suggest is to re-visit your architecture and structure and plan your packages to avoid having circular dependencies as in a mono repo / workspace is not ideal as even just to build a dependency graph it will be kinda messy apart from that this would require to be done by a number of parties as at the moment some other tools don't support circular dependencies.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants