-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
Relative Javascript imports are apparently using a different CWD when run async #258
Comments
@aisamu Thanks for the funny meme ;) Can you make a reproduction repository or maybe a PR with a failing test? You could also try |
Sure!
Oh, can I require cljs modules directly with |
Oh sorry, I mixed up I'll take a look later this week at your repro. |
@aisamu A potential solution: (ns main
(:require [promesa.core :as p]
[nbb.core :refer [await]]))
;; It tries to load helper.mjs from '/src/helper.mjs'
;; instead of '/src/modules/module/helper.mjs'
(await (js/Promise. #(require '[modules.module.core]))) So await the promise to be resolved. I think the issue is that the require gets execute outside of the namespace bindings. |
Thanks! I've updated the repo with two new entries:
Apart from the above |
It’s a known problem that using non-top-level (async) requires don’t work
well in nbb but this could potentially be fixed. So far I haven’t had many
use cases for this myself but perhaps you could clarify yours.
On Thu, 29 Sep 2022 at 14:41, Samuel Tschiedel ***@***.***> wrote:
Thanks! I've updated the repo
<aisamu/nbb-relative-async-imports@8fe7ce2>
with two new entries:
- The one above, which half-works and stops executing code after the
await:
$> nbb src/main-imp-del-async-awaited.cljs
;; Half-works: Imperative promise-delayed async import awaited
Loaded helper.mjs
Loaded module
;; Where is "Loaded main.cljs?"
- Awaiting the existing p/do example, which works!:
$> nbb src/main-imp-async-awaited.cljs
;; Works: Imperative do-delayed async import awaited
Loaded helper.mjs
Loaded module
Loaded main.cljs
Would this be a solution?
Apart from the above Promise vs p/do issue, perhaps it would be helpful
to await on our behalf behind the scenes.
The failure mode felt a bit unorthodox to me, and tracing it back to the
root cause took a little while.
It was a bit surprising for the effect of failing to await a promise at
the right time to manifest as different results (vs pending results)!
—
Reply to this email directly, view it on GitHub
<#258 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACFSBUDDOBQRWSGZOTKVELWAWE7XANCNFSM6AAAAAAQVKYZMY>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Definitely! The following is a stripped-down view.
|
Is this repo public maybe? |
It isn't, but I can convert the example above into one if you prefer! |
I think your original repro is sufficient to exhibit the problem, so I think I'm good for now. I'll have a closer look. |
What I also discovered is that when you move the (p/do (require '[modules.module.core])
(println "Loaded main.cljs"))
So instead of firing the promise and then doing something else, it's probably better to chain the actions after the previous promise has been resolved. |
Instead of in JS:
I would also write:
since you can't state that the JS file was loaded before that promise has been resolved. |
Those imperative On the "real" project all the js imports are done by cljs, on the respective the Either way, it may be worth noting that the non-awaited # await import(...)
$> node src/main-imp-sync.mjs
Loaded helper.mjs
Loaded index.mjs
Loaded main.mjs
# import(...)
$> node src/main-imp-async.mjs
Loaded main.mjs
Loaded helper.mjs
Loaded index.mjs
# Promise(()=> import(...)
$> node src/main-imp-del-async.mjs
// Works; Imperative delayed async import
Loaded main.mjs
Loaded helper.mjs
Loaded index.mjs The print order changes when you The fact that they never show up on nbb on the equivalent # (await (js/Promise. #(require '[modules.module.core])))
$> nbb src/main-imp-del-async-awaited.cljs
Loaded helper.mjs
Loaded module
# Where is "Loaded main.cljs?" |
I'm still looking into a fix. "It's complicated". |
😄 I truly believe you. It would be unwise not to trust a former real-rock-star now also-programming-rock-star.
Confirmed! And surprising!
I don't recall it perfectly, so please take the following with a huge grain of salt: I think I tried awaiting( |
How I tested this locally:
for building nbb. Then from the nbb dir:
|
Updated the reproduction repository with Thanks a lot! |
Version
$> npm ls nbb nbb@0.7.135
Platform
Problem
When invoked asynchronously,
require
apparently uses an incorrectcwd
on relative node imports.Reproduction
Pure Node example
Equivalent ClojureScript Example
Happy to provide further details/examples.
Thanks again for this amazing tool!
The text was updated successfully, but these errors were encountered: