nikivdev/code · warning

invalid policy path {}

Error message

invalid policy path {}

What it means

init_policy_file resolves the gitignore policy file path and needs its parent directory to create it; if the path has no parent component (e.g. a bare relative path resolving to the root), it throws this error. It guards against calling fs::create_dir_all with an empty/invalid parent.

Source

Thrown at src/gitignore_policy.rs:267

                v.line,
                v.entry,
                v.blocked_pattern
            );
        }
    }

    if apply_fix {
        bail!("Some blocked entries remain; review output above")
    } else {
        bail!("Found blocked personal-tooling entries")
    }
}

fn init_policy_file(opts: GitignorePolicyInitOpts) -> Result<()> {
    let path = policy_path();
    let parent = path
        .parent()
        .ok_or_else(|| anyhow::anyhow!("invalid policy path {}", path.display()))?;
    fs::create_dir_all(parent).with_context(|| format!("failed to create {}", parent.display()))?;

    if path.exists() && !opts.force {
        bail!(
            "{} already exists (use --force to overwrite)",
            path.display()
        );
    }

    fs::write(&path, default_policy_template())
        .with_context(|| format!("failed to write {}", path.display()))?;
    println!("Wrote {}", path.display());
    Ok(())
}

fn setup_global_gitignore(print_only: bool) -> Result<()> {
    let policy = load_policy();
    let target = resolve_global_excludes_path()?;

View on GitHub (pinned to a747e741ae)

Solutions

  1. Set a proper policy path like .gitignore-policy/config.toml with a parent directory
  2. Fix HOME / relevant env var so policy_path() resolves under a real home dir
  3. Pass a path with at least one directory component

Example fix

// before
let path = policy_path(); // resolved to "/"
// after
let path = policy_path();
assert!(path.parent().is_some(), "policy path needs a parent dir: {}", path.display());
Defensive patterns

Strategy: validation

Validate before calling

let path = policy_path();
if path.parent().is_none() {
    anyhow::bail!("policy path must include a parent dir, got {}", path.display());
}

Type guard

fn has_parent(p: &std::path::Path) -> bool { p.parent().is_some() }

Try / catch

if let Err(e) = init_policy_file(opts) {
    if e.to_string().starts_with("invalid policy path") {
        eprintln!("fix policy path config / HOME env");
    } else { return Err(e); }
}

Prevention

When it happens

Trigger: Running the gitignore-policy init command when policy_path() returns a path whose .parent() is None — e.g. path configured as "/" or a degenerate relative path.

Common situations: Misconfigured HOME/env so the policy path collapses to a root-only path; overriding the policy path to "/" or empty in config or environment.

Related errors


AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01). Data as JSON: /api/errors/5366b7cb68a6ceec. Report an issue: GitHub.