Merge pull request #241 from davidmonterocrespo24/fix/littlefs-utf8-truncation

fix(micropython): write LittleFS files with UTF-8 byte length
This commit is contained in:
David Montero Crespo 2026-06-12 23:11:04 -03:00 committed by GitHub
commit 6345ba5fab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 1 deletions

View File

@ -157,7 +157,13 @@ export async function loadUserFiles(
for (const file of files) {
const fileName = file.name;
const content = file.content;
writeFile(lfsInstance, fileName, content, content.length);
// cwrap marshals `content` to the WASM heap as UTF-8, so the byte count we
// pass must be the UTF-8 length, NOT content.length (UTF-16 code units).
// A multi-byte char (e.g. an em-dash in a comment) makes UTF-8 longer, and
// passing the shorter content.length truncates the tail of the file —
// corrupting the final statement into a SyntaxError at EOF.
const byteLength = new TextEncoder().encode(content).length;
writeFile(lfsInstance, fileName, content, byteLength);
}
// Unmount and free