-
-
Notifications
You must be signed in to change notification settings - Fork 345
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
cmd/shfmt: support loading editorconfig conf when formatting a suffix-less file #664
Comments
I started reading your comment and was going to reply with these two threads. You have an accurate understanding of how I tried to solve the common case of extension-less shell scripts :) Another option which you didn't mention is to have a
I think using this thread to brainstorm ways to make this work in an editorconfig-compatible way is reasonable. I tried pushing the upstream spec to support languages without explicit filename extensions, but that stalled out, like you said. I personally think that supporting |
Just to address this really quickly: I bet this works for >90% of cases like mine, to the point where I almost didn't open an issue.
I took a look at a couple of the parser implementations to see how dangerous it would be to add this. The ruby one would be fine, so would the python one, ... but then I checked the docs and editorconfig explicitly says brackets are allowed in section names, so I think this is 100% safe. 😂 Is there some other case you're concerned about this breaking, which we should discuss? Also, after a bit of thought, I think I should maybe make some comment on the editorconfig issue to put forth a potential way forward. |
Syntactically, it's entirely safe, like you said. The only potential problem is anyone currently using All in all, I think it's fine if we support this kind of "extension" to EditorConfig. It's essential to scripting languages like shell. It can perhaps act as a driver to extending the upstream spec in the future, too. |
Thanks for being so receptive to this! I was looking at I see that the code currently loads sections in order and "last match wins". I would offer to help, but the last Go I wrote was "hello world" circa 2015. I'm not sure I'll be much help other than trying to define the right behavior. But if you like, I could give it a shot sometime. |
As a temporary measure, have you considered
That sounds about right. Though I'd keep it in a separate method, to avoid breaking users and to keep the non-standard functionality to an entirely separate API function. Something like
I don't have a strong opinion here, but I do think that consistency with the existing EditorConfig spec (i.e. "last match wins") would be good. Is there a particular reason you think we should define a different match priority? |
When I wrote my comment on editorconfig, I didn't know about that existing behavior, so I proposed that language blocks should be lower precedence. But now, knowing that multiple matches are covered by the spec, I see no reason to do that. As for |
I agree, and it would also result in error messages referencing the wrong file. I'll try to take a look at language support soon. That is:
The open question is what would we do for |
I just realised that we don't support language detection in shfmt per se; that's #622. So we should probably implement that first, then piggyback off the same language detection for this EditorConfig feature. |
My suggestion is that if you are going to continue deviating from the EditorConfig Specification, support should be added for a dedicated configuration file. Ideally this would be via a specific filename like I am running into the same problems with configuring shfmt for shell files that don't have a file extension, but am not comfortable with making my |
@per1234 to be clear, we are not deviating from the spec today. If configs per detected language is the only bit we lack from editorconfig, I don't think that's reason enough to add our own custom and specific config file. I agree it would be a nice to have, but once we add our own config file, we're stuck with it basically forever :) |
It feels to me like you are unnecessarily using |
I've always understood that EditorConfig encourages domain-specific properties. For example, see https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#ideas-for-domain-specific-properties. |
Hi @mvdan, I am concerned that other formatting tools might pick these settings up for scripts in different scripting languages. |
I'm still opposed to adding That said, the EditorConfig thread about For now we could just support |
I've just pushed a first implementation to master. Please try it out over the next few weeks; I would like to do a release in about a month's time. If there are any disagreements or significant input about how it works, I would like to flesh that out before a release. |
I stumbled over this issue by chance and read about edit: I tested it with shfmt 3.5.1 on openSUSE Leap 15.5 |
Support in shfmt is in master, and not in a release yet, per my last comment above. See 7f96e7d for the changes with docs. Upstream editorconfig has no support for |
Oh, nevermind, I misread the diff. |
Just for the record, what we are now using in our CI is this, without using editorconfig:
But we are looking forward to |
I just tried this out in a branch of the repo where I wanted this most badly, and it worked great! I'm very much looking forward to the release! As always, thanks so much for |
In a project where there are shell scripts with a shebang line and no suffix, editorconfig for configuration doesn't work cleanly.
If I run
shfmt -w scripts/some-script scripts/other-script.sh
(wheresome-script
is actually a bash script), the editorconfig section isn't loaded for that file and different formatting is used between the two.Ideally I would be able to run without flags and get behavior driven by
*.sh
in editorconfig as the "default" config.I've looked at #577 (I can't use
-filename PLACEHOLDER.sh
because the file isn't coming from stdin) and #393 as relevant.I'm also looking at editorconfig/editorconfig#404 which seems to have gone off the rails and stalled out. It's a shame because I already have
pre-commit
and it'sidentify
tool solving the detection problem for me, so an editorconfig section for[[shell]]
would be perfect. I'm not sure at this stage if a comment on that editorconfig issue would be productive, so I'm staying away.In case it's handy and anyone reading this wants to try shfmt via pre-commit, here's a snippet of pre-commit config for running shfmt
I think it would be viable to use a special editorconfig section for
shfmt
behavior when the filename, matched against editorconfig, does not produce a configuration section. Something like[*.{sh,bash,SHFMT_DEFAULT)]
.That's obviously a huge hack, and much worse on the surface than
[[shell]]
, but it would probably work okay.I very much appreciate
shfmt
's decision not to litter my filesystem with more itty-bitty files, but in this case it's a frustration. I'm not able to configureshfmt
behavior in a directory in a direct way, only through a layer of indirection (editorconfig) which does not support my project's structure.The text was updated successfully, but these errors were encountered: