Skip to content

bug(util): truncateMiddle returns full string when maxLength <= 2 due to slice(-0) #20880

Closed as not planned

Description

@MilesQLi

Description

truncateMiddle in packages/util/src/path.ts (and the identical implementation in packages/opencode/src/util/locale.ts) has an edge case bug when maxLength is 1 or 2.

When maxLength <= 2, end = Math.floor((maxLength - 1) / 2) evaluates to 0. The function then calls text.slice(-0), which in JavaScript is equivalent to text.slice(0) — returning the entire string rather than an empty suffix. The output therefore far exceeds maxLength.

Steps to reproduce

import { truncateMiddle } from "@opencode/util/path"

truncateMiddle("abcdefghijklmnop", 2)
// Expected: "a…"  (2 chars)
// Actual:   "a…abcdefghijklmnop"  (18 chars)

truncateMiddle("abcdefghijklmnop", 1)
// Expected: "…"   (1 char)
// Actual:   "…abcdefghijklmnop"  (17 chars)

Root cause

-0 === 0 in JavaScript, and String.prototype.slice treats -0 as 0, returning the full string.

Fix: guard the suffix slice — end > 0 ? text.slice(-end) : "".

The same bug is present in Locale.truncateMiddle in packages/opencode/src/util/locale.ts.

Activity

  1. added
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on Apr 3, 2026
  2. removed
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on May 3, 2026
  3. github-actions commented on Jul 3, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions