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

Added the requireStack to loader.js #45734

Closed
wants to merge 0 commits into from
Closed

Conversation

mertcanaltin
Copy link
Member

// TODO(BridgeAR): Add the requireStack as well.

I also added the require stack

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/modules

@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Dec 4, 2022
@@ -413,7 +413,7 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
err.code = 'MODULE_NOT_FOUND';
err.path = path.resolve(requestPath, 'package.json');
err.requestPath = originalPath;
// TODO(BridgeAR): Add the requireStack as well.
err.requireStack = new Error().stack;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think @BridgeAR's comment was referring to the requireStack that's generated in

const requireStack = [];
for (let cursor = parent;
cursor;
cursor = moduleParentCache.get(cursor)) {
ArrayPrototypePush(requireStack, cursor.filename || cursor.id);
}
. It's different from what new Error().stack produces.

Copy link
Member

@JakobJingleheimer JakobJingleheimer left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution!

I'm not super familiar with the CJS space of modules (so not officially 👍), but the implementation LGTM and there was an existing code comment suggesting it is needed, so that seems fine.

@GeoffreyBooth
Copy link
Member

Do we add arbitrary properties to Error anywhere else? @aduh95 ?

@aduh95
Copy link
Contributor

aduh95 commented Jan 2, 2023

Do we add arbitrary properties to Error anywhere else? @aduh95 ?

Yes, e.g. every error from a Node.js API has a code property.

Copy link
Contributor

@aduh95 aduh95 left a comment

Choose a reason for hiding this comment

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

We'd want to add tests before landing this. Also the same TODO is present twice in the file, IMO it would make sense to address them both at once unless one is more complicated than the other.

Copy link
Contributor

@aduh95 aduh95 left a comment

Choose a reason for hiding this comment

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

What is the use case that's would be fixed by it? I think we should start with writing some tests for the feature so we set the expectations for what we want the feature to look like, and then proceed with the implementation. Adding an empty array to an error object doesn't make much sense to me if I can't see in what context it can be used, having tests for that would help greatly.

@RaisinTen
Copy link
Contributor

The use case for this property has been discussed in #25690. As suggested in #45734 (comment), this array should be populated with paths of non-internal modules that are present in the stack trace.

// This function takes the error object and a function as arguments.
// The function is used as a marker for the point in the stack trace where the capture should begin.
// In this case, we are using the anonymous function that follows as the marker.
Error.captureStackTrace(err, () => {});
Copy link
Member

Choose a reason for hiding this comment

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

This function is a hook for users to register their own stack trace customizations. I’m not sure we should be using it internally.

@@ -0,0 +1,56 @@
import { tryPackage } from '../../lib/internal/modules/cjs/loader'
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be tested using the public APIs alone, so I would suggest you to revert the tryPackage exporting change from lib/internal/modules/cjs/loader.js and instead create a temporary file structure of this sort dynamically while running the test:

 tree -a
.
├── index.js
└── node_modules
    ├── a
       ├── a.js
       └── package.json
    └── b
        └── package.json

3 directories, 4 files
 cat index.js
require('a');
 cat node_modules/a/a.js
require('b');
 cat node_modules/a/package.json
{
  "main": "a.js"
}
 cat node_modules/b/package.json
{
  "main": "b.js"
}

and assert on the requireStack property of the error thrown by the index.js file.

Also, note that this is a CJS file, so the way you used import here won't work.

@@ -0,0 +1,56 @@
import { tryPackage } from '../../lib/internal/modules/cjs/loader'
Copy link
Member

Choose a reason for hiding this comment

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

test-loader.js is a much too generic name. test-require-package-errors.js perhaps?

Or even better, find where we currently test the errors potentially thrown by require and add your new cases there.

const assert = require('assert');

describe('tryPackage', () => {
it('should return the file path when the package is found and main is specified', () => {
Copy link
Member

Choose a reason for hiding this comment

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

Surely there’s an existing test that require of a package works?

// The function is called for each stack frame in the stack trace, and it should return a string representation of the stack frame.
// In the following example, we use the `util.inspect()` function to get a string representation of each stack frame.
// We then check if the file name of the stack frame is a core module or an internal Node.js module by checking if it starts with `'internal/'` or `'module'`.
// If it is not a core module or an internal Node.js module, we add the file name to the `requireStack` array:
Copy link
Member

Choose a reason for hiding this comment

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

Nit: line lengths here should be <= 100 chars.

@mertcanaltin
Copy link
Member Author

uh 🆘

@JakobJingleheimer
Copy link
Member

I can't re-open it 🤨

@MoLow
Copy link
Member

MoLow commented Feb 9, 2023

I guess it cannot be reopened since it has no commits

@mertcanaltin
Copy link
Member Author

I'm so sorry I accidentally blew my code here :(
i created a pr : #46586

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants