Skip to content
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

Hot reload - needs debounce on file watch #2987

Closed
e3dio opened this issue May 22, 2023 · 12 comments
Closed

Hot reload - needs debounce on file watch #2987

e3dio opened this issue May 22, 2023 · 12 comments
Labels
bug Something isn't working

Comments

@e3dio
Copy link
Contributor

e3dio commented May 22, 2023

What version of Bun is running?

0.6.2

What platform is your computer?

Linux 5.19.0-1025-aws aarch64 aarch64 (Ubuntu 22.04)

What steps can reproduce the bug?

While in Hot Reload mode, updating a module file via SSH gives error on reload. Looks like it successfully loads the updated file and continues running correctly, just displays an error: SyntaxError: Import named 'a' not found in module '/home/ubuntu/bun/a.mjs'.

// app.mjs
import { a } from './a.mjs';

export default {
  fetch(req) {
    return new Response(a);
  }
}
// a.mjs
export const a = 1;
bun --hot app.mjs

Now save new version of a.mjs

SyntaxError: Import named 'a' not found in module '/home/ubuntu/bun/a.mjs'.

What is the expected behavior?

No error

What do you see instead?

SyntaxError: Import named 'a' not found in module '/home/ubuntu/bun/a.mjs

Additional information

No response

@e3dio e3dio added the bug Something isn't working label May 22, 2023
@Jarred-Sumner
Copy link
Collaborator

Sometimes Bun hot reloads faster than your text editor can save the files.

I suspect that's the cause (the text editor truncated the file to 0, bun reloaded it, then it saved the rest, Bun reloaded again)

@e3dio
Copy link
Contributor Author

e3dio commented May 22, 2023

Ok can confirm issue only happens with file upload via SSH. Should probably add a short couple of millisecond debounce on file watching so it only reloads once. If multiple files get updated you need to give it time to update all files, like a git pull etc. Could make it configurable, --hot=100 for 100ms debounce. Or just have some reasonable default. It's more important to wait for files to correctly update than have premature reload to save a couple millisecond. This also applies to --watch

@e3dio
Copy link
Contributor Author

e3dio commented May 23, 2023

Also Iooks like the SSH file upload is appending to the file in chunks so over a certain size file 4KB it hot reloads a partial file. Just need some amount of debounce time either by default or configurable.

Hot Reload is a really nice feature for keeping WebSockets/HTTP connections open and updating the app so would be nice to make it work well

@e3dio e3dio changed the title Hot reload - Import named 'a' not found in module Hot reload - needs debounce on file watch May 23, 2023
@BeyondMagic
Copy link
Contributor

BeyondMagic commented Jan 26, 2024

This error still exists and I don't think there's anything I can do to from the command line to make it work properly. I save my files and sometimes, half the time, I save it and it gives me the error Cannot find module when it clearly exists there, it just needed a few milliseconds for the text editor to save it and import it (I'm using Vim, by the way!) Bun is too fast in watching files.

@brw
Copy link

brw commented Jan 27, 2024

This error still exists and I don't think there's anything I can do to from the command line to make it work properly. I save my files and sometimes, half the time, I save it and it gives me the error Cannot find module when it clearly exists there, it just needed a few milliseconds for the text editor to save it and import it (I'm using Vim, by the way!) Bun is too fast in watching files.

I'm also running into this issue with --watch when saving a file in Vim/Neovim. Bun will sometimes exit with error: Script not found. The problem does not occur if I turn off swap files using :set noswapfile (though I would like to keep using swap files).

I assume this is the same problem so I didn't create a new issue for it.

@BeyondMagic
Copy link
Contributor

Thank you @brw, the problem was completely mitigated from my environment using :set noswapfile. Guess now I just can hope to not crash the editor and end up and lose the files!

@jjt
Copy link

jjt commented Mar 20, 2024

Same issue as @brw with run --watch, but only if the formatter conform.nvim is loaded and set to format on save.

I don't love going without swap files, but I save frequently enough and nvim crashes infrequently enough that it shouldn't be an issue in Bun-based projects.

@Jarred-Sumner
Copy link
Collaborator

Jarred-Sumner commented Sep 2, 2024

This was done in >= Bun v1.1.20

When multiple reloads are sent before the next one finishes, they're deduped. This fixes cases where bun would not be watching files that need to be watched in order to reload at the right time

If you're still running into issues with this please leave a comment

@trya2l
Copy link

trya2l commented Oct 10, 2024

Hi @Jarred-Sumner, I'm using bun v1.1.29 (I also tried with v1.1.20), and with neovim the issue persist. I've confirm that this is not happening with vscodium for example.

Image

@BeyondMagic
Copy link
Contributor

The issue is not resolved here either.

@chrisdavies
Copy link

I'm seeing this still on Linux (Arch) and bun 1.1.38. I have prettierd which autoformats my files on save.

@ShikChen
Copy link

ShikChen commented Dec 4, 2024

Hit the same issue with neovim for both --hot and --watch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

8 participants