sjc5 / hwy

Hwy is a fullstack web framework for driving a (p)react frontend with a Go backend. Includes end-to-end typesafety, file-based nested UI routing, and much more.
BSD 3-Clause "New" or "Revised" License
317 stars 3 forks source link

How to run on Bun? #4

Closed gitblit closed 11 months ago

gitblit commented 1 year ago

I'm interested in getting this running, but of course I want to combine the new framework with the new runtime. I know Bun is living on the edge. Looks like there are npm/node calls embedded in the dev process which bun --bun can't decipher. Any Ideas on how to get this going?

james@RHOADS:~/git/my-hwy$ bun --bun run dev
$ hwy-dev-serve

 Hwy  New build initiated (triggered from run-build-tasks.js)

 Hwy  Running pre-build tasks...

 Hwy  Running npm run hwy-prebuild

Error running pre-build tasks: 462 | }, toPathIfFileURL = function(fileURLOrPath) {
463 |   if (!isURLInstance(fileURLOrPath))
464 |     return fileURLOrPath;
465 |   return Bun.fileURLToPath(fileURLOrPath);
466 |
467 | }, genericNodeError = function(message, options) {
                                                               ^
error: Command failed: npm run hwy-prebuild
npm ERR! undefined is not an object

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/james/.npm/_logs/2023-09-22T19_45_01_539Z-debug-0.log

 code: "1"

      at node:child_process:467:60
      at exitHandler (node:child_process:59:44)
      at emit (node:events:122:94)
      at #maybeClose (node:child_process:680:26)
      at #handleOnExit (node:child_process:530:30)
      at processTicksAndRejections (:55:76)

Completed pre-build tasks in 353ms.

 Hwy  Running standard build tasks...

Completed standard build tasks in 86ms.

[nodemon] 3.0.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): .dev/refresh.txt
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node dist/main.js`
Error: Invalid stdio option "[object EventEmitter]"
    at <anonymous> (node:child_process:355:24)
    at map (:1:11)
    at spawn (node:child_process:610:8)
    at <anonymous> (node:child_process:2:21)
    at run (/home/james/git/my-hwy/node_modules/nodemon/lib/monitor/run.js:146:23)
    at <anonymous> (/home/james/git/my-hwy/node_modules/nodemon/lib/nodemon.js:243:9)
    at <anonymous> (/home/james/git/my-hwy/node_modules/nodemon/lib/config/index.js:82:7)
    at processTicksAndRejections (:55:39)
error: Invalid stdio option "[object EventEmitter]"
      at node:child_process:356:24
sjc5 commented 1 year ago

Hey @gitblit, thanks for the issue!

Yeah, definitely wanting to run this in Bun as well. It works if you run it just as bun run dev, but I believe that is just running it in Node mode but with Bun as package manager, whereas adding the --bun runs it in Bun mode, hence that part blowing up.

There's nothing particularly goofy going on under the hood during the build process. It looks like you're probably using the Tailwind CLI in the prebuild there. But in the Hwy dev/build deps are all really popular node packages -- cross-env, chokidar, nodemon, esbuild -- stuff like that.

But to the actual issue -- it seems like the Bun global itself is undefined there? Maybe an issue with Bun 1.0.3?

sjc5 commented 1 year ago

Potentially related to non-prebuild error further down: https://github.com/oven-sh/bun/issues/5681

gitblit commented 1 year ago

Excellent! I'll keep an eye on that issue, but switch to Node in the meantime to keep playing with your creation. :)

sjc5 commented 1 year ago

Little update:

Regarding the first of the two errors (related to the pre-build), this seems to fix the error when running bun --bun run dev (can test with 0.2.0-beta.7).

Still left with nodemon erroring out with the Invalid stdio option "[object EventEmitter]" error. Given the popularity of nodemon, I bet this is fixed pretty soon by the Bun team. Alternatively, if Bun implements the ability to watch a specific file (we want to target ~/.dev/refresh.txt) instead of inferring which files to watch from the entry file, then we can create a bun-specific version of the hwy-dev-serve script and not use nodemon at all.

jharrell commented 1 year ago

No clue why I'm not getting more output, but this is not working w/ hwy 0.2.1 and bun 1.0.3

❯ bun --bun run dev
$ hwy-dev-serve
[0.01ms] ".env"
[0.01ms] ".env"

 Hwy  New build initiated (triggered from run-build-tasks.js) 

 Hwy  Running pre-build tasks... 

 Hwy  Running tailwindcss -i src/styles/tw-input.css -o src/styles/tw-output.bundle.css 

[0.02ms] ".env"
Rebuilding...
Done in 81ms.

Completed pre-build tasks in 336ms.

 Hwy  Running standard build tasks... 

Completed standard build tasks in 23ms.

Any idea what I can do to get more verbose output? Not expecting things to work, but would love to know exactly what is failing (I'm assuming it's a node API not yet implemented in bun)

EDIT: bun run dev works great! 😄

EDIT 2: using bun --bun run nodemon I get the following output:

❯ bun --bun run nodemon
[0.01ms] ".env"
34 | 
35 | // if nodemon was spawned via a fork, allow upstream communication
36 | // via process.send
37 | if (process.send) {
38 |   bus.emit = function (event, data) {
39 |     process.send({ type: event, data: data });
        ^
TypeError: process.send is not a function. (In 'process.send({ type: event, data })', 'process.send' is 4)
      at /Users/jharrell/code/jharrell/hwy-bun-prisma/node_modules/nodemon/lib/utils/bus.js:39:4
      at addListener (node:events:151:112)
      at /Users/jharrell/code/jharrell/hwy-bun-prisma/node_modules/nodemon/lib/monitor/watch.js:16:0
      at globalThis (/Users/jharrell/code/jharrell/hwy-bun-prisma/node_modules/nodemon/lib/monitor/watch.js:16:29)
      at require (:1:20)
      at /Users/jharrell/code/jharrell/hwy-bun-prisma/node_modules/nodemon/lib/monitor/run.js:10:4
      at globalThis (/Users/jharrell/code/jharrell/hwy-bun-prisma/node_modules/nodemon/lib/monitor/run.js:555:17)
      at require (:1:20)
error: "nodemon" exited with code 1 (SIGHUP)

Which judging by my google fu should have been resolved already...

sjc5 commented 1 year ago

No clue why I'm not getting more output, but this is not working w/ hwy 0.2.1 and bun 1.0.3


❯ bun --bun run dev

$ hwy-dev-serve

[0.01ms] ".env"

[0.01ms] ".env"

 Hwy  New build initiated (triggered from run-build-tasks.js) 

 Hwy  Running pre-build tasks... 

 Hwy  Running tailwindcss -i src/styles/tw-input.css -o src/styles/tw-output.bundle.css 

[0.02ms] ".env"

Rebuilding...

Done in 81ms.

Completed pre-build tasks in 336ms.

 Hwy  Running standard build tasks... 

Completed standard build tasks in 23ms.

Any idea what I can do to get more verbose output? Not expecting things to work, but would love to know exactly what is failing (I'm assuming it's a node API not yet implemented in bun)

EDIT: bun run dev works great! 😄

Ah, in the latest release we converted some bash scripts to node scripts to help with windows compat, and I bet I neglected to print any stdout. Will take a peek later, good callout.

sjc5 commented 1 year ago

@jharrell

Hmm, I am actually still seeing the same Invalid stdio option Bun/nodemon error as before (pasted at bottom) on Hwy 0.2.1 / Bun 1.0.3, when running in "bun mode" (bun --bun run dev) on Mac and Linux (with hwy-dev-serve as the dev script).

Are you not seeing that error at all? If so that's an interesting divergence.


For anyone reading that wants to dig in more, here is the equivalent script that hwy-dev-serve executes:

cross-env NODE_ENV=development hwy-build && cross-env NODE_ENV=development nodemon --watch .dev/refresh.txt

Which is basically:

  1. Run build step in dev mode (this is succeeding)
  2. Run the "start" script in dev mode with nodemon, and restart whenever .dev/refresh.txt changes (this is what's failing)

Theoretically this should not be that hard to recreate in Bun directly, but I do not think they have implemented the ability to watch just one specific file like you can with nodemon (hopefully soon 🤞).


My full output when running bun --bun run dev:

sjc5:~/bun-test-1$ bun --bun run dev
$ hwy-dev-serve

 Hwy  New build initiated (triggered from run-build-tasks.js) 

 Hwy  Running pre-build tasks... 

 Hwy  Running tailwindcss -i src/styles/tw-input.css -o src/styles/tw-output.bundle.css 

Rebuilding...
Done in 174ms.

Completed pre-build tasks in 747ms.

 Hwy  Running standard build tasks... 

Completed standard build tasks in 76ms.

[nodemon] 3.0.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): .dev/refresh.txt
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node dist/main.js`
Error: Invalid stdio option "[object EventEmitter]"
    at <anonymous> (node:child_process:355:24)
    at map (:1:11)
    at spawn (node:child_process:610:8)
    at <anonymous> (node:child_process:2:21)
    at run (/home/sjc/bun-test-1/node_modules/nodemon/lib/monitor/run.js:146:23)
    at <anonymous> (/home/sjc/bun-test-1/node_modules/nodemon/lib/nodemon.js:243:9)
    at <anonymous> (/home/sjc/bun-test-1/node_modules/nodemon/lib/config/index.js:82:7)
    at processTicksAndRejections (:55:39)
error: Invalid stdio option "[object EventEmitter]"
      at node:child_process:356:24
sjc5 commented 11 months ago

Happy to report this will be fixed in the next release. Ended up manually recreating what nodemon was doing in scripts and removing nodemon altogether. If anyone wants to try it out, you can run npx create-hwy@0.3.0-beta.2 and select Bun as a deployment target.

npx create-hwy@0.3.0-beta.2 # Select Bun as deployment target
cd <your-project-name>
bun i
bun run --bun dev
jharrell commented 11 months ago

@sjc5 awesome! Thank you so much and sorry for not getting back to you. I'll try it out as soon as I can and report back :)

jharrell commented 11 months ago

@sjc5 confirming that with bun 1.0.4 and hwy 0.3.0-beta.2 this works great! Thank you so much for your work.

gitblit commented 11 months ago

This is awesome! Thanks!