NodeJS API: Changes to .js config not being picked up due to NodeJS module caching
See original GitHub issueEnvironments:
- Prettier Version: 1.14.1 and above
- Running Prettier via: Node.js API
- Runtime: Any Node.js version
- Operating System: Any OS
Steps to reproduce: This is reproducible via any Prettier editor integration, e.g. prettier-vscode or Intellij prettier plugin:
- Create a .prettierrc.js config file with, for example, a specified tab size:
module.exports = {
tabWidth: 7
}
- Invoke ‘Reformat Code’ from the editor. File is reformatted according to .prettierrc correctly
- Modify .prettierrc.js, for example, changing
tabWidth
to 2 - Make sure the editor persists changes to the config file to disk
- Invoke ‘Reformat Code’ again
Expected behavior: File is reformatted with the new tabWidth (2). Actual behavior: File is reformatted with the previous tabWidth (7), until the editor is restarted. After that the file is reformatted with new settings.
With prettier
version 1.13.7 the changes to the file are picked up correctly.
Same with .json or .yaml config files - changes are picked up immediately after changing the config file.
This seems to be caused by the fact that Node.js require
caches the loaded modules for the duration of the process. Editor integrations usually create a long-running process that then invokes ‘reformat’ multiple times, so if the config is read via require
, subsequent attempts to read it will return the same result.
Prettier updated cosmiconfig
to version 5 here, in 1.14.1.
Cosmiconfig replaced require-from-string
with plain require
here.
require-from-string
had the side effect of not caching the loaded modules.
ESLint had a similar issue in the past: https://github.com/eslint/eslint/issues/5067 and addressed it by using require-uncached
.
Even though this mostly affects editor integrations, I believe this should be addressed in prettier
itself (or possibly in cosmiconfig
), and not in editor integrations because:
prettier
picks up changes correctly for *.json or *.yaml files- every editor integration would have to work around this issue, instead of only once.
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (8 by maintainers)
Top GitHub Comments
@ikatyang sounds good! thanks ill have a PR out tonight
Created the PR. but the test that I created seem to fail still. Seems cosmic config 5.0.7 update doesn’t work for us. Perhaps I am missing something