GNU bug report logs - #56075
[PATCH 0/2] Report location of invalid configuration field values

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: guix-patches; Reported by: Ludovic Courtès <ludo@HIDDEN>; Keywords: patch; dated Sat, 18 Jun 2022 21:38:01 UTC; Maintainer for guix-patches is guix-patches@HIDDEN.

Message received at 56075 <at> debbugs.gnu.org:


Received: (at 56075) by debbugs.gnu.org; 23 Jun 2022 16:05:28 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Jun 23 12:05:28 2022
Received: from localhost ([127.0.0.1]:39642 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1o4PKh-0005LC-T2
	for submit <at> debbugs.gnu.org; Thu, 23 Jun 2022 12:05:28 -0400
Received: from mail-qk1-f169.google.com ([209.85.222.169]:40713)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <maxim.cournoyer@HIDDEN>) id 1o4PKe-0005Kt-RO
 for 56075 <at> debbugs.gnu.org; Thu, 23 Jun 2022 12:05:27 -0400
Received: by mail-qk1-f169.google.com with SMTP id p21so3289139qki.7
 for <56075 <at> debbugs.gnu.org>; Thu, 23 Jun 2022 09:05:24 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
 h=from:to:cc:subject:references:date:in-reply-to:message-id
 :user-agent:mime-version:content-transfer-encoding;
 bh=j+qHXjpljw2+Iitx9Om3Glg/6jXKw2zb7ntKs1UCClQ=;
 b=U5WZPmCt1QhQkb/uzDDVLeA6F/sG/idbRQqvVqeg6KVzvQdPPx3uvWW6pgFwanmsGs
 LgSQQlkjZMWjI+OdV9OVxeHC7PCC4jOBqIPWsbapkFXjWsAam5SrteayvRWpqslqN0H9
 03f2ohNU1hfYLzZPyvQMia24Z5JTbL3mMRjgCW5KUdkaCw1sTIonWgx2sPVfWskHV/CI
 AJiCLL01MYxuZ6RCjtjh3YltH52IVoEEKhzgoo94tFLJHmocVEA+wOfBO7JZGEnLHg6u
 /YROKkGgPFAYXoWOvYTcwoe/hv6fbEppMxStgEaWIXq1VoM+0xbHnlD/5kE/JpbyMLaq
 HQHg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20210112;
 h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to
 :message-id:user-agent:mime-version:content-transfer-encoding;
 bh=j+qHXjpljw2+Iitx9Om3Glg/6jXKw2zb7ntKs1UCClQ=;
 b=cEzIRfJIFz6/Y7UiU/fyvQOohQ7m9gtw8r8OZyE9QEUkjIDz9eu2DCLulz/4p+hSrW
 qGeM0tSwh3fMpqZYke+668mNtsoYerDBymN6kRPAJZMOk4Y9zwHbctxqTP1B9UUtsjK7
 Ijr6MeGIqSjzDa8Cr9EL/3H7PMTULK2XiJd+an3n6fbHvlkepFKn14t4p2PLJzbrWmol
 KAR9zzA+6raVelSXrV6I1wfn0SIQdZt0eWzIc7O8WhrszBS5RLHAAZFaddFSn3fWKpUF
 AhC2PHWFNiV+WxThlpwqwNgb46XiRwvaCBmLlzQSDJDO0xWH0vSwiTPh2pegdiqYgY2l
 Ivfg==
X-Gm-Message-State: AJIora9zGaPtA32Eq0sQZ3+QluehrEdnfonJCQHLDq/z0BkLRHymcmCt
 4dz7eyygrmg8uqpve/CbLROGYV2vzHgw1BmV
X-Google-Smtp-Source: AGRyM1vvHyeLc8RBb/V6yOVsoFajhJ3hMjt3nTakxEfD4FZ5IvY4O97Qdgoj3upOlUj4HFKYm6TX7g==
X-Received: by 2002:a37:a5d0:0:b0:6a6:7cbc:dc2a with SMTP id
 o199-20020a37a5d0000000b006a67cbcdc2amr6943012qke.496.1656000318421; 
 Thu, 23 Jun 2022 09:05:18 -0700 (PDT)
Received: from hurd (dsl-158-129.b2b2c.ca. [66.158.158.129])
 by smtp.gmail.com with ESMTPSA id
 ey6-20020a05622a4c0600b0030a9dfb2898sm10375965qtb.85.2022.06.23.09.05.17
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Thu, 23 Jun 2022 09:05:18 -0700 (PDT)
From: Maxim Cournoyer <maxim.cournoyer@HIDDEN>
To: Ludovic =?utf-8?Q?Court=C3=A8s?= <ludo@HIDDEN>
Subject: Re: [bug#56075] [PATCH 1/2] services: configuration: Report the
 location of field type errors.
References: <20220618213640.25039-1-ludo@HIDDEN>
 <20220618213832.25165-1-ludo@HIDDEN>
Date: Thu, 23 Jun 2022 12:05:16 -0400
In-Reply-To: <20220618213832.25165-1-ludo@HIDDEN> ("Ludovic =?utf-8?Q?Cou?=
 =?utf-8?Q?rt=C3=A8s=22's?=
 message of "Sat, 18 Jun 2022 23:38:31 +0200")
Message-ID: <87y1xn2wn7.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-Spam-Score: -0.0 (/)
X-Debbugs-Envelope-To: 56075
Cc: 56075 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

Hello,

Ludovic Court=C3=A8s <ludo@HIDDEN> writes:

> Previously field type errors would be reported in a non-standard way,
> and without any source location information.  This fixes it.
>
> * gnu/services/configuration.scm (configuration-field-error): Add a
> 'loc' parameter and honor it.  Use 'formatted-message' instead of plain
> 'format'.
> (define-configuration-helper)[field-sanitizer]: New procedure.
> Use it.  Use STEM as the identifier of the syntactic constructor of the
> record type.  Add a 'sanitize' property to each field.  Remove now
> useless STEM macro that would call 'validate-configuration'.
> * gnu/services/mail.scm (serialize-listener-configuration): Adjust to
> new 'configuration-field-error' prototype.
> * tests/services/configuration.scm ("wrong type for a field"): New test.
> * po/guix/POTFILES.in: Add gnu/services/configuration.scm.

Very nice!  I had been meaning to look at what define-configure could be
improved w.r.t. the recently added sanitizers; I felt perhaps
`define-configure' would perhaps loose its relevance, but I'm happy to
see you saw value in upgrading it!

The first part LGTM, although I so rarely dabble with syntax-case that
it looks a bit alien to my eyes now.

[...]

> --- a/tests/services/configuration.scm
> +++ b/tests/services/configuration.scm
> @@ -19,6 +19,7 @@

You forgot to add your copyright notice line.

>  (define-module (tests services configuration)
>    #:use-module (gnu services configuration)
> +  #:use-module (guix diagnostics)
>    #:use-module (guix gexp)
>    #:use-module (srfi srfi-34)
>    #:use-module (srfi srfi-64))
> @@ -43,6 +44,17 @@ (define-configuration port-configuration
>    80
>    (port-configuration-port (port-configuration)))
>
> +(test-equal "wrong type for a field"
> +  '("configuration.scm" 56 11)                    ;error location
> +  (guard (c ((configuration-error? c)
> +             (let ((loc (error-location c)))
> +               (list (basename (location-file loc))
> +                     (location-line loc)
> +                     (location-column loc)))))
> +    (port-configuration
> +     ;; This is line 55; the test relies on line/column numbers!
> +     (port "This is not a number!"))))
> +

It seems fragile to rely on the line/column number, but if we truly need
to test that, I don't see a better options.

Thanks,

Maxim




Information forwarded to guix-patches@HIDDEN:
bug#56075; Package guix-patches. Full text available.

Message received at 56075 <at> debbugs.gnu.org:


Received: (at 56075) by debbugs.gnu.org; 18 Jun 2022 21:38:48 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sat Jun 18 17:38:48 2022
Received: from localhost ([127.0.0.1]:50286 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1o2g9Y-0004WB-Eb
	for submit <at> debbugs.gnu.org; Sat, 18 Jun 2022 17:38:48 -0400
Received: from eggs.gnu.org ([209.51.188.92]:35240)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <ludo@HIDDEN>) id 1o2g9W-0004Vo-Th
 for 56075 <at> debbugs.gnu.org; Sat, 18 Jun 2022 17:38:47 -0400
Received: from fencepost.gnu.org ([2001:470:142:3::e]:45072)
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <ludo@HIDDEN>)
 id 1o2g9R-0006fc-MM; Sat, 18 Jun 2022 17:38:41 -0400
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org;
 s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To:
 From; bh=kOrUXQ7Z8s/HeG6itWK3lXB0Vi+vJNd8r7NpAp1xodM=; b=JcLas+03Oy7sCKLB5ybx
 YVhRdxI5tEJjuXK2ZWKWVLUi0on8MasGXuLDaBKPFuy0wyIFGUzoBHeAThL8pMKoRR7ptUPUtf0Fh
 84rWIdE4TZilkvx0y4B5Me3VDBtfFvRIDHLl72dnhBL8C4eoUZS+V14vEzT23SnPSaIfu6Jn+lNr2
 qP2/67iPOSo1BDt5Qy9ai+Pw4mQJmoIV/Qnlcbrw8VMD91XnMKfeu4oFQKpGqF1RDKTsXuaj9f8Fs
 RXNxLrhlQnXUNQ86w1xsiptPZhayxJ/DN/TdGH0bxiCWJ0eMFfjCw1/9xzjCJmThfXXHKXpLJjBB/
 xxvgVbsoZ23nKg==;
Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201]:58587
 helo=gnu.org)
 by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <ludo@HIDDEN>)
 id 1o2g9R-00054e-A8; Sat, 18 Jun 2022 17:38:41 -0400
From: =?UTF-8?q?Ludovic=20Court=C3=A8s?= <ludo@HIDDEN>
To: 56075 <at> debbugs.gnu.org
Subject: [PATCH 2/2] services: configuration: Remove 'validate-configuration'.
Date: Sat, 18 Jun 2022 23:38:32 +0200
Message-Id: <20220618213832.25165-2-ludo@HIDDEN>
X-Mailer: git-send-email 2.36.1
In-Reply-To: <20220618213832.25165-1-ludo@HIDDEN>
References: <20220618213832.25165-1-ludo@HIDDEN>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 56075
Cc: =?UTF-8?q?Ludovic=20Court=C3=A8s?= <ludo@HIDDEN>
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

Now that configuration records use the 'sanitize' property for each
field, 'validate-configuration' has become useless because it's
impossible to construct an invalid configuration record.

* gnu/services/configuration.scm (validate-configuration): Remove.
* gnu/services/mail.scm (dovecot-service): Remove call.
* gnu/services/vpn.scm (openvpn-client-service)
(openvpn-server-service): Likewise.
* doc/guix.texi (Complex Configurations): Remove documentation.
---
 doc/guix.texi                  | 6 ------
 gnu/services/configuration.scm | 9 ---------
 gnu/services/mail.scm          | 4 ----
 gnu/services/vpn.scm           | 2 --
 4 files changed, 21 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 86348fc02c..45f2620476 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -38886,12 +38886,6 @@ Return a G-expression that contains the values corresponding to the
 disk by using something like @code{mixed-text-file}.
 @end deffn
 
-@deffn {Scheme Procedure} validate-configuration @var{configuration}
-@var{fields}
-Type-check @var{fields}, a list of field names of @var{configuration}, a
-configuration record created by @code{define-configuration}.
-@end deffn
-
 @deffn {Scheme Procedure} empty-serializer @var{field-name} @var{value}
 A serializer that just returns an empty string.  The
 @code{serialize-package} procedure is an alias for this.
diff --git a/gnu/services/configuration.scm b/gnu/services/configuration.scm
index c39ea5a02a..e3c101d042 100644
--- a/gnu/services/configuration.scm
+++ b/gnu/services/configuration.scm
@@ -57,7 +57,6 @@ (define-module (gnu services configuration)
             serialize-configuration
             define-maybe
             define-maybe/no-serialization
-            validate-configuration
             generate-documentation
             configuration->documentation
             empty-serializer
@@ -125,14 +124,6 @@ (define (serialize-configuration config fields)
                 ((configuration-field-getter field) config)))
              fields)))
 
-(define (validate-configuration config fields)
-  (for-each (lambda (field)
-              (let ((val ((configuration-field-getter field) config)))
-                (unless ((configuration-field-predicate field) val)
-                  (configuration-field-error
-                   (configuration-field-name field) val))))
-            fields))
-
 (define-syntax-rule (id ctx parts ...)
   "Assemble PARTS into a raw (unhygienic) identifier."
   (datum->syntax ctx (symbol-append (syntax->datum parts) ...)))
diff --git a/gnu/services/mail.scm b/gnu/services/mail.scm
index c2fd4d8670..10e6523861 100644
--- a/gnu/services/mail.scm
+++ b/gnu/services/mail.scm
@@ -1610,10 +1610,6 @@ (define* (dovecot-service #:key (config (dovecot-configuration)))
 by @code{dovecot-configuration}.  @var{config} may also be created by
 @code{opaque-dovecot-configuration}, which allows specification of the
 @code{dovecot.conf} as a string."
-  (validate-configuration config
-                          (if (opaque-dovecot-configuration? config)
-                              opaque-dovecot-configuration-fields
-                              dovecot-configuration-fields))
   (service dovecot-service-type config))
 
 ;; A little helper to make it easier to document all those fields.
diff --git a/gnu/services/vpn.scm b/gnu/services/vpn.scm
index 8be632d55f..ec9ef6b6f0 100644
--- a/gnu/services/vpn.scm
+++ b/gnu/services/vpn.scm
@@ -540,11 +540,9 @@ (define openvpn-client-service-type
 to an existing @acronym{VPN, virtual private network}.")))
 
 (define* (openvpn-client-service #:key (config (openvpn-client-configuration)))
-  (validate-configuration config openvpn-client-configuration-fields)
   (service openvpn-client-service-type config))
 
 (define* (openvpn-server-service #:key (config (openvpn-server-configuration)))
-  (validate-configuration config openvpn-server-configuration-fields)
   (service openvpn-server-service-type config))
 
 (define (generate-openvpn-server-documentation)
-- 
2.36.1





Information forwarded to guix-patches@HIDDEN:
bug#56075; Package guix-patches. Full text available.

Message received at 56075 <at> debbugs.gnu.org:


Received: (at 56075) by debbugs.gnu.org; 18 Jun 2022 21:38:48 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sat Jun 18 17:38:48 2022
Received: from localhost ([127.0.0.1]:50284 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1o2g9X-0004W8-V9
	for submit <at> debbugs.gnu.org; Sat, 18 Jun 2022 17:38:48 -0400
Received: from eggs.gnu.org ([209.51.188.92]:35234)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <ludo@HIDDEN>) id 1o2g9W-0004Vn-Ka
 for 56075 <at> debbugs.gnu.org; Sat, 18 Jun 2022 17:38:47 -0400
Received: from fencepost.gnu.org ([2001:470:142:3::e]:45070)
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <ludo@HIDDEN>)
 id 1o2g9R-0006fU-4H; Sat, 18 Jun 2022 17:38:41 -0400
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org;
 s=fencepost-gnu-org; h=MIME-Version:Date:Subject:To:From:in-reply-to:
 references; bh=SLGijAEP8SZ5PZxaqvol+Om7X6k2mVpt45+qQvZxwuM=; b=rHj+/MgRtzf4Hm
 qCrqfAwsBoHgsyPmjF/IGAFgSbF7veP4ZL7HPhE+xwed9fxLyoBTswQ2ZunYL7U8qEbD0t0bf0IKV
 zhnqRjSW/ihyUbgVUrMbQH/V+cHOCPmxRI2Dkpbr6hXDrcmP8Bi1NeaYvtVLbWhyMJ/68vwAKP/e4
 IN4TXYL2CUwbifC7bunTjAyYcFajZ4g0h4pkxc+jutF+6NWeng5nr0KxSDkVj83qDt8dDx9uzWsaj
 +B2Dht5KfJPtdKE9pOmkiSi1c4eEkOsPlu89QrbLWicZDPpN3PIGeWRmr7FHti8hOerJfEiegL5+P
 wXsQsVk2U6w3y6FDn+fg==;
Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201]:58587
 helo=gnu.org)
 by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <ludo@HIDDEN>)
 id 1o2g9Q-00054e-Nx; Sat, 18 Jun 2022 17:38:40 -0400
From: =?UTF-8?q?Ludovic=20Court=C3=A8s?= <ludo@HIDDEN>
To: 56075 <at> debbugs.gnu.org
Subject: [PATCH 1/2] services: configuration: Report the location of field
 type errors.
Date: Sat, 18 Jun 2022 23:38:31 +0200
Message-Id: <20220618213832.25165-1-ludo@HIDDEN>
X-Mailer: git-send-email 2.36.1
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 56075
Cc: =?UTF-8?q?Ludovic=20Court=C3=A8s?= <ludo@HIDDEN>
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

Previously field type errors would be reported in a non-standard way,
and without any source location information.  This fixes it.

* gnu/services/configuration.scm (configuration-field-error): Add a
'loc' parameter and honor it.  Use 'formatted-message' instead of plain
'format'.
(define-configuration-helper)[field-sanitizer]: New procedure.
Use it.  Use STEM as the identifier of the syntactic constructor of the
record type.  Add a 'sanitize' property to each field.  Remove now
useless STEM macro that would call 'validate-configuration'.
* gnu/services/mail.scm (serialize-listener-configuration): Adjust to
new 'configuration-field-error' prototype.
* tests/services/configuration.scm ("wrong type for a field"): New test.
* po/guix/POTFILES.in: Add gnu/services/configuration.scm.
---
 gnu/services/configuration.scm   | 55 +++++++++++++++++++++++++-------
 gnu/services/mail.scm            |  2 +-
 po/guix/POTFILES.in              |  1 +
 tests/services/configuration.scm | 12 +++++++
 4 files changed, 57 insertions(+), 13 deletions(-)

diff --git a/gnu/services/configuration.scm b/gnu/services/configuration.scm
index f6b20fb82b..c39ea5a02a 100644
--- a/gnu/services/configuration.scm
+++ b/gnu/services/configuration.scm
@@ -27,7 +27,8 @@ (define-module (gnu services configuration)
   #:use-module (guix records)
   #:use-module (guix gexp)
   #:use-module ((guix utils) #:select (source-properties->location))
-  #:use-module ((guix diagnostics) #:select (formatted-message location-file))
+  #:use-module ((guix diagnostics)
+                #:select (formatted-message location-file &error-location))
   #:use-module ((guix modules) #:select (file-name->module-name))
   #:use-module (guix i18n)
   #:autoload   (texinfo) (texi-fragment->stexi)
@@ -87,9 +88,17 @@ (define-condition-type &configuration-error &error
 (define (configuration-error message)
   (raise (condition (&message (message message))
                     (&configuration-error))))
-(define (configuration-field-error field val)
-  (configuration-error
-   (format #f "Invalid value for field ~a: ~s" field val)))
+(define (configuration-field-error loc field value)
+  (raise (apply
+          make-compound-condition
+          (formatted-message (G_ "invalid value ~s for field '~a'")
+                             value field)
+          (condition (&configuration-error))
+          (if loc
+              (list (condition
+                     (&error-location (location loc))))
+              '()))))
+
 (define (configuration-missing-field kind field)
   (configuration-error
    (format #f "~a configuration missing required field ~a" kind field)))
@@ -210,9 +219,33 @@ (define (define-configuration-helper serialize? serializer-prefix syn)
                                 (id #'stem #'serialize- type))))))
                   #'(field-type ...)
                   #'((custom-serializer ...) ...))))
+         (define (field-sanitizer name pred)
+           ;; Define a macro for use as a record field sanitizer, where NAME
+           ;; is the name of the field and PRED is the predicate that tells
+           ;; whether a value is valid for this field.
+           #`(define-syntax #,(id #'stem #'validate- #'stem #'- name)
+               (lambda (s)
+                 ;; Make sure the given VALUE, for field NAME, passes PRED.
+                 (syntax-case s ()
+                   ((_ value)
+                    (with-syntax ((name #'#,name)
+                                  (pred #'#,pred)
+                                  (loc (datum->syntax #'value
+                                                      (syntax-source #'value))))
+                      #'(if (pred value)
+                            value
+                            (configuration-field-error
+                             (and=> 'loc source-properties->location)
+                             'name value))))))))
+
          #`(begin
+             ;; Define field validation macros.
+             #,@(map field-sanitizer
+                     #'(field ...)
+                     #'(field-predicate ...))
+
              (define-record-type* #,(id #'stem #'< #'stem #'>)
-               #,(id #'stem #'% #'stem)
+               stem
                #,(id #'stem #'make- #'stem)
                #,(id #'stem #'stem #'?)
                (%location #,(id #'stem #'stem #'-location)
@@ -220,10 +253,13 @@ (define-record-type* #,(id #'stem #'< #'stem #'>)
                                           source-properties->location))
                           (innate))
                #,@(map (lambda (name getter def)
-                         #`(#,name #,getter (default #,def)))
+                         #`(#,name #,getter (default #,def)
+                                   (sanitize
+                                    #,(id #'stem #'validate- #'stem #'- name))))
                        #'(field ...)
                        #'(field-getter ...)
                        #'(field-default ...)))
+
              (define #,(id #'stem #'stem #'-fields)
                (list (configuration-field
                       (name 'field)
@@ -240,12 +276,7 @@ (define #,(id #'stem #'stem #'-fields)
                                '#,(id #'stem #'% #'stem) 'field)
                               field-default)))
                       (documentation doc))
-                     ...))
-             (define-syntax-rule (stem arg (... ...))
-               (let ((conf (#,(id #'stem #'% #'stem) arg (... ...))))
-                 (validate-configuration conf
-                                         #,(id #'stem #'stem #'-fields))
-                 conf))))))))
+                     ...))))))))
 
 (define no-serialization         ;syntactic keyword for 'define-configuration'
   '(no serialization))
diff --git a/gnu/services/mail.scm b/gnu/services/mail.scm
index d99743ac31..c2fd4d8670 100644
--- a/gnu/services/mail.scm
+++ b/gnu/services/mail.scm
@@ -285,7 +285,7 @@ (define (serialize-listener-configuration field-name val)
     (serialize-fifo-listener-configuration field-name val))
    ((inet-listener-configuration? val)
     (serialize-inet-listener-configuration field-name val))
-   (else (configuration-field-error field-name val))))
+   (else (configuration-field-error #f field-name val))))
 (define (listener-configuration-list? val)
   (and (list? val) (and-map listener-configuration? val)))
 (define (serialize-listener-configuration-list field-name val)
diff --git a/po/guix/POTFILES.in b/po/guix/POTFILES.in
index 201e5dcc87..f50dd00422 100644
--- a/po/guix/POTFILES.in
+++ b/po/guix/POTFILES.in
@@ -4,6 +4,7 @@ gnu.scm
 gnu/packages.scm
 gnu/services.scm
 gnu/system.scm
+gnu/services/configuration.scm
 gnu/services/shepherd.scm
 gnu/home/services.scm
 gnu/home/services/ssh.scm
diff --git a/tests/services/configuration.scm b/tests/services/configuration.scm
index 334a1e409b..cf3e504233 100644
--- a/tests/services/configuration.scm
+++ b/tests/services/configuration.scm
@@ -19,6 +19,7 @@
 
 (define-module (tests services configuration)
   #:use-module (gnu services configuration)
+  #:use-module (guix diagnostics)
   #:use-module (guix gexp)
   #:use-module (srfi srfi-34)
   #:use-module (srfi srfi-64))
@@ -43,6 +44,17 @@ (define-configuration port-configuration
   80
   (port-configuration-port (port-configuration)))
 
+(test-equal "wrong type for a field"
+  '("configuration.scm" 56 11)                    ;error location
+  (guard (c ((configuration-error? c)
+             (let ((loc (error-location c)))
+               (list (basename (location-file loc))
+                     (location-line loc)
+                     (location-column loc)))))
+    (port-configuration
+     ;; This is line 55; the test relies on line/column numbers!
+     (port "This is not a number!"))))
+
 (define-configuration port-configuration-cs
   (port (number 80) "The port number." empty-serializer))
 
-- 
2.36.1





Information forwarded to guix-patches@HIDDEN:
bug#56075; Package guix-patches. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 18 Jun 2022 21:37:01 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sat Jun 18 17:37:01 2022
Received: from localhost ([127.0.0.1]:50276 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1o2g7p-0004T0-DV
	for submit <at> debbugs.gnu.org; Sat, 18 Jun 2022 17:37:01 -0400
Received: from lists.gnu.org ([209.51.188.17]:46840)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <ludo@HIDDEN>) id 1o2g7l-0004Sk-4R
 for submit <at> debbugs.gnu.org; Sat, 18 Jun 2022 17:36:59 -0400
Received: from eggs.gnu.org ([2001:470:142:3::10]:47728)
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <ludo@HIDDEN>) id 1o2g7k-0005QP-UZ
 for guix-patches@HIDDEN; Sat, 18 Jun 2022 17:36:56 -0400
Received: from fencepost.gnu.org ([2001:470:142:3::e]:45052)
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <ludo@HIDDEN>)
 id 1o2g7j-0006DI-S0; Sat, 18 Jun 2022 17:36:56 -0400
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org;
 s=fencepost-gnu-org; h=MIME-Version:Date:Subject:To:From:in-reply-to:
 references; bh=6kEeTT6vYh4+B1K/28/3vgoKDBwD/vTEMs0E4lJkfc0=; b=nYIvuPXacuK7Up
 QAcJAlFiVa428bGMOOuSZbZw0h73t+R0wRGO5lniJ30JDVwTTXhnR4y7utWXlR8mv21d7CbRCUdkZ
 uLhb0yijZkhp/hybo/CNUpCHSwBSA61ScKQU/WMHglkQjr2EY71yXuEAGB23wXJ3/yZaCr+lENtK+
 LL+VSPZKKNCGTh0Bpxp2H/PExb4RIpnMvfo3YAyoHmIQdbuC5iBEgmde130jpbn9+zQJK5n6zEQrf
 yvozUYqi0jeQIaTTHJMsizbqxPyp00SVTd98Z3DeYL+wJ6HIsEPQXGsH4+mpn3LSg3SnRN9d9BP8/
 AgFVNiETUgsV0aDqf7Mw==;
Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201]:56130
 helo=gnu.org)
 by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <ludo@HIDDEN>)
 id 1o2g7j-0004rO-Fo; Sat, 18 Jun 2022 17:36:55 -0400
From: =?UTF-8?q?Ludovic=20Court=C3=A8s?= <ludo@HIDDEN>
To: guix-patches@HIDDEN
Subject: [PATCH 0/2] Report location of invalid configuration field values
Date: Sat, 18 Jun 2022 23:36:40 +0200
Message-Id: <20220618213640.25039-1-ludo@HIDDEN>
X-Mailer: git-send-email 2.36.1
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: submit
Cc: =?UTF-8?q?Ludovic=20Court=C3=A8s?= <ludo@HIDDEN>
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

Hello Guix!

This change has ‘define-configuration’ use the ‘sanitize’ property to
type-check fields instead of a custom mechanism.  More importantly, it
improves error reporting of invalid field value such that instead of:

  guix home: error: Invalid value for field latitude: "44.81"

you get the location of the faulty field value:

  home-config.scm:391:23: error: invalid value "44.81" for field 'latitude'

Additionally the message is now internationalized.

Thoughts?

Ludo’.

Ludovic Courtès (2):
  services: configuration: Report the location of field type errors.
  services: configuration: Remove 'validate-configuration'.

 doc/guix.texi                    |  6 ---
 gnu/services/configuration.scm   | 64 +++++++++++++++++++++-----------
 gnu/services/mail.scm            |  6 +--
 gnu/services/vpn.scm             |  2 -
 po/guix/POTFILES.in              |  1 +
 tests/services/configuration.scm | 12 ++++++
 6 files changed, 57 insertions(+), 34 deletions(-)


base-commit: 7f208f68dea828fe02718ca8ce81d5975136cff8
-- 
2.36.1





Acknowledgement sent to Ludovic Courtès <ludo@HIDDEN>:
New bug report received and forwarded. Copy sent to guix-patches@HIDDEN. Full text available.
Report forwarded to guix-patches@HIDDEN:
bug#56075; Package guix-patches. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Thu, 23 Jun 2022 16:15:01 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.