denoland/deno · error · TypeError
Unexpected 'fn' field in options, bench function is already
Error message
Unexpected 'fn' field in options, bench function is already provided as the third argument
What it means
Before formatting, `read_file_contents` reads the raw bytes, records whether a UTF-8 BOM is present, detects the charset, and decodes via `decode_owned_source`. When decoding fails, the file contains byte sequences that are not valid UTF-8 under the detected charset, and fmt refuses to touch it because it cannot round-trip the text safely.
Source
Thrown at cli/js/40_bench.js:125
ignore: false,
baseline: false,
only: false,
sanitizeExit: true,
permissions: null,
};
if (typeof nameOrFnOrOptions === "string") {
if (!nameOrFnOrOptions) {
throw new TypeError("The bench name can't be empty");
}
if (typeof optionsOrFn === "function") {
benchDesc = { fn: optionsOrFn, name: nameOrFnOrOptions, ...defaults };
} else {
if (!maybeFn || typeof maybeFn !== "function") {
throw new TypeError("Missing bench function");
}
if (optionsOrFn.fn != undefined) {
throw new TypeError(
"Unexpected 'fn' field in options, bench function is already provided as the third argument",
);
}
if (optionsOrFn.name != undefined) {
throw new TypeError(
"Unexpected 'name' field in options, bench name is already provided as the first argument",
);
}
benchDesc = {
...defaults,
...optionsOrFn,
fn: maybeFn,
name: nameOrFnOrOptions,
};
}
} else if (typeof nameOrFnOrOptions === "function") {
if (!nameOrFnOrOptions.name) {
throw new TypeError("The bench function must have a name");View on GitHub (pinned to 9ad36f7a2c)
Solutions
- Re-save the file as UTF-8: `iconv -f WINDOWS-1252 -t UTF-8 file.ts > file.fixed && mv file.fixed file.ts` (or use the editor's 'Save with Encoding')
- If the file is binary or a data fixture, exclude it via `"fmt": { "exclude": [...] }` in deno.json or `--ignore`
- Verify with `file file.ts` / a UTF-8 validator which files are invalid before re-running fmt
Example fix
# before $ deno fmt data/legacy.ts # Error reading .../legacy.ts: ... is not a valid UTF-8 file # after $ iconv -f WINDOWS-1252 -t UTF-8 data/legacy.ts > /tmp/fixed.ts && mv /tmp/fixed.ts data/legacy.ts $ deno fmt # ok
Defensive patterns
Strategy: validation
Validate before calling
// Pre-check every file fmt will touch decodes as strict UTF-8
for (const f of files) {
const bytes = await Deno.readFile(f);
try {
new TextDecoder("utf-8", { fatal: true }).decode(bytes);
} catch {
console.error(`not UTF-8: ${f} — convert or exclude it`);
}
} Prevention
- Save sources as UTF-8 (no BOM needed) and set editor encoding accordingly
- Keep binary/data files out of directories fmt scans, or add them to fmt exclude
- After converting legacy-encoded files, validate with `iconv -t UTF-8 file > /dev/null` before committing
When it happens
Trigger: A file matched by the formatter glob contains invalid UTF-8: a binary file with a source-like extension, or a text file saved in a legacy single-byte encoding (Latin-1, Windows-1252, Shift-JIS) without a BOM/charset declaration, so charset detection falls through to UTF-8 and decoding fails.
Common situations: Files created by Windows tools in ANSI encoding; CSV/data fixtures with non-UTF8 bytes accidentally matching fmt includes; a binary accidentally named `.json`/`.ts`; files corrupted by a bad patch or transfer.
Related errors
- Failed to read from stdin
- Missing bench function
- failed to validate startup order {}: {error}
- The bench name can't be empty
- TypeError [ERR_INVALID_OPT_VALUE_ENCODING]: The value "${enc
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/c806e30335e85921.
Report an issue: GitHub.