Skip to content
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

[Patch+test] Fix bug with postqualified imports and qualifiedStyle=unrestricted #1498

Merged
merged 4 commits into from
Sep 19, 2023

Conversation

ulidtko
Copy link
Contributor

@ulidtko ulidtko commented Apr 21, 2023

Hi!

I'll be brief. The bug:

# testcase-rule.yaml

- modules:
  - name: Data.Text
    importStyle: qualified
    qualifiedStyle: unrestricted
    asRequired: True
    as: T

on the single-line file:

import Data.Text qualified as T

emits, erroneously, a silly hint:

> hlint -h testcase-rule.yaml test.hs
test.hs:1:1-31: Warning: Data.Text should be imported qualified
Found:
  import Data.Text qualified as T
Perhaps:
  import qualified Data.Text as T
Note: may break the code

1 hint

I submit a direct testcase for the bug, then a fix, then more tests for better regression coverage.

Hoping for easy review & merge; release isn't urgent, batch up at will 🙌 Thanks in advance

Fixes #1499

, Just $ Just (Exactly, noLocA []) )
ImportStyleUnqualified -> (Just (NotQualified, "unqualified"), Nothing)
expectedQualStyleDef = expectedQualStyle <|> Just (QualifiedPre, "qualified")
Copy link
Contributor Author

@ulidtko ulidtko Apr 21, 2023

Choose a reason for hiding this comment

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

My ID of the root cause is this line 206 👀

Right here, QualifiedStyleUnrestricted (no pre-/post- preference) was getting mapped into QualifiedPre.

@ulidtko
Copy link
Contributor Author

ulidtko commented May 9, 2023

Hey @zliu41 @ndmitchell — I noticed somebody has run CI on the PR; thanks.

I've checked all 4 workflows — none of them failed because of changes in the PR. Timeout; 143 oomkill; exitcode 251 (?) when building yaml haddock on Windows; build oomkilled on MacOS too.

Tests do pass ✔️ on this branch; I explicitly centered the bugfix around tests. See PR diff.


As a sidenote, the CI setup could use a brush-up. Misuse of actions/cache caught my eye right away — when used properly, it will dramatically speed up builds, reduce OOMs, and that without build integrity issues (from stale cache), when done properly.

@ulidtko ulidtko changed the title [PATCH] bug with postqualified imports + tests [Patch+test] Fix bug with postqualified imports and qualifiedStyle=unrestricted Sep 15, 2023
@ulidtko
Copy link
Contributor Author

ulidtko commented Sep 15, 2023

@zliu41 @ndmitchell ping. Please review.

@googleson78
Copy link
Contributor

Also hitting this same problem

Copy link
Collaborator

@zliu41 zliu41 left a comment

Choose a reason for hiding this comment

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

I'm inclined to remove the "should be imported post-qualified" hint, because there are tools available (e.g., stylish Haskell) for converting pre-qualified imports to post-qualified imports (or vice versa). If one cares about using pre-qualified or post-qualified imports consistently in their project, they can simply run the tool in the pre-commit hook.

That said, we can still merge this. @ulidtko the CI has been fixed since this PR was opened, can you rebase?

src/Hint/Restrict.hs Show resolved Hide resolved
src/Hint/Restrict.hs Show resolved Hide resolved
The test should not fail, but it does:

> WANT: No hints
> FULL OUTPUT FOR GOT:
> tests/importStyle-3.hs:1:1-36: Warning: HypotheticalModule2 should be imported qualified or with an explicit import list
> Found:
>   import HypotheticalModule2 qualified
> Perhaps:
>   import qualified HypotheticalModule2
> Note: may break the code
>
> tests/importStyle-3.hs:2:1-43: Warning: HypotheticalModule4 should be imported qualified
> Found:
>   import HypotheticalModule4 qualified as HM4
> Perhaps:
>   import qualified HypotheticalModule4 as HM4
> Note: may break the code
>
> 2 hints
This fixes the bug showcased in parent commit's test.
Including both the "positive" scenarios (intended usage allowed by the
rule; no lint emitted) -- and "negative", where the corresponding rule
is intended to emit suggestions.
@ulidtko ulidtko force-pushed the fix/postqualified-imports branch from 2ffc129 to 0343ec5 Compare September 17, 2023 06:40
Copy link
Contributor Author

@ulidtko ulidtko left a comment

Choose a reason for hiding this comment

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

Hi @zliu41, thanks for review.

I'm inclined to remove the "should be imported post-qualified" hint [...]

No, please don't. In a large enough codebase, a "stop the world" PR with mass-rewrite of most imports has negligible chances to ever land, due to pervasive conflicts.

That's why an incremental approach can be preferred, based on hlint rules about individual modules.

That's also why qualifiedStyle=unrestricted is an important feature: it enables smooth nondisruptive incremental transitions. That can be the only way in some conditions (e.g. big repo with moderate code-churn velocity and strict hlint-compliance) to get PostQualifiedImports.

@ulidtko the CI has been fixed since this PR was opened, can you rebase?

Sure. Done.

I'll going to add clarifying comments in next commit.

src/Hint/Restrict.hs Show resolved Hide resolved
src/Hint/Restrict.hs Show resolved Hide resolved
@ulidtko ulidtko requested a review from zliu41 September 17, 2023 07:56
@shayne-fletcher
Copy link
Collaborator

I'm inclined to remove the "should be imported post-qualified" hint, because there are tools available (e.g., stylish Haskell) for converting pre-qualified imports to post-qualified imports (or vice versa). If one cares about using pre-qualified or post-qualified imports consistently in their project, they can simply run the tool in the pre-commit hook.

Note also that the flag -Wprepositive-qualified-module (off by default) will warn on any prepositive qualified annotations.

@ulidtko
Copy link
Contributor Author

ulidtko commented Sep 17, 2023

Note also that the flag -Wprepositive-qualified-module (off by default) will warn on any prepositive qualified annotations.

Good point. With {-# OPTIONS_GHC -Wprepositive-qualified-module #-} — is possible to migrate module-by-module (as opposed to, across entire project at once).

To explain a motivating usecase further; my point to keep (and fix) qualifiedStyle=unrestricted is with rules like this:

# (project-local) .hlint.yaml

- modules:
  # ...
  - name: Core.Effects,
    importStyle: explicitOrQualified,
    asRequired: True,
    as: Effects,

— which encodes the intent to allow e.g. import Core.Effects (WithMetrics) as well as import qualified Core.Effects as Effects... but, forbids import Core.Effects qualified as Effects, completely by accident.

Copy link
Collaborator

@zliu41 zliu41 left a comment

Choose a reason for hiding this comment

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

Looks good. CI is somehow broken again, so I'll just merge it.

No, please don't. In a large enough codebase, a "stop the world" PR with mass-rewrite of most imports has negligible chances to ever land, due to pervasive conflicts.

A pre-commit hook can be made to run a program (hlint or a code formatter) only on the changed or added files. Alternatively, one can run a program (hlint or a code formatter) only on selected files, which allows them to do the migration piece by piece.

So the real question is, whether something is a formatting issue and can be taken care of by a code formatter, or a code smell that can only be detected by hlint.

@zliu41 zliu41 merged commit e760b31 into ndmitchell:master Sep 19, 2023
0 of 5 checks passed
@ulidtko ulidtko deleted the fix/postqualified-imports branch September 19, 2023 15:13
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.

Incorrect hint for postpositive qualified syntax
4 participants