microsoft / microsoft/TypeScript

Automatic multi-file namespace references in AMD/System modules are buggy in complicated situations

Open
#38,325 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.8.3

Search Terms: AMD, System, Reference, Circular reference, Automatic Reference, Auto Reference, Multi-file Namespace, Splitting Across Files

Problem

While most people use TypeScript with a commonjs way of modeling. There are a fraction of this community that uses the AMD / System setup—just like the TypeScript compiler itself—and a common practice is to have outFile enabled. In this situation the compiler automatically detects which file is the dependency of what other file and arranges the namespaces in such way that it can be put into a single file.

This however is not when it comes to rather complicated structures in which one has namespaces separated in multiple files and have circular referencing in them. For that TypeScript does have these /// <reference path="..." /> comments that act like the require / import features.

Using the reference comments always solves any kind of dependency resolution. But the problem is while the compiler halts on using an identifier without importing on other modules (import / require), there is no compiler error or even warning on not using the reference comments.

I do not know what is the bug in here:

  • Is it that the compiler gives no error on absence of the reference comment?
  • Or is the file dependency graph resolver that is buggy?

I have demonstrated a situation in which a simple project with a complicated enough graph of dependencies show how the problem can be replicated. This is the graph describing the structure of this project and a git repository containing both a working version and a buggy version is provided respectively:

Code

Expected behavior:

  • I think either the compiler should have the right dependency resolver and insert reference tags for all the calls to other files in each file.
  • Or the reference comments should be forced on all files and the compiler gives errors that one identifier cannot be found because it is not resolved with the reference comment.

Actual behavior:

If you use the reference comments for all the imported identifiers in every file the code will run successfully but if you remove them it will compile but the code will not work because it has type checking problems on undefined modules. This screenshot shows the working and buggy versions for good:

Playground Link:

Related Issues:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the buggy-version and fixed-version repositories linked in the issue, using the AMD/System configuration with outFile and the multi-file circular namespace references. Trace the compiler's dependency resolution and reference-comment handling, then verify that the chosen behavior compiles and runs correctly for the demonstrated project.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.