1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014 Cyril Roelandt <tipecaml@gmail.com>
3 ;;; Copyright © 2014, 2015 Eric Bavier <bavier@member.fsf.org>
4 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
5 ;;; Copyright © 2015, 2016 Mathieu Lirzin <mthl@gnu.org>
6 ;;; Copyright © 2016 Danny Milosavljevic <dannym+a@scratchpost.org>
7 ;;; Copyright © 2016 Hartmut Goebel <h.goebel@crazy-compilers.com>
8 ;;; Copyright © 2017 Alex Kost <alezost@gmail.com>
9 ;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
10 ;;; Copyright © 2017, 2018, 2020 Efraim Flashner <efraim@flashner.co.il>
11 ;;; Copyright © 2018, 2019 Arun Isaac <arunisaac@systemreboot.net>
13 ;;; This file is part of GNU Guix.
15 ;;; GNU Guix is free software; you can redistribute it and/or modify it
16 ;;; under the terms of the GNU General Public License as published by
17 ;;; the Free Software Foundation; either version 3 of the License, or (at
18 ;;; your option) any later version.
20 ;;; GNU Guix is distributed in the hope that it will be useful, but
21 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;;; GNU General Public License for more details.
25 ;;; You should have received a copy of the GNU General Public License
26 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
28 (define-module (guix lint)
29 #:use-module (guix store)
30 #:use-module (guix base32)
31 #:use-module (guix diagnostics)
32 #:use-module (guix download)
33 #:use-module (guix ftp-client)
34 #:use-module (guix http-client)
35 #:use-module (guix packages)
36 #:use-module (guix i18n)
37 #:use-module (guix licenses)
38 #:use-module (guix records)
39 #:use-module (guix grafts)
40 #:use-module (guix upstream)
41 #:use-module (guix utils)
42 #:use-module (guix memoization)
43 #:use-module (guix scripts)
44 #:use-module ((guix ui) #:select (texi->plain-text fill-paragraph))
45 #:use-module (guix gnu-maintenance)
46 #:use-module (guix cve)
47 #:use-module ((guix swh) #:hide (origin?))
48 #:autoload (guix git-download) (git-reference?
49 git-reference-url git-reference-commit)
50 #:use-module (ice-9 match)
51 #:use-module (ice-9 regex)
52 #:use-module (ice-9 format)
53 #:use-module (web client)
54 #:use-module (web uri)
55 #:use-module ((guix build download)
56 #:select (maybe-expand-mirrors
57 (open-connection-for-uri
58 . guix:open-connection-for-uri)))
59 #:use-module (web request)
60 #:use-module (web response)
61 #:use-module (srfi srfi-1)
62 #:use-module (srfi srfi-6) ;Unicode string ports
63 #:use-module (srfi srfi-9)
64 #:use-module (srfi srfi-11)
65 #:use-module (srfi srfi-26)
66 #:use-module (srfi srfi-34)
67 #:use-module (srfi srfi-35)
68 #:use-module (ice-9 rdelim)
69 #:export (check-description-style
70 check-inputs-should-be-native
71 check-inputs-should-not-be-an-input-at-all
72 check-patch-file-names
77 check-source-file-name
78 check-source-unstable-tarball
91 lint-warning-message-text
92 lint-warning-message-data
96 %network-dependent-checkers
102 lint-checker-description
104 lint-checker-requires-store?))
111 (define-record-type* <lint-warning>
112 lint-warning make-lint-warning
114 (package lint-warning-package)
115 (message-text lint-warning-message-text)
116 (message-data lint-warning-message-data
118 (location lint-warning-location
121 (define (lint-warning-message warning)
123 (G_ (lint-warning-message-text warning))
124 (lint-warning-message-data warning)))
126 (define (package-file package)
128 (package-location package)))
130 (define* (%make-warning package message-text
131 #:optional (message-data '())
132 #:key field location)
138 (package-field-location package field)
139 (package-location package))))
141 (define-syntax make-warning
143 ((_ package (G_ message) rest ...)
144 (%make-warning package message rest ...))))
151 (define-record-type* <lint-checker>
152 lint-checker make-lint-checker
154 ;; TODO: add a 'certainty' field that shows how confident we are in the
155 ;; checker. Then allow users to only run checkers that have a certain
156 ;; 'certainty' level.
157 (name lint-checker-name)
158 (description lint-checker-description)
159 (check lint-checker-check)
160 (requires-store? lint-checker-requires-store?
163 (define (properly-starts-sentence? s)
164 (string-match "^[(\"'`[:upper:][:digit:]]" s))
166 (define (starts-with-abbreviation? s)
167 "Return #t if S starts with what looks like an abbreviation or acronym."
168 (string-match "^[A-Z][A-Z0-9]+\\>" s))
170 (define %quoted-identifier-rx
171 ;; A quoted identifier, like 'this'.
172 (make-regexp "['`][[:graph:]]+'"))
174 (define (check-description-style package)
175 ;; Emit a warning if stylistic issues are found in the description of PACKAGE.
176 (define (check-not-empty description)
177 (if (string-null? description)
179 (make-warning package
180 (G_ "description should not be empty")
181 #:field 'description))
184 (define (check-texinfo-markup description)
185 "Check that DESCRIPTION can be parsed as a Texinfo fragment. If the
186 markup is valid return a plain-text version of DESCRIPTION, otherwise #f."
188 (lambda () (texi->plain-text description))
189 (lambda (keys . args)
190 (make-warning package
191 (G_ "Texinfo markup in description is invalid")
192 #:field 'description))))
194 (define (check-trademarks description)
195 "Check that DESCRIPTION does not contain '™' or '®' characters. See
196 http://www.gnu.org/prep/standards/html_node/Trademarks.html."
197 (match (string-index description (char-set #\™ #\®))
198 ((and (? number?) index)
200 (make-warning package
201 (G_ "description should not contain ~
202 trademark sign '~a' at ~d")
203 (list (string-ref description index) index)
204 #:field 'description)))
207 (define (check-quotes description)
208 "Check whether DESCRIPTION contains single quotes and suggest @code."
209 (if (regexp-exec %quoted-identifier-rx description)
211 (make-warning package
212 ;; TRANSLATORS: '@code' is Texinfo markup and must be kept
214 (G_ "use @code or similar ornament instead of quotes")
215 #:field 'description))
218 (define (check-proper-start description)
219 (if (or (string-null? description)
220 (properly-starts-sentence? description)
221 (string-prefix-ci? (package-name package) description))
226 (G_ "description should start with an upper-case letter or digit")
227 #:field 'description))))
229 (define (check-end-of-sentence-space description)
230 "Check that an end-of-sentence period is followed by two spaces."
232 (reverse (fold-matches
233 "\\. [A-Z]" description '()
235 ;; Filter out matches of common abbreviations.
236 (if (find (lambda (s)
237 (string-suffix-ci? s (match:prefix m)))
238 '("i.e" "e.g" "a.k.a" "resp"))
239 r (cons (match:start m) r)))))))
240 (if (null? infractions)
243 (make-warning package
244 (G_ "sentences in description should be followed ~
245 by two spaces; possible infraction~p at ~{~a~^, ~}")
246 (list (length infractions)
248 #:field 'description)))))
250 (let ((description (package-description package)))
251 (if (string? description)
253 (check-not-empty description)
254 (check-quotes description)
255 (check-trademarks description)
256 ;; Use raw description for this because Texinfo rendering
257 ;; automatically fixes end of sentence space.
258 (check-end-of-sentence-space description)
259 (match (check-texinfo-markup description)
260 ((and warning (? lint-warning?)) (list warning))
262 (check-proper-start plain-description))))
264 (make-warning package
265 (G_ "invalid description: ~s")
267 #:field 'description)))))
269 (define (package-input-intersection inputs-to-check input-names)
270 "Return the intersection between INPUTS-TO-CHECK, the list of input tuples
271 of a package, and INPUT-NAMES, a list of package specifications such as
273 (match inputs-to-check
274 (((labels packages . outputs) ...)
275 (filter-map (lambda (package output)
276 (and (package? package)
277 (let ((input (string-append
278 (package-name package)
279 (if (> (length output) 0)
280 (string-append ":" (car output))
282 (and (member input input-names)
286 (define (check-inputs-should-be-native package)
287 ;; Emit a warning if some inputs of PACKAGE are likely to belong to its
289 (let ((inputs (append (package-inputs package)
290 (package-propagated-inputs package)))
300 "extra-cmake-modules"
304 "gobject-introspection"
315 "python-coverage" "python2-coverage"
316 "python-cython" "python2-cython"
317 "python-docutils" "python2-docutils"
318 "python-mock" "python2-mock"
319 "python-nose" "python2-nose"
320 "python-pbr" "python2-pbr"
321 "python-pytest" "python2-pytest"
322 "python-pytest-cov" "python2-pytest-cov"
323 "python-setuptools-scm" "python2-setuptools-scm"
324 "python-sphinx" "python2-sphinx"
330 "xorg-server-for-tests"
335 (G_ "'~a' should probably be a native input")
338 (package-input-intersection inputs input-names))))
340 (define (check-inputs-should-not-be-an-input-at-all package)
341 ;; Emit a warning if some inputs of PACKAGE are likely to should not be
343 (let ((input-names '("python-setuptools"
350 (G_ "'~a' should probably not be an input at all")
353 (package-input-intersection (package-direct-inputs package)
356 (define (package-name-regexp package)
357 "Return a regexp that matches PACKAGE's name as a word at the beginning of a
359 (make-regexp (string-append "^" (regexp-quote (package-name package))
363 (define (check-synopsis-style package)
364 ;; Emit a warning if stylistic issues are found in the synopsis of PACKAGE.
365 (define (check-final-period synopsis)
366 ;; Synopsis should not end with a period, except for some special cases.
367 (if (and (string-suffix? "." synopsis)
368 (not (string-suffix? "etc." synopsis)))
370 (make-warning package
371 (G_ "no period allowed at the end of the synopsis")
375 (define check-start-article
376 ;; Skip this check for GNU packages, as suggested by Karl Berry's reply to
377 ;; <http://lists.gnu.org/archive/html/bug-womb/2014-11/msg00000.html>.
378 (if (false-if-exception (gnu-package? package))
381 (if (or (string-prefix-ci? "A " synopsis)
382 (string-prefix-ci? "An " synopsis))
384 (make-warning package
385 (G_ "no article allowed at the beginning of \
390 (define (check-synopsis-length synopsis)
391 (if (>= (string-length synopsis) 80)
393 (make-warning package
394 (G_ "synopsis should be less than 80 characters long")
398 (define (check-proper-start synopsis)
399 (if (properly-starts-sentence? synopsis)
402 (make-warning package
403 (G_ "synopsis should start with an upper-case letter or digit")
404 #:field 'synopsis))))
406 (define (check-start-with-package-name synopsis)
407 (if (and (regexp-exec (package-name-regexp package) synopsis)
408 (not (starts-with-abbreviation? synopsis)))
410 (make-warning package
411 (G_ "synopsis should not start with the package name")
415 (define (check-texinfo-markup synopsis)
416 "Check that SYNOPSIS can be parsed as a Texinfo fragment. If the
417 markup is valid return a plain-text version of SYNOPSIS, otherwise #f."
420 (texi->plain-text synopsis)
422 (lambda (keys . args)
424 (make-warning package
425 (G_ "Texinfo markup in synopsis is invalid")
426 #:field 'synopsis)))))
429 (list check-proper-start
432 check-start-with-package-name
433 check-synopsis-length
434 check-texinfo-markup))
436 (match (package-synopsis package)
439 (make-warning package
440 (G_ "synopsis should not be empty")
442 ((? string? synopsis)
449 (make-warning package
450 (G_ "invalid synopsis: ~s")
452 #:field 'synopsis)))))
454 (define* (probe-uri uri #:key timeout)
455 "Probe URI, a URI object, and return two values: a symbol denoting the
456 probing status, such as 'http-response' when we managed to get an HTTP
457 response from URI, and additional details, such as the actual HTTP response.
459 TIMEOUT is the maximum number of seconds (possibly an inexact number) to wait
460 for connections to complete; when TIMEOUT is #f, wait as long as needed."
462 '((User-Agent . "GNU Guile")
467 (match (uri-scheme uri)
471 (let ((port (guix:open-connection-for-uri
472 uri #:timeout timeout))
473 (request (build-request uri #:headers headers)))
478 (write-request request port)
480 (read-response port))
484 (case (response-code response)
485 ((302 ; found (redirection)
487 307 ; temporary redirection
488 308) ; permanent redirection
489 (let ((location (response-location response)))
490 (if (or (not location) (member location visited))
491 (values 'http-response response)
492 (loop location (cons location visited))))) ;follow the redirect
493 ((301) ; moved permanently
494 (let ((location (response-location response)))
495 ;; Return RESPONSE, unless the final response as we follow
496 ;; redirects is not 200.
498 (let-values (((status response2)
499 (loop location (cons location visited))))
502 (values 'http-response
503 (if (= 200 (response-code response2))
507 (values status response2))))
508 (values 'http-response response)))) ;invalid redirect
510 (values 'http-response response)))))
513 ((bad-header bad-header-component)
514 ;; This can happen if the server returns an invalid HTTP header,
515 ;; as is the case with the 'Date' header at sqlite.org.
516 (values 'invalid-http-response #f))
517 ((getaddrinfo-error system-error
518 gnutls-error tls-certificate-error)
521 (apply throw key args))))))
525 (let ((conn (ftp-open (uri-host uri) #:timeout timeout)))
530 (ftp-chdir conn (dirname (uri-path uri)))
531 (ftp-size conn (basename (uri-path uri))))
534 (values 'ftp-response '(ok))))
538 (values 'ftp-response `(error ,@args)))
539 ((getaddrinfo-error system-error gnutls-error)
542 (apply throw key args))))))
544 (values 'unknown-protocol #f)))))
546 (define (tls-certificate-error-string args)
547 "Return a string explaining the 'tls-certificate-error' arguments ARGS."
548 (call-with-output-string
550 (print-exception port #f
551 'tls-certificate-error args))))
553 (define (validate-uri uri package field)
554 "Return #t if the given URI can be reached, otherwise return a warning for
555 PACKAGE mentioning the FIELD."
556 (let-values (((status argument)
557 (probe-uri uri #:timeout 3))) ;wait at most 3 seconds
560 (cond ((= 200 (response-code argument))
561 (match (response-content-length argument)
563 ;; As of July 2016, SourceForge returns 200 (instead of 404)
564 ;; with a small HTML page upon failure. Attempt to detect
565 ;; such malicious behavior.
567 (make-warning package
568 (G_ "URI ~a returned \
569 suspiciously small file (~a bytes)")
570 (list (uri->string uri)
574 ((= 301 (response-code argument))
575 (if (response-location argument)
576 (make-warning package
577 (G_ "permanent redirect from ~a to ~a")
578 (list (uri->string uri)
580 (response-location argument)))
582 (make-warning package
583 (G_ "invalid permanent redirect \
585 (list (uri->string uri))
588 (make-warning package
589 (G_ "URI ~a not reachable: ~a (~s)")
590 (list (uri->string uri)
591 (response-code argument)
592 (response-reason-phrase argument))
597 (('error port command code message)
598 (make-warning package
599 (G_ "URI ~a not reachable: ~a (~s)")
600 (list (uri->string uri)
601 code (string-trim-both message))
604 (make-warning package
605 (G_ "URI ~a domain not found: ~a")
606 (list (uri->string uri)
607 (gai-strerror (car argument)))
610 (make-warning package
611 (G_ "URI ~a unreachable: ~a")
612 (list (uri->string uri)
615 (cons status argument))))
617 ((tls-certificate-error)
618 (make-warning package
619 (G_ "TLS certificate error: ~a")
620 (list (tls-certificate-error-string argument))
622 ((invalid-http-response gnutls-error)
623 ;; Probably a misbehaving server; ignore.
625 ((unknown-protocol) ;nothing we can do
628 (error "internal linter error" status)))))
630 (define (check-home-page package)
631 "Emit a warning if PACKAGE has an invalid 'home-page' field, or if that
632 'home-page' is not reachable."
633 (let ((uri (and=> (package-home-page package) string->uri)))
636 (match (validate-uri uri package 'home-page)
637 ((and (? lint-warning? warning) warning)
640 ((not (package-home-page package))
641 (if (or (string-contains (package-name package) "bootstrap")
642 (string=? (package-name package) "ld-wrapper"))
645 (make-warning package
646 (G_ "invalid value for home page")
647 #:field 'home-page))))
650 (make-warning package
651 (G_ "invalid home page URL: ~s")
652 (list (package-home-page package))
653 #:field 'home-page))))))
655 (define %distro-directory
657 (dirname (search-path %load-path "gnu.scm"))))
659 (define (check-patch-file-names package)
660 "Emit a warning if the patches requires by PACKAGE are badly named or if the
661 patch could not be found."
662 (guard (c ((message-condition? c) ;raised by 'search-patch'
664 ;; Use %make-warning, as condition-mesasge is already
666 (%make-warning package (condition-message c)
667 #:field 'patch-file-names))))
669 (or (and=> (package-source package) origin-patches)
673 (if (every (match-lambda ;patch starts with package name?
675 (and=> (string-contains (basename patch)
676 (package-name package))
678 (_ #f)) ;must be an <origin> or something like that.
684 (G_ "file names of patches should start with the package name")
685 #:field 'patch-file-names)))
687 ;; Check whether we're reaching tar's maximum file name length.
688 (let ((prefix (string-length (%distro-directory)))
689 (margin (string-length "guix-2.0.0rc3-10000-1234567890/"))
691 (filter-map (match-lambda
693 (if (> (+ margin (if (string-prefix? (%distro-directory)
695 (- (string-length patch) prefix)
696 (string-length patch)))
700 (G_ "~a: file name is too long")
701 (list (basename patch))
702 #:field 'patch-file-names)
707 (define (escape-quotes str)
708 "Replace any quote character in STR by an escaped quote character."
710 (string-fold-right (lambda (chr result)
712 (#\" (cons* #\\ #\"result))
713 (_ (cons chr result))))
717 (define official-gnu-packages*
719 "A memoizing version of 'official-gnu-packages' that returns the empty
720 list when something goes wrong, such as a networking issue."
721 (let ((gnus (false-if-exception (official-gnu-packages))))
724 (define (check-gnu-synopsis+description package)
725 "Make sure that, if PACKAGE is a GNU package, it uses the synopsis and
726 descriptions maintained upstream."
727 (match (find (lambda (descriptor)
728 (string=? (gnu-package-name descriptor)
729 (package-name package)))
730 (official-gnu-packages*))
731 (#f ;not a GNU package, so nothing to do
733 (descriptor ;a genuine GNU package
735 (let ((upstream (gnu-package-doc-summary descriptor))
736 (downstream (package-synopsis package)))
738 (or (not (string? downstream))
739 (not (string=? upstream downstream))))
741 (make-warning package
742 (G_ "proposed synopsis: ~s~%")
747 (let ((upstream (gnu-package-doc-description descriptor))
748 (downstream (package-description package)))
750 (or (not (string? downstream))
751 (not (string=? (fill-paragraph upstream 100)
752 (fill-paragraph downstream 100)))))
756 (G_ "proposed description:~% \"~a\"~%")
757 (list (fill-paragraph (escape-quotes upstream) 77 7))
758 #:field 'description))
761 (define (origin-uris origin)
762 "Return the list of URIs (strings) for ORIGIN."
763 (match (origin-uri origin)
769 (define (check-source package)
770 "Emit a warning if PACKAGE has an invalid 'source' field, or if that
771 'source' is not reachable."
772 (define (warnings-for-uris uris)
773 (let loop ((uris uris)
779 (match (validate-uri uri package 'source)
781 ;; We found a working URL, so stop right away.
783 ((? lint-warning? warning)
784 (loop rest (cons warning warnings))))))))
786 (let ((origin (package-source package)))
788 (eqv? (origin-method origin) url-fetch))
789 (let* ((uris (append-map (cut maybe-expand-mirrors <> %mirrors)
790 (map string->uri (origin-uris origin))))
791 (warnings (warnings-for-uris uris)))
793 ;; Just make sure that at least one of the URIs is valid.
794 (if (= (length uris) (length warnings))
795 ;; When everything fails, report all of WARNINGS, otherwise don't
798 ;; XXX: Ideally we'd still allow warnings to be raised if *some*
799 ;; URIs are unreachable, but distinguish that from the error case
800 ;; where *all* the URIs are unreachable.
802 (make-warning package
803 (G_ "all the source URIs are unreachable:")
809 (define (check-source-file-name package)
810 "Emit a warning if PACKAGE's origin has no meaningful file name."
811 (define (origin-file-name-valid? origin)
812 ;; Return #f if the source file name contains only a version or is #f;
813 ;; indicates that the origin needs a 'file-name' field.
814 (let ((file-name (origin-actual-file-name origin))
815 (version (package-version package)))
817 ;; Common in many projects is for the filename to start
818 ;; with a "v" followed by the version,
819 ;; e.g. "v3.2.0.tar.gz".
820 (not (string-match (string-append "^v?" version) file-name)))))
822 (let ((origin (package-source package)))
823 (if (or (not origin) (origin-file-name-valid? origin))
826 (make-warning package
827 (G_ "the source file name should contain the package name")
830 (define (check-source-unstable-tarball package)
831 "Emit a warning if PACKAGE's source is an autogenerated tarball."
832 (define (check-source-uri uri)
833 (if (and (string=? (uri-host (string->uri uri)) "github.com")
834 (match (split-and-decode-uri-path
835 (uri-path (string->uri uri)))
836 ((_ _ "archive" _ ...) #t)
838 (make-warning package
839 (G_ "the source URI should not be an autogenerated tarball")
843 (let ((origin (package-source package)))
844 (if (and (origin? origin)
845 (eqv? (origin-method origin) url-fetch))
846 (filter-map check-source-uri
847 (origin-uris origin))
850 (define (check-mirror-url package)
851 "Check whether PACKAGE uses source URLs that should be 'mirror://'."
852 (define (check-mirror-uri uri) ;XXX: could be optimized
853 (let loop ((mirrors %mirrors))
857 (((mirror-id mirror-urls ...) rest ...)
858 (match (find (cut string-prefix? <> uri) mirror-urls)
862 (make-warning package
866 (string-drop uri (string-length prefix)))
867 #:field 'source)))))))
869 (let ((origin (package-source package)))
870 (if (and (origin? origin)
871 (eqv? (origin-method origin) url-fetch))
872 (let ((uris (origin-uris origin)))
873 (filter-map check-mirror-uri uris))
876 (define* (check-github-url package #:key (timeout 3))
877 "Check whether PACKAGE uses source URLs that redirect to GitHub."
878 (define (follow-redirect url)
879 (let* ((uri (string->uri url))
880 (port (guix:open-connection-for-uri uri #:timeout timeout))
881 (response (http-head uri #:port port)))
883 (case (response-code response)
885 (uri->string (assoc-ref (response-headers response) 'location)))
888 (define (follow-redirects-to-github uri)
890 ((string-prefix? "https://github.com/" uri) uri)
891 ((string-prefix? "http" uri)
892 (and=> (follow-redirect uri) follow-redirects-to-github))
893 ;; Do not attempt to follow redirects on URIs other than http and https
894 ;; (such as mirror, file)
897 (let ((origin (package-source package)))
898 (if (and (origin? origin)
899 (eqv? (origin-method origin) url-fetch))
902 (and=> (follow-redirects-to-github uri)
904 (if (string=? github-uri uri)
908 (G_ "URL should be '~a'")
911 (origin-uris origin))
916 ;; Guile 3.0.0 does not export this predicate.
917 (define exception-with-kind-and-args?
918 (exception-predicate &exception-with-kind-and-args)))
920 (define exception-with-kind-and-args?
923 (define* (check-derivation package #:key store)
924 "Emit a warning if we fail to compile PACKAGE to a derivation."
925 (define (try store system)
926 (catch #t ;TODO: Remove 'catch' when Guile 2.x is no longer supported.
928 (guard (c ((store-protocol-error? c)
929 (make-warning package
930 (G_ "failed to create ~a derivation: ~a")
932 (store-protocol-error-message c))))
933 ((exception-with-kind-and-args? c)
934 (make-warning package
935 (G_ "failed to create ~a derivation: ~s")
937 (cons (exception-kind c)
938 (exception-args c)))))
939 ((message-condition? c)
940 (make-warning package
941 (G_ "failed to create ~a derivation: ~a")
943 (condition-message c)))))
944 (parameterize ((%graft? #f))
945 (package-derivation store package system #:graft? #f)
947 ;; If there's a replacement, make sure we can compute its
949 (match (package-replacement package)
952 (package-derivation store replacement system
955 (make-warning package
956 (G_ "failed to create ~a derivation: ~s")
957 (list system args)))))
959 (define (check-with-store store)
960 (filter lint-warning?
961 (map (cut try store <>) (package-supported-systems package))))
963 ;; For backwards compatability, don't rely on store being set
964 (or (and=> store check-with-store)
966 (check-with-store store))))
968 (define (check-license package)
969 "Warn about type errors of the 'license' field of PACKAGE."
970 (match (package-license package)
976 (make-warning package (G_ "invalid license field")
977 #:field 'license)))))
979 (define (call-with-networking-fail-safe message error-value proc)
980 "Call PROC catching any network-related errors. Upon a networking error,
981 display a message including MESSAGE and return ERROR-VALUE."
982 (guard (c ((http-get-error? c)
983 (warning (G_ "~a: HTTP GET error for ~a: ~a (~s)~%")
985 (uri->string (http-get-error-uri c))
986 (http-get-error-code c)
987 (http-get-error-reason c))
992 (('getaddrinfo-error errcode)
993 (warning (G_ "~a: host lookup failure: ~a~%")
995 (gai-strerror errcode))
997 (('tls-certificate-error args ...)
998 (warning (G_ "~a: TLS certificate error: ~a")
1000 (tls-certificate-error-string args))
1002 ((and ('system-error _ ...) args)
1003 (let ((errno (system-error-errno args)))
1004 (if (member errno (list ECONNRESET ECONNABORTED ECONNREFUSED))
1005 (let ((details (call-with-output-string
1007 (print-exception port #f (car args)
1009 (warning (G_ "~a: ~a~%") message details)
1011 (apply throw args))))
1013 (apply throw args))))))
1015 (define-syntax-rule (with-networking-fail-safe message error-value exp ...)
1016 (call-with-networking-fail-safe message error-value
1017 (lambda () exp ...)))
1019 (define (current-vulnerabilities*)
1020 "Like 'current-vulnerabilities', but return the empty list upon networking
1021 or HTTP errors. This allows network-less operation and makes problems with
1022 the NIST server non-fatal."
1023 (with-networking-fail-safe (G_ "while retrieving CVE vulnerabilities")
1025 (current-vulnerabilities)))
1027 (define package-vulnerabilities
1028 (let ((lookup (delay (vulnerabilities->lookup-proc
1029 (current-vulnerabilities*)))))
1031 "Return a list of vulnerabilities affecting PACKAGE."
1032 ;; First we retrieve the Common Platform Enumeration (CPE) name and
1033 ;; version for PACKAGE, then we can pass them to LOOKUP.
1034 (let ((name (or (assoc-ref (package-properties package)
1036 (package-name package)))
1037 (version (or (assoc-ref (package-properties package)
1039 (package-version package))))
1040 ((force lookup) name version)))))
1042 (define* (check-vulnerabilities package
1043 #:optional (package-vulnerabilities
1044 package-vulnerabilities))
1045 "Check for known vulnerabilities for PACKAGE. Obtain the list of
1046 vulnerability records for PACKAGE by calling PACKAGE-VULNERABILITIES."
1047 (let ((package (or (package-replacement package) package)))
1048 (match (package-vulnerabilities package)
1051 ((vulnerabilities ...)
1052 (let* ((patched (package-patched-vulnerabilities package))
1053 (known-safe (or (assq-ref (package-properties package)
1056 (unpatched (remove (lambda (vuln)
1057 (let ((id (vulnerability-id vuln)))
1058 (or (member id patched)
1059 (member id known-safe))))
1061 (if (null? unpatched)
1066 (G_ "probably vulnerable to ~a")
1067 (list (string-join (map vulnerability-id unpatched)
1070 (define (check-for-updates package)
1071 "Check if there is an update available for PACKAGE."
1072 (match (with-networking-fail-safe
1073 (format #f (G_ "while retrieving upstream info for '~a'")
1074 (package-name package))
1076 (package-latest-release* package (force %updaters)))
1077 ((? upstream-source? source)
1078 (if (version>? (upstream-source-version source)
1079 (package-version package))
1081 (make-warning package
1082 (G_ "can be upgraded to ~a")
1083 (list (upstream-source-version source))
1086 (#f '()))) ; cannot find newer upstream release
1089 (define (check-archival package)
1090 "Check whether PACKAGE's source code is archived on Software Heritage. If
1091 it's not, and if its source code is a VCS snapshot, then send a \"save\"
1092 request to Software Heritage.
1094 Software Heritage imposes limits on the request rate per client IP address.
1095 This checker prints a notice and stops doing anything once that limit has been
1097 (define (response->warning url method response)
1098 (if (request-rate-limit-reached? url method)
1099 (list (make-warning package
1100 (G_ "Software Heritage rate limit reached; \
1103 (list (make-warning package
1104 (G_ "'~a' returned ~a")
1105 (list url (response-code response))
1108 (define skip-key (gensym "skip-archival-check"))
1110 (define (skip-when-limit-reached url method)
1111 (or (not (request-rate-limit-reached? url method))
1112 (throw skip-key #t)))
1114 (parameterize ((%allow-request? skip-when-limit-reached))
1117 (match (and (origin? (package-source package))
1118 (package-source package))
1121 ((= origin-uri (? git-reference? reference))
1123 (git-reference-url reference))
1125 (git-reference-commit reference))
1127 (match (if (commit-id? commit)
1128 (or (lookup-revision commit)
1129 (lookup-origin-revision url commit))
1130 (lookup-origin-revision url commit))
1131 ((? revision? revision)
1134 ;; Revision is missing from the archive, attempt to save it.
1137 (save-origin (git-reference-url reference) "git")
1140 ;; TRANSLATORS: "Software Heritage" is a proper noun
1141 ;; that must remain untranslated. See
1142 ;; <https://www.softwareheritage.org>.
1143 (G_ "scheduled Software Heritage archival")
1145 (lambda (key url method response . _)
1146 (cond ((= 429 (response-code response))
1149 (G_ "archival rate limit exceeded; \
1153 (response->warning url method response))))))))
1155 ;; Since "save" origins are not supported for non-VCS source, all
1156 ;; we can do is tell whether a given tarball is available or not.
1157 (if (origin-sha256 origin) ;XXX: for ungoogled-chromium
1158 (match (lookup-content (origin-sha256 origin) "sha256")
1160 (list (make-warning package
1161 (G_ "source not archived on Software \
1168 ((key url method response)
1169 (response->warning url method response))
1171 (if (eq? key skip-key)
1173 (with-networking-fail-safe
1174 (G_ "while connecting to Software Heritage")
1176 (apply throw key args))))))))
1180 ;;; Source code formatting.
1183 (define (report-tabulations package line line-number)
1184 "Warn about tabulations found in LINE."
1185 (match (string-index line #\tab)
1188 (make-warning package
1189 (G_ "tabulation on line ~a, column ~a")
1190 (list line-number index)
1192 (location (package-file package)
1196 (define (report-trailing-white-space package line line-number)
1197 "Warn about trailing white space in LINE."
1198 (and (not (or (string=? line (string-trim-right line))
1199 (string=? line (string #\page))))
1200 (make-warning package
1201 (G_ "trailing white space on line ~a")
1204 (location (package-file package)
1208 (define (report-long-line package line line-number)
1209 "Emit a warning if LINE is too long."
1210 ;; Note: We don't warn at 80 characters because sometimes hashes and URLs
1211 ;; make it hard to fit within that limit and we want to avoid making too
1213 (and (> (string-length line) 90)
1214 (make-warning package
1215 (G_ "line ~a is way too long (~a characters)")
1216 (list line-number (string-length line))
1218 (location (package-file package)
1222 (define %hanging-paren-rx
1223 (make-regexp "^[[:blank:]]*[()]+[[:blank:]]*$"))
1225 (define (report-lone-parentheses package line line-number)
1226 "Emit a warning if LINE contains hanging parentheses."
1227 (and (regexp-exec %hanging-paren-rx line)
1228 (make-warning package
1229 (G_ "parentheses feel lonely, \
1230 move to the previous or next line")
1233 (location (package-file package)
1237 (define %formatting-reporters
1238 ;; List of procedures that report formatting issues. These are not separate
1239 ;; checkers because they would need to re-read the file.
1240 (list report-tabulations
1241 report-trailing-white-space
1243 report-lone-parentheses))
1245 (define* (report-formatting-issues package file starting-line
1246 #:key (reporters %formatting-reporters))
1247 "Report white-space issues in FILE starting from STARTING-LINE, and report
1249 (define (sexp-last-line port)
1250 ;; Return the last line of the sexp read from PORT or an estimate thereof.
1251 (define &failure (list 'failure))
1253 (let ((start (ftell port))
1254 (start-line (port-line port))
1255 (sexp (catch 'read-error
1256 (lambda () (read port))
1258 (let ((line (port-line port)))
1259 (seek port start SEEK_SET)
1260 (set-port-line! port start-line)
1261 (if (eq? sexp &failure)
1262 (+ start-line 60) ;conservative estimate
1265 (call-with-input-file file
1267 (let loop ((line-number 1)
1270 (let ((line (read-line port)))
1271 (if (or (eof-object? line)
1272 (and last-line (> line-number last-line)))
1274 (if (and (= line-number starting-line)
1276 (loop (+ 1 line-number)
1277 (+ 1 (sexp-last-line port))
1279 (loop (+ 1 line-number)
1283 (if (< line-number starting-line)
1285 (filter-map (lambda (report)
1286 (report package line line-number))
1287 reporters)))))))))))
1289 (define (check-formatting package)
1290 "Check the formatting of the source code of PACKAGE."
1291 (let ((location (package-location package)))
1293 (and=> (search-path %load-path (location-file location))
1295 ;; Report issues starting from the line before the 'package'
1296 ;; form, which usually contains the 'define' form.
1297 (report-formatting-issues package file
1298 (- (location-line location) 1))))
1303 ;;; List of checkers.
1306 (define %local-checkers
1310 (description "Validate package descriptions")
1311 (check check-description-style))
1313 (name 'inputs-should-be-native)
1314 (description "Identify inputs that should be native inputs")
1315 (check check-inputs-should-be-native))
1317 (name 'inputs-should-not-be-input)
1318 (description "Identify inputs that shouldn't be inputs at all")
1319 (check check-inputs-should-not-be-an-input-at-all))
1322 ;; TRANSLATORS: <license> is the name of a data type and must not be
1324 (description "Make sure the 'license' field is a <license> \
1326 (check check-license))
1329 (description "Suggest 'mirror://' URLs")
1330 (check check-mirror-url))
1332 (name 'source-file-name)
1333 (description "Validate file names of sources")
1334 (check check-source-file-name))
1336 (name 'source-unstable-tarball)
1337 (description "Check for autogenerated tarballs")
1338 (check check-source-unstable-tarball))
1341 (description "Report failure to compile a package to a derivation")
1342 (check check-derivation)
1343 (requires-store? #t))
1345 (name 'patch-file-names)
1346 (description "Validate file names and availability of patches")
1347 (check check-patch-file-names))
1350 (description "Look for formatting issues in the source")
1351 (check check-formatting))))
1353 (define %network-dependent-checkers
1357 (description "Validate package synopses")
1358 (check check-synopsis-style))
1360 (name 'gnu-description)
1361 (description "Validate synopsis & description of GNU packages")
1362 (check check-gnu-synopsis+description))
1365 (description "Validate home-page URLs")
1366 (check check-home-page))
1369 (description "Validate source URLs")
1370 (check check-source))
1373 (description "Suggest GitHub URLs")
1374 (check check-github-url))
1377 (description "Check the Common Vulnerabilities and Exposures\
1379 (check check-vulnerabilities))
1382 (description "Check the package for new upstream releases")
1383 (check check-for-updates))
1386 (description "Ensure source code archival on Software Heritage")
1387 (check check-archival))))
1389 (define %all-checkers
1390 (append %local-checkers
1391 %network-dependent-checkers))