lovell / lovell/sharp

Reading an empty file results in a confusing "Input file contains unsupported image format" error

Open
#4,312 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

blocked-upstream-dependency enhancement
Dominant language
JavaScript
Stars
32.7k
Forks
1.4k
Avg merge
1d 14h
Merged PRs (30d)
5

Description

## Possible bug

### Is this a possible bug in a feature of sharp, unrelated to installation?

- [x] Running `npm install sharp` completes without error.
- [x] Running `node -e "require('sharp')"` completes without error.

If you cannot confirm both of these, please open an [installation issue](https://github.com/lovell/sharp/issues/new?labels=installation&template=installation.md) instead.

### Are you using the latest version of sharp?

- [x] I am using the latest version of `sharp` as reported by `npm view sharp dist-tags.latest`.

If you cannot confirm this, please upgrade to the latest version and try again before opening an issue.

If you are using another package which depends on a version of `sharp` that is not the latest, please open an issue against that package instead.

### What is the output of running `npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp`?

```
System:
OS: macOS 14.4.1
CPU: (10) arm64 Apple M1 Max
Memory: 117.39 MB / 18.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.6.0 - ~/.local/state/fnm_multishells/85165_1734955094405/bin/node
npm: 10.8.2 - ~/.local/state/fnm_multishells/85165_1734955094405/bin/npm
npmPackages:
sharp: ^0.33.5 => 0.33.5
```

### Does this problem relate to file caching?

The default behaviour of libvips is to cache input files, which can lead to `EBUSY` or `EPERM` errors on Windows.
Use [`sharp.cache(false)`](https://sharp.pixelplumbing.com/api-utility#cache) to switch this feature off.

- [x] Adding `sharp.cache(false)` does not fix this problem.

### Does this problem relate to images appearing to have been rotated by 90 degrees?

Images that contain EXIF Orientation metadata are not auto-oriented. By default, EXIF metadata is removed.

- To auto-orient pixel values use the parameter-less [`rotate()`](https://sharp.pixelplumbing.com/api-operation#rotate) operation.
- To retain EXIF Orientation use [`keepExif()`](https://sharp.pixelplumbing.com/api-output#keepexif).

- [x] Using `rotate()` or `keepExif()` does not fix this problem.

### What are the steps to reproduce?

1. Create an empty image file `touch test.jpg`
2. Try reading it, e.g. by doing `sharp('test.jpg').toBuffer();`

File extension doesn't matter, I've tried `*.avif`, `*.png`, etc.

Result:

```
> node index.js
/Users/dmitry/Projects/sharp-empty-image/node_modules/sharp/lib/output.js:163
const stack = Error();
^

Error: Input file contains unsupported image format
at Sharp.toBuffer (/Users/dmitry/Projects/sharp-empty-image/node_modules/sharp/lib/output.js:163:17)
at Object. (/Users/dmitry/Projects/sharp-empty-image/index.js:3:19)
at Module._compile (node:internal/modules/cjs/loader:1546:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
at Module.load (node:internal/modules/cjs/loader:1317:32)
at Module._load (node:internal/modules/cjs/loader:1127:12)
at TracingChannel.traceSync (node:diagnostics_channel:315:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:166:5)
at node:internal/main/run_main_module:30:49
```

### What is the expected behaviour?

Some kind of error which explicitly tells that the file is empty.

### Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem

```js
const sharp = require('sharp');

// test.jpg must exists and be empty
sharp('test.jpg').toBuffer();
```

### Please provide sample image(s) that help explain this problem

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the empty-file case from the issue using `node index.js` and `sharp('test.jpg').toBuffer()`. Start at the reported `node_modules/sharp/lib/output.js:163` stack location, then identify the relevant input-error handling and add coverage for an explicitly empty-file error; done means the error distinguishes an empty file from an unsupported image format.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.