bun/no-literal-temp-dir

Disallow hard-coded temporary directories such as /tmp in favor of os.tmpdir()

Windows has no /tmp, and on other systems TMPDIR can point somewhere else, as it does for every user on macOS. A path that starts with /tmp ignores that. os.tmpdir() returns the directory that the system wants you to use.

The rule reports a string or a template that's one of the directories or starts with one followed by /. Text and attribute strings in JSX aren't checked.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"bun/no-literal-temp-dir" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"bun/no-literal-temp-dir" = ["error", {
  directories = ["/tmp", "/var/tmp"],
}]

Examples#

example.js2 problems
import { writeFileSync } from "node:fs";

writeFileSync("/tmp/report.json", JSON.stringify(report));
writeFileSync(`/var/tmp/${name}.log`, lines.join("\n"));
example.jsNo problems
import { writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

writeFileSync(join(tmpdir(), "report.json"), JSON.stringify(report));

A name that only begins with the same letters is fine:

example.jsNo problems
const template = "/tmpl/email.html";

Options#

OptionDefaultWhat it does
directories["/tmp", "/var/tmp"]The directories to report. It replaces the default list.
example.jswith { "directories": ["/tmp", "/var/tmp", "/private/tmp"] }1 problem
const socket = "/private/tmp/worker.sock";