prettier: Potential Breaking Change in `resolveConfig`

It seems that 2.0 may have introduced an unintended breaking change in the resolveConfig function. This function is used in the Prettier VS Code extension here: https://github.com/prettier/prettier-vscode/blob/master/src/ConfigResolver.ts#L99

Since upgrading to 2.0 we are now seeing errors when trying to resolve configs:

Error: Cannot find module 'prettier-config-greenelab' from 'c:\Users\Vincent\Desktop\adage-frontend'
	at Function.Module._resolveFilename (internal/modules/cjs/loader.js:717:15)
	at Function.n.resolve (c:\Program Files\Microsoft VS Code\resources\app\out\vs\loader.js:15:584)
	at Object.transform (c:\Users\Vincent\.vscode\extensions\esbenp.prettier-vscode-4.0.0\node_modules\prettier\index.js:23283:40)
	at run (c:\Users\Vincent\.vscode\extensions\esbenp.prettier-vscode-4.0.0\node_modules\prettier\third-party.js:11472:51)
	at async cacheWrapper (c:\Users\Vincent\.vscode\extensions\esbenp.prettier-vscode-4.0.0\node_modules\prettier\third-party.js:11300:20)
	at async cacheWrapper (c:\Users\Vincent\.vscode\extensions\esbenp.prettier-vscode-4.0.0\node_modules\prettier\third-party.js:11300:20)
	at async cacheWrapper (c:\Users\Vincent\.vscode\extensions\esbenp.prettier-vscode-4.0.0\node_modules\prettier\third-party.js:11300:20)
	at async cacheWrapper (c:\Users\Vincent\.vscode\extensions\esbenp.prettier-vscode-4.0.0\node_modules\prettier\third-party.js:11300:20)
	at async cacheWrapper (c:\Users\Vincent\.vscode\extensions\esbenp.prettier-vscode-4.0.0\node_modules\prettier\third-party.js:11300:20)
	at async Explorer.search (c:\Users\Vincent\.vscode\extensions\esbenp.prettier-vscode-4.0.0\node_modules\prettier\third-party.js:11457:22)
	at async Promise.all (index 0)

For repro steps and more details see: https://github.com/prettier/prettier-vscode/issues/1289

One thing to note, is that everything behaves as expected from the CLI which is odd.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 32 (30 by maintainers)

Most upvoted comments

Okay, I’m about to submit a PR.

I have a fix: require('module').createRequire(dir).resolve.length === 2

I guess everyone has their own require? First jest, now vscode

It’s not about cosmiconfig, it gives us the string, which is expected, we require it on our side.

@ntotten It doesn’t look like your loader overrides require.resolve. Or am I wrong? The problem seems to be the fact that in Node 8.9.0+ require.resolve accepts a second argument, which Prettier 2.0 uses, but in the extension require.resolve seems to not support that.

It looks like the issue is that prettier removed support for config for files with just the string content:

In .prettierrc.yaml this no longer works:

"prettier-config-greenelab"

However, this does work:

prettier: "prettier-config-greenelab"

I think as long as prettier returns a better error message and the breaking change is noted, this is probably okay - assuming it was deliberate.