tweag / ormolu

A formatter for Haskell source code
https://ormolu-live.tweag.io
Other
944 stars 83 forks source link

Fixity info from .ormolu overrides CLI flags #1030

Closed brandonchinn178 closed 5 months ago

brandonchinn178 commented 1 year ago

Describe the bug

Reading the code here: https://github.com/tweag/ormolu/blob/49eb083d2b94ad882b1ff5f8b65f9870ee94a53d/src/Ormolu.hs#L193-L207

It seems like configuration in .ormolu files take precedence over command line flags, when the help text (and standard behavior of flags vs config files) seems to indicate that flags take precedence. Specifically, Map.union is left-biased, so fixityOverrides (coming from mfixityOverrides, which comes from mDotOrmolu) is preferred over cfgFixityOverrides rawConfig (which is parsed from command line flags)

To Reproduce

  1. Create some Haskell file with fixities
  2. Specify the wrong fixity in .ormolu
  3. Specify the right fixity with --fixity
  4. Observe ormolu formatting with the wrong fixity

Expected behavior Ormolu should format with the fixity specified on the command line

Environment

Additional context Add any other context about the problem here.

amesgen commented 1 year ago

I don't immediately see a reason to use the CLI flags when one already has an .ormolu file; is there a concrete use case?

But indeed, this was changed in #994, this seems to be the relevant review comment: https://github.com/tweag/ormolu/pull/994#discussion_r1168357940 (before that, the behavior you are expecting was preserved by that diff).

brandonchinn178 commented 1 year ago

Perhaps this is where the expected behavior of refineConfig fights with the usage in Main.hs. From the perspective of refineConfig, it makes sense that the passed-in fixity info should override the config passed in.

I personally don't see a concrete use-case for specifying fixities and reexports on the command line. But given that the flags exist, this is surprising behavior. I would be more in favor of removing the flags altogether than keeping the current behavior.

tbagrel1 commented 1 year ago

I think it is related to my remark here: https://github.com/tweag/ormolu/pull/994#discussion_r1168357940

I didn't have full context to understand the code when I wrote my review, and then @mrkkrp accepted my suggestion.

Now, I agree with @brandonchinn178 and I think CLI flags should have priority over config file ones. I suppose we could revert the change?

mrkkrp commented 1 year ago

It is true that there is a conflict between semantics of refineConfig and its usage in Main, it did not occur to me before.

I would be more in favor of removing the flags altogether than keeping the current behavior.

I think the flags are very useful in situations when you don't want to write to the filesystem, e.g. in scripts when you want to execute a one-off action without extra state.

I will see if I can adjust this...

mrkkrp commented 1 year ago

I think we should probably keep refineConfig as is (it is reasonable for the arguments to override the "raw" config, after all) and instead change the way it is used in Main.