Repository: TypeScript
Issue: Automatic multi-file namespace references in AMD/System modules are buggy in complicated situations
Labels: Needs Investigation

**TypeScript Version:**  3.8.3

<!-- Search terms you tried before logging this (so others can find this issue more easily) -->
**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&mdash;just like the TypeScript compiler itself&mdash;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: <br><br>

![](https://user-images.githubusercontent.com/2157285/80984509-33216100-8e43-11ea-99e5-a0f943877d5b.png)

**Code**

- Buggy version: http://codes.kary.us/playgrounds/typescript-amd-automatic-reference-bug/tree/buggy-version
- Working version: http://codes.kary.us/playgrounds/typescript-amd-automatic-reference-bug/tree/fixed-version

**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:

![](https://user-images.githubusercontent.com/2157285/80987475-5817d300-8e47-11ea-95fd-f4c331bc8ae6.png)

**Playground Link:** <!-- A link to a TypeScript Playground "Share" link which demonstrates this behavior -->

**Related Issues:** <!-- Did you find other bugs that looked similar? -->