Repository navigation
fix(BREAKING): make --config file patterns relative to the config file's directory - #1257
Merged
Merged
Conversation
…ile's directory A local config file specified via `--config` now uses its directory as the base path, the same as when the config file is discovered. Previously it used the current working directory, so the same config file matched different files depending on how it was found. Remote and npm config files still use the current working directory.
Patterns in the config file dprint is run with can now start with `../` to match files in ancestor directories of the config file's directory (ex. `../../**/Cargo.toml`), which also covers keeping a config file in a sub directory while formatting from the repository root now that `--config` patterns are relative to the config file. Config files discovered in descendant directories still only apply to files in their own directory.
# Conflicts: # crates/dprint/src/patterns.rs
This was referenced Sep 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A local config file specified via
--config/-cnow resolves its patterns (includes,excludes, etc.) relative to the config file's directory, the same as when dprint discovers the config file. Remote and npm config files have no directory, so they still resolve relative to the current working directory.Previously the base path for
--configwas always the current working directory, so the same config file matched different files depending on how it was found. For example, withsub/dprint.jsoncontaining"includes": ["src/**/*.ts"]:cd sub && dprint fmtformatssub/src/**/*.tsdprint fmt --config sub/dprint.json(from the parent) formattedsrc/**/*.tsin the parent directoryThis also affects the VS Code extension, which passes
--configfor config files in sub directories of a workspace folder, so their relativeincludesdidn't match (dprint/dprint-vscode#54, dprint/dprint-vscode#71).../in config patternsSince patterns are now always relative to the config file,
includesandexcludesin the config file dprint is run with (via--configor discovered from the current directory) can start with../to match files in ancestor directories of the config file's directory (ex."includes": ["../../**/Cargo.toml"],"excludes": ["../../target"]). The pattern is based at the ancestor directory and the file walk starts from the highest include's base.Config files discovered in descendant directories while walking still only apply to files in their own directory, so a
../include there doesn't claim files from the parent config's scope.Breaking change
Running
dprint fmt --config path/to/dir/dprint.jsonfrom another directory now only formats files inpath/to/dirand its descendants.Workarounds
For a setup like #725, which keeps the config at
tools/dprint/dprint.jsonwith"includes": ["**/Cargo.toml"]and runsdprint check --config tools/dprint/dprint.jsonfrom the repository root, any of these format theCargo.tomlfiles across the repository again:Make the patterns relative to the config file:
{ "includes": ["../../**/Cargo.toml"] }This works both with
--configfrom the root and when runningdprintfromtools/dprint.Pass the patterns on the command line, which are relative to the current working directory:
dprint check --config tools/dprint/dprint.json "**/Cargo.toml"Add a root
dprint.jsonthat has theincludesand extends the config in the sub directory, then rundprint checkwithout--config:{ "extends": "tools/dprint/dprint.json", "includes": ["**/Cargo.toml"] }The
includesneed to be in the root config because the extended config'sincludesaren't used.Move the config file to the root.
To only format specific files with a plugin regardless of the directory they're in, this can be combined with plugin associations, which are evaluated in order since #1258:
{ "includes": ["../../**"], "toml": { "associations": ["!**/*.toml", "Cargo.toml"] } }Limitations
../is only supported at the start ofincludesandexcludespatterns. Pluginassociationsand thefilesofoverridesare matched relative to the config file's directory and a../pattern in them doesn't match anything.Changes
resolve_main_config_path.rs: use a local--configfile's parent directory as the base path.patterns.rs:new_config_glob_patternresolves leading../segments of a config pattern against the config file's directory. It's used for includes, excludes, inherited ancestor excludes (resolve_config.rs), and the patterns ofOrderedPatternsMatcher(associations and override files).paths.rs:get_and_resolve_file_pathstakes anIncludeScope. Only the main config (resolution.rs) widens the file walk and pattern base to its includes' base, so descendant config files stay within their directory.Testing
--configin sub dir in some cases (regression) #727 for the new behavior. It still covers that the--configfile isn't resolved a second time when found while traversing its directory, and now also checks thatexcludesare relative to the config file's directory and files outside it aren't formatted.--configpath withincludes, a--configfile with../includes and excludes, a discovered config file with a../include, a descendant config file's../include not reaching outside its directory, and a unit test for the pattern resolution.../resolution, the file walk widening, or the descendant restriction is removed. The existing remote--configtests still expect the current working directory as the base path.tools/dprint/dprint.jsonwithCargo.tomland other.tomlfiles across the repository).