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

  1. 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')
  2. If the file is binary or a data fixture, exclude it via `"fmt": { "exclude": [...] }` in deno.json or `--ignore`
  3. 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

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


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/c806e30335e85921. Report an issue: GitHub.