RFC: clang-format option to include another configuration file

TL;DR: I would like to add support for an “include” syntax in _clang-format configuration files, allowing an arbitrary configuration file to be read and merged into the current configuration, without having to be located in an ancestor directory (nor named “_clang-format”).

My team is starting to adopt clang-format for formatting our (very large) C++ codebase. One maintenance issue we’ve identified is the existing behavior of reading _clang-format configuration files in ancestor directories, with “more local” configuration files overriding the “more global” ones. This isn’t flexible enough for my needs, as we have multiple, competing style conventions in different parts of our repo, and this does not map neatly onto our directory hierarchy.

Because of this situation, I’m currently stuck with the necessity of cloning my team’s specific style settings in about 6 different places in our repo, which makes me cry a little. Thus, I would like to add support for one _clang-format file to include another one, at an arbitrary path (and, ideally, expanding environment variables in the process).

Thoughts? Objections? Alternatives?

4 Likes

I have exactly the same situation, and was surprised to find that this doesn’t seem to be possible. It’s possible to specify an arbitrary config file on the command line using the --style argument, but I’d really like to be able to do something like

Include: path/to/my/config

or even

BasedOnStyle: file:path/to/my/config

1 Like

I recently ran into a similar request for editorconfig spec: Feature request: extending external configs · Issue #236 · editorconfig/editorconfig · GitHub

I note someone mentions other lint tools that do have include support. Maybe looking at those implementations would give some useful hints.

But I also note the concerns brought up on that thread about potential for security issues from including random files on the filesystem. Anyone proposing a concrete solution here probably should ensure their proposal addresses such concerns.