From patchwork Fri Sep 6 04:17:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Nigko Yerden X-Patchwork-Id: 67660 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 7B31B27BBEA; Fri, 6 Sep 2024 05:32:20 +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=-6.6 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,DKIM_VALID,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_VALIDITY_CERTIFIED,RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE, 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 D527D27BBE2 for ; Fri, 6 Sep 2024 05:32:19 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1smQdh-0000st-9F; Fri, 06 Sep 2024 00:32:05 -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 1smQda-0000sO-VD for guix-patches@gnu.org; Fri, 06 Sep 2024 00:31:59 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1smQdZ-00053P-M7; Fri, 06 Sep 2024 00:31:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:From:To:In-Reply-To:References:Subject; bh=IkwfKRKfkhXXvd96s28C+sohNwKjTlOkT28L1F9j9iU=; b=Cu7aC5FSeofrH5IbuTKOV7UkIpLrQzMD8KyC89mWmdBalvyK158MSL/BAy9iT0K29jFamQFNDzN/EDr65TdZxajLW2fLAHwPwHfQIApYA5rkp9pQWHWc4cq7uaHzVcMLZsovfdxLVGmma3tfJ7mjaUcXM4PFtVQfKB1RjvaUv0v0FjheRkTWbOK2mFT3k4u2b5N9/+Y+aLn+hHv1YNCSqq1F2gczsPmGco2/3SkFCnQM5EuP8kVkaH57dSQUNzEqn+PFHPo/OtT7kjkdk7p8RnF/EfkFBamJq1ikM4HSJrWi5UKfbGH2a5V0GEN1akl7s9OCr/b7Lj6rRqQUxgvPlg==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1smQec-0004Pw-Df; Fri, 06 Sep 2024 00:33:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#72867] [PATCH v5] gexp: Make 'local-file' follow symlinks. References: In-Reply-To: Resent-From: Nigko Yerden Original-Sender: "Debbugs-submit" Resent-CC: guix@cbaines.net, dev@jpoiret.xyz, ludo@gnu.org, othacehe@gnu.org, zimon.toutoune@gmail.com, me@tobias.gr, guix-patches@gnu.org Resent-Date: Fri, 06 Sep 2024 04:33:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 72867 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 72867@debbugs.gnu.org Cc: Florian Pelz , Nigko Yerden , Christopher Baines , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Simon Tournier , Tobias Geerinckx-Rice X-Debbugs-Original-Xcc: Christopher Baines , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Simon Tournier , Tobias Geerinckx-Rice Received: via spool by 72867-submit@debbugs.gnu.org id=B72867.172559715816938 (code B ref 72867); Fri, 06 Sep 2024 04:33:02 +0000 Received: (at 72867) by debbugs.gnu.org; 6 Sep 2024 04:32:38 +0000 Received: from localhost ([127.0.0.1]:38763 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1smQeD-0004P8-N2 for submit@debbugs.gnu.org; Fri, 06 Sep 2024 00:32:38 -0400 Received: from mail-lj1-f193.google.com ([209.85.208.193]:43424) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1smQeB-0004Ot-Cp for 72867@debbugs.gnu.org; Fri, 06 Sep 2024 00:32:37 -0400 Received: by mail-lj1-f193.google.com with SMTP id 38308e7fff4ca-2f6580c2bbfso22462771fa.1 for <72867@debbugs.gnu.org>; Thu, 05 Sep 2024 21:31:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725597022; x=1726201822; darn=debbugs.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=IkwfKRKfkhXXvd96s28C+sohNwKjTlOkT28L1F9j9iU=; b=YJzakMEsT2ewBqvKMxTaN3y7Ga3+gq7hagsC6Z7ad1mBHF/osb+PtcS8YmIFDx7hBW jSUVrYx79KkqQMVxddWbbBBi9gntlfkueyexayJeH4SDeWYSR6Qi/3+cfH1IiilNyQ81 +JTrs/BapcK7CPSFq6AfgdqQHs5vAOvKuZsdAzjAZtO1xM585F4rqfUWe00Noqym0V2y yGzWh7zvNMah2+OPuJGl2XdepomMEXJiFc8HobqBG4ehgJv9DJTIslNercMzC2szf8YX lFu9zBeXeiFsb+qt/Y480emSUcVMVm+FGiQeO2s59dEySMjrDPkYGqViNV9sD62D21d1 d5Og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725597022; x=1726201822; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=IkwfKRKfkhXXvd96s28C+sohNwKjTlOkT28L1F9j9iU=; b=L5aBvI85I4YAslbm92AqMbc/dyGgZw/n48eKM0BLvpiRao/mONbBtcVqYrmtt32wNn NVEVw3xjJLznI725N9yjKFx8/O5P76CHy7V+9LD+oRN4FmJDAPmzIGRCu2Gtwuat9wEl NLLQB2XOGqJQKCcyVmixvRRb3QpSrktDESjMKIgH991ZL76PLnUhpr3JYdJN02Z9Gnk5 VOSoZnHmlwOqSaxOjs58a+GBY1QbqFClVVUHMJP0/sO3yAIoHI/1QKLj7UWmDTcQPmxm q8OXM3vMw5ZcZsLU5zh9PNVW/2zLP31yZNI+mymDgkIMwPJRKiwjxw12Y4yq1isQ9x3f nv7w== X-Gm-Message-State: AOJu0YwbE3DuetLv6HA1DOeqvpaLSExST/ekM33CLOlfBQYZhpxLiAKI G+Qf793oYtfZobTGJJKACYPLrn+cP9hq8f/XR9Lnx5l83+zDDhx8NXkaN/j5 X-Google-Smtp-Source: AGHT+IHbSLuPiVJmkvvXAJRhfkz93EizG3KhYD14x2zC+iTkzAGEIZrbvTgZkMQEzyUq2JO5J4075g== X-Received: by 2002:a05:6512:2398:b0:533:1d4:546d with SMTP id 2adb3069b0e04-5356776f777mr2541975e87.7.1725597021344; Thu, 05 Sep 2024 21:30:21 -0700 (PDT) Received: from localhost.localdomain ([188.168.141.132]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5356793daa9sm599247e87.56.2024.09.05.21.30.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 21:30:21 -0700 (PDT) From: Nigko Yerden Date: Fri, 6 Sep 2024 09:17:42 +0500 Message-ID: <6e87ddd086b9188539eecfc83bdd6712aaf53a1a.1725596262.git.nigko.yerden@gmail.com> X-Mailer: git-send-email 2.45.2 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 Fixes While the issue can be easily fixed (a one line change in 'absolute-dirname') by changing 'current-source-directory' so that it always follows symlinks, such a change may break someone else's code. Instead, this patch keeps the original behavior of 'current-source-directory' macro and adds optional 'follow-symlinks?' argument to it. ;;; Copyright © 2024 Nigko Yerden * guix/utils.scm (absolute-dirname): Add 'follow-symlinks?' mandatory argument. (%guix-source-root-directory): Pass #f to 'absolute-dirname' 'follow-symlinks?' argument. (current-source-directory): Add 'follow-symlinks?' optional argument. * guix/gexp.scm (local-file): Pass #t to 'current-source-directory' 'follow-symlinks?' argument. * tests/gexp.scm ("local-file, load through symlink"): New test. Change-Id: Ieb30101275deb56b7436df444f9bc21d240fba59 --- Using of 'eval' in test from v4 is wrong. It does not play any role there. Most importantly it does not prevent spoiling of '%load-path' for the rest of 'tests/gexp.scm' module. Here is the better version of the test that uses 'dynamic-wind'. guix/gexp.scm | 2 +- guix/utils.scm | 53 ++++++++++++++++++++++++++++---------------------- tests/gexp.scm | 33 +++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 24 deletions(-) base-commit: 4c49cd171e2aa06af05cf52403050b18f100867a diff --git a/guix/gexp.scm b/guix/gexp.scm index 74b4c49f90..5911ca4815 100644 --- a/guix/gexp.scm +++ b/guix/gexp.scm @@ -508,7 +508,7 @@ (define-syntax local-file (string? (syntax->datum #'file)) ;; FILE is a literal, so resolve it relative to the source directory. #'(%local-file file - (delay (absolute-file-name file (current-source-directory))) + (delay (absolute-file-name file (current-source-directory #t))) rest ...)) ((_ (assume-valid-file-name file) rest ...) ;; FILE is not a literal, so resolve it relative to the current diff --git a/guix/utils.scm b/guix/utils.scm index d8ce6ed886..ea3d80707e 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -1110,41 +1110,48 @@ (define (canonical-newline-port port) (define (%guix-source-root-directory) "Return the source root directory of the Guix found in %load-path." - (dirname (absolute-dirname "guix/packages.scm"))) + (dirname (absolute-dirname "guix/packages.scm" #f))) (define absolute-dirname ;; Memoize to avoid repeated 'stat' storms from 'search-path'. - (mlambda (file) + (mlambda (file follow-symlinks?) "Return the absolute name of the directory containing FILE, or #f upon -failure." +failure. Follow symlinks if FOLLOW-SYMLINKS? is true." (match (search-path %load-path file) (#f #f) ((? string? file) - ;; If there are relative names in %LOAD-PATH, FILE can be relative and - ;; needs to be canonicalized. - (if (string-prefix? "/" file) - (dirname file) - (canonicalize-path (dirname file))))))) + (if follow-symlinks? + (dirname (canonicalize-path file)) + ;; If there are relative names in %LOAD-PATH, FILE can be relative + ;; and needs to be canonicalized. + (if (string-prefix? "/" file) + (dirname file) + (canonicalize-path (dirname file)))))))) (define-syntax current-source-directory (lambda (s) "Return the absolute name of the current directory, or #f if it could not -be determined." +be determined. Do not follow symlinks if FOLLOW-SYMLINKS? is false (the default)." + (define (source-directory follow-symlinks?) + (match (assq 'filename (or (syntax-source s) '())) + (('filename . (? string? file-name)) + ;; If %FILE-PORT-NAME-CANONICALIZATION is 'relative, then FILE-NAME + ;; can be relative. In that case, we try to find out at run time + ;; the absolute file name by looking at %LOAD-PATH; doing this at + ;; run time rather than expansion time is necessary to allow files + ;; to be moved on the file system. + (if (string-prefix? "/" file-name) + (dirname (if follow-symlinks? + (canonicalize-path file-name) + file-name)) + #`(absolute-dirname #,file-name #,follow-symlinks?))) + ((or ('filename . #f) #f) + ;; raising an error would upset Geiser users + #f))) (syntax-case s () - ((_) - (match (assq 'filename (or (syntax-source s) '())) - (('filename . (? string? file-name)) - ;; If %FILE-PORT-NAME-CANONICALIZATION is 'relative, then FILE-NAME - ;; can be relative. In that case, we try to find out at run time - ;; the absolute file name by looking at %LOAD-PATH; doing this at - ;; run time rather than expansion time is necessary to allow files - ;; to be moved on the file system. - (if (string-prefix? "/" file-name) - (dirname file-name) - #`(absolute-dirname #,file-name))) - ((or ('filename . #f) #f) - ;; raising an error would upset Geiser users - #f)))))) + ((_) (source-directory #f)) + ((_ follow-symlinks?) + (source-directory (syntax->datum #'follow-symlinks?)))))) ;;; diff --git a/tests/gexp.scm b/tests/gexp.scm index b35bfc920f..eec0f6e7ca 100644 --- a/tests/gexp.scm +++ b/tests/gexp.scm @@ -292,6 +292,39 @@ (define %extension-package (equal? (scandir (string-append dir "/tests")) '("." ".." "gexp.scm")))))) +(test-assert "local-file, load through symlink" + ;; See . + (call-with-temporary-directory + (lambda (tmp-dir) + (chdir tmp-dir) + ;; create content file + (call-with-output-file "content" + (lambda (port) (display "Hi!" port))) + ;; Create module that call 'local-file' + ;; with the content file and returns its + ;; absolute file-name. An error is raised + ;; if the content file can't be found. + (call-with-output-file "test-local-file.scm" + (lambda (port) (display "\ +(define-module (test-local-file) + #:use-module (guix gexp)) +(define file (local-file \"content\" \"test-file\")) +(local-file-absolute-file-name file)" port))) + (mkdir "dir") + (chdir "dir") + (symlink "../test-local-file.scm" "test-local-file.scm") + ;; 'local-file' in turn calls 'current-source-directory' + ;; which has an 'if' branching condition depending on whether + ;; 'file-name' is absolute or relative path. To test both + ;; of these branches we execute 'test-local-file.scm' symlink + ;; first as a module (corresponds to relative path): + (dynamic-wind + (lambda () (set! %load-path (cons "." %load-path))) + (lambda () (use-modules (test-local-file))) + (lambda () (set! %load-path (cdr %load-path)))) + ;; and then as a regular code (corresponds to absolute path): + (load (string-append tmp-dir "/dir/test-local-file.scm"))))) + (test-assert "one plain file" (let* ((file (plain-file "hi" "Hello, world!")) (exp (gexp (display (ungexp file))))