Skip to content
This repository has been archived by the owner on Jun 27, 2018. It is now read-only.

toJavaScript() throws "Cannot read property 'text' of undefined" error on Windows #37

Closed
takenspc opened this issue Mar 24, 2016 · 5 comments

Comments

@takenspc
Copy link

toJavaScript() throws "Cannot read property 'text' of undefined" error on Windows.

The typescript-simple uses path.join to join paths while Typescript doesn't. This difference causes following error in toJavaScript() on Windows.

let outDir = 'outDir' in this.options ? this.options.outDir : '.';
let outputFileName: string;
if (this.options.jsx === ts.JsxEmit.Preserve) {
    outputFileName = path.join(outDir, fileName.replace(/\.tsx$/, '.jsx'));
} else {
    outputFileName = path.join(outDir, fileName.replace(/\.tsx?$/, '.js'));
}
let file = output.outputFiles.filter((file) => file.name === outputFileName)[0];
let text = file.text;

On Windows,

  • outputFileName is backslash separated path (foo\bar.ts)
  • file.name is slash separated path (foo/bar.ts).

Thus filter returns an empty array and file is undefined.
Then accessing file.text causes "Cannot read property 'text' of undefined" error.

Following workaround works for me though I'm not sure this workaround is appropriate.

outputFileName = ts.normalizeSlashes(outputFileName); // ADDED
let file = output.outputFiles.filter((file) => file.name === outputFileName)[0];

ts.normalizeSlahes replaces \ with / (https://github.com/Microsoft/TypeScript/blob/master/src/compiler/core.ts#L586).

@takenspc
Copy link
Author

Changed issue title to make it clearer.

@takenspc takenspc changed the title Add support Windows toJavaScript() throws "Cannot read property 'text' of undefined" error on Windows Mar 24, 2016
@takenspc takenspc changed the title toJavaScript() throws "Cannot read property 'text' of undefined" error on Windows toJavaScript() throws "Cannot read property 'text' of undefined" error on Windows Mar 24, 2016
@teppeis teppeis mentioned this issue Mar 29, 2016
@teppeis
Copy link
Owner

teppeis commented Mar 29, 2016

Thank you for reporting!
Fixed in v5.0.1.

@HerringtonDarkholme
Copy link

I still found this error in v5.0.2, in espower-typescript. But it is caused by another reason.

When a file is imported by an entry file, its full path is used in outputFiles.name, while outputFileName is its relative path.

@teppeis
Copy link
Owner

teppeis commented Apr 19, 2016

@HerringtonDarkholme please give me PoC source code to reproduce or PR to resolve.

@alejo90
Copy link

alejo90 commented Apr 21, 2016

I found this issue too in v5.0.2. My case is really weird because I have two different projects that are configured exactly the same but the error only appears on one of them. I'll try to debug it.

In my case, I have a folder called test and a file called test.ts under test. My tsconfig.json looks like this:

{
    "compilerOptions": {
        "module": "commonjs",
        "inlineSourceMap": true,
        "declaration": true,
        "removeComments": false,
        "target": "es5",
        "moduleResolution": "node",
        "noImplicitReturns": true,
        "outDir": "lib"
    },
    "files": [
        "typings/main.d.ts",
        "src/main.ts"
    ]
}

The problem I found is that the output of service.getEmitOutput is:

{
  outputFiles: [{
   name: 'lib/test.js',
   ...
  }, {
   name: 'lib/test.d.ts',
   ...
  }]
}

And the value of the variable fileName is test/test.js and since I have no rootDir in my configuration, the outputFileName variable ends up being lib/test/test.js, which is not contained in outputFiles. As I see it, it seems like the typescript language service is compiling with an assumed rootDir test. I hope this information helps.

I created a minimum project that reproduces the issue in Windows and OSX: alejo90/typescript-simple-bug.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants