Skip to content

Conversation

@benallfree
Copy link

@benallfree benallfree commented Feb 17, 2025

This PR mimics NODE_DEBUG=module by outputting module resolution debugging information.

Example:

NODE_DEBUG=module air

Outputs...

resolve buffer (native)
resolve util (native)
resolve console (native)
resolve process (native)
resolve /my_project/node_modules/pocketpages/dist/node_modules/qs-lite (not found)
resolve /my_project/node_modules/pocketpages/dist/node_modules/qs-lite.js (not found)
resolve /my_project/node_modules/pocketpages/dist/node_modules/qs-lite.json (not found)
resolve /my_project/node_modules/pocketpages/dist/node_modules/qs-lite/index.js (not found)
resolve /my_project/node_modules/pocketpages/dist/node_modules/qs-lite/index.json (not found)
resolve /my_project/node_modules/pocketpages/node_modules/qs-lite (not found)
resolve /my_project/node_modules/pocketpages/node_modules/qs-lite.js (not found)
resolve /my_project/node_modules/pocketpages/node_modules/qs-lite.json (not found)
resolve /my_project/node_modules/pocketpages/node_modules/qs-lite/index.js (not found)
resolve /my_project/node_modules/pocketpages/node_modules/qs-lite/index.json (not found)
resolve /my_project/node_modules/qs-lite (not found)
resolve /my_project/node_modules/qs-lite.js (not found)
resolve /my_project/node_modules/qs-lite.json (not found)
resolve /my_project/node_modules/qs-lite/qs-lite.js (ok)
resolve /my_project/node_modules/pocketpages (not found)
resolve /my_project/node_modules/pocketpages.js (not found)
resolve /my_project/node_modules/pocketpages.json (not found)
resolve /my_project/node_modules/pocketpages/index.js (not found)
resolve /my_project/node_modules/pocketpages/index.json (not found)
resolve pocketpages (fatal)
Failed to execute pocketpages.pb.js:
 - GoError: Invalid module at github.com/dop251/goja_nodejs/require.(*RequireModule).require-fm (native)

@benallfree
Copy link
Author

Tagging @ganigeorgiev for input and discussion if needed.

@ganigeorgiev
Copy link
Contributor

ganigeorgiev commented Feb 17, 2025

@benallfree Please stop spamming me! I have no context about why the suggested change is even needed and I'm not the maintainer of the goja_nodejs repo. It is up to the author (dop251) to decide whether the PR is welcomed in the repo or not.

p := path.Join(start, modpath)
if isFileOrDirectoryPath(origPath) {
if module = r.modules[p]; module != nil {
moduleDebug(fmt.Sprintf("resolve %s (cached)", p))
Copy link
Owner

Choose a reason for hiding this comment

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

It's not the most efficient way of doing it. The fmt.Sprintf() should not be called if debug is disabled.

}

func moduleDebug(msg string) {
if os.Getenv("NODE_DEBUG") == "module" {
Copy link
Owner

Choose a reason for hiding this comment

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

os.Getenv will do a syscall every time. Again, not the most efficient way. It should be cached once and then re-used. I think node works this way.

Copy link
Owner

Choose a reason for hiding this comment

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

Also, NODE_DEBUG may contain comma-separated values.

@benallfree
Copy link
Author

I've added support for comma-separated module names and made the optimization improvements you suggested. Let me know what you think :)

@benallfree
Copy link
Author

@dop251 I realized a while ago that my method of finding the module name from the module path is probably not accurate because a user could do something like require("package/dist/index.js"). If you have ideas on how to find the original module name requested by the user so we can filter properly, please share thoughts.

stampy88 and others added 11 commits May 2, 2025 19:36
* Added variable length `Buffer.read*` methods
- added `readIntBE`, `readIntLE`, `readUIntBE`, and `readUIntLE`.
- also refactored out some common methods to create errors

* added errors.NewRangeError. Also fixed mispelled `ErrCodedOutOfRange` -> `ErrCodeOutOfRange` (extra `d`)

* added the `readUint*` (small `i`) aliases

---------

Co-authored-by: dave sinclair <david_sinclair@cable.comcast.com>
* Added write method
- added the `write` method and fleshed out the `toString` method to match the behavior with Node's `Buffer` class. The test cases are pretty exhaustive to ensure all the edge cases, and Node specific behavior is covered.

* Use getRequiredStringArgument
* Added writeBig* methods
- added `writeBigInt64BE`, `writeBigInt64LE`, `writeBigUInt64BE`, and `writeBigUInt64LE` along with their aliases

* Simplified tests
- added the `assertions.js` file for the buffer tests
- refactored the `assert.throws*` method to reduce the duplicate code
…link resolution. Most path operations are now done in the OS semantics (i.e. using filepath/path). (dop251#101)
- added the rest of the `Buffer.write*` methods. Also updated the `assert._isSameValue` to handle number specifically to handle floating point differences that are within a precision.
@benallfree
Copy link
Author

@dop251 Just sync'd with master. How do you feel about this PR? It has been very useful in local testing for debugging module resolution.

@dop251
Copy link
Owner

dop251 commented May 3, 2025

I'm not sure this is implemented in line with how it's working in node. The syntax is defined as NODE_DEBUG=module[,…] here, and the value is a ','-separated list of core modules that should print debug information. It's not debugMode, params, and parans don't seem to have anything to do with the path of a JS module...

@benallfree
Copy link
Author

As I understand it, the presence of module should govern whether we display anything at all. module,a,b,c should only display debug for modules a, b, and c.

Do you agree?

@dop251
Copy link
Owner

dop251 commented May 5, 2025

I think it's just "a,b,c,..." and "a" in this case is "module". So the "module" in the documentation is not a literal, it's a name of a module, which in our case happens to be "module".

@benallfree
Copy link
Author

🤣 my bad I'll fix. I always thought module was a special value to specifically enable module debugging.

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

Successfully merging this pull request may close these issues.

4 participants