# Hugo crashes when there's a data file with no data in it

**URL:** https://discourse.gohugo.io/t/hugo-crashes-when-theres-a-data-file-with-no-data-in-it/33189
**Category:** support
**Created:** [June 1, 2021, 4:42pm UTC](https://discourse.gohugo.io/t/hugo-crashes-when-theres-a-data-file-with-no-data-in-it/33189 "2021-06-01T16:42:58Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![eazolan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/eazolan/32/14515_2.png) [@eazolan](https://discourse.gohugo.io/u/eazolan)
#### Post date: [June 1, 2021, 4:42pm UTC](https://discourse.gohugo.io/t/hugo-crashes-when-theres-a-data-file-with-no-data-in-it/33189/1 "2021-06-01T16:42:58Z")

</div>

The data files are auto generated by my MS SQL server. They’re output in JSON format, but if the SQL gets zero rows, an empty file is generated. 0 bytes.

Hugo server evaluates the file for proper JSON, and when it doesn’t find it, it dies.

Is there a non-strict setting for evaluating data files?

---

<div class="post-metadata">

### Author: ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)
#### Post date: [June 1, 2021, 5:17pm UTC](https://discourse.gohugo.io/t/hugo-crashes-when-theres-a-data-file-with-no-data-in-it/33189/2 "2021-06-01T17:17:02Z")

</div>

> [@eazolan](#):
>
> Is there a non-strict setting for evaluating data files?

You can use [`len`](https://gohugo.io/functions/len/#readout) to conditionally load the data file only if its content is not empty.

---

<div class="post-metadata">

### Author: ![eazolan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/eazolan/32/14515_2.png) [@eazolan](https://discourse.gohugo.io/u/eazolan)
#### Post date: [June 1, 2021, 6:21pm UTC](https://discourse.gohugo.io/t/hugo-crashes-when-theres-a-data-file-with-no-data-in-it/33189/3 "2021-06-01T18:21:09Z")

</div>

Maybe it’s just me. Try this in your working Hugo site.  
Shut down Hugo, create a 0 byte file in your data directory. “anything.json”  
Bring up Hugo server.  
Hugo crashes for me. And I haven’t even written any code to reference it.

---

<div class="post-metadata">

### Author: ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)
#### Post date: [June 1, 2021, 6:55pm UTC](https://discourse.gohugo.io/t/hugo-crashes-when-theres-a-data-file-with-no-data-in-it/33189/4 "2021-06-01T18:55:23Z")

</div>

> [@eazolan](#):
>
> Shut down Hugo, create a 0 byte file in your data directory. “anything.json”  
> Bring up Hugo server.  
> Hugo crashes for me. And I haven’t even written any code to reference it.

I just tested what you said in a project that does not call data files and the project generates fine.

However in a project that does call other data files from templates placing an empty JSON under the `dataDir` _will_ break the build, even if the empty file is not referenced in the project’s templates.

Perhaps you should file a bug report at [the main Hugo repository](https://github.com/gohugoio/hugo/issues).

---

<div class="post-metadata">

### Author: ![eazolan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/eazolan/32/14515_2.png) [@eazolan](https://discourse.gohugo.io/u/eazolan)
#### Post date: [June 1, 2021, 7:06pm UTC](https://discourse.gohugo.io/t/hugo-crashes-when-theres-a-data-file-with-no-data-in-it/33189/5 "2021-06-01T19:06:15Z")

</div>

Thank you for checking with that extra step! I wouldn’t have thought of that condition.  
I’ll create a bug report.

---

<div class="post-metadata">

### Author: ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)
#### Post date: [June 2, 2021, 11:40am UTC](https://discourse.gohugo.io/t/hugo-crashes-when-theres-a-data-file-with-no-data-in-it/33189/6 "2021-06-02T11:40:40Z")

</div>

I am not trying to rant on anything here, but I feel like the proper way to handle the issue is to tell MS SQL server to not export empty JSON files 😉 `EMPTY` in sense of JSON is `[{}]` or even just `[]`. I am not sure how to do that, but there is always the last resort of checking if a query is empty and “just” printing an empty json-string into the file.

Maybe there is an easy config option or addition to the `JSON AUTO` command in MSSql to do so? Did you look at `JSON_LENGTH` as an option?

> **[Select for empty JSON array in MySQL](https://blog.digital-craftsman.de/select-for-empty-json-array-in-mysql/)**
>
> Should be as simple as NOT list = '\[\]', but of course it's not. There is a separate JSON function for it called JSON\_LENGTH with which we can check it: SELECT id FROM users WHERE NOT JSON\_LENGTH(roles) = 0

---

<div class="post-metadata">

### Author: ![eazolan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/eazolan/32/14515_2.png) [@eazolan](https://discourse.gohugo.io/u/eazolan)
#### Post date: [June 2, 2021, 3:40pm UTC](https://discourse.gohugo.io/t/hugo-crashes-when-theres-a-data-file-with-no-data-in-it/33189/7 "2021-06-02T15:40:59Z")

</div>

I agree that there’s a bug in MS SQL. I’m pretty sure they took the reasonable JSON output:

> If no results, do not create data object.

And just dumped it to a file. That needs to be fixed.

However, the way Hugo handles zero byte files is _brittle_. Having a bad file shouldn’t bring down the whole server.

I had tried fixing this in MS SQL (Tried JSON AUTO), but I’m not an SQL expert and couldn’t come up with a solution within the time I was given to work on the issue at work. Also, I don’t get to config the MS SQL server.

My fix involved a Ant script between MS SQL and Hugo.

---

<div class="post-metadata">

### Author: ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)
#### Post date: [June 4, 2021, 3:41pm UTC](https://discourse.gohugo.io/t/hugo-crashes-when-theres-a-data-file-with-no-data-in-it/33189/8 "2021-06-04T15:41:43Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
