Reloading command pieces not working in Bun
Reloading a command piece, either with
<piece>.reload()
, with <CommandStore>.loadAll()
or the @sapphire/plugin-hmr
plugin, doesn't work and the command remains the same as before.
After long trial and error I found that I could make it work by first unloading the piece (with <piece>.unload()
), then removing the command file from the import cache (with delete require.cache[piece.location.full]
, this works in Bun with TS/ES6), and then loading the piece again (either with <piece>.reload()
or with <CommandStore>.loadAll()
), and the changes were successfully applied to the commands.
This was tested in a minimal project, with only the following dependencies and file structure:
28 Replies
Bun version used:
v1.2.19
That's interesting but I don't think there is much we can do here since it's so very specific to how Bun handles the cache.
For now I just made a patch that adds this line to the Store's insert funcion in the pieces package so anytime a piece is loaded or reloaded it's import cache is cleared before doing so

It works so far, but I don't know if doing this may cause any problems later :/
I suppose we can accept that change upstream, feel free to make a PR for it.
althoug hchange that first bit of the if statement to
Reflect.has(globalThis, 'Bun')
I'm on bun and it works for me
await Promise.all(this.container.stores.map(store => store.loadAll()));
I do it this way
It reloads everything
1.2.18 bun versionIs that only on 1.2.19 or?
Rolled back to 1.2.18 and used your way of reloading the pieces, still not working without the aforementioned patch.
Reload command:

Problem persist after a complete Bun reinstall. Also tried inside wsl (with the project inside the linux fs) and it still happened, though I don't have any experience with wsl so I may have set it up wrong.
Happens on three devices, two running Win 11 and one running Win 10.
It's really weird
Did you save file?
It works for me 😭
both ubuntu 24.04 and windows 11
maybe its only slash command issue
it works with message commands
lemme try
Yeah, you can see it when the tab's unsaved indicator disappears.
my chatInputRun wont work at all for some reason
hmmm
The HMR plugin can also detect the changes and reloads the piece, but it also doesn't end up updating.
Which version of
@sapphire/framework
are you using?
Maybe it broke in an update?ok it works
with slash commands
it reloads
hmmm let me try once again on linux
yep works on linux too
Well, that's weird
I'm gonna try to find what's causing it to not work
I'm running out of things to try but haven't really achieved anything. I'm gonna try booting into a linux liveiso and run it there.
Welp, it's also not working on neither fedora or even the arch iso .-.
Could you share what version of
@sapphire/pieces
is installed in your node_modules?Yes
second
4.4.1
looks like
yep
Same here
idk this looks like issue on your end somewhere...
Yep, just don't know how I got it to reproduce on 5 different environments lol
At this point I think I'll just leave the patch inside my project, I've spent more time debugging this issue than trying out the framework 😅
If you have the time, can you try to reproduce it using this repo?
This is what I have been using to reproduce this bug, it's a minimal project with just discord.js and the framework, to isolate this problem from other modules.
https://github.com/AngelCMHxD/sapphire-reload-test
GitHub
GitHub - AngelCMHxD/sapphire-reload-test
Contribute to AngelCMHxD/sapphire-reload-test development by creating an account on GitHub.
The
/reload
command does <piece>.reload()
, while the /clear-reload
does the fix that I mentioned at the start of this thread.
Just doing /reload
should work, but it's not working for meYes
@AngelCMHxD did u get it
im gonna try now

with your code its not working for me
so it might be tsconfig
or typescript version
This happens with .mjs too, so neither of those could have been the problem.
Though, I already found the issue.
@sapphire/pieces
import pieces with an URL object (file://
). There ended up being an issue in Bun as changing the params in that URL object doesn't make Bun re-import the file, even though Node.js does. I've reported the issue to Bun with steps on how to reproduce it, and made a PR to @sapphire/pieces
in case they want to add a workaround until the bug is fixed in Bun.
I'm still wondering how it worked in your codebase though. Maybe a module is changing how that behaves?Idk it works every time for me