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

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?

You can use len to conditionally load the data file only if its content is not empty.

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.

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.

1 Like

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

1 Like

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 :wink: 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?

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.

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