JuliusBrussee/caveman · error · Error

contains an invalid path character

Error message

${name} contains an invalid path character

What it means

Kilo receives config paths from environment variables and passes them directly to its loader, so the CLI pre-validates them. This error is thrown when KILO_CONFIG_DIR or KILO_CONFIG contains NUL, carriage return, or newline characters, which are invalid in filesystem paths and can silently truncate or corrupt downstream path handling.

Solutions

  1. Inspect the variable: `printf '%s' "$KILO_CONFIG" | od -c` to find stray \n or \r, then re-export a clean value
  2. Use quotes and strip newlines: `export KILO_CONFIG="$(command | tr -d '\r\n')"`
  3. Fix the generator (script, env file, direnv config) so it emits a single-line absolute path

Example fix

// before
export KILO_CONFIG=$(op path)
// after
export KILO_CONFIG="$(op path | tr -d '\r\n')"
Defensive patterns

Strategy: validation

Validate before calling

function assertCleanPath(name: string, value?: string) {
  if (value === undefined) return;
  if (/[\0\r\n]/.test(value)) throw new Error(`${name} has newline/NUL; re-export a clean single-line path`);
}
assertCleanPath("KILO_CONFIG", process.env.KILO_CONFIG);
assertCleanPath("KILO_CONFIG_DIR", process.env.KILO_CONFIG_DIR);

Type guard

function isCleanPathValue(v: unknown): v is string {
  return typeof v === "string" && v.length > 0 && !/[\0\r\n]/.test(v);
}

Try / catch

try {
  await kiloCommand();
} catch (e) {
  if ((e as Error).message.includes("invalid path character")) {
    console.error("Fix the env var: export KILO_CONFIG=\"$(printf %s $KILO_CONFIG)\"");
  } else throw e;
}

Prevention

When it happens

Trigger: Exporting KILO_CONFIG or KILO_CONFIG_DIR with an embedded newline (common when paths are produced by command substitution or copied from terminal output containing line breaks), or a NUL byte from binary-corrupted env sources.

Common situations: `export KILO_CONFIG=$(some_command)` where the command outputs a trailing newline captured into the value; YAML/JSON-generated env files with wrapped lines; Windows-to-Unix path migrations leaving stray CR characters.

Understand the failure class

Background: "is not a valid" / "Invalid ... value" environment variable errors: how libraries validate env vars and what to do when they reject yours — this error's family across 48 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/df768154ab7215d2. Report an issue: GitHub.

Appendix: source

Thrown at packages/cli/src/index.ts:11128

  if (!(keyPath[keyPath.length - 1]! in cur)) return true;
  delete cur[keyPath[keyPath.length - 1]!];
  try {
    writeFileSync(path, JSON.stringify(root, null, 2) + "\n");
    return true;
  } catch (e) {
    console.error(`${mark("warn")} cannot write ${path}: ${(e as Error).message}`);
    return false;
  }
}

type KiloConfigLocation = {
  path: string;
  jsonc: boolean;
  source: "KILO_CONFIG_DIR" | "KILO_CONFIG" | "XDG_CONFIG_HOME";
};

function kiloPathFromEnv(value: string, name: string): string {
  if (/[\0\r\n]/.test(value)) throw new Error(`${name} contains an invalid path character`);
  // Kilo passes env paths directly to its loader. Shell-style "~/" expansion
  // does not occur inside an environment value; relative paths resolve from cwd.
  return normalize(isAbsolute(value) ? value : resolve(value));
}

function kiloConfigInDirectory(directory: string, source: KiloConfigLocation["source"]): KiloConfigLocation {
  // Mirror pinned Kilo's native global writer: prefer an existing canonical
  // config in this order, otherwise create kilo.json. JSONC stays read-only here
  // because a JSON stringify would destroy comments.
  const candidates = ["kilo.jsonc", "kilo.json", "opencode.jsonc", "opencode.json"];
  const selected = candidates.map((name) => join(directory, name)).find((path) => existsSync(path))
    ?? join(directory, "kilo.json");
  return { path: selected, jsonc: extname(selected).toLowerCase() === ".jsonc", source };
}

// Kilo 7.5.6's native config writer targets KILO_CONFIG_DIR when present. That
// directory is also loaded after KILO_CONFIG, so it must win for effective MCP
// ownership. Explicit KILO_CONFIG wins only without a custom config directory;

View on GitHub (pinned to 3ee70a1026)