Skip to content

fix(utils): don't cut a character in half in shortName - #12791

Open
kevin9327 wants to merge 1 commit into
meta:mainfrom
kevin9327:fix/shortname-char-boundary
Open

kevin9327 wants to merge 1 commit into
meta:mainfrom
kevin9327:fix/shortname-char-boundary

Conversation

@kevin9327

@kevin9327 kevin9327 commented Oct 28, 2026 •

Copy link
Copy Markdown

Pre-flight checklist

  • I have read the Contributing Guidelines on pull requests.
  • If this is a code change: I have written unit tests and/or added dogfooding pages to fully verify the new behavior.
  • If this is a new API or substantial change: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan.

Motivation

shortName (used by docuHash when a generated file name is too long) truncates at a fixed position without looking at character boundaries:

  • On Linux (byte limit), it slices the UTF-8 buffer at byte 244 and decodes it. When that cuts a multi-byte character, the decoded name ends in U+FFFD. The existing test pins this for a long run of 字, with a comment calling it "pretty bad (a character clipped in half)". A long CJK title produces such a route/file name in practice.
  • On macOS/Windows (UTF-16 code unit limit), it slices at code unit 244. When that lands inside a surrogate pair (an emoji, for example), the name ends in a lone high surrogate.

This change backs off to the previous character boundary in both cases, so the name drops the character that doesn't fit whole instead of keeping a broken half. Names still fit the same limits, and nothing changes for names that don't cut a character.

Test Plan

packages/docusaurus-utils/src/__tests__/pathUtils.test.ts:

  • The xfs expectation for the long 字 name no longer ends with �. It drops the 82nd character, which doesn't fit whole.
  • New test does not cut a character in half: '字'.repeat(100) on Linux gives '字'.repeat(81), and `a${'😀'.repeat(200)}` on macOS gives `a${'😀'.repeat(121)}` without a trailing lone surrogate.

Without the source change both tests fail (Received: "字…字�" and "a😀…😀\ud83d"). With it, all 12 tests in the file pass (vitest 5). Formatted with oxfmt.

Related issues/PRs

None found.


I used an AI assistant to help write this change. I reproduced the bug, ran the tests and reviewed the diff myself.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed path-name truncation so it preserves complete emoji and other Unicode characters across platforms, avoiding replacement characters.

@meta-cla meta-cla Bot added the CLA Signed Signed Facebook CLA label Oct 28, 2026
@coderabbitai

coderabbitai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 593bf371-cf55-4f4c-8ba2-b4062589a820
Commits

Reviewing files that changed from the base of the PR and between 4e6fe61 and c4bc677.

Files selected for processing: 2
  • packages/docusaurus-utils/src/__tests__/pathUtils.test.ts
  • packages/docusaurus-utils/src/pathUtils.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

shortName now adjusts truncation boundaries to avoid retaining partial Unicode characters. Tests cover emoji and Han characters on Darwin and Android.

Changes: Unicode-safe path truncation

Layer / File(s) Summary
Truncation boundaries and coverage
packages/docusaurus-utils/src/pathUtils.ts, packages/docusaurus-utils/src/__tests__/pathUtils.test.ts
On macOS and Windows, shortName removes a trailing high surrogate from the retained prefix. On other platforms, it backs up over UTF-8 continuation bytes before decoding. Tests check the retained emoji and Han characters and verify that the result contains no \uFFFD.

Priority: Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: josh-cena

Merge Risk: Minimal · up to c4bc6

The change adjusts Unicode truncation boundaries. No merge-blocking issue is evident.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: preventing shortName from splitting Unicode characters during truncation.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 2 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@netlify

netlify Bot commented Oct 28, 2026

Copy link
Copy Markdown

✅ [V2]

Built without sensitive environment variables

Name Link
🔨 Latest commit c4bc677
🔍 Latest deploy log https://app.netlify.com/projects/docusaurus-2/deploys/6ae194354e468318502ad8c7
😎 Deploy Preview https://deploy-preview-12791--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

This branch has not been deployed

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

Labels

CLA Signed Signed Facebook CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant