-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(file): remove jschardet confidence check for encoding detection #1007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
72b27e4
fix(file): remove jschardet confidence check for encoding detection
yamadashy c4354e7
fix(file): improve U+FFFD detection for UTF-8 encoding
yamadashy 0604b7e
fix(deps): downgrade isbinaryfile to v5.0.2 for Node.js 20+ support
yamadashy 47398ae
test(file): Add test for legitimate U+FFFD character handling
yamadashy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import * as fs from 'node:fs/promises'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
| import { afterEach, beforeEach, describe, expect, test } from 'vitest'; | ||
| import { readRawFile } from '../../../src/core/file/fileRead.js'; | ||
|
|
||
| describe('readRawFile', () => { | ||
| let testDir: string; | ||
|
|
||
| beforeEach(async () => { | ||
| testDir = await fs.mkdtemp(path.join(os.tmpdir(), 'repomix-fileRead-')); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await fs.rm(testDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| test('should read normal text file successfully', async () => { | ||
| const filePath = path.join(testDir, 'normal.txt'); | ||
| const content = 'Hello World'; | ||
| await fs.writeFile(filePath, content, 'utf-8'); | ||
|
|
||
| const result = await readRawFile(filePath, 1024); | ||
|
|
||
| expect(result.content).toBe(content); | ||
| expect(result.skippedReason).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('should read file with low jschardet confidence (Issue #869)', async () => { | ||
| // This tests that files with low confidence scores from jschardet | ||
| // are NOT skipped if they contain valid UTF-8 content | ||
| const filePath = path.join(testDir, 'server.py'); | ||
| const content = `import json | ||
| import time | ||
| import uuid | ||
|
|
||
| def hello(): | ||
| print("Hello, World!") | ||
| `; | ||
| await fs.writeFile(filePath, content, 'utf-8'); | ||
|
|
||
| const result = await readRawFile(filePath, 1024 * 1024); | ||
|
|
||
| expect(result.content).toBe(content); | ||
| expect(result.skippedReason).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('should read HTML file with Thymeleaf syntax (Issue #847)', async () => { | ||
| // This tests that HTML files with special syntax like Thymeleaf (~{}) | ||
| // are NOT skipped even if jschardet returns low confidence | ||
| const filePath = path.join(testDir, 'thymeleaf.html'); | ||
| const content = '<html lang="en" xmlns:th="http://www.thymeleaf.org" layout:decorate="~{layouts/default}"></html>'; | ||
| await fs.writeFile(filePath, content, 'utf-8'); | ||
|
|
||
| const result = await readRawFile(filePath, 1024); | ||
|
|
||
| expect(result.content).toBe(content); | ||
| expect(result.skippedReason).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('should read empty file successfully', async () => { | ||
| // Empty files should not be skipped (jschardet may return 0 confidence for empty files) | ||
| const filePath = path.join(testDir, '__init__.py'); | ||
| await fs.writeFile(filePath, '', 'utf-8'); | ||
|
|
||
| const result = await readRawFile(filePath, 1024); | ||
|
|
||
| expect(result.content).toBe(''); | ||
| expect(result.skippedReason).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('should skip file with actual decode errors (U+FFFD)', async () => { | ||
| const filePath = path.join(testDir, 'invalid.txt'); | ||
| // Create a file with a UTF-8 BOM followed by valid text and invalid UTF-8 sequences | ||
| // The BOM forces UTF-8 detection, and the invalid sequence will produce U+FFFD | ||
| const utf8Bom = Buffer.from([0xef, 0xbb, 0xbf]); // UTF-8 BOM | ||
| const validText = 'Hello World\n'.repeat(50); | ||
| // Invalid UTF-8: 0x80 is a continuation byte without a leading byte | ||
| const invalidSequence = Buffer.from([0x80, 0x81, 0x82]); | ||
| const buffer = Buffer.concat([utf8Bom, Buffer.from(validText), invalidSequence, Buffer.from(validText)]); | ||
| await fs.writeFile(filePath, buffer); | ||
|
|
||
| const result = await readRawFile(filePath, 1024 * 1024); | ||
|
|
||
| expect(result.content).toBeNull(); | ||
| expect(result.skippedReason).toBe('encoding-error'); | ||
| }); | ||
|
|
||
| test('should skip file if it exceeds size limit', async () => { | ||
| const filePath = path.join(testDir, 'large.txt'); | ||
| const content = 'x'.repeat(1000); | ||
| await fs.writeFile(filePath, content, 'utf-8'); | ||
|
|
||
| const result = await readRawFile(filePath, 100); | ||
|
|
||
| expect(result.content).toBeNull(); | ||
| expect(result.skippedReason).toBe('size-limit'); | ||
| }); | ||
|
|
||
| test('should skip binary file by extension', async () => { | ||
| const filePath = path.join(testDir, 'test.jpg'); | ||
| const binaryData = Buffer.from([0xff, 0xd8, 0xff, 0xe0]); | ||
| await fs.writeFile(filePath, binaryData); | ||
|
|
||
| const result = await readRawFile(filePath, 1024); | ||
|
|
||
| expect(result.content).toBeNull(); | ||
| expect(result.skippedReason).toBe('binary-extension'); | ||
| }); | ||
|
|
||
| test('should skip binary content in text extension file', async () => { | ||
| const filePath = path.join(testDir, 'binary.txt'); | ||
| // Create file with binary content (null bytes and control characters) | ||
| const binaryData = Buffer.alloc(256); | ||
| for (let i = 0; i < 256; i++) { | ||
| binaryData[i] = i; | ||
| } | ||
| await fs.writeFile(filePath, binaryData); | ||
|
|
||
| const result = await readRawFile(filePath, 1024); | ||
|
|
||
| expect(result.content).toBeNull(); | ||
| expect(result.skippedReason).toBe('binary-content'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.