From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <guix-patches-bounces+larch=yhetil.org@gnu.org>
Received: from mp0.migadu.com ([2001:41d0:403:58f0::])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	by ms13.migadu.com with LMTPS
	id +AOuLaQ+2Wa0LgAAqHPOHw:P1
	(envelope-from <guix-patches-bounces+larch=yhetil.org@gnu.org>)
	for <larch@yhetil.org>; Thu, 05 Sep 2024 05:16:20 +0000
Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	by mp0.migadu.com with LMTPS
	id +AOuLaQ+2Wa0LgAAqHPOHw
	(envelope-from <guix-patches-bounces+larch=yhetil.org@gnu.org>)
	for <larch@yhetil.org>; Thu, 05 Sep 2024 07:16:20 +0200
X-Envelope-To: larch@yhetil.org
Authentication-Results: aspmx1.migadu.com;
	dkim=fail ("body hash did not verify") header.d=debbugs.gnu.org header.s=debbugs-gnu-org header.b=DwjobGmJ;
	dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20230601 header.b=jvhoFGW4;
	dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none);
	spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org"
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org;
	s=key1; t=1725513380;
	h=from:from:sender:sender:reply-to:subject:subject:date:date:
	 message-id:message-id:to:to:cc:cc:mime-version:mime-version:
	 content-type:content-type:
	 content-transfer-encoding:content-transfer-encoding:resent-cc:
	 resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to:
	 references:references:list-id:list-help:list-unsubscribe:
	 list-subscribe:list-post:dkim-signature;
	bh=zrpz1BrBBuRmrGlQa/8CYqMfghZ/bJLyls6lLf8KpTg=;
	b=PGMQUK7KWQcXF5sf/AGtsXJQpkKRTj30ydq8yEmmfHYbJnh8z1+waOJjv3OipT6imPPutf
	Q9TtK36qS+A2I1I+C0S/o1ISXRJecui3GSwqpa3Hei5TaKFRyC6Msejv/cWyIoXxUAP6oL
	SiYNCI20xXdSeiXygDC/XaqaS3ryW2IcVF6jOVnTVp+Vu6fie+nMVPtxRa4HUAZGu9kI3y
	wdVXJe+YoHlqr3lqB7FCPPHshABYWl1N+tLKh45n6XCd9+I9RfMYa16Aa20ZruQjypM399
	tN6MCFMIyHeNKNB0uPycDUoK/JvLNcng1jOcYWVQmxomLuHGUQWCVTU/ACWlhA==
ARC-Seal: i=1; s=key1; d=yhetil.org; t=1725513380; a=rsa-sha256; cv=none;
	b=jiMfTPMgRziLNlkBVDEaU5+B+La66nSTFT54QbMHHCwMPTnBRCFODePjS9ORcGrf8GSC6m
	pVZWKzhBTfag3KB1FdsG4R3eZLKVKDYDUGA4qWT+u0azQn8+FKYw1bT6rhApoeGewXgEsh
	s1ZwHIIbi+a+gTZElWRwrfxwh4IwAXVng+g2T6f9JMK72hPuy9+NxtcrE7M+KhIftLD/iA
	g297Kt7ZFgAwRGbvUJOB2wSml6YHlHeuJEZHr/N3rTxwN+D1TjznjhIGF23jmG+AxjjlFa
	GvPj1Fb5Q8IrcbKODt4CC0cpP9OAgXqn5v0sRwOuD5plN3Ksv1Ro89K9atFElA==
ARC-Authentication-Results: i=1;
	aspmx1.migadu.com;
	dkim=fail ("body hash did not verify") header.d=debbugs.gnu.org header.s=debbugs-gnu-org header.b=DwjobGmJ;
	dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20230601 header.b=jvhoFGW4;
	dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none);
	spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org"
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 78F6E8038
	for <larch@yhetil.org>; Thu, 05 Sep 2024 07:16:20 +0200 (CEST)
Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <guix-patches-bounces@gnu.org>)
	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 <Debian-debbugs@debbugs.gnu.org>)
 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 <Debian-debbugs@debbugs.gnu.org>)
 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 <Debian-debbugs@debbugs.gnu.org>)
 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: <e2bf165fc2905bcc8d33d23293eb3d31f3fbe4b8.1724911574.git.nigko.yerden@gmail.com>
In-Reply-To: <e2bf165fc2905bcc8d33d23293eb3d31f3fbe4b8.1724911574.git.nigko.yerden@gmail.com>
Resent-From: Nigko Yerden <nigko.yerden@gmail.com>
Original-Sender: "Debbugs-submit" <debbugs-submit-bounces@debbugs.gnu.org>
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: <handler.72867.B72867.17255103184019@debbugs.gnu.org>
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 <pelzflorian@pelzflorian.de>,
 Nigko Yerden <nigko.yerden@gmail.com>, Christopher Baines <guix@cbaines.net>,
 Josselin Poiret <dev@jpoiret.xyz>,
 Ludovic =?UTF-8?Q?Court=C3=A8s?= <ludo@gnu.org>,
 Mathieu Othacehe <othacehe@gnu.org>, Simon Tournier <zimon.toutoune@gmail.com>,
 Tobias Geerinckx-Rice <me@tobias.gr>
X-Debbugs-Original-Xcc: Christopher Baines <guix@cbaines.net>,
 Josselin Poiret <dev@jpoiret.xyz>,
 Ludovic =?UTF-8?Q?Court=C3=A8s?= <ludo@gnu.org>,
 Mathieu Othacehe <othacehe@gnu.org>, Simon Tournier <zimon.toutoune@gmail.com>,
 Tobias Geerinckx-Rice <me@tobias.gr>
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 <debbugs-submit-bounces@debbugs.gnu.org>)
 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 <nigko.yerden@gmail.com>) 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 <nigko.yerden@gmail.com>
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
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-BeenThere: debbugs-submit@debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
X-BeenThere: guix-patches@gnu.org
List-Id: <guix-patches.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/guix-patches>,
 <mailto:guix-patches-request@gnu.org?subject=unsubscribe>
List-Archive: <https://lists.gnu.org/archive/html/guix-patches>
List-Post: <mailto:guix-patches@gnu.org>
List-Help: <mailto:guix-patches-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/guix-patches>,
 <mailto:guix-patches-request@gnu.org?subject=subscribe>
Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org
Sender: guix-patches-bounces+larch=yhetil.org@gnu.org
X-Migadu-Flow: FLOW_IN
X-Migadu-Country: US
X-Migadu-Spam-Score: -0.89
X-Spam-Score: -0.89
X-Migadu-Queue-Id: 78F6E8038
X-Migadu-Scanner: mx11.migadu.com
X-TUID: C1odHtfmtZaQ

Fixes <https://lists.gnu.org/archive/html/guix-devel/2024-08/msg00047.html>

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 <nigko.yerden@gmail.com>

* 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(-)

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 <https://issues.guix.gnu.org/72867>.
+  (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))))

base-commit: 4c49cd171e2aa06af05cf52403050b18f100867a
-- 
2.45.2