From patchwork Mon Sep 2 04:41:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nigko Yerden X-Patchwork-Id: 67491 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 2E2EF27BBEA; Mon, 2 Sep 2024 06:25:27 +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 DB73A27BBE2 for ; Mon, 2 Sep 2024 06:25:25 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1skzYp-0005CL-7a; Mon, 02 Sep 2024 01: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 1skzYn-0005Bl-UG for guix-patches@gnu.org; Mon, 02 Sep 2024 01:25:06 -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 1skzYm-0001mG-MO; Mon, 02 Sep 2024 01:25:04 -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=zejtdo0J87SFBzYbjXQy5nY9TsabxKK2J2c9hZ/a8Ss=; b=TroVGaMKHHx3RlRXPq2VeYjq+a+ReBofVNb3+jRfJ3kfvBpNtT746j9HqROJd3bIs2+3pPp1Y9a/+VgqrSQ9FCJJozFPQIWvdKri57nV1ox0050xU3wsjxvHa0cO6UrycqrAn0O5LP9QrMtGqk/KtRfJkus2baRvR5v5Ua42BpBtAk9BATSLX4rSZNCxiWA5nq+ro4XWhXQAKSM4tjiLnCDKg5ryCVEl6gTydYM7M3EOcCMurxCzG+L5Moky2aCihOsbOHE7ock83iRD6CX4wi2DjtYXIuaIT7DrZsva/mZbZKQFMOeYbrL4owvl7XePbqrtdHLrJefhOJctXo1PYA==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1skzZi-0005f2-GR; Mon, 02 Sep 2024 01:26:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#72867] [PATCH v2] 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: Mon, 02 Sep 2024 05: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: pelzflorian@pelzflorian.de, 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.172525471621649 (code B ref 72867); Mon, 02 Sep 2024 05:26:02 +0000 Received: (at 72867) by debbugs.gnu.org; 2 Sep 2024 05:25:16 +0000 Received: from localhost ([127.0.0.1]:43100 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1skzYx-0005d6-Rf for submit@debbugs.gnu.org; Mon, 02 Sep 2024 01:25:16 -0400 Received: from mail-lj1-f196.google.com ([209.85.208.196]:51495) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1skyy6-0003ZO-Sd for 72867@debbugs.gnu.org; Mon, 02 Sep 2024 00:47:11 -0400 Received: by mail-lj1-f196.google.com with SMTP id 38308e7fff4ca-2f50966c469so42150111fa.3 for <72867@debbugs.gnu.org>; Sun, 01 Sep 2024 21:46:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725252303; x=1725857103; 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=zejtdo0J87SFBzYbjXQy5nY9TsabxKK2J2c9hZ/a8Ss=; b=VWNMDzuUoWIRRmgevL9084vA2m2do/KpMvqahkY6CylKUWgoQIcmdCa5YwXjZTqbbo FsfjaIEpNqAMMMOje1uK3vocaaKTsVpGL/sOMiQsQNU0m8/YPa++oCXoMHLXLEv1XARP jTsl3mMnITg+nzcomJbLUvT79ZOhknC9lSt0RJWwKaO7CJnWHDs3bxMnhQRrr+SBzHDo +SHqaLpcWhvj3hbErHDHnljEFDi0BDyWZCjdnz7QHWb9k04BJHtBwoGhzU4CIA2kebrs 81WRTpZEbAzo1UmqggrXHWDSpeIdPmgpBLFiUkW7BHXdrTQrfqNLwxl/4Et2jw0OFzkF JKQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725252303; x=1725857103; 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=zejtdo0J87SFBzYbjXQy5nY9TsabxKK2J2c9hZ/a8Ss=; b=Sgu3MXXZjOGoyHxMObqt4mvVTX4WD3M8C60JcaLvUZy/Xd8ZuDNAvo/ahtVrH340iR ETcxXlZAYlttwZvnfaHe1BtYcB+dSTd2YbApiJUWSMM8pWZywukRN4teydUr52Zq/B6Z hd4hYJMmA6babUk0iCaVoWl9nMLm/Y+ehcC41z3/GcBLxEQ8e/YPnHi8uyvPSau+3Y2L lJvTkWVKWfzPIdEasxSO0UbaqRlJxtECShZwNHW6E2xAw9mMLeCpGiI5jXvw8ND+SpKq i5sShOXLtDgif76p7nh03eO+WeEZdxkWe9+AfoQUDexn7CJBgSkZKgvNh+zwfhef7Nvz J1Jg== X-Gm-Message-State: AOJu0Yznx9EACVrn4q3GoRPwgQLSFEXcGejrBHEMW2vRgHeMvH5ZgpE9 i5tfsQJHDsXFWludh/R1/TbOfV1V9G7Q3B8WcQPQpDo61B16kS/Vlkks+EB4 X-Google-Smtp-Source: AGHT+IFU2tt/wevjZbrLOsnDGi1MgSksByyxKIf/2zqKA0R7Y4eHnOLnaEt+bRV2eKgbIHOheF/G7w== X-Received: by 2002:a05:6512:6cc:b0:530:ae4f:337a with SMTP id 2adb3069b0e04-53546afaddamr6289499e87.3.1725252302422; Sun, 01 Sep 2024 21:45:02 -0700 (PDT) Received: from localhost.localdomain ([188.68.137.106]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-535407b8727sm1452656e87.119.2024.09.01.21.45.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Sep 2024 21:45:02 -0700 (PDT) From: Nigko Yerden Date: Mon, 2 Sep 2024 09:41:59 +0500 Message-ID: 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. This patch is the result of collective work of Florian Pelz and 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 Ludo, Florian, Add test to 'local-file'. Regards, Nigko guix/gexp.scm | 2 +- guix/utils.scm | 52 ++++++++++++++++++++++++++++---------------------- tests/gexp.scm | 23 ++++++++++++++++++++++ 3 files changed, 53 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..b5fcf8cb28 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -1110,41 +1110,47 @@ (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 #'follow-symlinks?))))) ;;; diff --git a/tests/gexp.scm b/tests/gexp.scm index b35bfc920f..843037fa84 100644 --- a/tests/gexp.scm +++ b/tests/gexp.scm @@ -292,6 +292,29 @@ (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 code 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 "code.scm" + (lambda (port) (display "\ +(use-modules (guix gexp)) +(define file (local-file \"content\" \"test-file\")) +(local-file-absolute-file-name file)" port))) + (mkdir "dir") + (chdir "dir") + (symlink "../code.scm" "link-to-code.scm") + ;; call 'local-file' through symlink + (primitive-load (string-append tmp-dir "/dir/link-to-code.scm"))))) + (test-assert "one plain file" (let* ((file (plain-file "hi" "Hello, world!")) (exp (gexp (display (ungexp file))))