From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <guix-patches-bounces+larch=yhetil.org@gnu.org>
Received: from mp1.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 ED+OOwSN2mZK+wAA62LTzQ:P1
	(envelope-from <guix-patches-bounces+larch=yhetil.org@gnu.org>)
	for <larch@yhetil.org>; Fri, 06 Sep 2024 05:03:01 +0000
Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	by mp1.migadu.com with LMTPS
	id ED+OOwSN2mZK+wAA62LTzQ
	(envelope-from <guix-patches-bounces+larch=yhetil.org@gnu.org>)
	for <larch@yhetil.org>; Fri, 06 Sep 2024 07:03:01 +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=Cu7aC5FS;
	dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20230601 header.b=YJzakMEs;
	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=1725598980;
	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=W3HPJGmJqcASWtJt4YNznM2oCo8XFYA2l7hiRmQqBc8=;
	b=eTA97R9LIus9O4Hy3gur48rHyi4tnUvQxB1yYPEEijTCnrAhJFHolQmRpzWBa0bmyfaq/4
	CFiuyOcZ+725kyPNY3lEDoeHfIl0JEcKMZY0uP5j8P1Sx53Be9zS+GUC7wrMPa0bbblEyK
	cBuT0F90m4hNYdASI4owQvrlJtH7PqnOvfAH80x3wDvYMH3DH3WBv8n1gM8dLg+/fPA8vG
	wRaSTGcIprWyOZV2U3Vu0BogI6Y2fzPUz7igOQuID/7twHzUx/sIXKD4ZFHHuSwzZ2Zrew
	G6c2dWEL2dOdim5XMpbdtc3XcRpt5f4OixAGyU4Q+MH6NEvSMJTuyy1W3YLQ7A==
ARC-Seal: i=1; s=key1; d=yhetil.org; t=1725598980; a=rsa-sha256; cv=none;
	b=Hy6WkMtrEdpG/PbNi42BihCmWaQe2/Ib/wLBzpF0H68CLexFdLPKPzcxZh1djsZL1E56QR
	7B7lzSbY8y/2uC5ZaS+4/UUahm5feiQ254UGkCUn4+rtzY+BF9ZMUp7u2welWml7unfOSb
	2aHq8Bmb/pVEqk02xh0lASdZCLJ1qUU/pMrZV7vidL0aFuofnKZuOxrR8kN+gHqJQPWF9Y
	6WsEA4MMkdjMqZnL6IpxGtvcLbfGmiVX4vrHsE4RGtr2Syl5qnlfeh+qZtnzAtfr1Z6bpR
	bslNtjbtiDOyLsZFxusL01CXGjr8QTdW4zabDP+Xf6uaKD2Aos/4nCSkINZ+IQ==
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=Cu7aC5FS;
	dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20230601 header.b=YJzakMEs;
	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 49B7EEC63
	for <larch@yhetil.org>; Fri, 06 Sep 2024 07:03:00 +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 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 <Debian-debbugs@debbugs.gnu.org>)
 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 <Debian-debbugs@debbugs.gnu.org>)
 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 <Debian-debbugs@debbugs.gnu.org>)
 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: <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: Fri, 06 Sep 2024 04:33:02 +0000
Resent-Message-ID: <handler.72867.B72867.172559715816938@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.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 <debbugs-submit-bounces@debbugs.gnu.org>)
 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 <nigko.yerden@gmail.com>) 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 <nigko.yerden@gmail.com>
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
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: 49B7EEC63
X-Migadu-Scanner: mx11.migadu.com
X-TUID: tgpZmamZ59Lo

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

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 <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):
+     (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))))

base-commit: 4c49cd171e2aa06af05cf52403050b18f100867a
-- 
2.45.2