denoland/deno · error

{ENV_VAR}: failed to open {} for append: {e}

Error message

{ENV_VAR}: failed to open {} for append: {e}

What it means

deno_core can append the JS module import graph as JSONL to the file named by DENO_SNAPSHOT_IMPORT_GRAPH — an opt-in instrumentation knob for snapshot/startup analysis. The first time an entry is written, the file is opened for append; if that open fails (missing parent directory, permission denied), the process panics with the env var name, the offending path, and the underlying OS error.

Source

Thrown at libs/core/modules/import_graph.rs:59

const ENV_VAR: &str = "DENO_SNAPSHOT_IMPORT_GRAPH";
const STDERR_ENV_VAR: &str = "DENO_LOG_LAZY_LOAD";

struct Writer {
  inner: Mutex<BufWriter<File>>,
}

fn writer() -> Option<&'static Writer> {
  static WRITER: OnceLock<Option<Writer>> = OnceLock::new();
  WRITER
    .get_or_init(|| {
      let path = std::env::var_os(ENV_VAR)?;
      let file = OpenOptions::new()
        .create(true)
        .append(true)
        .open(&path)
        .unwrap_or_else(|e| {
          panic!(
            "{ENV_VAR}: failed to open {} for append: {e}",
            std::path::Path::new(&path).display()
          )
        });
      Some(Writer {
        inner: Mutex::new(BufWriter::new(file)),
      })
    })
    .as_ref()
}

pub(crate) fn is_enabled() -> bool {
  writer().is_some()
}

fn stderr_log_enabled() -> bool {
  static ENABLED: OnceLock<bool> = OnceLock::new();
  *ENABLED.get_or_init(|| {

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Create the parent directory: `mkdir -p` the dir containing the path you set
  2. Point the env var at an existing writable directory (e.g. under /tmp)
  3. Pre-verify with a quick `touch <path>` as the same user that runs Deno
  4. Unset the variable if you no longer need the instrumentation

Example fix

# before
DENO_SNAPSHOT_IMPORT_GRAPH=/nonexistent/graph.jsonl deno run app.ts

# after
mkdir -p /tmp/graph
DENO_SNAPSHOT_IMPORT_GRAPH=/tmp/graph/graph.jsonl deno run app.ts
Defensive patterns

Strategy: validation

Validate before calling

# create and probe the output file before launching deno
if [ -n "$DENO_SNAPSHOT_IMPORT_GRAPH" ]; then
  mkdir -p "$(dirname "$DENO_SNAPSHOT_IMPORT_GRAPH")"
  : >> "$DENO_SNAPSHOT_IMPORT_GRAPH" || exit 1
fi
deno run app.ts

Prevention

When it happens

Trigger: Launching any Deno command with DENO_SNAPSHOT_IMPORT_GRAPH pointing at a path whose parent directory does not exist or is not writable — e.g. `DENO_SNAPSHOT_IMPORT_GRAPH=/tmp/graph/out.jsonl` before creating /tmp/graph, or a read-only location.

Common situations: CI scripts adding the knob with an uncreated output directory; local debugging after the output dir was cleaned; typos in the env var value.

Related errors


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