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:
"@sapphire/framework": "^5.3.6",
"@sapphire/plugin-hmr": "^3.0.2",
"discord.js": "^14.21.0"
"@sapphire/framework": "^5.3.6",
"@sapphire/plugin-hmr": "^3.0.2",
"discord.js": "^14.21.0"
| .env
| bun.lock
| package.json
| tsconfig.json
|
\---src
| index.ts
|
\---commands
ping.ts
reload.ts
| .env
| bun.lock
| package.json
| tsconfig.json
|
\---src
| index.ts
|
\---commands
ping.ts
reload.ts
28 Replies
AngelCMHxD
AngelCMHxDOP3mo ago
Bun version used: v1.2.19
Favna
Favna3mo ago
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.
AngelCMHxD
AngelCMHxDOP3mo ago
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
No description
AngelCMHxD
AngelCMHxDOP3mo ago
It works so far, but I don't know if doing this may cause any problems later :/
Favna
Favna3mo ago
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')
čamdžić
čamdžić3mo ago
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 version
čamdžić
čamdžić3mo ago
čamdžić
čamdžić3mo ago
Is that only on 1.2.19 or?
AngelCMHxD
AngelCMHxDOP3mo ago
Rolled back to 1.2.18 and used your way of reloading the pieces, still not working without the aforementioned patch.
AngelCMHxD
AngelCMHxDOP3mo ago
Reload command:
No description
AngelCMHxD
AngelCMHxDOP3mo ago
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.
čamdžić
čamdžić3mo ago
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
AngelCMHxD
AngelCMHxDOP3mo ago
Yeah, you can see it when the tab's unsaved indicator disappears.
čamdžić
čamdžić3mo ago
my chatInputRun wont work at all for some reason hmmm
AngelCMHxD
AngelCMHxDOP3mo ago
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?
čamdžić
čamdžić3mo ago
ok it works with slash commands it reloads
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@types/bun": "^1.2.18",
"@types/lodash": "^4.17.20",
"lefthook": "^1.12.2",
"prisma": "^6.12.0"
},
"peerDependencies": {
"typescript": "5.7.3"
},
"dependencies": {
"@kingsworld/plugin-cron": "^3.0.5",
"@prisma/client": "^6.12.0",
"@sapphire/decorators": "^6.2.0",
"@sapphire/framework": "^5.3.6",
"@sapphire/plugin-logger": "^4.1.0",
"@sapphire/plugin-subcommands": "^7.0.1",
"@sapphire/plugin-utilities-store": "^2.0.3",
"discord.js": "^14.21.0",
"zod": "^3.25.76"
}
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@types/bun": "^1.2.18",
"@types/lodash": "^4.17.20",
"lefthook": "^1.12.2",
"prisma": "^6.12.0"
},
"peerDependencies": {
"typescript": "5.7.3"
},
"dependencies": {
"@kingsworld/plugin-cron": "^3.0.5",
"@prisma/client": "^6.12.0",
"@sapphire/decorators": "^6.2.0",
"@sapphire/framework": "^5.3.6",
"@sapphire/plugin-logger": "^4.1.0",
"@sapphire/plugin-subcommands": "^7.0.1",
"@sapphire/plugin-utilities-store": "^2.0.3",
"discord.js": "^14.21.0",
"zod": "^3.25.76"
}
hmmm let me try once again on linux yep works on linux too
AngelCMHxD
AngelCMHxDOP3mo ago
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?
čamdžić
čamdžić3mo ago
Yes second 4.4.1 looks like yep
AngelCMHxD
AngelCMHxDOP3mo ago
Same here
čamdžić
čamdžić3mo ago
idk this looks like issue on your end somewhere...
AngelCMHxD
AngelCMHxDOP3mo ago
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 😅
AngelCMHxD
AngelCMHxDOP3mo ago
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.
AngelCMHxD
AngelCMHxDOP3mo ago
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 me
čamdžić
čamdžić3mo ago
Yes @AngelCMHxD did u get it im gonna try now
čamdžić
čamdžić3mo ago
No description
čamdžić
čamdžić3mo ago
with your code its not working for me so it might be tsconfig or typescript version
AngelCMHxD
AngelCMHxDOP3mo ago
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?
čamdžić
čamdžić3mo ago
Idk it works every time for me

Did you find this page helpful?