From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id CLEKFEC4+14dAgAA0tVLHw (envelope-from ) for ; Tue, 30 Jun 2020 22:10:08 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id WOAREEC4+16SHgAAB5/wlQ (envelope-from ) for ; Tue, 30 Jun 2020 22:10:08 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id A4CA3940666 for ; Tue, 30 Jun 2020 22:10:07 +0000 (UTC) Received: from localhost ([::1]:59894 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jqOS6-0004Qp-DI for larch@yhetil.org; Tue, 30 Jun 2020 18:10:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58696) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jqOS2-0004Pu-6d for guix-patches@gnu.org; Tue, 30 Jun 2020 18:10:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:40343) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jqOS1-0000p2-Su for guix-patches@gnu.org; Tue, 30 Jun 2020 18:10:01 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jqOS1-00089C-Nb for guix-patches@gnu.org; Tue, 30 Jun 2020 18:10:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#42146] [PATCH core-updates 1/?] build: substitute: Don't fail silently. Resent-From: Jakub =?UTF-8?Q?K=C4=85dzio=C5=82ka?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 30 Jun 2020 22:10:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 42146 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 42146@debbugs.gnu.org Cc: mbakke@fastmail.com X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.159355497531278 (code B ref -1); Tue, 30 Jun 2020 22:10:01 +0000 Received: (at submit) by debbugs.gnu.org; 30 Jun 2020 22:09:35 +0000 Received: from localhost ([127.0.0.1]:51889 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jqORa-00088Q-Ky for submit@debbugs.gnu.org; Tue, 30 Jun 2020 18:09:34 -0400 Received: from lists.gnu.org ([209.51.188.17]:57628) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jqORY-00088H-4o for submit@debbugs.gnu.org; Tue, 30 Jun 2020 18:09:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58504) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jqORY-00040g-00 for guix-patches@gnu.org; Tue, 30 Jun 2020 18:09:32 -0400 Received: from pat.zlotemysli.pl ([37.59.186.212]:34672) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jqORV-0000k5-QK for guix-patches@gnu.org; Tue, 30 Jun 2020 18:09:31 -0400 Received: (qmail 6193 invoked by uid 1009); 1 Jul 2020 00:09:21 +0200 Received: from 188.123.215.55 (kuba@kadziolka.net@188.123.215.55) by pat.zlotemysli.pl (envelope-from , uid 1002) with qmail-scanner-2.08st (clamdscan: 0.98.6/25858. spamassassin: 3.4.0. perlscan: 2.08st. Clear:RC:1(188.123.215.55):. Processed in 0.018843 secs); 30 Jun 2020 22:09:21 -0000 Received: from unknown (HELO localhost.localdomain) (kuba@kadziolka.net@188.123.215.55) by pat.zlotemysli.pl with AES256-SHA encrypted SMTP; 1 Jul 2020 00:09:20 +0200 From: Jakub =?UTF-8?Q?K=C4=85dzio=C5=82ka?= Date: Wed, 1 Jul 2020 00:09:13 +0200 Message-Id: <20200630220913.14665-1-kuba@kadziolka.net> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: none client-ip=37.59.186.212; envelope-from=kuba@kadziolka.net; helo=pat.zlotemysli.pl X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/30 18:09:21 X-ACL-Warn: Detected OS = Linux 3.11 and newer X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_NONE=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-Spam-Score: -2.3 (--) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-Spam-Score: -3.3 (---) X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Spam-Score: -0.01 X-TUID: 5cWY2FEUgr5m * guix/build/utils.scm (substitute, substitute*)[require-matches?]: New argument. --- Of course, this will require some changes in the build recipes. However, many of the bugs I have seen before could've been prevented by this behavior, so I believe it is worth it. I am currently running 'guix build hello' with these changes; I will send any follow-up fixes needed to this bug#. guix/build/utils.scm | 47 +++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/guix/build/utils.scm b/guix/build/utils.scm index dc55c6745d..1bfb774c60 100644 --- a/guix/build/utils.scm +++ b/guix/build/utils.scm @@ -6,6 +6,7 @@ ;;; Copyright © 2018 Arun Isaac ;;; Copyright © 2018, 2019 Ricardo Wurmus ;;; Copyright © 2020 Efraim Flashner +;;; Copyright © 2020 Jakub Kądziołka ;;; ;;; This file is part of GNU Guix. ;;; @@ -747,29 +748,42 @@ PROC's result is returned." (lambda (key . args) (false-if-exception (delete-file template)))))) -(define (substitute file pattern+procs) +(define* (substitute file pattern+procs #:key (require-matches? #t)) "PATTERN+PROCS is a list of regexp/two-argument-procedure pairs. For each line of FILE, and for each PATTERN that it matches, call the corresponding PROC as (PROC LINE MATCHES); PROC must return the line that will be written as a substitution of the original line. Be careful about using '$' to match the -end of a line; by itself it won't match the terminating newline of a line." - (let ((rx+proc (map (match-lambda - (((? regexp? pattern) . proc) - (cons pattern proc)) - ((pattern . proc) - (cons (make-regexp pattern regexp/extended) - proc))) - pattern+procs))) +end of a line; by itself it won't match the terminating newline of a line. + +By default, SUBSTITUTE will raise a &message condition if one of the patterns +fails to match. If a lack of matches is acceptable, pass #:require-matches? #f +to disable this check." + (let ((rx+proc (map (match-lambda + (((? regexp? pattern) . proc) + (cons* #f "" pattern proc)) + ((pattern . proc) + (cons* #f pattern (make-regexp pattern regexp/extended) + proc))) + pattern+procs))) (with-atomic-file-replacement file (lambda (in out) (let loop ((line (read-line in 'concat))) (if (eof-object? line) - #t + (when require-matches? + (for-each + (match-lambda + ((#f pat . _) + (raise (condition + (&message + (message (format #f "substitute: ~a: pattern failed to match: ~a" file pat)))))) + ((#t . _) #t)) + rx+proc)) (let ((line (fold (lambda (r+p line) (match r+p - ((regexp . proc) + ((_ _ regexp . proc) (match (list-matches regexp line) ((and m+ (_ _ ...)) + (set-car! r+p #t) (proc line m+)) (_ line))))) line @@ -814,9 +828,17 @@ match substring. Alternatively, FILE may be a list of file names, in which case they are all subject to the substitutions. +By default, SUBSTITUTE* will raise a &message condition if one of the patterns +fails to match on one of the files. If a lack of matches is acceptable, +add #:require-matches? #f after FILE to disable this check. + Be careful about using '$' to match the end of a line; by itself it won't match the terminating newline of a line." ((substitute* file ((regexp match-var ...) body ...) ...) + (substitute* file #:require-matches? #t + ((regexp match-var ...) body ...) ...)) + ((substitute* file #:require-matches? require-matches? + ((regexp match-var ...) body ...) ...) (let () (define (substitute-one-file file-name) (substitute @@ -840,7 +862,8 @@ match the terminating newline of a line." (begin body ...) (substring l o (match:start m)) r)))))))) - ...))) + ...) + #:require-matches? require-matches?)) (match file ((files (... ...)) -- 2.26.2