Self-referencing module declaration causes RangeError: Maximum call stack size exceeded


Given these files:

moduleDef.d.ts :
declare module "moduleC" {
    import self = require("moduleC");
    export = self;
moduleB.ts :
class ClassB{}
export = ClassB;
moduleA.ts :
/// <reference path="moduleDef.d.ts"/>
import moduleC = require("moduleC");
import ClassB = require("moduleB");    // import order matters
export var b:ClassB;
when compiling moduleA.ts

tsc (0.9.7) gives:
(function (exports, require, module, __filename, __dirname) { /*! ************
RangeError: Maximum call stack size exceeded

file attachments

Closed Apr 1, 2014 at 12:34 AM by RyanCavanaugh
The fix should be visible in the upcoming release. Thanks!


danquirk wrote Mar 19, 2014 at 9:57 PM

Marked as active, we don't want to be crashing like this. That said, it's not clear to me what you're actually trying to do. How can a module require itself as your moduleC is doing? It's not too surprising that this doesn't work, we should just be cutting off the infinite recursion and giving a proper error before overflowing.

Benjamin_CAS wrote Mar 21, 2014 at 9:44 AM

Hi danquirk, indeed, this scenario has no real-world usage (as far as I can imagine) but I came across it by mistake. It took me a while to figure out what was wrong with my code base, but at last I could track the issue down to this constellation.
The interesting thing is, that the queer declaration in file moduleDef.d.ts itself compiles just fine. Please let me know if you can reproduce this crash with just one file.

Background: First, relying on WebStorm's behavior, I thought that require("moduleC") would point to a module (file) with that name at the side of the declaration file. Now I've read the specification and it is pretty clear about the external module resolution in "11.2.1 External Module Names".

Flion wrote Nov 1, 2014 at 12:07 PM

just wanted to add that this is still a problem.
I just started converting a 100+ file large project from internal modules to external modules.
I accidentally had one module refer to itself at some point, and from then on I got the same compiler error as above. Took quite a bit of searching to find out where the self referencing module was.
It would be really helpfull if the compiler would give a warning or error when a module references to itself.

Benjamin_CAS wrote Nov 4, 2014 at 7:42 AM

Hi Flion, can you provide code / a set of files to reproduce your issue? Then I will re-open the issue. Please note that the dev moved to github. maybe you want to refer to https://github.com/Microsoft/TypeScript/issues/806