Repository navigation
Conversation
…record of a shared file A text import in a standalone executable is embedded as a string body. A file import is embedded as the file's bytes. Both took the path [name]-[hash].[ext] from a hash of the source bytes, so two same-named files with equal bytes printed one path, and to_bytes kept the first output file at it. The other import then read a record of the wrong form: the text import ran the file as JavaScript at startup, or the file import read the string body. - The [hash] of a text module covers its stored form. A text module and a raw copy of the same bytes print two paths. - When a file import and a sqlite embed of the same bytes share a path, to_bytes keeps the record of the database import, in either import order. require() opens a database by the loader in its record, and the file import reads the same bytes.
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Asset hash propagation src/bundler/ParseTask.rs |
register_embedded_asset uses caller-provided hashes. Text-loader assets include the "text" tag in the hash input. SQLite and NAPI assets hash source contents. |
Duplicate destination resolution src/standalone_graph/StandaloneModuleGraph.rs |
to_bytes replaces a retained duplicate only for matching Loader::SqliteEmbedded hashes. |
Embedded asset behavior tests test/bundler/bundler_compile.test.ts |
Tests cover loader combinations, import order, backend modes, decoding, file identity, SQLite contents, asset names, and page bytes. |
Suggested reviewers: jarred-sumner, alii
Priority:
Merge Risk: 
Minimal · up to 4c600
-
No merge-blocking regression was identified in the changed embedded-asset behavior; this is ready for normal checks.
Pre-merge checks: 
4
-
Full details: Description check
-
Explanation
The description explains the problem, fix, verification, background, limitations, and trade-offs. It does not use the template headings exactly, but it provides the required information in equivalent sections.
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
|
Updated 11:43 AM PT - Oct 24th, 2026
✅ @robobun, your commit 4c600420143a34993cca8b4ae0e8b2bb8cf86820 passed in 🧪 To try this PR locally: bunx bun-pr 46760That installs a local version of the PR into your bun-46760 --bun |
|
Status Reproduced on 1.4.3 and on main (6292c65) with two same-named files with equal bytes, one imported as mkdir a b && printf 'bytes of data' > a/data.txt && printf 'bytes of data' > b/data.txt
cat > e1.ts <<'EOF2'
import f from "./a/data.txt" with { type: "file" };
import t from "./b/data.txt" with { type: "text" };
console.log(f, t);
EOF2
bun build e1.ts --compile --outfile=e1 && ./e1The build exits 0. CI on 4c60042 (build 132092): no job failed (179 of 181 passed, 2 were still queued when I wrote this). 9 test files failed once and passed on retry. This diff touches none of them. All review threads are resolved. PR: #46760 |
No output file carries Loader::Sqlite: a sqlite import that is not embedded stays external, and only Loader::SqliteEmbedded registers an embedded asset.
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
The follow-up commit narrows the repeated-path check in to_bytes to Loader::SqliteEmbedded, which addresses the dead Loader::Sqlite arm raised last time. Beyond the inline nit, I also checked whether a file import whose bytes literally begin with text could collide with a text module's new [hash] — ContentHasher::write length-prefixes each chunk (src/bundler/bundle_v2.rs:8306), so "text" + contents and a single raw buffer never hash the same way.
Extended reasoning...
The change touches the compile-time embedded-asset hash in src/bundler/ParseTask.rs (text modules now hash a "text" tag before the contents, giving them a distinct [hash] from a file import of the same bytes) and the path-dedup loop in src/standalone_graph/StandaloneModuleGraph.rs::to_bytes (a later SqliteEmbedded record with an equal content hash replaces the kept record). No security-sensitive surface is involved; the hash equality guard also keeps the entry-point pointer in module_files intact since a chunk and a database cannot share a content hash. A human look is still worthwhile because the fix changes the [hash] of every text module in compile builds (renames, plus a second embedded copy for file+text pairs), which is a user-visible output decision rather than a mechanical fix.
Problem
bun build --compileexecutable gives afileand atextimport of same-named, equal-bytes files one embedded path.filefirst: the text import runs the file as JavaScript (SyntaxError: Unexpected identifier 'of'at/$bunfs/root/data-srdqwvvw.txt:1:1).textfirst: thefileimport reads the string body.to_byteskeeps the first file at a path (src/standalone_graph/StandaloneModuleGraph.rs:1600), sofilebefore asqliteembed dies the same way.Fix
[hash]covers its stored form (ParseTask.rs): it never prints a raw copy's path.to_byteskeeps asqliteembed's record over another copy of the same bytes. Thefileimport reads either.test/bundler/bundler_compile.test.ts(6 new tests, all fail on main). Alsocompile-asset-bunfsand 11 more suites (Notes)..nodetext import fix, a separate mechanism (Notes).Background
sqliteembed opens a database, others are bytes.to_byteskeeps the text copy (filestill reads a string), or one record with two views (a branch per module load).Downsides
[hash]of each text module and of each hashed chunk that reaches one, and the executable'sBuildArtifact.hash.file+textpair of a 3 MB file, 4,096 bytes of bun binary.Notes
History. No user has reported this. There is no issue.
file+text: 1.3.13 and 1.4.0 run both import orders. 1.4.1, 1.4.2, 1.4.3 and main (6292c65) do not. bun build --compile: embed text imports as pre-encoded strings #40177 made a text import an embedded string body and madeto_bytesskip a repeated path.file+sqliteembed: one order fails in every release. Through 1.4.0 the last record at a path won, sosqlitefirst died at startup. Since 1.4.1 the first wins, sofilefirst dies. This branch is the first build that runs both orders.Outcomes, release builds of main and of this branch. Each row is two files with one basename and equal bytes, imported in that order. A row is right when every import gives what it gives in a control build where the two files have different basenames.
SyntaxErrorSyntaxErrorSyntaxErrorSyntaxErrorBun.embeddedFilesTypeError: the database import isundefined.nodefile. See "Not changed here".62616420efbfbdefbfbdfor the bytes62616420fffeand does not list the file inBun.embeddedFiles, in both entry orders. This branch serves the bytes and lists the file.file+sqlitepair under--asset-naming='[name].[ext]'also runs in both orders (compile/SameNameSameBytes/FileFirst/NoHash). There the content hash, not the path, tells a copy from another file.Two different files at one embedded path. This PR does not change that case. Main keeps the first file and exits 0 (through 1.4.0 it kept the last), and so does this branch. Examples: two same-named files with different bytes under
--asset-naming='[name].[ext]', or an asset printed at the name of a chunk. #46655 adds copied assets to the linker's duplicate output path check, for every build mode, and fails those builds. It lets copies with equal bytes share a path, so it does not fix the pairs in the table. An earlier version of this branch failed such a build into_bytes. The self-review moved that out: it changes builds that exit 0 today, and #46655 is where that decision is open.Costs for builds that never have such a pair (release builds of main 6292c65 and of this branch, gdb breakpoint hit counters on
bun-profile).fileimports: 1,121 calls on both builds.to_bytes(from the entry ofto_executableto its call oftarget_executable): allocator calls 8, 1,033, 10 and 12 for hello world, 1,000 file assets, two equal text files, and a page that shares an image with its server. The counts are the same on both builds.memcmpin that window is the 17-byte path, on both builds, also when the shared image is 64 MB.cmp, main against this branch, onto one runtime. Main against main is also 0.compile/TextImportfixture: 6 of 6 text modules change, 0 of 1 file assets.Bun.embeddedFilesnames do not change.--splittingfixture with 4 hashed chunks and 2 text modules: 2 of 2 text modules and 3 of 4 chunks change. The 3 chunks embed or reach the path of a text module.BuildArtifact.hashof the executable changes with a text import (t502y850tox6zyrah2) and does not change without one.file+textpair of a 3 MB file: the executable grows by 3,145,728 bytes. On main the two imports share one record. 1.4.0 also stored the text and the file..text+768 bytes (size -A), 4,096 bytes on disk.get_ast+661,to_executable+323,register_embedded_asset-201 (nm -S).__bun_fetch_builtin_module+0. No run-time source changes.perf,valgrind,straceandbloatyare not installed on this machine.Not changed here.
.nodefile exits 1 withERR_DLOPEN_FAILED, with or without a second import. 1.4.0 prints the string.require()sends every id that ends in.nodetoprocess.dlopenbefore it reads the record (src/js/builtins/CommonJS.ts). It is the only extension of 23 probed that fails. The self-review proposed to keep such a module inline in this PR. It is a separate mechanism with no report, so it is left for its own change.--compile-executable-path) fails withSyntaxErrorat startup. The older runtime does not know the string body record.--asset ./c/logo.fileplus afileimport of the same file under--asset-naming='[name].[ext]'fails the build withcollides with another embedded file, on main and on this branch.sqliteembeds of equal bytes under one basename share oneDatabaseobject.process.dlopenandbun:ffiread the stored body of a record. For a text module that is the string body.Related open PRs.
fileand astextkeeps the first loader and does not reach this code. With bundler: key modules by (path, loader) so one file under two import attributes is two modules #41834 it does. This PR should land first: then that import gets two paths, and one.dbimported asfileand as asqliteembed gets the database record.bytesloader whose record the runtime reads by its loader tag. It needs the same care astextfor its[hash].require()of an embeddedfilerecord loads. It does not fix this bug: the text import would get{ default: string }.--assetfiles by their stored name.Suites. Release builds of main and of this branch:
bundler_compile,bundler_compile_splitting,bundler_compile_prelinked,bun-build-compile,bun-build-api,cli,compile-asset-bunfs,bundler_html_server,bundler_naming,bundler_loader,standalone,html-import-manifest,text-loader. This branch: 0 failures. Main: the 6 new tests fail. The debug build of this branch passesbundler_compile(91 tests). In the other suites it fails only tests that exceed their 5 s or 60 s timeout in a debug build (a debug--compiletakes 4 to 30 s here). Those with a default timeout pass when the timeout is longer.Self-review. Addressed: the build error for two different files at one path is removed and left to #46655. The rename in Downsides names chunks and
BuildArtifact.hash. A test at a path without[hash]is added. Thefile+sqlitehistory, the severity of thefilefirst order and the.nodenote are corrected above. The hand-written table of record forms is gone with the error. Comments are one line. Not taken: the.nodefix, for the reason above.no test proof · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/bundler/bundler_compile.test.ts