Skip to content

fix(BREAKING): make --config file patterns relative to the config file's directory - #1257

Merged
dsherret merged 5 commits into
mainfrom
fix/config-flag-base-path
Sep 29, 2026
Merged

dsherret merged 5 commits into
mainfrom
fix/config-flag-base-path

Conversation

@dsherret

@dsherret dsherret commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

A local config file specified via --config / -c now 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 --config was always the current working directory, so the same config file matched different files depending on how it was found. For example, with sub/dprint.json containing "includes": ["src/**/*.ts"]:

  • cd sub && dprint fmt formats sub/src/**/*.ts
  • dprint fmt --config sub/dprint.json (from the parent) formatted src/**/*.ts in the parent directory

This also affects the VS Code extension, which passes --config for config files in sub directories of a workspace folder, so their relative includes didn't match (dprint/dprint-vscode#54, dprint/dprint-vscode#71).

../ in config patterns

Since patterns are now always relative to the config file, includes and excludes in the config file dprint is run with (via --config or 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.json from another directory now only formats files in path/to/dir and its descendants.

Workarounds

For a setup like #725, which keeps the config at tools/dprint/dprint.json with "includes": ["**/Cargo.toml"] and runs dprint check --config tools/dprint/dprint.json from the repository root, any of these format the Cargo.toml files across the repository again:

  1. Make the patterns relative to the config file:

    {
      "includes": ["../../**/Cargo.toml"]
    }

    This works both with --config from the root and when running dprint from tools/dprint.

  2. Pass the patterns on the command line, which are relative to the current working directory:

    dprint check --config tools/dprint/dprint.json "**/Cargo.toml"
  3. Add a root dprint.json that has the includes and extends the config in the sub directory, then run dprint check without --config:

    {
      "extends": "tools/dprint/dprint.json",
      "includes": ["**/Cargo.toml"]
    }

    The includes need to be in the root config because the extended config's includes aren't used.

  4. 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 of includes and excludes patterns. Plugin associations and the files of overrides are 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 --config file's parent directory as the base path.
  • patterns.rs: new_config_glob_pattern resolves 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 of OrderedPatternsMatcher (associations and override files).
  • paths.rs: get_and_resolve_file_paths takes an IncludeScope. 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

  • Updated the test from fix: do not error for --config in sub dir in some cases (regression) #727 for the new behavior. It still covers that the --config file isn't resolved a second time when found while traversing its directory, and now also checks that excludes are relative to the config file's directory and files outside it aren't formatted.
  • New tests for a relative --config path with includes, a --config file 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.
  • The new tests fail when the base path change, the ../ resolution, the file walk widening, or the descendant restriction is removed. The existing remote --config tests still expect the current working directory as the base path.
  • Checked the workarounds above with a local build against the dprint fails for config file nested in sub directory in some cases (regression in 0.40.0) #725 layout (tools/dprint/dprint.json with Cargo.toml and other .toml files across the repository).

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant