denoland/deno · error · anyhow::Error

Invalid file path

Error message

Invalid file path '{}'

What it means

specifier_from_file_path (cli/util/fs.rs:175-179) is a shared CLI helper that turns a filesystem path into a file:// ModuleSpecifier via Url::from_file_path, which accepts only absolute, well-formed paths. It backs collect_specifiers — file discovery for deno test/lint/doc (fs.rs:115) — and the linter's explicit file list (linter.rs:130). The error means one of those paths could not become a valid absolute file URL.

Solutions

  1. Re-run with concrete, existing file paths (relative to cwd or absolute).
  2. Inspect the path in the message for stray quotes, spaces glued to names, or empty strings from a variable expansion.
  3. Filter your file list before invoking deno (exclude non-files / odd names) when driving it from scripts.

Example fix

# before — glob leaked a malformed entry
$ deno lint $(cat files.txt)
error: Invalid file path 'src/"mod.ts'

# after — validate each entry exists first
$ deno lint $(while read -r f; do [ -f "$f" ] && printf '%s\n' "$f"; done < files.txt)
Defensive patterns

Strategy: validation

Validate before calling

// Filter a candidate list down to real, absolute-or-cwd-relative files
const files = list.map((p) => p.trim()).filter((p) => p.length > 0);
for (const f of files) {
  const abs = new URL(f, import.meta.resolve("./")).pathname; // fails loudly on junk
  if (!Deno.statSync(abs).isFile) throw new Error(`not a file: ${f}`);
}

Type guard

const isUsablePath = (p: string) =>
  p.length > 0 && !/[\u0000]/.test(p) && !p.endsWith("/") && !p.endsWith("..");

Prevention

When it happens

Trigger: Passing a path argument that is not usable as an absolute file URL: an empty path, a relative path that escaped cwd-joining, or OS-specific malformed paths (e.g. paths with characters Windows rejects).

Common situations: Scripted invocations feeding deno lint/test unfiltered glob output; a file created with a name containing stray control characters; arguments mangled by shell interpolation or CI matrix templating.

Related errors


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

Appendix: source

Thrown at cli/util/fs.rs:179

/// Gets the total size (in bytes) of a directory.
pub fn dir_size(path: &Path) -> std::io::Result<u64> {
  let entries = std::fs::read_dir(path)?;
  let mut total = 0;
  for entry in entries {
    let entry = entry?;
    total += match entry.metadata()? {
      data if data.is_dir() => dir_size(&entry.path())?,
      data => data.len(),
    };
  }
  Ok(total)
}

pub fn specifier_from_file_path(
  path: &Path,
) -> Result<ModuleSpecifier, AnyError> {
  ModuleSpecifier::from_file_path(path)
    .map_err(|_| anyhow!("Invalid file path '{}'", path.display()))
}

#[derive(Default)]
pub struct FsCleaner {
  pub files_removed: u64,
  pub dirs_removed: u64,
  pub bytes_removed: u64,
  /// Paths that could not be removed, along with the error encountered. The
  /// cleaner is best-effort: a single locked or read-only file (for example a
  /// cache database held open by a running Deno process) should not abort the
  /// entire clean, so failures are collected here and reported afterwards.
  pub failed: Vec<(PathBuf, std::io::Error)>,
  pub progress_guard: Option<UpdateGuard>,
}

impl FsCleaner {
  pub fn new(progress_guard: Option<UpdateGuard>) -> Self {
    Self {

View on GitHub (pinned to 9ad36f7a2c)