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

Use cli error chaining + add clickable link to bad config #2602

Merged
merged 22 commits into from
Jun 17, 2024

Conversation

olivroy
Copy link
Collaborator

@olivroy olivroy commented Jun 11, 2024

fix #2588, follows-up to #2418

Thanks @IndrajeetPatil for merging #2418. I think it is a good improvement.

Taking advantage of this to send this up.

Basically uses parent from cli to format error messages.

Add a link to config file.

Tweak some wordings that didn't seem clear to me and use https://style.tidyverse.org/error-messages.html as a reference.

Screenshots of how this looks: (I had more but I erased my console output) (config is a clickable link to .lintr)
image

Changed wording of this one, but made the note about the warning that will disappear a little less prominent
image

Removed some {.cls}. Imo, it decreases readability, by adding < >, I think it reads better as a sentence.

R/lint.R Outdated
x = "Instead, it was {.val {column_number}}."
))
cli_abort(
"{.arg column_number} must be an integer between \\
Copy link
Collaborator

Choose a reason for hiding this comment

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

please don't use the end-of-line \\, I find the behavior pretty confusing & it's not a well-documented part of the R parser.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Note that this repo uses line width=120, so you also don't need to squeeze your code so tightly :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

@MichaelChirico Out of curiosity, what's the kosher way of doing readable, multiline strings? That is, one that doesn't use \\?

Because this is what I do as well 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I ended up not putting \\ but still keeping line break. cli doesn't care about line breaks (or double spaces) like glue does. With cli, you have to put f to force a line break.

Lmk if you want another readjustment!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Out of curiosity, what's the kosher way of doing readable, multiline strings?

paste(), paste0()

R/settings.R Outdated
conditionMessage(e)
))
cli_abort(
"Error from {config_link} setting {.code {setting}} in {.code {format(conditionCall(e))}}:",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is format(conditionCall(e)) redundant with parent=e?

Copy link
Collaborator Author

@olivroy olivroy Jun 12, 2024

Choose a reason for hiding this comment

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

I had not looked closely enough at this one.

Here is what it looks like with my changes
image

I think it could still read a bit better
image
(Fr missing argument with no default

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agree it's nicer to surface 'lint_dir()' as the error source up front.

Copy link
Collaborator Author

@olivroy olivroy Jun 13, 2024

Choose a reason for hiding this comment

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

Oh, sorry. In 07a96a0, I changed this back to Error in read_settings() : since read_settings() is exported by lintr. I could add a call argument to read_settings() if it makes sense to do so, but I didn't want to change anything in the user-facing functions. I can open an issue to do this in a follow-up? Or do it in this PR? Thanks for the second review btw

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, read_settings() is not exported though 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, okay! It is just a forgotten #' @noRd that triggered build failure. I will add this and unrevert my change.

R/settings.R Outdated Show resolved Hide resolved
R/with.R Outdated Show resolved Hide resolved
R/with.R Outdated Show resolved Hide resolved
@MichaelChirico
Copy link
Collaborator

Thanks for the PR! I'll let @IndrajeetPatil add final review.

* Pass call to the malformed error message (to show Error in `lint()`: instead of Error in `read_config_file()`:
* Remove conditionCall(e) since we use `parent = e`
… argument.

Config error will therefore read as

Error in `read_settings()`: instead of Error in `lint()`:
@olivroy olivroy changed the title Improve cli msg II Use cli error chaining + add clickable link to bad config Jun 12, 2024
R/lint.R Outdated Show resolved Hide resolved
R/lint.R Outdated Show resolved Hide resolved
R/settings.R Outdated Show resolved Hide resolved
R/with.R Outdated Show resolved Hide resolved
R/with.R Outdated Show resolved Hide resolved
R/with.R Outdated Show resolved Hide resolved
R/with.R Outdated
if (missing(defaults) || !is.list(defaults) || !all(nzchar(names2(defaults)))) {
cli_abort("{.arg defaults} must be a named list.")
if (missing(defaults)) {
cli_abort("Missing required argument {.arg defaults} should be a named list.")
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 this error message should only mention that the required argument is missing.
"Missing required argument" sounds a bit odd, and combines the error message for two different checks into one.

cli_abort("{.arg defaults} is a required argument, but is missing. [It should be a named list.]") # second sentence is optional

Copy link
Collaborator Author

@olivroy olivroy Jun 14, 2024

Choose a reason for hiding this comment

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

I agree, it would mimic more what rlang::check_required() provides. I can make this change. and adjust test.

! `x` is absent but must be supplied.

When you see this message, you either know what to provide instead, or can look at the documentation

R/settings.R Outdated
config_link <- cli::style_hyperlink( # nolint: object_usage_linter. TODO(#2252).
cli::col_blue(basename(config_file)),
url = paste0("file://", config_file)
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we create a helper to create these clickable links?

We are already doing it twice in settings.R, and maybe will need to do it a few more times in future, and I don't wish for these nolint directives to proliferate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved it to helper. However, still need the nolint directive until #2252 is fixed. It is that the variable created in unused, except in the error message.
The nolint can only be removed if the function is called directly in the cli message

Copy link
Collaborator

@IndrajeetPatil IndrajeetPatil left a comment

Choose a reason for hiding this comment

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

Michael had already thoroughly reviewed it, so I have no further concerns.

Thanks!

@IndrajeetPatil IndrajeetPatil merged commit 10de2ae into r-lib:main Jun 17, 2024
18 checks passed
@olivroy olivroy deleted the 2588 branch June 17, 2024 14:05
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.

Use bulleted list for {cli}-formatted messages involving conditionMessage()
3 participants