stefanpenner / get-caller-file

ISC License
37 stars 11 forks source link

Wrong file returned - ATENTION #13

Open Deathrage opened 5 years ago

Deathrage commented 5 years ago

`// root/index.js module.exports = function() { return getCallerFile(); }

// root/dir/app.js const func = require('../index.js'); func(); // returns root/index.js`

Firstly encourntered this when using TS-NODE. It occurs in node too. Caller file is app.js yet definition file index.js is returned.

stefanpenner commented 5 years ago

@Deathrage interesting, that seem strange as getCallerFiles tests do test these common scenarios, and they seem to work fine.

Could you provide a reproduction, I can gladly debug it then.

Deathrage commented 5 years ago

It seems like Typescript related issue. I am testing with typescript default settings through TSNODE.

Typescript:

// ts/folder/index.ts

import testFunc from '../func';
testFunc().then(console.log);

// ts/func.ts
const getCallerFile = require('get-caller-file');

export default async function () {
    let caller = getCallerFile();
    return caller;
}

This logs /func.ts. (incorrect)

Javascript (not result of TSC, I wrote vanila counterpart as test):

// vanila/folder/index.js
var func = require('../func.js');

func ().then(console.log);

// vanila/func.js
const getCallerFile = require('get-caller-file');

module.exports = async function () {
    let caller = getCallerFile();
    return caller;
}

This logs /index.js. (correct)

Firstly I suspected it might be related to TS-NODE as I use it during development but after transpiling to JS and executing JS directly the issue persists. TSC does not do any kind of bundling, it just spawns transpiled file next to ts file. It might be realted to the way how TSC handles transpilation of ES6 to CommonJS. Code written in vanila JS works well.

Version of TSC: 3.3.3 Version of Node: 10.9.0

Deathrage commented 5 years ago

@stefanpenner