Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__importStar can result in undefined imports when importing cyclical dependencies #114

Open
VeriClock-Bryce opened this issue May 15, 2020 · 0 comments

Comments

@VeriClock-Bryce
Copy link

I've run across what appears to be a bug with __importStar when cyclical dependencies are involved.

I've replicated the issue this sandbox:
https://codesandbox.io/s/eloquent-khayyam-nn14o?file=/src/tsFile.ts

Which contains 3 key files:

index.js

const jsFile = require("./jsFile");
jsFile.doMainThingInJsFile();
console.log("All is good!");

tsFile.ts

//this fails
import * as jsFile from "./jsFile";

//this works
// import jsFile = require("./jsFile");

export function doSomethingInTsFile() {
  console.log("tsFile.doSomethingInTsFile()");
  jsFile.doSomethingInJsFile2();
}

jsFile.js

const tsFile = require("./tsFile");

function doMainThingInJsFile() {
  console.log("jsFile: in doMainThingInJsFile()");
  tsFile.doSomethingInTsFile();
}
exports.doMainThingInJsFile = doMainThingInJsFile;
exports.doSomethingInJsFile2 = function() {
  console.log("jsFile.doSomethingInJsFile2()");
};

tsFile.ts and jsFile.js are cyclical dependencies.

When the 'fail' version of the code runs, doSomethingInTsFile() inside of tsFile.ts is undefined.

Looking at the source code for __importStar it becomes clear what is happening: the reference returned from require(...) is replaced with a new container object. It is unclear to me why this occurs and instead the original isn't used, but I suspect its related to emitting code that will ultimately work with other code that goes looking for a 'default' export in that output.

I would chalk this up to me misusing import * or other settings being incorrect, but if the cyclical dependency is removed, then the module imports just fine with an import *. It's this differing behaviour that leads me to think there is a bug here. We encountered this in our project when some code that rarely ran failed at runtime because a cyclical dependency had been added elsewhere that ultimately caused the member functions of the imported library (old commonjs code) to be undefined.

@VeriClock-Bryce VeriClock-Bryce changed the title __importStar can result in undefined imports with cyclical dependencies __importStar can result in undefined imports when importing cyclical dependencies May 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant