-
Notifications
You must be signed in to change notification settings - Fork 95
Fix: in require(), use os.Getwd() when frames[1].SrcName() is empty #95
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
Conversation
|
This fix is not correct because resolve() should not assume it's running on a local filesystem (or any filesystem for that matter). Could you please come up with a test case? |
|
I reverted the change and added a failing test case instead. Given a package residing in
|
|
I meant a test case to explain why |
|
Oh, I'm sorry. That is coming from a use case in PocketBase. I believe when PocketBase stringifies JS code so it can be executed inside callback handlers, it loses its SrcName. I'm not the author of PocketBase so I tagged Gani in case he can offer more insight. If you feel this is better handled downstream in PocketBase, I will try that instead. I just thought there might be various reasons SrcName might be empty and that there could be a way to handle that case gracefully with Getwd(). But that might be wrong. |
|
@benallfree If you think that the issue originate from the fact that in PocketBase we don't specify a "name" when compiling the goja scripts then we can easily generate one, but is this really the issue here? Could you provide a minimal code sample or repo that illustrates the actual problem (aka. what and how to trigger and see the error you are experiencing)? |
|
@ganigeorgiev Here is a minimal sample showing that traversal fails in monorepos where packages are hoisted to the monorepo's root Apologies to you both if I have posted this issue in the wrong place, it appeared at first to be fixable in goja-nodejs but of course a fix in PocketBase would work for my case too. And Gani, apologies for tagging you in #96 as well. That PR was how I pinpointed the problem and I think it would be very helpful for JSVM users to have module resolution debugging capability instead of just |
|
@benallfree I've added a default scripts name in the latest PocketBase Regarding whether this should be fixed in |
|
@ganigeorgiev Yes, it is fixed. Thank you! @dop251 It does seem that module resolution won't work correctly with |
|
@dop251 I found another issue. The NodeJS module resolution algorithm follows symlinks. Example for // url-prase/package.json
"dependencies": {
"querystringify": "^2.1.1",
},pnpm will store this as follows: Given an require('url-parse')Node's resolution will resolve symlinks for the parent path as follows: Whereas goja_nodejs will do only traverse up the symlink path: The following code fixes it, but then we are back to module paths may not be filesystem paths: fname := frames[1].SrcName()
println("original", fname)
if runtime.GOOS != "windows" {
if resolved, err := filepath.EvalSymlinks(fname); err == nil {
fname = resolved
}
}
println("resolved", fname)
return path.Dir(fname) |
|
Please check #101. As it's a rather substantial and potentially breaking change, I'd leave it open for some time to see if there are any objections. |
There are cases such as in PocketBase where
frames[1].SrcName()is empty.An empty src name causes
getCurrentModulePath()to return., but that will not traverse up to parent directories to search for ancestornode_modulesbecause parent := path.Dir(start) fails (path.Dir(".") == ".").This fix returns an absolute path instead of
., which allowsresolve()to correctly traverse parents.Tagging @ganigeorgiev for further discussion and input if needed.