prettier / tslint-plugin-prettier

Runs Prettier as a TSLint rule and reports differences as individual TSLint issues

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Resolve configuration using current directory

n0nick opened this issue · comments

In prettierRule.ts:38, tslint-plugin-prettier tries to resolve a prettier configuration file based on the examined file's path.

Some editor integrations do fancy things such as running tslint on temp files (in order to catch lint errors before file save), so this breaks that. Specifically I've hit that with Vim+ALE: dense-analysis/ale#956.

My suggestion would be to try to fall back to the system's current directory (process.cwd()) after trying to resolve based on file path.
This would allow the editor integration to fix this by cding into the project directory.

commented

Thanks for the report, but I have no idea how to fix it properly since cosmiconfig (prettier use this module to find config) will lookup its file system structure to find the config file, that is, the config file can be anywhere in its parent directory, not only look at the root folder. As far as I know, caches won't remain its fs structure, they're always flattened.

In my opinion, this issue should be fixed on the editor/editor-plugin side since they touched the file path, so they should have the responsibility to correct them back, but I'm open to fix this on my side if the proposal is reasonable.

Thanks for the quick response @ikatyang.

I'm not too familiar with cosmiconfig or the prettier logic, but I imagined something like the following replacing prettierRule.ts:37-42:

const resolved_config =
  prettier.resolveConfig.sync(source_file.fileName) ||
  prettier.resolveConfig.sync(process.cwd());
if (resolved_config !== null) {
  options = resolved_config;
}

If this sounds like wanted behavior for tslint-plugin-prettier, I can try and see if that would work.

It even seems that cosmiconfig defaults to process.cwd() when no search path is specified:
https://github.com/davidtheclark/cosmiconfig#usage

commented

Prettier CLI always find config based on its filepath:

https://github.com/prettier/prettier/blob/1.7.0/src/cli-util.js#L245
https://github.com/prettier/prettier/blob/1.7.0/src/cli-util.js#L257

I'd like to keep the same behavior with CLI so that people can move smoothly if they want to switch back to use CLI. Another solution is to specify a config file path manually, e.g.

{
  "rules": {
    "prettier": [true, "path/to/config/file/search/path/based/on/cwd"]
  }
}

And you'll have to set something like "prettier": [true, "."] to indicate looking in the current work directory.

Is that looks good to you?

I agree re consistency with the CLI, I see your point.

If something like what you suggest is easy to add and would work with a relative path (i.e. "."), that would be awesome.
Especially since I don't have a better idea for the editor plugin's side (they want to allow linting before saves, so files need to be outside of project scope).

Thank you @ikatyang for the responsiveness and quick turnaround! 🎉