Subject: [PATCH 1/2] modules: Remove load path prefix from module name.

From: itd <[email-masked]>
Date: Sun, 02 Oct 2022 16:35:33 +0200

* guix/modules.scm (file-name->module-name): Ignore load path prefix
when building module name.
---
It was mentioned on IRC, that (guix modules)'s file-name->module-name
might be function to be used by the JSON importer (and fixed if needed).
This patch attempts to implement the idea from the bug report.

 guix/modules.scm | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/guix/modules.scm b/guix/modules.scm
index 61bc8e1978..269d52ae1e 100644
--- a/guix/modules.scm
+++ b/guix/modules.scm
@@ -100,11 +100,23 @@ (define module-file-dependencies
            '()))))))
 
 (define file-name->module-name
-  (let ((not-slash (char-set-complement (char-set #\/))))
+  (let ((not-slash (char-set-complement (char-set #\/)))
+       (load-path-prefix-length
+        (lambda (file)
+          ;; Length of the longest prefix among all given load paths.
+          (apply max (map
+                      (lambda (path) (if (string-prefix? path file)
+                                       (string-length path)
+                                       0))
+                      %load-path)))))
     (lambda (file)
       "Return the module name (a list of symbols) corresponding to FILE."
       (map string->symbol
-           (string-tokenize (string-drop-right file 4) not-slash)))))
+           (string-tokenize
+             (string-drop
+              (string-drop-right file 4)
+              (load-path-prefix-length file))
+           not-slash)))))
 
 (define (module-name->file-name module)
   "Return the file name for MODULE."

base-commit: ae221813745783ef1b7eee47561a2208cd5ad512
-- 
2.37.3