diff mbox series

[bug#49671] guix: records: Improve error reporting.

Message ID 20210721014047.3878a0c7@tachikoma.lepiller.eu
State New
Headers show
Series [bug#49671] guix: records: Improve error reporting. | expand

Checks

Context Check Description
cbaines/applying patch fail View Laminar job
cbaines/issue success View issue

Commit Message

Julien Lepiller July 20, 2021, 11:40 p.m. UTC
Hi Guix!

This patch improves error reporting a bit when making mistakes in guix
records. This is motivated by a user getting "invalid field specifier"
for their whole services field in their os record. With this patches,
they would have seen:

multiple values in field specifier. Got 2 values associated with key
services. Values are:
(append (list (service ...) ...))
(modify-services %desktop-services ...)

Which would have hinted them at fixing the parenthesis. Or at least, it
would have saved us some time trying to count them :)

Here are the cases that are handled and the associated message:

(operating-system
  services)
guix system: error: services: invalid field specifier. The format of a
field is `(services value)'

(operating-system
  (services))
test.scm:2:2: error: (services): Value missing in field specifier. The
format of a field is `(services value)'.

(operating-system
  (services 1 2 3))
test.scm:2:2: error: (services 1 2 3): multiple values in field
specifier. Got 3 values associated with key services. Values are:
1
2
3

(operating-system
  ())
guix system: error: (): invalid field specifier. The format of a field
is `(field value)'

(operating-system
  ((services %desktop-services)))
test.scm:2:2: error: ((services %desktop-services)): invalid field
specifier. (services %desktop-services) is not a valid field name.


Of course, we can improve these error messages, and internationalize
them.

WDYT?

Comments

Sarah Morgensen July 21, 2021, 7:21 p.m. UTC | #1
Hi Julien,

Julien Lepiller <julien@lepiller.eu> writes:

> Hi Guix!
>
> This patch improves error reporting a bit when making mistakes in guix
> records. This is motivated by a user getting "invalid field specifier"
> for their whole services field in their os record. With this patches,
> they would have seen:

After applying your patch, I get:

--8<---------------cut here---------------start------------->8---
guix/records.scm:108:19: warning: "multiple values in field specifier. Got ~a values associated with key ~a. Values are:~%~{~a~%~}": unsupported format option ~{, use (ice-9 format) instead
--8<---------------cut here---------------end--------------->8---

After adding `(ice-9 format)` to imports it works as expected. I see
this also applies to package records! This will be great for those
starting to package in Guix.

>
> multiple values in field specifier. Got 2 values associated with key
> services. Values are:
> (append (list (service ...) ...))
> (modify-services %desktop-services ...)
>
> Which would have hinted them at fixing the parenthesis. Or at least, it
> would have saved us some time trying to count them :)
>
> Here are the cases that are handled and the associated message:
>
> (operating-system
>   services)
> guix system: error: services: invalid field specifier. The format of a
> field is `(services value)'
>
> (operating-system
>   (services))
> test.scm:2:2: error: (services): Value missing in field specifier. The
> format of a field is `(services value)'.
>
> (operating-system
>   (services 1 2 3))
> test.scm:2:2: error: (services 1 2 3): multiple values in field
> specifier. Got 3 values associated with key services. Values are:
                                              ^ Wrap in `'?
> 1
> 2
> 3
>
> (operating-system
>   ())
> guix system: error: (): invalid field specifier. The format of a field
> is `(field value)'
>
> (operating-system
>   ((services %desktop-services)))
> test.scm:2:2: error: ((services %desktop-services)): invalid field
> specifier. (services %desktop-services) is not a valid field name.
             ^ Should this also be wrapped in `'?

Why do some of these messages lose their context and come from `guix
system` instead?

>
> Of course, we can improve these error messages, and internationalize
> them.
>
> WDYT?

[...]

> -         (apply syntax-violation name "invalid field specifier"
> -                (if parent-form
> +         (syntax-case #'weird ()
> +           (()                             ;the empty list
> +             (apply syntax-violation name
> +                    "invalid field specifier. The format of a field is `(field value)'"
>                      (list parent-form #'weird)
> -                    (list #'weird)))))))
> +                    (list #'weird)))

Why the extra `(list #'weird')`? AFAICT right now this is providing
`(list parent-form #:'weird)` as the parent form.  And since parent-form
is optional, shouldn't this be

--8<---------------cut here---------------start------------->8---
  (apply syntax-violation name
         "invalid field specifier. The format of a field is `(field value)'"
         (if parent-form (list parent-form #:'weird) (list weird)))
--8<---------------cut here---------------end--------------->8---

(and similar for the others)?

--
Sarah
Ludovic Courtès Aug. 4, 2021, 3:19 p.m. UTC | #2
Hello!

Julien Lepiller <julien@lepiller.eu> skribis:

> Here are the cases that are handled and the associated message:
>
> (operating-system
>   services)
> guix system: error: services: invalid field specifier. The format of a
> field is `(services value)'
>
> (operating-system
>   (services))
> test.scm:2:2: error: (services): Value missing in field specifier. The
> format of a field is `(services value)'.
>
> (operating-system
>   (services 1 2 3))
> test.scm:2:2: error: (services 1 2 3): multiple values in field
> specifier. Got 3 values associated with key services. Values are:
> 1
> 2
> 3
>
> (operating-system
>   ())
> guix system: error: (): invalid field specifier. The format of a field
> is `(field value)'
>
> (operating-system
>   ((services %desktop-services)))
> test.scm:2:2: error: ((services %desktop-services)): invalid field
> specifier. (services %desktop-services) is not a valid field name.
>
>
> Of course, we can improve these error messages, and internationalize
> them.

I like the idea!

I would prefer for error messages to be plain errors, without hints, so:

  test.scm:2:2: error: (services 1 2 3): multiple values in field specifier

(Not a sentence, no period.)

But I’d also like to have hints, and ideally all hints should be
reported consistently, via ‘&fix-hint’ or similar.

Thankfully, we can do that via (guix diagnostics) + SRFI-35/34:

  (raise (condition
           (&syntax (form 'x) (subform 'y))
           (&fix-hint (hint "Consider fixing this."))))

‘call-with-error-handling’ in (guix ui) might need to be adjusted.

The only downside is that (guix records) would now depend on another
Guix modules, which I tried to avoid so far so it could be more easily
reused elsewhere.  But that’s the price to pay to get consistent error
reports, and I think it’s okay.

Note that ‘tests/guix-system.sh’ and a couple of other files test exact
error reports, so we’ll have to make sure it still works and possibly
augment the tests.

WDYT?

Thanks,
Ludo’.
diff mbox series

Patch

From 4cde5555682ca6a3c78cb274cc308087e51ea0e9 Mon Sep 17 00:00:00 2001
From: Julien Lepiller <julien@lepiller.eu>
Date: Wed, 21 Jul 2021 01:30:04 +0200
Subject: [PATCH] guix: records: Improve error reporting.

* guix/records.scm (report-invalid-field-specifier): Handle various
invalidity causes separately.
---
 guix/records.scm | 37 ++++++++++++++++++++++++++++++++++---
 1 file changed, 34 insertions(+), 3 deletions(-)

diff --git a/guix/records.scm b/guix/records.scm
index 3d54a51956..3c3dd478a5 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -1,6 +1,7 @@ 
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2018 Mark H Weaver <mhw@netris.org>
+;;; Copyright © 2021 Julien Lepiller <julien@lepiller.eu>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -83,10 +84,40 @@  error-reporting purposes."
          ;; WEIRD may be an identifier, thus lacking source location info, and
          ;; BINDINGS is a list, also lacking source location info.  Hopefully
          ;; PARENT-FORM provides source location info.
-         (apply syntax-violation name "invalid field specifier"
-                (if parent-form
+         (syntax-case #'weird ()
+           (()                             ;the empty list
+             (apply syntax-violation name
+                    "invalid field specifier. The format of a field is `(field value)'"
                     (list parent-form #'weird)
-                    (list #'weird)))))))
+                    (list #'weird)))
+           (((field ...) _ ...)            ;a list whose first element is a list
+            (apply syntax-violation name
+                   (format #f "invalid field specifier. ~a is not a valid field name."
+                           (map syntax->datum #'(field ...)))
+                   (list parent-form #'weird)
+                   (list #'weird)))
+           ((field)                        ;a list with one element
+            (apply syntax-violation name
+                   (format #f "Value missing in field specifier. The format of \
+a field is `(~a value)'."
+                           (syntax->datum #'field))
+                   (list parent-form #'weird)
+                   (list #'weird)))
+           ((field value ...)              ;any other list
+            (apply syntax-violation name
+                   (format #f "multiple values in field specifier. Got ~a values \
+associated with key ~a. Values are:~%~{~a~%~}"
+                           (length #'(value ...)) (syntax->datum #'field)
+                           (map syntax->datum #'(value ...)))
+                    (list parent-form #'weird)
+                    (list #'weird)))
+           (field                          ;not a list
+             (apply syntax-violation name
+                    (format #f "invalid field specifier. The format of a field \
+is `(~a value)'"
+                            (syntax->datum #'field))
+                    (list parent-form #'weird)
+                    (list #'weird))))))))
 
   (define (report-duplicate-field-specifier name ctor)
     "Report the first duplicate identifier among the bindings in CTOR."
-- 
2.32.0