Skip to content

Add strict flag #252

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

Merged
merged 1 commit into from
Apr 30, 2021
Merged

Add strict flag #252

merged 1 commit into from
Apr 30, 2021

Conversation

fortmarek
Copy link
Contributor

Hi!

I'd find it quite useful to be able to promote warnings to be errors when running swift-format lint.

My use-case is that I'd like to validate on CI that no formatting is needed but we want to leave it up to the developer to fix it - this is very similar to what eg. swiftlint offers (and swiftformat offers a similar functionality as well, for that matter).
The solution is quite simple and more-or-less copies if frontend.errorsWereEmitted { throw ExitCode.failure }.

This PR is only a draft to validate whether such feature would be welcome, so there are no tests (I have validated this only by running it locally).

Sorry if this is already possible and I have missed it 🙇 (I have tried to see if there's any mention of this in the repo and in the previous PRs, but have not found any)

@fortmarek fortmarek marked this pull request as draft April 8, 2021 19:04
Copy link
Member

@allevato allevato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

I somewhat think that this feature would be served better by having it actually upgrade the severity of the diagnostics themselves to .error instead of .warning, and then the extra check wouldn't be necessary. But that would also require a significant refactoring of the way we currently create those diagnostics... and I don't think we should do that yet, because there are other changes that may be worth doing at the same time as well (like letting the severity of warnings be controlled on a case-by-case basis in the configuration file).

So what you have here seems like a reasonable approach in the meantime.

Can you rebase your change on top of the main branch? (I know it doesn't build against a current development snapshot; unfortunately I haven't had cycles to update it based on recent syntax changes upstream, but I can try to do that soon in order to merge this in.)

@fortmarek
Copy link
Contributor Author

fortmarek commented Apr 21, 2021

Hey @allevato! I have rebased on top of main - which has made the diff between 5.3 branch and this PR quite larger (plus it generated a bunch of merge conflicts with the current branch). Should I keep pointing to swift-5.3-branch or change it to main?

I somewhat think that this feature would be served better by having it actually upgrade the severity of the diagnostics themselves to .error instead of .warning

I agree, let me know if you want help with the subsequent refactor. Promoting warnings to errors themselves would also enable me to write tests - I am not sure if it is possible at the moment to do so as the changes are limited to Lint command.

@fortmarek fortmarek marked this pull request as ready for review April 21, 2021 08:32
@allevato
Copy link
Member

Hey @allevato! I have rebased on top of main - which has made the diff between 5.3 branch and this PR quite larger (plus it generated a bunch of merge conflicts with the current branch). Should I keep pointing to swift-5.3-branch or change it to main?

Yes, please point the PR at main.

I agree, let me know if you want help with the subsequent refactor. Promoting warnings to errors themselves would also enable me to write tests - I am not sure if it is possible at the moment to do so as the changes are limited to Lint command.

I'm always happy to discuss design proposals and review changes! Since it hasn't felt terribly urgent, I haven't given it a great deal of thought yet so I don't have a strong lean about how to best handle the change. The one immediate issue that comes to mind is that our diagnostic values are created completely context-less, and they have the severity baked in. So if we wanted to move to a model where the Configuration could determine the severity of diagnostics at runtime instead, we'd probably need to create a new formatter-specific diagnostic manager abstraction that owns an instance of the Configuration and accepts a slightly re-modeled notion of what a diagnostic value is, and then transforms those into the actual SwiftSyntax diagnostics.

We also can't tie the "category" of a diagnostic 1:1 to the "rule" concept because we emit diagnostics from other places as well, such as the whitespace linter, and the frontend itself. Heck, some rules emit multiple kinds of diagnostics, and someone might want to control their severity independently (we'd have to decide if that makes sense and if we want to allow that level of granularity).

Unfortunately these changes would ripple through the entire codebase—we'd probably need to re-model the Configuration quite a bit (both in its in-memory and on-disk representations), and the unit tests would be pretty deeply affected as well, where we use the diagnostic values directly for assertions. 😭

I'll try to think on it some more and see where we can get with smaller incremental changes.

@fortmarek fortmarek changed the base branch from swift-5.3-branch to main April 29, 2021 06:02
@fortmarek
Copy link
Contributor Author

Yes, please point the PR at main.

Done!

we'd probably need to create a new formatter-specific diagnostic manager abstraction that owns an instance of the Configuration and accepts a slightly re-modeled notion of what a diagnostic value is, and then transforms those into the actual SwiftSyntax diagnostics.

This approach seems sensible to me - but as you said, it can create a lot of changes. But for the long run, it is probably beneficial to have a stronger control over the diagnostics. Once we'd have our own diagnostics engine layer, promoting warnings to errors (or some other modification of the severity) could be done very easily.

Copy link
Member

@allevato allevato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good now, thank you!

@allevato allevato merged commit 08185e8 into swiftlang:main Apr 30, 2021
aaditya-chandrasekhar pushed a commit to val-verde/swift-format that referenced this pull request May 20, 2021
* Add Brewfile.lock.json file

* Update dependencies

Fix CommonMark version specifier

* Add version-specific manifest for Swift 5.4

* Change minimum Swift version requirements to version 5.3+

* Fix failing tests
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.

2 participants