oven-sh/bun · error · Error
Unknown $rust() file identifier ${JSON.stringify(filename)}.
Error message
Unknown $rust() file identifier ${JSON.stringify(filename)}. Add it to rustIdentifierPaths in src/codegen/generate-js2native.ts. What it means
For $rust() calls, generate-js2native.ts resolves .rs filenames through an explicit allowlist, rustIdentifierPaths, mapping known Rust file names to their paths under src/ (e.g. "udp_socket.rs" → "runtime/socket/udp_socket.rs"). Unlike other call types there is no filesystem search, so passing any .rs filename not in that map fails immediately with this error, which tells you to add it to the map.
Source
Thrown at src/codegen/generate-js2native.ts:117
"udp_socket.rs": "runtime/socket/udp_socket.rs",
"upgrade_command.rs": "runtime/cli/upgrade_command.rs",
"virtual_machine_exports.rs": "jsc/virtual_machine_exports.rs",
};
function callBaseName(x: string) {
return x.split(/[^A-Za-z0-9]/g).pop()!;
}
function resolveNativeFileId(call_type: NativeCallType, filename: string) {
const ext = call_type === "bind" ? ".bind.ts" : call_type === "rust" ? ".rs" : `.${call_type}`;
if (!filename.endsWith(ext)) {
throw new Error(`Expected filename for $${call_type} to have ${ext} extension, got ${JSON.stringify(filename)}`);
}
if (call_type === "rust") {
const relative = rustIdentifierPaths[filename];
if (!relative) {
throw new Error(
`Unknown $rust() file identifier ${JSON.stringify(filename)}. Add it to rustIdentifierPaths in src/codegen/generate-js2native.ts.`,
);
}
return path.join(srcDir, relative.replaceAll("/", sep));
}
filename = filename.replaceAll("/", sep);
const resolved = sourceFiles.find(file => file.endsWith(sep + filename));
if (!resolved) {
const fnName = call_type === "bind" ? "bindgenFn" : call_type;
throw new Error(`Could not find file ${filename} in $${fnName} call`);
}
return filename;
}
export function registerNativeCall(View on GitHub (pinned to 8c5296ac45)
Solutions
- Open src/codegen/generate-js2native.ts and find the rustIdentifierPaths object near the top
- Add an entry mapping your filename to its path relative to src/, e.g. "my_new_module.rs": "runtime/api/my_new_module.rs",
- Re-run codegen — the error message itself confirms this is the intended fix
Example fix
// src/codegen/generate-js2native.ts
// before
const rustIdentifierPaths = {
"udp_socket.rs": "runtime/socket/udp_socket.rs",
};
// after
const rustIdentifierPaths = {
"udp_socket.rs": "runtime/socket/udp_socket.rs",
"my_new_module.rs": "runtime/api/my_new_module.rs",
}; Defensive patterns
Strategy: validation
Validate before calling
// every $rust() filename must already be in the allowlist
import { rustIdentifierPaths } from "./generate-js2native.ts";
function isKnownRustFile(filename: string): boolean {
return filename.endsWith(".rs") && Object.hasOwn(rustIdentifierPaths, filename);
} Prevention
- Adding a new Rust-backed binding is a two-step change: create the .rs file AND add its entry to rustIdentifierPaths in the same commit
- When moving a .rs file, update both the map entry and any $rust() call sites that reference it
When it happens
Trigger: Writing $rust("my_new_module.rs", "BunMyApi") in a .bind.ts file for a newly created Rust source file, without updating src/codegen/generate-js2native.ts.
Common situations: Contributors adding new Rust-backed JS APIs and hitting the codegen allowlist for the first time; moving a .rs file to a different directory so its old map entry no longer matches the new filename passed in.
Related errors
- Expected filename for $${call_type} to have ${ext} extension
- Could not find file ${filename} in $${fnName} call
- "builtin" should be string
- ${typeName}: 'internal: true' on a property is not implement
- ${typeName}.${name}: `this: true` accessors require `sharedT
AI-assisted analysis of oven-sh/bun@8c5296ac45 (2026-08-16).
Data as JSON: /api/errors/6b2d86b60e4b8f24.
Report an issue: GitHub.