Skip to content

[Bug]: Files viewer rejects GBK / GB18030 text files ('This file is not UTF-8 text') #43192

Done

Description

@ethan-blue

Before submitting

  • I searched existing issues and did not find a duplicate.
  • I included enough detail to reproduce or investigate the problem.

Area

packages/contracts or packages/shared

Steps to reproduce

  1. Create a GBK / GB18030 / GB2312 encoded text or source file (with no NUL bytes) in a project workspace:
    python -c 'open("main.c", "wb").write("// 中文注释\nint a = 1;\n".encode("gbk"))'
  2. Open the project in T3 Code (verified on 0.1.13-nightly.20261125.5258, which already includes the Diff panel fix from fix(server): diff panel reads GBK/GB18030 files instead of showing mojibake #42735).
  3. Open main.c in the Files Viewer (either from the workspace file tree or by clicking a file link in a message).

Expected behavior

The Files Viewer should decode and display readable Chinese text (for example by falling back to gb18030 when strict UTF-8 decoding fails, consistent with #42735 for the Diff panel and #42827 for RTF previews), while keeping valid UTF-8 files (including files truncated at FILE_TEXT_PREVIEW_MAX_BYTES) decoded as UTF-8.

Actual behavior

The Files Viewer fails to render the file and shows:

This file is not UTF-8 text. Open it in another app to view its contents.

In packages/shared/src/filePreview.ts (decodeFilePreviewText, lines 171–184):

const decoder = new TextDecoder("utf-8", { fatal: true });
try {
  return {
    text: decoder.decode(bounded, { stream: truncated || bytes.length > bounded.length }),
    truncated: truncated || bytes.length > bounded.length,
  };
} catch {
  throw new Error("This file is not UTF-8 text. Open it in another app to view its contents.");
}

Because decodeFilePreviewText only tries strict utf-8 and throws immediately on error, non-UTF-8 CJK text files (such as GBK/GB18030 .c, .h, .txt, .csv files, and text entries inside zip previews) cannot be opened in the Files Viewer even though the Diff panel (#42735) and RTF preview (#42827) now handle GBK/GB18030.

Impact

Major degradation or frequent failure

Version or commit

0.1.13-nightly.20261125.5258 (also verified on main: packages/shared/src/filePreview.ts)

Environment

Windows 11, T3 Code Desktop 0.1.13-nightly.20261125.5258

Activity

  1. 2SinceJuly commented on Nov 26, 2026

    @2SinceJuly
    Contributor

    I'm on it. decodeFilePreviewText only attempts strict UTF-8, so it throws before any fallback; I'll try a GB18030 decode when strict UTF-8 fails (same approach as #42735), while keeping UTF-8 precedence and the binary/NUL check intact.

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

Metadata

Metadata

Assignees

No one assigned

    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