From patchwork Mon Jun 5 21:56:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 50723 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 8717E27BBE9; Mon, 5 Jun 2023 22:57:24 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id D0B7E27BBE2 for ; Mon, 5 Jun 2023 22:57:23 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q6ICK-0008Mq-Ss; Mon, 05 Jun 2023 17:57:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q6ICH-0008Ko-Ei for guix-patches@gnu.org; Mon, 05 Jun 2023 17:57:06 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1q6ICH-00088n-5Y; Mon, 05 Jun 2023 17:57:05 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1q6ICE-0004cq-64; Mon, 05 Jun 2023 17:57:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63917] [PATCH] packages: 'package-transitive-supported-systems' detects cycles. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: mail@cbaines.net, dev@jpoiret.xyz, ludo@gnu.org, othacehe@gnu.org, rekado@elephly.net, zimon.toutoune@gmail.com, me@tobias.gr, guix-patches@gnu.org Resent-Date: Mon, 05 Jun 2023 21:57:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 63917 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 63917@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= , Christopher Baines , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice X-Debbugs-Original-To: guix-patches@gnu.org X-Debbugs-Original-Xcc: Christopher Baines , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice Received: via spool by submit@debbugs.gnu.org id=B.168600218917711 (code B ref -1); Mon, 05 Jun 2023 21:57:02 +0000 Received: (at submit) by debbugs.gnu.org; 5 Jun 2023 21:56:29 +0000 Received: from localhost ([127.0.0.1]:50449 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1q6IBg-0004bb-Kg for submit@debbugs.gnu.org; Mon, 05 Jun 2023 17:56:29 -0400 Received: from lists.gnu.org ([209.51.188.17]:49852) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1q6IBe-0004bT-Jc for submit@debbugs.gnu.org; Mon, 05 Jun 2023 17:56:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q6IBc-0008Bm-VF for guix-patches@gnu.org; Mon, 05 Jun 2023 17:56:26 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q6IBc-0007sZ-CX; Mon, 05 Jun 2023 17:56:24 -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=kwGfiWwNqdKoUx6LMgwl6OPcybh5VeyadLvmhV20uDE=; b=L+gJBthrkBwDSA CUNMUi7dLPVBTpnbG8TmK2Fc7IhVWqEB0AHXdh/JnNd9EMjtS8Mdz3oRQELUH1AKcNZoJqRGoyMW4 s4QfXZkfNcr4a9zr1aSKkhL6knsxXlVq9arjBkn+lA0HGUr0l+3U0rqos/FBewTo6fzcZRfFwR8rO +1K8zrVat8UAOAKpCYykyQCGzGyWXehNyRTc7scravW6JP0+PwGhr+S8EUbbKZuZpXlJq8MI/KkDA hw9F7yq5Zx2E4MDkSr+hp/kBS/lL3QxH7+4gJTDf5ENme6GHx316LgLI+qrQz2j8e+36kcDseBNv6 cCQO7tSTSWsseeu93Drw==; Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201] helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q6IBb-00009w-WC; Mon, 05 Jun 2023 17:56:24 -0400 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Mon, 5 Jun 2023 23:56:09 +0200 Message-Id: X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches With this change, commands such as 'guix build' or 'guix package' report obvious package-level cycles upfront. Derivation-level cycles are not detected. * guix/packages.scm (&package-cyclic-dependency-error): New condition type. (package-transitive-supported-systems): Define 'visited', check it, and parameterize it. * guix/ui.scm (call-with-error-handling): Handle '&package-cyclic-dependency-error'. * tests/packages.scm ("package-transitive-supported-systems detects cycles"): Add test. --- guix/packages.scm | 41 ++++++++++++++++++++++++++++++----------- guix/ui.scm | 9 +++++++++ tests/packages.scm | 17 +++++++++++++++++ 3 files changed, 56 insertions(+), 11 deletions(-) Hi! At long last! This is a pretty basic cycle detection trick: it can only detect package-level cycles and not more subtle things like that in , but it’s also less expensive and intrusive than something right into ‘package->derivation’. Bonus: since ‘package-transitive-supported-systems’ is used when building ‘guix-package-cache.drv’, it would fail right during ‘guix pull’ instead of eating up all the memory, as in . Thoughts? Ludo’. base-commit: 940665301de4effd065d24c167f619286f2adf4c diff --git a/guix/packages.scm b/guix/packages.scm index e26602d589..ba98bb0fb4 100644 --- a/guix/packages.scm +++ b/guix/packages.scm @@ -168,6 +168,9 @@ (define-module (guix packages) package-error-invalid-license &package-input-error package-input-error? + &package-cyclic-dependency-error + package-cyclic-dependency-error? + package-error-dependency-cycle package-error-invalid-input &package-cross-build-system-error package-cross-build-system-error? @@ -806,6 +809,10 @@ (define-condition-type &package-input-error &package-error package-input-error? (input package-error-invalid-input)) +(define-condition-type &package-cyclic-dependency-error &package-error + package-cyclic-dependency-error? + (cycle package-error-dependency-cycle)) + (define-condition-type &package-cross-build-system-error &package-error package-cross-build-system-error?) @@ -1317,17 +1324,29 @@ (define package-transitive-supported-systems (let () (define (supported-systems-procedure system) (define supported-systems - (mlambdaq (package) - (parameterize ((%current-system system)) - (fold (lambda (input systems) - (match input - ((label (? package? package) . _) - (lset-intersection string=? systems - (supported-systems package))) - (_ - systems))) - (package-supported-systems package) - (bag-direct-inputs (package->bag package system #f)))))) + ;; The VISITED parameter allows for cycle detection. This is a pretty + ;; strategic place to do that: most commands call it upfront, yet it's + ;; not on the hot path of 'package->derivation'. The downside is that + ;; only package-level cycles are detected. + (let ((visited (make-parameter (setq)))) + (mlambdaq (package) + (when (set-contains? (visited) package) + (raise (condition + (&package-cyclic-dependency-error + (package package) + (cycle (set->list (visited))))))) + + (parameterize ((visited (set-insert package (visited))) + (%current-system system)) + (fold (lambda (input systems) + (match input + ((label (? package? package) . _) + (lset-intersection string=? systems + (supported-systems package))) + (_ + systems))) + (package-supported-systems package) + (bag-direct-inputs (package->bag package system #f))))))) supported-systems) diff --git a/guix/ui.scm b/guix/ui.scm index 7540e2194f..47a118364a 100644 --- a/guix/ui.scm +++ b/guix/ui.scm @@ -722,6 +722,15 @@ (define (call-with-error-handling thunk) (leave (G_ "~a:~a:~a: package `~a' has an invalid input: ~s~%") file line column (package-full-name package) input))) + ((package-cyclic-dependency-error? c) + (let ((package (package-error-package c))) + (leave (package-location package) + (G_ "~a: dependency cycle detected: + ~a~{ -> ~a~}~%") + (package-full-name package) + (package-full-name package) + (map package-full-name + (package-error-dependency-cycle c))))) ((package-cross-build-system-error? c) (let* ((package (package-error-package c)) (loc (package-location package)) diff --git a/tests/packages.scm b/tests/packages.scm index 5e8eac99dc..2b7ab01f7d 100644 --- a/tests/packages.scm +++ b/tests/packages.scm @@ -368,6 +368,23 @@ (define %store (package-transitive-supported-systems d) (package-transitive-supported-systems e)))) +(test-equal "package-transitive-supported-systems detects cycles" + '("c" "a" "b" "c") + (letrec* ((a (dummy-package "a" + (build-system trivial-build-system) + (native-inputs (list c)))) + (b (dummy-package "b" + (build-system trivial-build-system) + (inputs (list a)))) + (c (dummy-package "c" + (build-system trivial-build-system) + (inputs (list b))))) + (guard (c ((package-cyclic-dependency-error? c) + (map package-name + (cons (package-error-package c) + (package-error-dependency-cycle c))))) + (package-transitive-supported-systems c)))) + (test-assert "package-development-inputs" ;; Note: Due to propagated inputs, 'package-development-inputs' returns a ;; couple more inputs, such as 'linux-libre-headers'.