question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Would you take a PR to throw an error for all empty configs?

See original GitHub issue

Current behavior:

When using load(null, configPath) cosmiconfig throws an error

When using load(dirPath) cosmiconfig continues to traverse up the directory path

If this was intentional then please help me understand why. Otherwise I would like to create a PR to throw in all cases. I don’t really care whether an error is thrown, we’re mostly looking for a way to differentiate empty configurations from those which are non-existent. e.g. package.json with an empty prettier property should behave differently from a package.json without a prettier property.

Edit: I was basing the above off of issue comments I was reading in the prettier repo - not off of local tests. When cosmiconfig sees a package.json file with "prettier": {}, it correctly returns an empty object and does not traverse up the file tree. However it still treats an empty *rc file as non-existent. I now understand this is an edge case that really can really only help expose configuration errors made by developers.

Why

prettier-atom would like to allow users to specify settings within the plugin for one-off cases where files do not belong to projects. However if any configuration is found, even empty then we would like to use that configuration instead. Empty signifying default settings.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sudo-suhascommented, Feb 5, 2018

@olsonpm, When loading a specified file, the behavior was inconsistent if the file was empty(see https://github.com/davidtheclark/cosmiconfig/pull/78#issuecomment-318810892) and this was fixed in v3.0.0. However, when performing a search up the directory tree, I am not entirely sure that we should throw an error when we find an empty config file.

package.json with an empty prettier property should behave differently from a package.json without a prettier property.

If the pkg has an empty property, the search resolves with {}. The same is true for a JSON file with {} as the contents. So would it be enough to check if the value that cosmiconfig search resolves to, is an empty object?

resolve to empty cfg

package.json:

{
  "name": "prettier-pkg-empty-cfg",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "prettier": "^1.10.2"
  },
  "prettier": {}
}
λ node
> let prettier = require('prettier')
> let filepath = path.resolve('./index.js')
'E:\\Projects\\experiments\\prettier-pkg-empty-cfg\\index.js'
> prettier.resolveConfig(filepath).then(console.log);
> {}
0reactions
davidtheclarkcommented, Mar 25, 2018

Empty configs are now handled consistently in the v5 branch, so closing this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Throw warning on invalid config #250 - laravel/ideas - GitHub
Currently if you call a config value that is does not exist/invalid - it is a silent failure: dd(config('custom.i_do_not_exist')) // null.
Read more >
husky - Strange error when attempting to commit. [subject-empty]
What it means is that you've added a non-Git program (specifically, something called "husky") and told Git to use Husky to check your...
Read more >
ASP.NET Error Handling - Microsoft Learn
You would typically use a page-level error handler to log unhandled errors or to take the user to a page that can display...
Read more >
Handle errors | Stripe Documentation
They can reflect external events, like declined payments and network interruptions, or code problems, like invalid API calls. To handle errors, use some...
Read more >
Error Messages | Cypress Documentation
You 'll likely get this message if you have an empty test file and have not yet written any tests. No tests found....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found