-
Notifications
You must be signed in to change notification settings - Fork 250
Add dump-effective-configuration subcommand #923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A high-level suggestion before I review this further: Since this is so similar to the existing dump-configuration
command, I think this would be expressed more simply as an optional flag for that command instead of a completely separate command. Something like dump-configuration --effective
would work and would probably cut down on the implementation complexity here.
I thought about this initially, and wasn't happy that it would need to be explained that the I'll give merging this into the |
@allevato implemented your change request. |
I’m curious what the real-world use case for that options is. swift-format doesn’t have a complicated search algorithm to find the format files (it just walks up the directory structure) and it doesn’t merge files either. So, IIUC, all this effectively does is to find the first |
I'm not sure of the PR author's motivation, but one thought I've had in the past is that if we ever make a breaking change to the configuration and bump the version number, it would be nice to have a way for users to explicitly update their configuration to the new version. One way to handle that would be an option like this: since older versions of the config would be translated to the new model in-memory during decoding, printing back that in-memory model afterwards would render it as the latest version. |
Sure, let me elaborate a bit on the motivation:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your explanation of the use case @andre-richter. The change makes sense to me. I have a few comments regarding the implementation inline.
let frontend = DumpConfigurationFrontend( | ||
configurationOptions: configurationOptions, | ||
lintFormatOptions: lintFormatOptions | ||
) | ||
frontend.run() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not a big fan of running a dummy frontend that stores the configuration used as a side effect. I’d prefer to just call into Frontend.configuration(fromPathOrString:orInfoerredFromSwiftFileAt:)
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean by turning Frontend.configuration
static somehow? Asking because the current implementation uses instance properties such as diagnosticsEngine
.
Adding a dummy fronted seemed the least intrusive change for existing code to me, but I’m happy to make adaptions if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, looks like it shouldn’t be too hard. It looks like ConfigurationLoader
can be a local variable instead of a member and DiagnsoticsEngine
can be passed in.
@ahoppen, there was also Therefore, I decided to carve out the whole configuration resolution logic into its own helper struct that saves a |
4ee4610
to
805bdec
Compare
closes swiftlang#667 Implement the subcommand `dump-effective-configuration`, which dumps the configuration that would be used if `swift-format` was executed from the current working directory (cwd), incorporating configuration files found in the cwd or its parents, or input from the `--configuration` option. This helps when composing a configuration or with configuration debugging/verification activities.
805bdec
to
1e8c05e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for addressing my comments @andre-richter, this seems a lot cleaner to me. I have a few more questions/comments inline.
/// Loads formatter configuration files and chaches them in memory. | ||
private var configurationLoader: ConfigurationLoader = ConfigurationLoader() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I don’t think this needs to be a member at all. A local variable in provide
should be sufficient, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It stores the cache of previously loaded configurations. So it wouldn't work as a local variable (as in: stack-allocated inside provide
, if that is what you meant?).
/// Provides formatter configurations for given `.swift` source files, configuration files or configuration strings. | ||
struct ConfigurationProvider { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering: Would it be simpler if we just had a static method (or global function) like func configuration(forConfigPathOrString pathOrString: String?, orForSwiftFileAt swiftFileURL: URL?, diagnosticsEngine: DiagnosticsEngine)
and then either make checkForUnrecognizedRules
a nested function or make it take a DiagnosticsEngine
as well? To me, having a type here implies that there is some state being stored, which isn’t actually the case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me, having a type here implies that there is some state being stored, which isn’t actually the case.
There is state being stored in the form of configurationLoader
, which stores the cache for previously loaded configs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see. Fair enough then.
Head branch was pushed to by a user without write access
9c16a60
to
11d6de8
Compare
closes #667
Implement the subcommand
dump-effective-configuration
, which dumps the configuration that would be used ifswift-format
was executed from the current working directory (cwd), incorporating configuration files found in the cwd or its parents, or input from the--configuration
option.This helps when composing a configuration or with configuration debugging/verification activities.