-
-
Notifications
You must be signed in to change notification settings - Fork 493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creating a new JSON file should not throw error when empty (or crash) #2299
Comments
But isn't an empty JSON file technically malformed JSON? I ran some quick tests using require("json5/lib/register");
const fse = require("fs-extra");
try {
// SyntaxError: /private/tmp/json-test/empty.json: Unexpected end of JSON input
const emptyRequire = require("./empty.json");
console.log({emptyRequire});
} catch (err) {
console.error("1.", err.message);
}
try {
// SyntaxError: ./empty.json: Unexpected end of JSON input
const emptyFse = fse.readJsonSync("./empty.json");
console.log(emptyFse);
} catch (err) {
console.error("2.", err.message);
}
try {
// SyntaxError: /private/tmp/json-test/empty.json5: JSON5: invalid end of input at 1:1
const emptyJson5 = require("./empty.json5");
console.log({emptyJson5});
} catch (err) {
console.error("3.", err.message);
}
try {
const emptyJson = safeLoad("./empty.json");
// { emptyJson: {} }
console.log({emptyJson});
} catch (err) {
console.error("4.", err.message);
}
function safeLoad(p) {
try {
return require(p);
} catch (err) {
return {};
}
} 1. /private/tmp/json-test/empty.json: Unexpected end of JSON input
2. ./empty.json: Unexpected end of JSON input
3. /private/tmp/json-test/empty.json5: JSON5: invalid end of input at 1:1
{ emptyJson: {} } I'm not too familiar w/ the internals of 11ty's global data files vs directory data files, but it sounds like directory data files might have a slightly better developer experience. Wrapping the JSON loading in a |
Baby steps... const json5 = require("json5");
module.exports = eleventyConfig => {
eleventyConfig.addDataExtension("json5", contents => json5.parse(contents));
...
}; It seems if I use json5 as a custom data file format (also see https://www.npmjs.com/package/json5 for details). It seems to handle empty files a bit better when I use Anyways, not sure if it helps or not, but I learned something today, so it was a good day. |
I totally understand if it this isn't something that is fixable - just thought it might trip others up the first time they create a global data file. I'm not familiar with 11ty internals, but before trying to read as JSON, could you first use Definitely adds a step when processing which might not be ideal though. Thanks for investigating regardless! |
I agree that this change would be a DX improvement! 👍🏻 I think it makes the most sense to add this to the enhancement queue
|
This is shipping with 2.0.0-canary.19, thank you! Fixed by 7c03471 |
Describe the bug
While the 11ty-dev-server is running, if you create a new JSON file in the
_data
directory, the server will crash with the following error.If you create a JSON file in a different directory, say a file called
docs.json
in a directory calleddocs
(to add tags to all files in folder) you will receive the same error but will be able to continue. That being said, I would rather it not show an error at all and simply ignore empty JSON files (error if they are malformed, sure).To Reproduce
Steps to reproduce the behavior:
eleventy --serve
_data/test.json
Expected behavior
No error for empty JSON file - error for malformed JSON (ideally no crashing)
Environment:
The text was updated successfully, but these errors were encountered: