Loopback-next: Circular dependencies created by the CLI are creating invalid code (too many imports)

Created on 26 Nov 2020  路  2Comments  路  Source: strongloop/loopback-next

Steps to reproduce

Start from a fresh Todo tutorial app.
Let's say you want to have subtasks in big tasks, and want to create child-parent Todos.
Use the cli to create the relation between Todo and Todo. At least belongsTo and hasMany will create the error, but others certainly will as well.

Current Behavior

Code generated cannot start, with these messages:

src/models/todo.model.ts(6,9): error TS2395: Individual declarations in merged declaration 'Todo' must be all exported or all local.
src/models/todo.model.ts(6,9): error TS2440: Import declaration conflicts with local declaration of 'Todo'.
src/models/todo.model.ts(13,14): error TS2395: Individual declarations in merged declaration 'Todo' must be all exported or all local.
src/repositories/todo.repository.ts(9,9): error TS2395: Individual declarations in merged declaration 'TodoRepository' must be all exported or all local.
src/repositories/todo.repository.ts(9,9): error TS2440: Import declaration conflicts with local declaration of 'TodoRepository'.
src/repositories/todo.repository.ts(11,14): error TS2395: Individual declarations in merged declaration 'TodoRepository' must be all exported or all local.
src/controllers/todo-todo.controller.ts(19,3): error TS2300: Duplicate identifier 'Todo'.
src/controllers/todo-todo.controller.ts(20,3): error TS2300: Duplicate identifier 'Todo'.

Expected Behavior

To work, without these messages.

Link to reproduction sandbox

Additional information

These errors happen because the CLI blindly appends the imports to each files. The Todo model declaration file tries to import its own definition, same thing happens with the repository. The controller that handles the relation imports both related models, but since they are the same, it tries to import it twice!

Related Issues

_See Reporting Issues for more tips on writing good issues_

CLI bug

All 2 comments

@Salketer, would this docs https://loopback.io/doc/en/lb4/BelongsTo-relation.html#handling-recursive-relations help?

cc @agnes512

Thank you @dhmlau this may help some people facing the issue.

There is indeed a little subtlety that is pointed out in the doc you are showing, yes. But the CLI should not create bad code, that is counter productive...

PS: Sorry for late reply, the loopback.io wasn't working when you replied...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joeytwiddle picture joeytwiddle  路  3Comments

rexliu0715 picture rexliu0715  路  3Comments

aceraizel picture aceraizel  路  3Comments

zero-bugs picture zero-bugs  路  3Comments

shahulhameedp picture shahulhameedp  路  3Comments