Skip to content

NodeJS API: Changes to .js config not being picked up due to NodeJS module caching #5311

@undeadcat

Description

@undeadcat

Environments:

  • 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: eslint/eslint#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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:apiIssues with Prettier's Application Programming Interfacelocked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.scope:dependencyIssues that cannot be solved inside Prettier itself, and must be fixed in a dependencystatus:has prIssues with an accompanying pull request. These issues will probably be fixed soon!

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions