sharp icon indicating copy to clipboard operation
sharp copied to clipboard

Sharp write error with Chinese username on Windows when using .toBuffer()or .toFile()

Open Tithy opened this issue 3 months ago • 5 comments

Problem Description We're encountering a write error when using Sharp for image processing on Windows systems (both Win10 and Win11) where the username contains Chinese characters.

Tithy avatar Nov 06 '25 08:11 Tithy

Error: VipsImage: unable to write to "C:\Users\�����˻�\AppData\Local\Temp\vips-0-3148017085.v" windows error: �豸��ʶ������

Note: The garbled characters in the error message are likely Chinese text that got encoding issues. Based on common Windows errors, �豸��ʶ������probably translates to "Device not ready" or "Device not recognized".

Environment Platform: Windows 10 & Windows 11

Node.js: 16.14.2

Sharp: 0.30.2

Electron: 19.1.9

User Profile: Contains Chinese characters in the username

// Approach 1: Using .toBuffer() const data = await sharp(filePath, { limitInputPixels: false, }) .rotate(rotate) .extract(cropAction) .resize(sizeW, sizeH) .jpeg({ quality: 100 }) .withMetadata(metaData) .toBuffer(); await fs.writeFile(tmpFile, data);

// Approach 2: Using .toFile() await sharp(filePath, { limitInputPixels: false, }) .rotate(rotate) .extract(cropAction) .resize(sizeW, sizeH) .jpeg({ quality: 100 }) .withMetadata(metaData) .toFile(tmpFile);

Tithy avatar Nov 06 '25 08:11 Tithy

Sharp: 0.30.2

Please upgrade to the latest version.

lovell avatar Nov 06 '25 09:11 lovell

Upstream PR https://github.com/libvips/libvips/pull/4721 should fix this. As a workaround for now, you can set the TMPDIR environment variable.

kleisauke avatar Nov 06 '25 11:11 kleisauke

Thank you for confirming the issue in #4470. We've implemented the suggested workaround of setting TMPDIR to an ASCII-only path, which resolves the issue.

Thanks for your great work on sharp!

Tithy avatar Nov 06 '25 14:11 Tithy

Note that the TMPDIR environment variable is not limited to ASCII characters. It supports Unicode paths as well as system variables such as %LocalAppData%\Temp or %Temp% (which both expand to C:\Users\username\AppData\Local\Temp). See for example:

$ C:\Users\☃\Downloads\vips-dev-w64-web-8.17.3-static\vips-dev-8.17\bin\vips.exe rotate st-francis.jpg x.jpg 90 --vips-progress
vips.exe temp-24: 26319 x 30000 pixels, 4 threads, 128 x 128 tiles, 128 lines in buffer
vips.exe temp-24: done in 0,0219s
VipsImage: unable to write to "C:\Users\?\AppData\Local\Temp\vips-0-1593240234.v"
system error: Invalid argument
$ set TMPDIR=%Temp%
$ C:\Users\☃\Downloads\vips-dev-w64-web-8.17.3-static\vips-dev-8.17\bin\vips.exe rotate st-francis.jpg x.jpg 90 --vips-progress
vips.exe temp-24: 26319 x 30000 pixels, 4 threads, 128 x 128 tiles, 128 lines in buffer
vips.exe st-francis.jpg: 30000 x 26319 pixels, 4 threads, 30000 x 16 tiles, 128 lines in buffer
vips.exe st-francis.jpg: done in 11,6s
vips.exe temp-24: done in 216s

kleisauke avatar Nov 07 '25 12:11 kleisauke