-
Notifications
You must be signed in to change notification settings - Fork 437
feat: add configurable typo detection for package dependency constraints #11759
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,7 @@ let make ~name ~of_string ~default = | |
;; | ||
|
||
let make_toggle ~name ~default = make ~name ~default ~of_string:Toggle.of_string | ||
|
||
let global_lock = make ~name:"global_lock" ~of_string:Toggle.of_string ~default:`Enabled | ||
|
||
let cutoffs_that_reduce_concurrency_in_watch_mode = | ||
|
@@ -147,3 +148,7 @@ let threaded_console_frames_per_second = | |
| None -> Error (sprintf "could not parse %S as an integer" x)) | ||
~default:`Default | ||
;; | ||
|
||
let typo_warnings = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should have a more descriptive name, as it only enables checking for a very specific type of typo. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean this file config.ml? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean the variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't resolved. The variable is still named |
||
make ~name:"typo_warnings" ~of_string:Toggle.of_string ~default:`Enabled | ||
;; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,10 +60,13 @@ val threaded_console : Toggle.t t | |
(** The number of frames per second for the threaded console. *) | ||
val threaded_console_frames_per_second : [ `Default | `Custom of int ] t | ||
|
||
(** Control whether to show warnings for common typos in package dependencies *) | ||
val typo_warnings : Toggle.t t | ||
|
||
(** Before any configuration value is accessed, this function must be called | ||
with all the configuration values from the relevant config file | ||
([dune-workspace], or [dune-config]). | ||
|
||
Note that environment variables take precedence over the values passed here | ||
for easy overriding. *) | ||
val init : (Loc.t * string) String.Map.t -> unit | ||
val init : (Loc.t * string) String.Map.t -> unit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not remove the trailing newlines. It creates changed lines where there are no changes actually. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,57 +28,62 @@ module Well_formed_name = struct | |
module TT = Dune_util.Stringlike.Make (T) | ||
|
||
let decode = | ||
let open Decoder in | ||
let open Dune_sexp.Decoder in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did something change to make this addition necessary now? |
||
TT.decode >>| T.to_package_name | ||
;; | ||
end | ||
|
||
let encode { name; constraint_ } = | ||
let open Encoder in | ||
let open Dune_sexp.Encoder in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this change necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reverted. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still see the change in the diff. Can you remove this change (and the one above that I commented on). |
||
match constraint_ with | ||
| None -> Package_name.encode name | ||
| Some c -> pair Package_name.encode Package_constraint.encode (name, c) | ||
;; | ||
|
||
(* Check for common typos in package dependency constraints *) | ||
kemsguy7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let check_for_typo ~loc { name; constraint_ } = | ||
let open Package_constraint in | ||
match constraint_ with | ||
| Some (Uop (Relop.Eq, Value.String_literal "version")) -> | ||
let message = | ||
User_message.make | ||
~loc | ||
[ Pp.textf | ||
"Possible typo in constraint for dependency %S: '(= version)' might be a \ | ||
mistake." | ||
(Package_name.to_string name) | ||
] | ||
~hints: | ||
[ Pp.textf | ||
"Did you mean to use the `:version` variable instead? Example: (depends \ | ||
(%s (= :version)))" | ||
(Package_name.to_string name) | ||
] | ||
in | ||
Some message | ||
| Some (Bvar var) when String.equal (Package_variable_name.to_string var) "with_test" -> | ||
let message = | ||
User_message.make | ||
~loc | ||
[ Pp.textf | ||
"Possible typo in constraint for dependency %S: ':with_test' might be a \ | ||
mistake." | ||
(Package_name.to_string name) | ||
] | ||
~hints: | ||
[ Pp.textf | ||
"Did you mean to use ':with-test' instead? Example: (depends (%s \ | ||
:with-test))" | ||
(Package_name.to_string name) | ||
] | ||
in | ||
Some message | ||
| _ -> None | ||
let dependency_constraint_variable_typo_warnings ~loc { name; constraint_ } = | ||
match Dune_config.Config.(get typo_warnings) with | ||
| `Enabled -> | ||
(match constraint_ with | ||
| Some | ||
(Package_constraint.Uop | ||
(Relop.Eq, Package_constraint.Value.String_literal "version")) -> | ||
let message = | ||
User_message.make | ||
~loc | ||
[ Pp.textf | ||
"Possible typo in constraint for dependency %S: '(= version)' might be a \ | ||
mistake." | ||
(Package_name.to_string name) | ||
] | ||
~hints: | ||
[ Pp.textf | ||
"Did you mean to use the `:version` variable instead? Example: (depends \ | ||
(%s (= :version)))" | ||
(Package_name.to_string name) | ||
] | ||
in | ||
Some message | ||
| Some (Package_constraint.Bvar var) | ||
when String.equal (Package_variable_name.to_string var) "with_test" -> | ||
let message = | ||
User_message.make | ||
~loc | ||
[ Pp.textf | ||
"Possible typo in constraint for dependency %S: ':with_test' might be a \ | ||
mistake." | ||
(Package_name.to_string name) | ||
] | ||
~hints: | ||
[ Pp.textf | ||
"Did you mean to use ':with-test' instead? Example: (depends (%s \ | ||
:with-test))" | ||
(Package_name.to_string name) | ||
] | ||
in | ||
Some message | ||
| _ -> None) | ||
| `Disabled -> None | ||
;; | ||
|
||
let decode = | ||
|
@@ -88,8 +93,7 @@ let decode = | |
F438 and+ name = Package_name.decode | ||
and+ expr = Package_constraint.decode in | ||
let result = { name; constraint_ = Some expr } in | ||
(* Check for typos and emit warnings *) | ||
(match check_for_typo ~loc result with | ||
(match dependency_constraint_variable_typo_warnings ~loc result with | ||
| Some msg -> User_warning.emit_message msg | ||
| None -> ()); | ||
result | ||
|
kemsguy7 marked this conversation as resolved.
Show resolved
Hide resolved
kemsguy7 marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
Test detection and disabling of common typos in package dependencies | ||
|
||
First, create a dune-project file with typos to see warnings with default settings (warnings enabled) | ||
$ cat > dune-project <<EOF | ||
> (lang dune 3.11) | ||
> (package | ||
> (name foo) | ||
> (allow_empty) | ||
> (depends | ||
> (bar (= version)) ; Should detect typo and suggest :version | ||
> (baz :with_test) ; Should detect typo and suggest :with-test | ||
> qux)) | ||
> EOF | ||
|
||
$ dune pkg lock | ||
File "dune-project", line 6, characters 3-20: | ||
6 | (bar (= version)) ; Should detect typo and suggest :version | ||
^^^^^^^^^^^^^^^^^ | ||
Warning: Possible typo in constraint for dependency "bar": '(= version)' might be a mistake. | ||
Hint: Did you mean to use the `:version` variable instead? Example: (depends (bar (= :version))) | ||
File "dune-project", line 7, characters 3-19: | ||
7 | (baz :with_test) ; Should detect typo and suggest :with-test | ||
^^^^^^^^^^^^^^^^ | ||
Warning: Possible typo in constraint for dependency "baz": ':with_test' might be a mistake. | ||
Hint: Did you mean to use ':with-test' instead? Example: (depends (baz :with-test)) | ||
|
||
Now demonstrate that fixed dependencies don't cause warnings | ||
$ cat > fixed-dune-project <<EOF | ||
> (lang dune 3.11) | ||
> (package | ||
> (name foo) | ||
> (allow_empty) | ||
> (depends | ||
> (bar (= :version)) ; Fixed version | ||
> (baz :with-test) ; Fixed with-test | ||
> qux)) | ||
> EOF | ||
|
||
$ dune pkg lock --project-file=fixed-dune-project | ||
|
||
Now test that we can disable warnings via the environment variable | ||
$ DUNE_CONFIG__TYPO_WARNINGS=disabled dune pkg lock | ||
|
||
$ # Create clean directory for next test | ||
$ mkdir disable_test | ||
$ cd disable_test | ||
|
||
$ cat > dune-project <<EOF | ||
> (lang dune 3.11) | ||
> (package | ||
> (name foo) | ||
> (allow_empty) | ||
> (depends | ||
> (bar (= version)) ; Should NOT show warning with typo_warnings disabled | ||
> (baz :with_test) ; Should NOT show warning with typo_warnings disabled | ||
> qux)) | ||
> EOF | ||
|
||
$ DUNE_CONFIG__TYPO_WARNINGS=disabled dune pkg lock | ||
$ # No warnings should be produced when the feature is disabled | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no command being run here, so it is missing the test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please try to remember to run the autoformatter on your changes. I thought we set up your vscode to automatically format files when you save them. Did that stop working?