dprint / dprint-plugin-exec

Formatting plugin for dprint that formats code via other formatting CLI tools.
MIT License
21 stars 1 forks source link

Add a variable to expose the dprint.json folder #35

Closed bradzacher closed 4 months ago

bradzacher commented 5 months ago

For things like rustfmt it's common to have a rustfmt.toml in the root of the repo. This can be passed via the --config-path CLI flag.

Unfortunately the plugin executes the command from the CWD. https://github.com/dprint/dprint-plugin-exec/blob/160b28c8ec08317b2c52e8f2c2fba6e60597a50f/src/handler.rs#L153-L154

This means that you cannot reference that root config file at all!

It'd be great if one could use the dprint.json as a marker of the repo root and use that folder to orient ones self in the repo - irrespective of the CWD.

dsherret commented 5 months ago

Yeah that's a pretty bad design flaw for this plugin. The config is provided by the cli to the plugin because there could be a single running plugin and many configs. An issue is configs could be extended from other configs. I think maybe we need to introduce something like ${configDir} in TypeScript and have this handled by the CLI. I opened https://github.com/dprint/dprint/issues/867

bradzacher commented 5 months ago

We work around this at canva with a little global executable called ensure-repo-root:

#!/usr/bin/env bash
repo_root=$(git rev-parse --show-toplevel)
cd "$repo_root"
"$@"

Which we then add to the config eg:

{
  "command": "ensure-repo-root rustfmt --edition 2021 --config-path rustfmt.toml --emit stdout",
  "associations": ["**/*.rs"]
}

But it would be good to have a variable to do this for us and save the git invocation!

dsherret commented 5 months ago

I opened https://github.com/dprint/dprint/pull/870 -- will merge and release it this weekend.