Skip to content
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

Incorrect order with project references, extends tsconfig.json and yarn workspaces #29643

Closed
sgronblo opened this issue Jan 30, 2019 · 12 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@sgronblo
Copy link

TypeScript Version: 3.4.0-dev.20190129

Search Terms:

project references order

Code

I made a small PoC here: https://github.com/sgronblo/tsc-build-order-failure

Run yarn install and then yarn build to see the build failure.

Expected behavior:

tsc --build figures out that the client project depends on the core project and builds core before client/src. Even though the project "references" section of the top level "meta" tsconfig.json file happens to list client/src before core. There should be a dependency relationship between client and core because client/src/tsconfig.json extends ../tsconfig.json which defines the reference.

Actual behavior:

tsc just tries to build the projects in the order they are listed in the top level "references" section. When it tries to build client, core has not yet been built, which results in a build error.

@sheetalkamat
Copy link
Member

sheetalkamat commented Jan 30, 2019

I think this is working as intended. The references section is suppose to be used to get the dependency list and determine the order of the build.
cc: @RyanCavanaugh

@sheetalkamat sheetalkamat added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jan 30, 2019
@MLoughry
Copy link

Does Typescript not do a topological sorting of the project references?

@sheetalkamat
Copy link
Member

Its kind of depth first So even though the out config references Project1 and Project2 in that order if Project1 references Project2 then Project2 comes before Project1.

@sgronblo
Copy link
Author

I'm a bit confused now. You say the references section should "determine the order of the build". My understanding of project references is based on this document: https://www.typescriptlang.org/docs/handbook/project-references.html which states that the --build flag will "Build out-of-date projects in the correct order".

Also you said:

"So even though the out config references Project1 and Project2 in that order if Project1 references Project2 then Project2 comes before Project1."

Which to me sounds like you are agreeing with my assessment that TS should be able to figure out the order automatically.

The Project References page also contains this:

Another good practice is to have a “solution” tsconfig.json file that simply has references to all of your leaf-node projects. This presents a simple entry point; e.g. in the TypeScript repo we simply run tsc -b src to build all endpoints because we list all the subprojects in src/tsconfig.json

I interpreted this as ts being able to figure out the order of projects thanks to their project reference links? Is this a misunderstanding?

@yortus
Copy link
Contributor

yortus commented Jan 31, 2019

@sgronblo to have tsc do a correct topological sort, you need to also maintain a "references" list in each project's tsconfig.json, so each project lists the projects it directly depends on. This is regardless of whether or not you use a top-level "solution" tsconfig.

tsc computes the overall dependency graph by combining all these "references" lists, much the same way as yarn or npm use "dependencies" in each package.json to build an overall dependency graph.

It has been pointed out that this is a duplication of effort and tsc could just use "dependencies" in package.json like eg lerna does. See #25376. Please consider leaving your feedback there, as it would be great to see this addressed.

@sgronblo
Copy link
Author

@yortus Yes, and that is exactly what my sample project is trying to demonstrate.

There is a top level "solution" tsconfig.json for convenience, but if you look at the tsconfig.json of the "client" project you would see that it references the "core" project but still tsc ignores that relationship for this specific case.

@yortus
Copy link
Contributor

yortus commented Jan 31, 2019

@sgronblo yes I see now. I suspect the problem is with /client/src/tsconfig.json extending /client/tsconfig.json - either the references are not inherited, or the relative paths become invalid. You could try listing "references" directly in /client/src/tsconfig.json to see if this is the issue.

@sgronblo
Copy link
Author

@yortus Yes. I guess I should have made it clearer that defining the references directly in /client/src/tsconfig.json seems to solve the problem. At least I think it did, when trying the other day.

@yortus
Copy link
Contributor

yortus commented Jan 31, 2019

@sgronblo yes it's in your OP, sorry I skimmed it too fast.

I took a look at the tsc source, in particular the parseConfig function. It seems that when a tsconfig file extends another tsconfig file, the "references" list is not copied across.

Not sure it this is by design, or has just been overlooked, since the "references" top-level tsconfig key was added after "extends" was implemented. It might be an oversight, since "references" is also not mentioned at all in the documentation for tsconfig extends.

If this is working as intended, I think the documentation should clearly state that references are not inherited by "extends".

@sgronblo
Copy link
Author

@yortus Thanks for checking. I think probably some more detailed confirmation by someone on the TS team is required to tell whether this is actually "working as intended".

@sheetalkamat
Copy link
Member

#27098 tracks the inheritance.

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants