diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2017-11-08 13:33:25 +0100 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2017-11-08 22:36:20 +0100 |
| commit | c5a4a92f1a796e342b7db4c458f1fdb61ffc8d40 (patch) | |
| tree | b7b59ee6fc7d0153120b333c82145a830b83988a | |
| parent | 49483f71381ad32cdbe81b1c8ed2cc023329cc18 (diff) | |
gnu: Improve error reporting of the use-.*modules macros.
Suggested by Julien Lepiller and myglc2
at <https://lists.gnu.org/archive/html/guix-devel/2017-11/msg00106.html>.
* gnu.scm (%try-use-modules): New procedure.
(package-module-hint, service-module-hint): New procedures.
(try-use-modules): New macro.
(use-package-modules, use-service-modules, use-system-modules): Use it.
* tests/guix-system.sh: Test it.
| -rw-r--r-- | gnu.scm | 100 | ||||
| -rw-r--r-- | po/guix/POTFILES.in | 1 | ||||
| -rw-r--r-- | tests/guix-system.sh | 28 |
3 files changed, 125 insertions, 4 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | ;;; GNU Guix --- Functional package management for GNU | 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org> | 2 | ;;; Copyright © 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org> |
| 3 | ;;; Copyright © 2015 Joshua S. Grant <jgrant@parenthetical.io> | 3 | ;;; Copyright © 2015 Joshua S. Grant <jgrant@parenthetical.io> |
| 4 | ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com> | 4 | ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com> |
| 5 | ;;; | 5 | ;;; |
| @@ -19,6 +19,14 @@ | |||
| 19 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | 19 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
| 20 | 20 | ||
| 21 | (define-module (gnu) | 21 | (define-module (gnu) |
| 22 | #:use-module (guix i18n) | ||
| 23 | #:use-module (guix utils) | ||
| 24 | #:use-module (srfi srfi-34) | ||
| 25 | #:use-module (srfi srfi-35) | ||
| 26 | #:use-module (ice-9 match) | ||
| 27 | #:use-module (guix packages) | ||
| 28 | #:use-module (gnu packages) | ||
| 29 | #:use-module (gnu services) | ||
| 22 | #:export (use-package-modules | 30 | #:export (use-package-modules |
| 23 | use-service-modules | 31 | use-service-modules |
| 24 | use-system-modules)) | 32 | use-system-modules)) |
| @@ -52,13 +60,97 @@ | |||
| 52 | (module-use! i (resolve-interface m)))) | 60 | (module-use! i (resolve-interface m)))) |
| 53 | %public-modules))) | 61 | %public-modules))) |
| 54 | 62 | ||
| 63 | (define (%try-use-modules modules location make-hint) | ||
| 64 | "Attempt to load all of MODULES. Report errors as coming from LOCATION, a | ||
| 65 | <location> record, and use MAKE-HINT to produce a fix hint." | ||
| 66 | (define (location->string loc) | ||
| 67 | (match loc | ||
| 68 | (#f "") | ||
| 69 | (($ <location> file line column) | ||
| 70 | (format #f "~a:~a:~a: " file line column)))) | ||
| 71 | |||
| 72 | (for-each (lambda (module) | ||
| 73 | (catch 'misc-error | ||
| 74 | (lambda () | ||
| 75 | (process-use-modules `((,module)))) | ||
| 76 | (lambda _ | ||
| 77 | (raise | ||
| 78 | (apply | ||
| 79 | make-compound-condition | ||
| 80 | (condition | ||
| 81 | (&message | ||
| 82 | (message (format #f (G_ "module ~a not found") | ||
| 83 | module)))) | ||
| 84 | (condition | ||
| 85 | (&error-location (location location))) | ||
| 86 | (or (and=> (make-hint module) list) | ||
| 87 | '())))))) | ||
| 88 | modules)) | ||
| 89 | |||
| 90 | (define (package-module-hint module) | ||
| 91 | (define last-name | ||
| 92 | (match module | ||
| 93 | ((_ ... last) | ||
| 94 | (symbol->string last)))) | ||
| 95 | |||
| 96 | (match (find-packages-by-name last-name) | ||
| 97 | (() | ||
| 98 | (condition | ||
| 99 | (&fix-hint | ||
| 100 | (hint (G_ "\ | ||
| 101 | You may use @command{guix package --show=foo | grep location} to search | ||
| 102 | for the location of package @code{foo}. | ||
| 103 | If you get the line @code{location: gnu/packages/bar.scm:174:2}, | ||
| 104 | add @code{bar} to the @code{use-package-modules} form."))))) | ||
| 105 | ((package _ ...) | ||
| 106 | (condition | ||
| 107 | (&fix-hint | ||
| 108 | (hint (format #f (G_ "\ | ||
| 109 | Try adding @code{(use-package-modules ~a)}.") | ||
| 110 | (basename (location-file (package-location package)) | ||
| 111 | ".scm")))))))) | ||
| 112 | |||
| 113 | (define (service-module-hint module) | ||
| 114 | (define last-name | ||
| 115 | (match module | ||
| 116 | ((_ ... last) | ||
| 117 | last))) | ||
| 118 | |||
| 119 | (match (lookup-service-types last-name) | ||
| 120 | (() | ||
| 121 | (condition | ||
| 122 | (&fix-hint | ||
| 123 | (hint (format #f (G_ "\ | ||
| 124 | You may use @command{guix system search ~a} to search for a service | ||
| 125 | matching @code{~a}. | ||
| 126 | If you get the line @code{location: gnu/services/foo.scm:188:2}, | ||
| 127 | add @code{foo} to the @code{use-service-modules} form.") | ||
| 128 | last-name last-name))))) | ||
| 129 | ((package _ ...) | ||
| 130 | (condition | ||
| 131 | (&fix-hint | ||
| 132 | (hint (format #f (G_ "\ | ||
| 133 | Try adding @code{(use-service-modules ~a)}.") | ||
| 134 | (basename (location-file (service-type-location package)) | ||
| 135 | ".scm")))))))) | ||
| 136 | |||
| 137 | (define-syntax-rule (try-use-modules hint modules ...) | ||
| 138 | (eval-when (expand load eval) | ||
| 139 | (%try-use-modules '(modules ...) | ||
| 140 | (source-properties->location | ||
| 141 | (current-source-location)) | ||
| 142 | hint))) | ||
| 143 | |||
| 55 | (define-syntax-rule (use-package-modules module ...) | 144 | (define-syntax-rule (use-package-modules module ...) |
| 56 | (use-modules (gnu packages module) ...)) | 145 | (try-use-modules package-module-hint |
| 146 | (gnu packages module) ...)) | ||
| 57 | 147 | ||
| 58 | (define-syntax-rule (use-service-modules module ...) | 148 | (define-syntax-rule (use-service-modules module ...) |
| 59 | (use-modules (gnu services module) ...)) | 149 | (try-use-modules service-module-hint |
| 150 | (gnu services module) ...)) | ||
| 60 | 151 | ||
| 61 | (define-syntax-rule (use-system-modules module ...) | 152 | (define-syntax-rule (use-system-modules module ...) |
| 62 | (use-modules (gnu system module) ...)) | 153 | (try-use-modules (const #f) ;no hint |
| 154 | (gnu system module) ...)) | ||
| 63 | 155 | ||
| 64 | ;;; gnu.scm ends here | 156 | ;;; gnu.scm ends here |
diff --git a/po/guix/POTFILES.in b/po/guix/POTFILES.in index e3f767cc677..6510b99e8f5 100644 --- a/po/guix/POTFILES.in +++ b/po/guix/POTFILES.in | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | # List of source files which contain translatable strings. | 1 | # List of source files which contain translatable strings. |
| 2 | # This should be source files of the various tools, and not package modules. | 2 | # This should be source files of the various tools, and not package modules. |
| 3 | gnu.scm | ||
| 3 | gnu/packages.scm | 4 | gnu/packages.scm |
| 4 | gnu/services.scm | 5 | gnu/services.scm |
| 5 | gnu/system.scm | 6 | gnu/system.scm |
diff --git a/tests/guix-system.sh b/tests/guix-system.sh index 31ee6371336..1346d8d5a8c 100644 --- a/tests/guix-system.sh +++ b/tests/guix-system.sh | |||
| @@ -68,6 +68,34 @@ else | |||
| 68 | fi | 68 | fi |
| 69 | 69 | ||
| 70 | 70 | ||
| 71 | # Reporting of module not found errors. | ||
| 72 | |||
| 73 | cat > "$tmpfile" <<EOF | ||
| 74 | ;; Line 1. | ||
| 75 | (use-modules (gnu)) | ||
| 76 | (use-service-modules openssh) | ||
| 77 | EOF | ||
| 78 | |||
| 79 | if guix system build "$tmpfile" -n 2> "$errorfile" | ||
| 80 | then false | ||
| 81 | else | ||
| 82 | grep "$tmpfile:3:2: .*module .*openssh.*not found" "$errorfile" | ||
| 83 | grep "Try.*use-service-modules ssh" "$errorfile" | ||
| 84 | fi | ||
| 85 | |||
| 86 | cat > "$tmpfile" <<EOF | ||
| 87 | ;; Line 1. | ||
| 88 | (use-modules (gnu)) | ||
| 89 | (use-package-modules qemu) | ||
| 90 | EOF | ||
| 91 | |||
| 92 | if guix system build "$tmpfile" -n 2> "$errorfile" | ||
| 93 | then false | ||
| 94 | else | ||
| 95 | grep "$tmpfile:3:2: .*module .*qemu.*not found" "$errorfile" | ||
| 96 | grep "Try.*use-package-modules virtualization" "$errorfile" | ||
| 97 | fi | ||
| 98 | |||
| 71 | # Reporting of unbound variables. | 99 | # Reporting of unbound variables. |
| 72 | 100 | ||
| 73 | cat > "$tmpfile" <<EOF | 101 | cat > "$tmpfile" <<EOF |
