denoland/deno · error

Unexpected termination of string.

Error message

Unexpected termination of string.

What it means

While scanning a parenthesized pattern, cli/lsp/path_to_regex.rs:156 pushes a backslash then requires one more character for the escape pair. If the input ends right after that backslash, chars.next() returns None and the lexer reports "Unexpected termination of string." — the regex pattern was cut off mid-escape.

Source

Thrown at cli/lsp/path_to_regex.rs:156

        if chars.peek() == Some(&'?') {
          return Err(anyhow!(
            "Pattern cannot start with \"?\" at {}.",
            index + 1
          ));
        }

        loop {
          let next_char = chars.peek();
          if next_char.is_none() {
            break;
          }
          if next_char == Some(&'\\') {
            pattern.push(chars.next().unwrap());
            pattern.push(
              chars
                .next()
                .ok_or_else(|| anyhow!("Unexpected termination of string."))?,
            );
            continue;
          }
          if next_char == Some(&')') {
            count -= 1;
            if count == 0 {
              chars.next();
              break;
            }
          } else if next_char == Some(&'(') {
            count += 1;
            pattern.push(chars.next().unwrap());
            if chars.peek() != Some(&'?') {
              return Err(anyhow!(
                "Capturing groups are not allowed at {}.",
                index + pattern.len()
              ));
            }

View on GitHub (pinned to 89f33cbef2)

Solutions

  1. Close the pattern properly and escape backslashes in pairs: "/x/:pkg([a-z\\.]+)"
  2. In JSON source, write "\\\\" for each literal backslash you want in the regex
  3. Validate the schema with a path-to-regex parser before shipping

Example fix

// before
{ "schema": "/x/:pkg([a-z+\\" }

// after
{ "schema": "/x/:pkg([a-z.]+)" }
Defensive patterns

Strategy: validation

Validate before calling

function endsWithDanglingEscapeInsidePattern(s: string): boolean {
  const open = s.lastIndexOf("(");
  if (open === -1) return false;
  const tail = s.slice(open);
  return !tail.includes(")") && endsWithDanglingEscape(tail);
}

Prevention

When it happens

Trigger: A schema like "/x/:pkg([a-z+\\" where the pattern's closing ')' is missing and the string ends on a lone backslash; truncated config.json payloads.

Common situations: Under-escaped JSON (a planned "\\\\" becoming "\\"), string truncation from template length limits, or hand-merging schema edits that deleted the tail of the pattern.

Related errors


AI-assisted analysis of denoland/deno@89f33cbef2 (2026-08-16). Data as JSON: /api/errors/0ae89bac5102bc4e. Report an issue: GitHub.