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

fix(caller): use calling file name for libraries calling Pino #1276

Merged
merged 4 commits into from
Dec 23, 2021

Conversation

FredericEspiau
Copy link
Contributor

@FredericEspiau FredericEspiau commented Dec 20, 2021

Excluding all the files in node_modules didn't work with nestjs-pino

I guess because there are too many files before reaching the user's files

This will return the file in second position in the stacktrace as get-caller-file did if we can't find a file outside node_modules

Stacktrace:

/Users/my-repo/node_modules/one-dependency/packages/file1.js
/Users/my-repo/node_modules/one-dependency/packages/file2.hs
/Users/my-repo/node_modules/pino-http/logger.js
/Users/my-repo/node_modules/pino-http/logger.js
/Users/my-repo/node_modules/nestjs-pino/LoggerModule.js
/Users/my-repo/node_modules/nestjs-pino/LoggerModule.js
/Users/my-repo/node_modules/@nestjs/core/middleware/middleware-module.js
/Users/my-repo/node_modules/@nestjs/core/middleware/middleware-module.js

Other example:

/Users/my-repo/node_modules/nestjs-pino/PinoLogger.js
/Users/my-repo/node_modules/@nestjs/core/injector/injector.js
/Users/my-repo/node_modules/@nestjs/core/injector/injector.js
/Users/my-repo/node_modules/@nestjs/core/injector/injector.js
/Users/my-repo/node_modules/@nestjs/core/injector/injector.js
/Users/my-repo/node_modules/@nestjs/core/injector/injector.js
/Users/my-repo/node_modules/@nestjs/core/injector/injector.js
/Users/my-repo/node_modules/@nestjs/core/injector/injector.js

I wasn't able to create a test for this case

This fixes #1277

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test?

@FredericEspiau
Copy link
Contributor Author

I will try, I just had an idea on how to do it

@mkaufmaner
Copy link

Oh this seems to be related; #1278

@FredericEspiau
Copy link
Contributor Author

FredericEspiau commented Dec 20, 2021

Yup its the same issue but I won't update my PR before tomorrow (2021-12-21T10:00:00+01:00)

lib/caller.js Outdated
@@ -21,4 +21,6 @@ module.exports = function getCaller () {
return file
}
}

return stack[2]?.getFileName()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use the ? as it's not in node v12.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, a comment explaining what this is would be very helpful.

@FredericEspiau FredericEspiau force-pushed the fix/caller-node_modules branch from 634bf4b to bf1bc72 Compare December 21, 2021 11:07
@FredericEspiau
Copy link
Contributor Author

done, this also handles #1278

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina
Copy link
Member

Thanks!

@mcollina
Copy link
Member

@ShogunPanda could you take a look?

Copy link
Contributor

@ShogunPanda ShogunPanda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single nit, but in general LGTM!

lib/caller.js Outdated
return typeof input === 'string' && fs.existsSync(input)
}

function isOutsideNodeModules (file) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little nit: I would join isFileName and isOutsideNodeModules as follow to avoid extra context switch:

function isOutsideNodeModules (file) {
  return typeof input === 'string' && fs.existsSync(input) && file.indexOf('node_modules') === -1
}}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@mcollina
Copy link
Member

CI is failing.

@FredericEspiau
Copy link
Contributor Author

FredericEspiau commented Dec 22, 2021

I can't reproduce the issue on my computer, I don't know what I can do to prevent the CI from failing 🤔

@ShogunPanda
Copy link
Contributor

@FredericEspiau I found the problem: you have several files (like fixtures/eval/node_modules/2-files.js) which are not included in the commit since node_modules is in .gitignore.

You have to explicitly add to the PR or, even better, provide them as an archive which you can unarchive during test time in a temporary folder.

@FredericEspiau
Copy link
Contributor Author

Excellent finding, indeed while doing my dev I kept on thinking that I should remove the this folder from the gitignore and then I forgot! Thanks I'll do it asap

@FredericEspiau
Copy link
Contributor Author

Awesome, the CI passed !

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug] 7.6.0 won't work with nestjs-pino
5 participants