From patchwork Thu Sep 5 04:16:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Nigko Yerden X-Patchwork-Id: 67623 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 82B4C27BBE2; Thu, 5 Sep 2024 05:25:33 +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 A0BA627BBE2 for ; Thu, 5 Sep 2024 05:25:31 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sm43P-0006jC-F4; Thu, 05 Sep 2024 00:25:07 -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 1sm43M-0006h5-7i for guix-patches@gnu.org; Thu, 05 Sep 2024 00:25:04 -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 1sm43K-0002xH-Ir; Thu, 05 Sep 2024 00:25:02 -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=D+SGKjHxQ6C0TpGYX79nXcha7uotwQ3NJJOCxA/P2l4=; b=DwjobGmJ09em1/4Ef1KRAG6k1TIjPKZC1poEd5oVt6W8YEvKP2BtFbJ021d5ttctjFEdjtdhX8Oc8O7EYrwcnujqAr9i+KDDI/61UcF3o5X/WMcJQGY0HLw5drSgboJ+uxMppxuK5NhDXzafFCYOv7z7AnMUbMlBBk/PS32jl1Xh5js4Cf/aPvmf9xGI9RuSeegyFHgzWSbXgF7f1+8SKAf/qAgqNcDWKIz76L+rTvwlhTsGKKNLeq5NYe891yGvYO66DB8av2H0RRB4Ee4rITTpaPLHlJIn4IrKbQPaOq6zW4pw9Rln/yAT1YGmQqLvys2snOjhzUh1FVx5vHfaJQ==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sm44I-00014t-El; Thu, 05 Sep 2024 00:26:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#72867] [PATCH v4] 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: Thu, 05 Sep 2024 04:26: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.17255103184019 (code B ref 72867); Thu, 05 Sep 2024 04:26:02 +0000 Received: (at 72867) by debbugs.gnu.org; 5 Sep 2024 04:25:18 +0000 Received: from localhost ([127.0.0.1]:35848 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sm43Z-00012k-FG for submit@debbugs.gnu.org; Thu, 05 Sep 2024 00:25:18 -0400 Received: from mail-lj1-f193.google.com ([209.85.208.193]:52425) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sm3yB-0000cH-IQ for 72867@debbugs.gnu.org; Thu, 05 Sep 2024 00:19:44 -0400 Received: by mail-lj1-f193.google.com with SMTP id 38308e7fff4ca-2f4f24263acso6088721fa.0 for <72867@debbugs.gnu.org>; Wed, 04 Sep 2024 21:18:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725509852; x=1726114652; 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=D+SGKjHxQ6C0TpGYX79nXcha7uotwQ3NJJOCxA/P2l4=; b=jvhoFGW4j1RyMTpB306twRTM/knt4GjNHtv3DXHs3evW/8/L1kk2BUbDjpYymj4IX1 yDTwmHq4KdP3UJV9pemnKeUU4tX736IxPk60da+6qlpzu0iz55X2bfgIqcSYSClePwwK T2Ww/ai6oSbxxauSqsnekKB9gESDtkWYuTp7bC6lGG+Na9ku+RpjKdBmAGHYCBr/WTBC 7P8cD9IkuMlCKbOQexbc7Maq5r9lZ5lhAyVnXF8zV1U59lOCXtrET/iC0endijHkRlut V/l5+4wC+rUCMyBv+1HwGWYyk1pbJGI5fow2C72s4dxA7j2iL7P7kiOP+vzCfWt11J0V qCDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725509852; x=1726114652; 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=D+SGKjHxQ6C0TpGYX79nXcha7uotwQ3NJJOCxA/P2l4=; b=ohYIx/ztr8YMWHBD9BF1J5waZxivDvvOgItdY2/Anz69QDMsLbl0CQsrzHa9rsFlTI 9g0TayOjmKj9ddqH/1clZcCovqawQvAnR/J2EBrO5jkjBcxJo0jsFiCTKqrPXSmRU9I5 JevL/uUNLSfEciy24zibXFnfzo07c6IHPbLiQBNOfAOG/vNSlPuZkC1DM2vGbgwhHw7f jQVA6KTvxYbC6kjWbqC6D6rwlP4D6emwFl9TRUO9bOoZLWYw3Xp9LMg/wZWZEM5jjZfe Vsbge9hTWcP6T4beO0kNP2o+88yjMJgqDPhg2FwhSwYjTEvqfOcATHe9ykDPHztzClYM H6Zg== X-Gm-Message-State: AOJu0Ywig6P6BICEuKtPDeLbNBP91vf2P5fImY+4V1VvG8klGq3GkeAa jxY+wsmQIKTTaNpZwR09lnEtsdfWs06oGJ+jGFDQSgkZmMgGMDD4734YA3xM X-Google-Smtp-Source: AGHT+IF1rEqKzxh9+dK00918pduC27f/FQNh9TQsoSsimsZHdAxdrRkiwoEhwVZeSB0bP2mWfXDIkA== X-Received: by 2002:a2e:b8d6:0:b0:2ef:2ba5:d214 with SMTP id 38308e7fff4ca-2f6105c4facmr222350691fa.4.1725509851120; Wed, 04 Sep 2024 21:17:31 -0700 (PDT) Received: from localhost.localdomain ([188.168.141.132]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-2f74c785b1csm253941fa.136.2024.09.04.21.17.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 21:17:30 -0700 (PDT) From: Nigko Yerden Date: Thu, 5 Sep 2024 09:16:51 +0500 Message-ID: <0676272a670d00cacf351da05bf909fec5435bea.1725509811.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 --- Hello Florian, pelzflorian (Florian Pelz) wrote: >> * tests/gexp.scm ("local-file, load through symlink"): New test. > >This one is a good test; but it tests only half, namely the >rare-in-practice case of `local-file' when loading a Scheme file. Here, >`current-source-directory' evaluate file-name to >"/tmp/guix-directory.VxrxZT/dir/link-to-code.scm", which has a slash as >prefix, so absolute-dirname is not called. Thanks for noticing this. Indeed 'absolute-dirname' was not called. I have fixed this by turning 'code.scm' into a module 'test-local-file.scm' and loading it twice: first using 'use-module' and then via 'load' (for some unclear reason 'primitive-load' causes an error here, so I replaced it with 'load'). >Thanks for the credit, but it would be unusual to mention me in the >commit message, where discussion does not count. >Please do not put me in the commit message; I made no code contribution. OK, I removed your name from the commit message. Regards, Nigko 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..8f267214cd 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): + (eval (begin + (add-to-load-path ".") + (use-modules (test-local-file))) + (current-module)) + ;; 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))))