remix-run / blues-stack

The Remix Stack for deploying to Fly with PostgreSQL, authentication, testing, linting, formatting, etc.
https://remix.run/stacks
MIT License
961 stars 236 forks source link

Race condition in npm run dev #159

Closed humphd closed 7 months ago

humphd commented 1 year ago

Have you experienced this bug with the latest version of the template?

Yes

Steps to Reproduce

npm run dev

Expected Behavior

I would expect npm run dev to always build and start the various servers without problems related to build timing/order.

Actual Behavior

Sometimes when running npm run dev, the various parallel build processes happen in an order such that the server can't start:

https://github.com/remix-run/blues-stack/blob/main/package.json#L10-L14

The only way to fix this is to manually run npm run build then re-try npm run dev. I guess because these commands aren't finishing (due to --watch), it isn't easy to know when to run the next step? Regardless, it would be good to find a better way to do this that doesn't randomly fail.

humphd commented 1 year ago

I found that even changing https://github.com/remix-run/blues-stack/blob/main/package.json#L10 to this helps a bunch:

"dev": "run-p dev:build dev:remix dev:server",
kwabe007 commented 1 year ago

Just ran into this issue today as well when trying to follow this stack's dev/build scripts. Explicitly expanding the script names didn't work for me but as a workaround I've set a three second delay for the dev:server script:

"delay": "node -e \"setTimeout(() => process.exit(0), 3000)\"",
"dev:server": "npm run delay && cross-env NODE_ENV=development node --inspect --require ./node_modules/dotenv/config --require ./mocks ./build/server.js"

And now it works without having to run npm run build first.

But it's still a potential race condition though, although the longer the delay the less likely it is.

UPDATE For anyone reading this in the future: if you don't mind installing an npm package, you can use wait-on to wait for the build files to exist before trying to run node. This way, you don't have to wait longer than needed (or risk not waiting long enough); you'll wait just until the files are built. In my case I installed the package and updated the scripts in package.json:

"scripts": {
  ...
  "dev:server": "cross-env NODE_ENV=development npm run wait-build && node --inspect --require ./node_modules/dotenv/config --require ./mocks ./build/server.js",
  "wait-build": "wait-on -t 20000 ./build/server.js ./build/index.js"
},

The -t 20000 option makes wait-on wait a maximum of 20,000 ms (20 seconds) for the files before failing.

Another alternative if you don't want to add another npm dependency is to write a custom node script which polls for the files existence and use that instead.

humphd commented 1 year ago

@kwabe007 this is better for sure, thanks for sharing.

jsrhodes15 commented 1 year ago

I'm wondering if this issue should be resolved with the latest release of remix?

https://github.com/remix-run/remix/releases/tag/remix%401.16.0

I have used the Blue Stack pretty much as-is, so I'm not 100% sure on the internals. But if I understand correctly, couldn't the scripts be changed such that dev is just a single command?

Jesus-fhz commented 7 months ago

Hi there,

Updating to node 20 fixed this issue for me.