Sharp write error with Chinese username on Windows when using .toBuffer()or .toFile()
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.
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);
Sharp: 0.30.2
Please upgrade to the latest version.
Upstream PR https://github.com/libvips/libvips/pull/4721 should fix this. As a workaround for now, you can set the TMPDIR environment variable.
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!
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