This project is read-only.

Environment.readFile() -> TypeError: Environment is null

Topics: Language Specification
Nov 6, 2013 at 4:24 PM

by executing the following TypeScript-Code, I get the error "TypeError: Environment is null" in FireFox and an equivalent error in IE. The code compiles in VS2012 without errors.

///<reference path='....\TypeScriptSourceCode\src\Services\es5compat.ts' />
///<reference path='....\TypeScriptSourceCode\src\compiler\syntax\references.ts' />
///<reference path='....\TypeScriptSourceCode\src\compiler\syntax\emitter.ts' />
///<reference path='....\TypeScriptSourceCode\src\compiler\syntax\prettyPrinter.ts' />
///<reference path='....\TypeScriptSourceCode\src\compiler\core\environment.ts' />
///<reference path='....\TypeScriptSourceCode\src\harness\diff.ts' />
///<reference path='....\TypeScriptSourceCode\src\compiler\syntaxTreeToAstVisitor.ts' />
    var fileName = "./sut.ts";
    var contents = Environment.readFile(fileName, /*codepage*/ null).contents;
    var text = TypeScript.TextFactory.createText(contents);
    var tree = TypeScript.Parser.parse(fileName, text, TypeScript.isDTSFile(fileName), new TypeScript.ParseOptions(TypeScript.LanguageVersion.EcmaScript5, true, false));
    var ast = TypeScript.SyntaxTreeToAstVisitor.visit(tree, fileName, new TypeScript.CompilationSettings(), /*incrementalAST:*/ true);
I copied (almost) all of the references and the code from the tests in the TypeScript source code (<TypeScriptSourceCode>\tests\Fidelity\program.ts). My HTML-Website looks like this:
<!DOCTYPE html>
<head><title>TypeScript AST Accessor Test</title></head>
  <script src="../../TypeScriptSourceCode/src/Services/es5compat.js"></script>
  <script src="../../TypeScriptSourceCode/src\compiler/syntax/references.js"></script>
  <script src="../../TypeScriptSourceCode/src/compiler/syntax/emitter.js"></script>
  <script src="../../TypeScriptSourceCode/src/compiler/syntax/prettyPrinter.js"></script>
  <script src="../../TypeScriptSourceCode/src/compiler/core/environment.js"></script>
  <script src="../../TypeScriptSourceCode/src/harness/diff.js"></script>
  <script src="../../TypeScriptSourceCode/src/compiler/syntaxTreeToAstVisitor.js"></script>
 <script src="ASTAccessorTests.js"></script>

It would be great, if anyone could help me with that problem!
Nov 9, 2013 at 7:01 AM
Already sometime ago I looked into this. But what I remember is that the Environment abstracts the underlying engine that is running the JavaScript (nodejs or MS script host) If it doesn't recognise the engine, it will be initialised to null.

In your case, I don't think right now the engine you are using (JavaScript on Firefox) is supported by the Language Services. So to get it working, you will have to implement the Environment API using the something like the W3C File API.
Nov 13, 2013 at 3:10 PM
Thank you for your reply. I don't think that this would solve my problem, because the same problem occurs with Internet Explorer and on different machines with different operating systems, too. I think that my problem resides on a higher level, because every functionality of the TypeScript-Framework I use, is not working. There's always the error "xyz is not defined".

The weird thing is, that if I copy the code of the TypeScript-Framework into my module, everything works fine. But if I reference the code of the TypeScript-Framework directly from the TypeScript source code (in the regarding .ts-file), the code breaks with an error. Example:
///<reference path='....\TypeScriptSourceCode\src\compiler\core\arrayUtilities.ts' />
module ASTAccessorTests {
//code copied from arrayUtilities.ts
export class ArrayUtilities {
public static isArray(value: any): boolean {
    return Object.prototype.toString.apply(value, []) === '[object Array]';
alert(ArrayUtilities.isArray("bla").toString()); //works
alert(TypeScript.ArrayUtilities.isArray("blubb").toString()); //fails

The same error occurs for example with
///<reference path='....\TypeScriptSourceCode\src\compiler\core\environment.ts' />
var fileName = "./sut.ts";
var contents = Environment.readFile(fileName, /codepage/ null).contents;

///<reference path='....\TypeScriptSourceCode\src\harness\harness.ts' />
var typescriptLS = new Harness.TypeScriptLS();