question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow circular references of project references

See original GitHub issue

Search Terms

circular reference project references graph

Suggestion

Currently, project reference graphs are required to be acyclical because

  • Project references imply a .d.ts file is loaded, and .d.ts files can’t exist before the build occurs
  • Only acyclic graphs can be topologically sorted

Both of these problems are solvable without too much work.

With the work done in #32028, we can effectively toggle the redirecting behavior, fixing the first issue. This would be done during the initial build phase only when an upstream project dependency isn’t built yet.

The other problem is that project build order might not be predictable if we arbitrarily pick some starting point in the unsortable graph. This is fixable if we force solution authors to indicate which edges in the graph should be treated as ignored for the purposes of the topological sort:

    "references": [
        { "path": "../a" },
        { "path": "../b", "circular": true },
                          ^^^^^^^^^^^^^^^^
        { "path": "../c" }
    ],

The benefits of this are:

  • In case of suboptimal .d.ts generation or memory pressure, developers can control where the “weak” link occurs in the build process
  • The build ordering is fully deterministic regardless of starting point
  • Graphs can’t become “accidentally” circular - this is a clear opt-in

Use Cases

npm and other package managers do allow circularities in dependency graphs, so this is apparently a thing. We’ve also gotten feedback from some industry partners that they want to move to project references, but their dependency graph is circular in a way that would require a lot of work to “fix”.

Examples

Given the graph:

A -> B -> C -(circular)-> A

The build order is deterministically C, B, A. During C’s compilation, source file redirects from C to A are not active.

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:50
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
dqsullycommented, Feb 4, 2020

@AlexLeung I’m not sure if this helps, but my team has been using pnpm workspaces to have our monorepo projects symlinked as node_module dependencies. I don’t know if this keeps any of the benefits of TypeScript project references, but it allows us to keep our code separated into distinct modules. The TypeScript compiler works great with this project structure, and the TypeScript Language Server for VSCode works too, although it can be a little slow at times in our project. I’m not sure if it doesn’t understand our symlinked structure and sometimes loads the same file multiple times, or if our project is just huge and abusive of TypeScript’s powers… Overall it works great for us though, and we’ve got 30+ modules

0reactions
JanKaczmarkiewiczcommented, Jan 9, 2022

I am trying to adopt strict mode in one package in monorepo. Packages have multiple circular dependencies, so right now project reference is not an option. It would be helpful to have the possibility to gradually migrate some of the packages to eg. strict mode without moving a lot of code between packages. I think many monorepo based projects can have similar problems and benefit from this proposal.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cyclic reference of projects not allowed - Microsoft Learn
Circular references to projects aren't allowed. For example, if MyProj references YourProj , then YourProj (or a project that references ...
Read more >
.net - How to deal with circular references? - Stack Overflow
Basically you use #if logic around the code that doesn't compile unless the reference exists, and you use conditional compilation in the project...
Read more >
Two Approaches to Addressing Circular References in SSDT
Strictly, SSDT will not allow you to add a circular reference, but there are two general workarounds to setting up such projects in...
Read more >
Circular reference in Excel: how to find, enable, use, or remove
Go to the Formulas tab, click the arrow next to Error Checking, and point to Circular References The last entered circular reference is ......
Read more >
Circular References in Project Planner - Indzara Support
This article discusses reasons for the 'Circular Reference' error that you may come across in the Project Planner (Advanced) Excel Template, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found