Skip to content

Detection and warning for common typos in package dependency constraints #11600

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 15, 2025

Conversation

kemsguy7
Copy link
Contributor

@kemsguy7 kemsguy7 commented Apr 3, 2025

@gridbugs, Here's my draft PR for issue. #11575, please kindly review and give feedback let me know if I'm on track.

Copy link
Collaborator

@gridbugs gridbugs left a comment

Choose a reason for hiding this comment

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

Nice work so far! In addition to the comments, please add some cram tests (see the contents of test/blackbox-tests/test-cases/ for examples).

README.md Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

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

Avoid including unrelated changes in PRs. If you want to update the readme file, it's more appropriate to do it in a separate PR.

boot/libs.ml Outdated
@@ -1,4 +1,4 @@
let external_libraries = [ "unix"; "threads" ]
let external_libraries = [ "threads.posix" ]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Dune's build process is a little annoying in that it sometimes changes this file. Get into the habit of not staging changes to this file when making commits to this repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted.

class.sol Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is this file? Please remove it from the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok my bad, will do

(* Function to emit warnings for common typos when processing dependencies *)
let warn_on_typos ~loc dependency =
(* We'll implement a copy of the check_for_version_typo function here
since it's not directly accessible from the other module *)
Copy link
Collaborator

Choose a reason for hiding this comment

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

See if you can come up with a way to avoid this duplication. You're welcome to extend the interface to Dune_lange.Package_dependency if necessary.

( sprintf
"Possible typo in package dependency for %s: '(= version)' might be a mistake."
(Package_name.to_string name)
, "Did you mean to use the `:version` variable instead? Use: (depends (bar :version))" )
Copy link
Collaborator

Choose a reason for hiding this comment

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

In the example, instead of (depends (bar :version)) it would be better to reproduce the text of the constraint the user actually used, but corrected.

(* Check for typos and emit warnings *)
(match check_for_version_typo result with
| Some (msg, suggestion) ->
User_warning.emit [Pp.text msg; Pp.text suggestion]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Take a look at User_message.make. You can create a message, possibly including hints which are formatted differently from the regular warning body and would be suitable for the "Did you mean..." parts of the warning. The message can then be printed as a warning with User_warning.emit_message.

So concretely I'm suggesting that you change the check_for_version_type function to return a User_message.t option.

"Possible typo in package dependency for %s: '(= version)' might be a mistake."
(Package_name.to_string name)
, "Did you mean to use the `:version` variable instead? Use: (depends (bar :version))" )
| Some (Bop (Relop.Eq, Value.String_literal "version", _)) ->
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we only expect version comparisons to use the Uop constructor.

| Some (Uop (Relop.Eq, Value.String_literal "version")) ->
Some
( sprintf
"Possible typo in package dependency for %s: '(= version)' might be a mistake."
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it would be more accurate to say "Possible typo in constraint for dependency %S: ...".

(match Constraint.opt_of_opam_condition condition with
| Ok constraint_ ->
let dep = { name; constraint_ } in
warn_on_typos ~loc dep;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will this print warnings for projects that don't use dune package management?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think printing the warning in decode is sufficient, since for all operations dune will need to decode the dune-project file anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll look at it again and give feedback.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gridbugs , I've added fixes based on your suggestions apart from cram tests, I'm currently working on that. Please kindly review and let me know if I'm on track, thanks.

Copy link
Collaborator

@gridbugs gridbugs left a comment

Choose a reason for hiding this comment

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

Looking pretty good. I've left several minor suggestions. Looking forward to seeing the tests.

README.md Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

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

Try to keep the diff to just contain the meaningful part of your change (e.g. explicitly stage individual files rather than doing git commit -a).

@@ -63,4 +96,4 @@ let to_dyn { name; constraint_ } =
let equal { name; constraint_ } t =
Package_name.equal name t.name
&& Option.equal Package_constraint.equal constraint_ t.constraint_
;;
;;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Try to avoid adding/removing the trailing newline from the diff (ie. only include the meaningful part of your change). Some editors automatically mess with file endings. The problem with checking this in is that the next person who touches the file will have to deal with the changed line ending from their editor and so on. My advice would be to configure your editor to leave file endings alone, and also to use git add -p or similar to only stage the meaningful changes.

"Possible typo in constraint for dependency %S: '(= version)' might be a mistake."
(Package_name.to_string name)
]
~hints:[ Pp.text "Did you mean to use the `:version` variable instead? Example: (depends (bar :version))" ]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of (depends (bar :version)), it would be better to print the actual package name from the user's dune-project. Also (depends (bar :version)) isn't the right syntax; it should be (depends (bar (= :version))).

"Possible typo in constraint for dependency %S: ':with_test' might be a mistake."
(Package_name.to_string name)
]
~hints:[ Pp.text "Did you mean to use ':with-test' instead? Example: (depends (bar :with-test))" ]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Replace the bar with the package name from the offending line.

| Ok constraint_ -> { name; constraint_ }
| Ok constraint_ ->
let dep = { name; constraint_ } in
(* We don't need to check for typos here since it's already handled in decode *)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this comment is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gridbugs Thanks for the feedback, I've noted all requested changes and will adjust accordingly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gridbugs
Screenshot 2025-04-07 at 05 12 04
, here are my updates, attached to this comment is a screenshot showing the result of running the test

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice. Please include the test in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice. Please include the test in this PR.

Added , please kindly review

@@ -229,7 +229,9 @@ let list_of_opam_disjunction loc filtered_formula =
| Atom (name, condition) ->
let name = Package_name.of_opam_package_name name in
(match Constraint.opt_of_opam_condition condition with
| Ok constraint_ -> { name; constraint_ }
| Ok constraint_ ->
let dep = { name; constraint_ } in
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's no need for this let binding anymore. You can just revert this file to its original state.

Copy link
Contributor Author

@kemsguy7 kemsguy7 Apr 7, 2025

Choose a reason for hiding this comment

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

There's no need for this let binding anymore. You can just revert this file to its original state.

@gridbugs Updated.

@@ -229,7 +229,7 @@ let list_of_opam_disjunction loc filtered_formula =
| Atom (name, condition) ->
let name = Package_name.of_opam_package_name name in
(match Constraint.opt_of_opam_condition condition with
| Ok constraint_ -> { name; constraint_ }
| Ok constraint_ -> {name; constraint_}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Run dune fmt to automatically fix the style.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright @gridbugs, thanks for your guiding me all the way. Implementing final changes now.

@gridbugs
Copy link
Collaborator

gridbugs commented Apr 8, 2025

This is getting pretty close to done. A few things left before I'll consider merging it:

  • undo your changes to the readme
  • fix the style (run dune fmt)
  • squash all the commits into a single commit, and sign that commit (ie. git commit -s ...)

I'll mark this as not a draft so others can take a look if they want.

@gridbugs gridbugs marked this pull request as ready for review April 8, 2025 07:05
@kemsguy7 kemsguy7 force-pushed the kemsguy7/typo-detection branch 2 times, most recently from efdef19 to d7af4b7 Compare April 8, 2025 17:32
@kemsguy7
Copy link
Contributor Author

kemsguy7 commented Apr 9, 2025

This is getting pretty close to done. A few things left before I'll consider merging it:

  • undo your changes to the readme
  • fix the style (run dune fmt)
  • squash all the commits into a single commit, and sign that commit (ie. git commit -s ...)

I'll mark this as not a draft so others can take a look if they want.

@gridbugs all done, please kindly review

README.md Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please remove unrelated changes from the readme.

@gridbugs
Copy link
Collaborator

Looks like your commit is not signed (the "DCO" test is failing). Run git commit --amend -s.

@kemsguy7
Copy link
Contributor Author

Looks like your commit is not signed (the "DCO" test is failing). Run git commit --amend -s.

Okay thanks, let me try again.

@gridbugs
Copy link
Collaborator

One more thing I'll ask of you is to add a file to doc/changes describing the change (follow the example of the other files in that directory).

@kemsguy7
Copy link
Contributor Author

kemsguy7 commented Apr 10, 2025

One more thing I'll ask of you is to add a file to doc/changes describing the change (follow the example of the other files in that directory).

Alright mentor, on it.

@kemsguy7 kemsguy7 force-pushed the kemsguy7/typo-detection branch from d7af4b7 to e62f5df Compare April 10, 2025 01:31
@kemsguy7
Copy link
Contributor Author

kemsguy7 commented Apr 11, 2025

One more thing I'll ask of you is to add a file to doc/changes describing the change (follow the example of the other files in that directory).

@gridbugs I've added the docs. Please kindly review. Thanks.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the detailed explanation, but this just needed to be a single line for our change log. You're welcome to write more docs elsewhere but I think for this particular change it won't be necessary. I'll fix up this file to match other changelog entries before and merge this PR today.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, noted with thanks.

@gridbugs gridbugs force-pushed the kemsguy7/typo-detection branch from 8ab32bc to debedeb Compare April 14, 2025 02:00
@gridbugs gridbugs changed the title Kemsguy7/typo detection Detection and warning for common typos in package dependency constraints Apr 14, 2025
@gridbugs gridbugs force-pushed the kemsguy7/typo-detection branch 2 times, most recently from 4eeb981 to 97f4491 Compare April 14, 2025 04:03
@kemsguy7 kemsguy7 force-pushed the kemsguy7/typo-detection branch from 61aca2c to c6bb552 Compare April 15, 2025 00:24
@kemsguy7
Copy link
Contributor Author

@gridbugs , added updates, please kindly review, thanks

@gridbugs gridbugs force-pushed the kemsguy7/typo-detection branch from c6bb552 to 38b9b8d Compare April 15, 2025 04:24
@gridbugs gridbugs merged commit df45715 into ocaml:main Apr 15, 2025
24 of 25 checks passed
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this is related to package management, shouldn't we move this to an experimental subfolder or not add a changelog?

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.

3 participants