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 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 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) #:hide (close-connection))
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 #:use-module (ice-9 match)
50 #:use-module (ice-9 regex)
51 #:use-module (ice-9 format)
52 #:use-module (web client)
53 #:use-module (web uri)
54 #:use-module ((guix build download)
55 #:select (maybe-expand-mirrors
56 (open-connection-for-uri
57 . 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
110 (define-record-type* <lint-warning>
111 lint-warning make-lint-warning
113 (package lint-warning-package)
114 (message-text lint-warning-message-text)
115 (message-data lint-warning-message-data
117 (location lint-warning-location
120 (define (lint-warning-message warning)
122 (G_ (lint-warning-message-text warning))
123 (lint-warning-message-data warning)))
125 (define (package-file package)
127 (package-location package)))
129 (define* (%make-warning package message-text
130 #:optional (message-data '())
131 #:key field location)
137 (package-field-location package field)
138 (package-location package))))
140 (define-syntax make-warning
142 ((_ package (G_ message) rest ...)
143 (%make-warning package message rest ...))))
150 (define-record-type* <lint-checker>
151 lint-checker make-lint-checker
153 ;; TODO: add a 'certainty' field that shows how confident we are in the
154 ;; checker. Then allow users to only run checkers that have a certain
155 ;; 'certainty' level.
156 (name lint-checker-name)
157 (description lint-checker-description)
158 (check lint-checker-check))
160 (define (properly-starts-sentence? s)
161 (string-match "^[(\"'`[:upper:][:digit:]]" s))
163 (define (starts-with-abbreviation? s)
164 "Return #t if S starts with what looks like an abbreviation or acronym."
165 (string-match "^[A-Z][A-Z0-9]+\\>" s))
167 (define %quoted-identifier-rx
168 ;; A quoted identifier, like 'this'.
169 (make-regexp "['`][[:graph:]]+'"))
171 (define (check-description-style package)
172 ;; Emit a warning if stylistic issues are found in the description of PACKAGE.
173 (define (check-not-empty description)
174 (if (string-null? description)
176 (make-warning package
177 (G_ "description should not be empty")
178 #:field 'description))
181 (define (check-texinfo-markup description)
182 "Check that DESCRIPTION can be parsed as a Texinfo fragment. If the
183 markup is valid return a plain-text version of DESCRIPTION, otherwise #f."
185 (lambda () (texi->plain-text description))
186 (lambda (keys . args)
187 (make-warning package
188 (G_ "Texinfo markup in description is invalid")
189 #:field 'description))))
191 (define (check-trademarks description)
192 "Check that DESCRIPTION does not contain '™' or '®' characters. See
193 http://www.gnu.org/prep/standards/html_node/Trademarks.html."
194 (match (string-index description (char-set #\™ #\®))
195 ((and (? number?) index)
197 (make-warning package
198 (G_ "description should not contain ~
199 trademark sign '~a' at ~d")
200 (list (string-ref description index) index)
201 #:field 'description)))
204 (define (check-quotes description)
205 "Check whether DESCRIPTION contains single quotes and suggest @code."
206 (if (regexp-exec %quoted-identifier-rx description)
208 (make-warning package
209 ;; TRANSLATORS: '@code' is Texinfo markup and must be kept
211 (G_ "use @code or similar ornament instead of quotes")
212 #:field 'description))
215 (define (check-proper-start description)
216 (if (or (string-null? description)
217 (properly-starts-sentence? description)
218 (string-prefix-ci? (package-name package) description))
223 (G_ "description should start with an upper-case letter or digit")
224 #:field 'description))))
226 (define (check-end-of-sentence-space description)
227 "Check that an end-of-sentence period is followed by two spaces."
229 (reverse (fold-matches
230 "\\. [A-Z]" description '()
232 ;; Filter out matches of common abbreviations.
233 (if (find (lambda (s)
234 (string-suffix-ci? s (match:prefix m)))
235 '("i.e" "e.g" "a.k.a" "resp"))
236 r (cons (match:start m) r)))))))
237 (if (null? infractions)
240 (make-warning package
241 (G_ "sentences in description should be followed ~
242 by two spaces; possible infraction~p at ~{~a~^, ~}")
243 (list (length infractions)
245 #:field 'description)))))
247 (let ((description (package-description package)))
248 (if (string? description)
250 (check-not-empty description)
251 (check-quotes description)
252 (check-trademarks description)
253 ;; Use raw description for this because Texinfo rendering
254 ;; automatically fixes end of sentence space.
255 (check-end-of-sentence-space description)
256 (match (check-texinfo-markup description)
257 ((and warning (? lint-warning?)) (list warning))
259 (check-proper-start plain-description))))
261 (make-warning package
262 (G_ "invalid description: ~s")
264 #:field 'description)))))
266 (define (package-input-intersection inputs-to-check input-names)
267 "Return the intersection between INPUTS-TO-CHECK, the list of input tuples
268 of a package, and INPUT-NAMES, a list of package specifications such as
270 (match inputs-to-check
271 (((labels packages . outputs) ...)
272 (filter-map (lambda (package output)
273 (and (package? package)
274 (let ((input (string-append
275 (package-name package)
276 (if (> (length output) 0)
277 (string-append ":" (car output))
279 (and (member input input-names)
283 (define (check-inputs-should-be-native package)
284 ;; Emit a warning if some inputs of PACKAGE are likely to belong to its
286 (let ((inputs (package-inputs package))
290 "extra-cmake-modules"
295 "python-coverage" "python2-coverage"
296 "python-cython" "python2-cython"
297 "python-docutils" "python2-docutils"
298 "python-mock" "python2-mock"
299 "python-nose" "python2-nose"
300 "python-pbr" "python2-pbr"
301 "python-pytest" "python2-pytest"
302 "python-pytest-cov" "python2-pytest-cov"
303 "python-setuptools-scm" "python2-setuptools-scm"
304 "python-sphinx" "python2-sphinx")))
308 (G_ "'~a' should probably be a native input")
311 (package-input-intersection inputs input-names))))
313 (define (check-inputs-should-not-be-an-input-at-all package)
314 ;; Emit a warning if some inputs of PACKAGE are likely to should not be
316 (let ((input-names '("python-setuptools"
323 (G_ "'~a' should probably not be an input at all")
326 (package-input-intersection (package-direct-inputs package)
329 (define (package-name-regexp package)
330 "Return a regexp that matches PACKAGE's name as a word at the beginning of a
332 (make-regexp (string-append "^" (regexp-quote (package-name package))
336 (define (check-synopsis-style package)
337 ;; Emit a warning if stylistic issues are found in the synopsis of PACKAGE.
338 (define (check-final-period synopsis)
339 ;; Synopsis should not end with a period, except for some special cases.
340 (if (and (string-suffix? "." synopsis)
341 (not (string-suffix? "etc." synopsis)))
343 (make-warning package
344 (G_ "no period allowed at the end of the synopsis")
348 (define check-start-article
349 ;; Skip this check for GNU packages, as suggested by Karl Berry's reply to
350 ;; <http://lists.gnu.org/archive/html/bug-womb/2014-11/msg00000.html>.
351 (if (false-if-exception (gnu-package? package))
354 (if (or (string-prefix-ci? "A " synopsis)
355 (string-prefix-ci? "An " synopsis))
357 (make-warning package
358 (G_ "no article allowed at the beginning of \
363 (define (check-synopsis-length synopsis)
364 (if (>= (string-length synopsis) 80)
366 (make-warning package
367 (G_ "synopsis should be less than 80 characters long")
371 (define (check-proper-start synopsis)
372 (if (properly-starts-sentence? synopsis)
375 (make-warning package
376 (G_ "synopsis should start with an upper-case letter or digit")
377 #:field 'synopsis))))
379 (define (check-start-with-package-name synopsis)
380 (if (and (regexp-exec (package-name-regexp package) synopsis)
381 (not (starts-with-abbreviation? synopsis)))
383 (make-warning package
384 (G_ "synopsis should not start with the package name")
388 (define (check-texinfo-markup synopsis)
389 "Check that SYNOPSIS can be parsed as a Texinfo fragment. If the
390 markup is valid return a plain-text version of SYNOPSIS, otherwise #f."
393 (texi->plain-text synopsis)
395 (lambda (keys . args)
397 (make-warning package
398 (G_ "Texinfo markup in synopsis is invalid")
399 #:field 'synopsis)))))
402 (list check-proper-start
405 check-start-with-package-name
406 check-synopsis-length
407 check-texinfo-markup))
409 (match (package-synopsis package)
412 (make-warning package
413 (G_ "synopsis should not be empty")
415 ((? string? synopsis)
422 (make-warning package
423 (G_ "invalid synopsis: ~s")
425 #:field 'synopsis)))))
427 (define* (probe-uri uri #:key timeout)
428 "Probe URI, a URI object, and return two values: a symbol denoting the
429 probing status, such as 'http-response' when we managed to get an HTTP
430 response from URI, and additional details, such as the actual HTTP response.
432 TIMEOUT is the maximum number of seconds (possibly an inexact number) to wait
433 for connections to complete; when TIMEOUT is #f, wait as long as needed."
435 '((User-Agent . "GNU Guile")
440 (match (uri-scheme uri)
444 (let ((port (guix:open-connection-for-uri
445 uri #:timeout timeout))
446 (request (build-request uri #:headers headers)))
451 (write-request request port)
453 (read-response port))
455 (close-connection port))))
457 (case (response-code response)
458 ((302 ; found (redirection)
460 307 ; temporary redirection
461 308) ; permanent redirection
462 (let ((location (response-location response)))
463 (if (or (not location) (member location visited))
464 (values 'http-response response)
465 (loop location (cons location visited))))) ;follow the redirect
466 ((301) ; moved permanently
467 (let ((location (response-location response)))
468 ;; Return RESPONSE, unless the final response as we follow
469 ;; redirects is not 200.
471 (let-values (((status response2)
472 (loop location (cons location visited))))
475 (values 'http-response
476 (if (= 200 (response-code response2))
480 (values status response2))))
481 (values 'http-response response)))) ;invalid redirect
483 (values 'http-response response)))))
486 ((bad-header bad-header-component)
487 ;; This can happen if the server returns an invalid HTTP header,
488 ;; as is the case with the 'Date' header at sqlite.org.
489 (values 'invalid-http-response #f))
490 ((getaddrinfo-error system-error
491 gnutls-error tls-certificate-error)
494 (apply throw key args))))))
498 (let ((conn (ftp-open (uri-host uri) #:timeout timeout)))
503 (ftp-chdir conn (dirname (uri-path uri)))
504 (ftp-size conn (basename (uri-path uri))))
507 (values 'ftp-response '(ok))))
511 (values 'ftp-response `(error ,@args)))
512 ((getaddrinfo-error system-error gnutls-error)
515 (apply throw key args))))))
517 (values 'unknown-protocol #f)))))
519 (define (tls-certificate-error-string args)
520 "Return a string explaining the 'tls-certificate-error' arguments ARGS."
521 (call-with-output-string
523 (print-exception port #f
524 'tls-certificate-error args))))
526 (define (validate-uri uri package field)
527 "Return #t if the given URI can be reached, otherwise return a warning for
528 PACKAGE mentioning the FIELD."
529 (let-values (((status argument)
530 (probe-uri uri #:timeout 3))) ;wait at most 3 seconds
533 (cond ((= 200 (response-code argument))
534 (match (response-content-length argument)
536 ;; As of July 2016, SourceForge returns 200 (instead of 404)
537 ;; with a small HTML page upon failure. Attempt to detect
538 ;; such malicious behavior.
540 (make-warning package
541 (G_ "URI ~a returned \
542 suspiciously small file (~a bytes)")
543 (list (uri->string uri)
547 ((= 301 (response-code argument))
548 (if (response-location argument)
549 (make-warning package
550 (G_ "permanent redirect from ~a to ~a")
551 (list (uri->string uri)
553 (response-location argument)))
555 (make-warning package
556 (G_ "invalid permanent redirect \
558 (list (uri->string uri))
561 (make-warning package
562 (G_ "URI ~a not reachable: ~a (~s)")
563 (list (uri->string uri)
564 (response-code argument)
565 (response-reason-phrase argument))
570 (('error port command code message)
571 (make-warning package
572 (G_ "URI ~a not reachable: ~a (~s)")
573 (list (uri->string uri)
574 code (string-trim-both message))
577 (make-warning package
578 (G_ "URI ~a domain not found: ~a")
579 (list (uri->string uri)
580 (gai-strerror (car argument)))
583 (make-warning package
584 (G_ "URI ~a unreachable: ~a")
585 (list (uri->string uri)
588 (cons status argument))))
590 ((tls-certificate-error)
591 (make-warning package
592 (G_ "TLS certificate error: ~a")
593 (list (tls-certificate-error-string argument))
595 ((invalid-http-response gnutls-error)
596 ;; Probably a misbehaving server; ignore.
598 ((unknown-protocol) ;nothing we can do
601 (error "internal linter error" status)))))
603 (define (check-home-page package)
604 "Emit a warning if PACKAGE has an invalid 'home-page' field, or if that
605 'home-page' is not reachable."
606 (let ((uri (and=> (package-home-page package) string->uri)))
609 (match (validate-uri uri package 'home-page)
610 ((and (? lint-warning? warning) warning)
613 ((not (package-home-page package))
614 (if (or (string-contains (package-name package) "bootstrap")
615 (string=? (package-name package) "ld-wrapper"))
618 (make-warning package
619 (G_ "invalid value for home page")
620 #:field 'home-page))))
623 (make-warning package
624 (G_ "invalid home page URL: ~s")
625 (list (package-home-page package))
626 #:field 'home-page))))))
628 (define %distro-directory
630 (dirname (search-path %load-path "gnu.scm"))))
632 (define (check-patch-file-names package)
633 "Emit a warning if the patches requires by PACKAGE are badly named or if the
634 patch could not be found."
635 (guard (c ((message-condition? c) ;raised by 'search-patch'
637 ;; Use %make-warning, as condition-mesasge is already
639 (%make-warning package (condition-message c)
640 #:field 'patch-file-names))))
642 (or (and=> (package-source package) origin-patches)
646 (if (every (match-lambda ;patch starts with package name?
648 (and=> (string-contains (basename patch)
649 (package-name package))
651 (_ #f)) ;must be an <origin> or something like that.
657 (G_ "file names of patches should start with the package name")
658 #:field 'patch-file-names)))
660 ;; Check whether we're reaching tar's maximum file name length.
661 (let ((prefix (string-length (%distro-directory)))
662 (margin (string-length "guix-0.13.0-10-123456789/"))
664 (filter-map (match-lambda
666 (if (> (+ margin (if (string-prefix? (%distro-directory)
668 (- (string-length patch) prefix)
669 (string-length patch)))
673 (G_ "~a: file name is too long")
674 (list (basename patch))
675 #:field 'patch-file-names)
680 (define (escape-quotes str)
681 "Replace any quote character in STR by an escaped quote character."
683 (string-fold-right (lambda (chr result)
685 (#\" (cons* #\\ #\"result))
686 (_ (cons chr result))))
690 (define official-gnu-packages*
692 "A memoizing version of 'official-gnu-packages' that returns the empty
693 list when something goes wrong, such as a networking issue."
694 (let ((gnus (false-if-exception (official-gnu-packages))))
697 (define (check-gnu-synopsis+description package)
698 "Make sure that, if PACKAGE is a GNU package, it uses the synopsis and
699 descriptions maintained upstream."
700 (match (find (lambda (descriptor)
701 (string=? (gnu-package-name descriptor)
702 (package-name package)))
703 (official-gnu-packages*))
704 (#f ;not a GNU package, so nothing to do
706 (descriptor ;a genuine GNU package
708 (let ((upstream (gnu-package-doc-summary descriptor))
709 (downstream (package-synopsis package)))
711 (or (not (string? downstream))
712 (not (string=? upstream downstream))))
714 (make-warning package
715 (G_ "proposed synopsis: ~s~%")
720 (let ((upstream (gnu-package-doc-description descriptor))
721 (downstream (package-description package)))
723 (or (not (string? downstream))
724 (not (string=? (fill-paragraph upstream 100)
725 (fill-paragraph downstream 100)))))
729 (G_ "proposed description:~% \"~a\"~%")
730 (list (fill-paragraph (escape-quotes upstream) 77 7))
731 #:field 'description))
734 (define (origin-uris origin)
735 "Return the list of URIs (strings) for ORIGIN."
736 (match (origin-uri origin)
742 (define (check-source package)
743 "Emit a warning if PACKAGE has an invalid 'source' field, or if that
744 'source' is not reachable."
745 (define (warnings-for-uris uris)
746 (let loop ((uris uris)
752 (match (validate-uri uri package 'source)
754 ;; We found a working URL, so stop right away.
756 ((? lint-warning? warning)
757 (loop rest (cons warning warnings))))))))
759 (let ((origin (package-source package)))
761 (eqv? (origin-method origin) url-fetch))
762 (let* ((uris (append-map (cut maybe-expand-mirrors <> %mirrors)
763 (map string->uri (origin-uris origin))))
764 (warnings (warnings-for-uris uris)))
766 ;; Just make sure that at least one of the URIs is valid.
767 (if (= (length uris) (length warnings))
768 ;; When everything fails, report all of WARNINGS, otherwise don't
771 ;; XXX: Ideally we'd still allow warnings to be raised if *some*
772 ;; URIs are unreachable, but distinguish that from the error case
773 ;; where *all* the URIs are unreachable.
775 (make-warning package
776 (G_ "all the source URIs are unreachable:")
782 (define (check-source-file-name package)
783 "Emit a warning if PACKAGE's origin has no meaningful file name."
784 (define (origin-file-name-valid? origin)
785 ;; Return #f if the source file name contains only a version or is #f;
786 ;; indicates that the origin needs a 'file-name' field.
787 (let ((file-name (origin-actual-file-name origin))
788 (version (package-version package)))
790 ;; Common in many projects is for the filename to start
791 ;; with a "v" followed by the version,
792 ;; e.g. "v3.2.0.tar.gz".
793 (not (string-match (string-append "^v?" version) file-name)))))
795 (let ((origin (package-source package)))
796 (if (or (not origin) (origin-file-name-valid? origin))
799 (make-warning package
800 (G_ "the source file name should contain the package name")
803 (define (check-source-unstable-tarball package)
804 "Emit a warning if PACKAGE's source is an autogenerated tarball."
805 (define (check-source-uri uri)
806 (if (and (string=? (uri-host (string->uri uri)) "github.com")
807 (match (split-and-decode-uri-path
808 (uri-path (string->uri uri)))
809 ((_ _ "archive" _ ...) #t)
811 (make-warning package
812 (G_ "the source URI should not be an autogenerated tarball")
816 (let ((origin (package-source package)))
817 (if (and (origin? origin)
818 (eqv? (origin-method origin) url-fetch))
819 (filter-map check-source-uri
820 (origin-uris origin))
823 (define (check-mirror-url package)
824 "Check whether PACKAGE uses source URLs that should be 'mirror://'."
825 (define (check-mirror-uri uri) ;XXX: could be optimized
826 (let loop ((mirrors %mirrors))
830 (((mirror-id mirror-urls ...) rest ...)
831 (match (find (cut string-prefix? <> uri) mirror-urls)
835 (make-warning package
839 (string-drop uri (string-length prefix)))
840 #:field 'source)))))))
842 (let ((origin (package-source package)))
843 (if (and (origin? origin)
844 (eqv? (origin-method origin) url-fetch))
845 (let ((uris (origin-uris origin)))
846 (filter-map check-mirror-uri uris))
849 (define* (check-github-url package #:key (timeout 3))
850 "Check whether PACKAGE uses source URLs that redirect to GitHub."
851 (define (follow-redirect url)
852 (let* ((uri (string->uri url))
853 (port (guix:open-connection-for-uri uri #:timeout timeout))
854 (response (http-head uri #:port port)))
856 (case (response-code response)
858 (uri->string (assoc-ref (response-headers response) 'location)))
861 (define (follow-redirects-to-github uri)
863 ((string-prefix? "https://github.com/" uri) uri)
864 ((string-prefix? "http" uri)
865 (and=> (follow-redirect uri) follow-redirects-to-github))
866 ;; Do not attempt to follow redirects on URIs other than http and https
867 ;; (such as mirror, file)
870 (let ((origin (package-source package)))
871 (if (and (origin? origin)
872 (eqv? (origin-method origin) url-fetch))
875 (and=> (follow-redirects-to-github uri)
877 (if (string=? github-uri uri)
881 (G_ "URL should be '~a'")
884 (origin-uris origin))
887 (define (check-derivation package)
888 "Emit a warning if we fail to compile PACKAGE to a derivation."
892 (guard (c ((store-protocol-error? c)
893 (make-warning package
894 (G_ "failed to create ~a derivation: ~a")
896 (store-protocol-error-message c))))
897 ((message-condition? c)
898 (make-warning package
899 (G_ "failed to create ~a derivation: ~a")
901 (condition-message c)))))
903 ;; Disable grafts since it can entail rebuilds.
904 (parameterize ((%graft? #f))
905 (package-derivation store package system #:graft? #f)
907 ;; If there's a replacement, make sure we can compute its
909 (match (package-replacement package)
912 (package-derivation store replacement system
915 (make-warning package
916 (G_ "failed to create ~a derivation: ~s")
917 (list system args)))))
919 (filter lint-warning?
920 (map try (package-supported-systems package))))
922 (define (check-license package)
923 "Warn about type errors of the 'license' field of PACKAGE."
924 (match (package-license package)
930 (make-warning package (G_ "invalid license field")
931 #:field 'license)))))
933 (define (call-with-networking-fail-safe message error-value proc)
934 "Call PROC catching any network-related errors. Upon a networking error,
935 display a message including MESSAGE and return ERROR-VALUE."
936 (guard (c ((http-get-error? c)
937 (warning (G_ "~a: HTTP GET error for ~a: ~a (~s)~%")
939 (uri->string (http-get-error-uri c))
940 (http-get-error-code c)
941 (http-get-error-reason c))
946 (('getaddrinfo-error errcode)
947 (warning (G_ "~a: host lookup failure: ~a~%")
949 (gai-strerror errcode))
951 (('tls-certificate-error args ...)
952 (warning (G_ "~a: TLS certificate error: ~a")
954 (tls-certificate-error-string args))
956 ((and ('system-error _ ...) args)
957 (let ((errno (system-error-errno args)))
958 (if (member errno (list ECONNRESET ECONNABORTED ECONNREFUSED))
959 (let ((details (call-with-output-string
961 (print-exception port #f (car args)
963 (warning (G_ "~a: ~a~%") message details)
965 (apply throw args))))
967 (apply throw args))))))
969 (define-syntax-rule (with-networking-fail-safe message error-value exp ...)
970 (call-with-networking-fail-safe message error-value
971 (lambda () exp ...)))
973 (define (current-vulnerabilities*)
974 "Like 'current-vulnerabilities', but return the empty list upon networking
975 or HTTP errors. This allows network-less operation and makes problems with
976 the NIST server non-fatal."
977 (with-networking-fail-safe (G_ "while retrieving CVE vulnerabilities")
979 (current-vulnerabilities)))
981 (define package-vulnerabilities
982 (let ((lookup (delay (vulnerabilities->lookup-proc
983 (current-vulnerabilities*)))))
985 "Return a list of vulnerabilities affecting PACKAGE."
986 ;; First we retrieve the Common Platform Enumeration (CPE) name and
987 ;; version for PACKAGE, then we can pass them to LOOKUP.
988 (let ((name (or (assoc-ref (package-properties package)
990 (package-name package)))
991 (version (or (assoc-ref (package-properties package)
993 (package-version package))))
994 ((force lookup) name version)))))
996 (define (check-vulnerabilities package)
997 "Check for known vulnerabilities for PACKAGE."
998 (let ((package (or (package-replacement package) package)))
999 (match (package-vulnerabilities package)
1002 ((vulnerabilities ...)
1003 (let* ((patched (package-patched-vulnerabilities package))
1004 (known-safe (or (assq-ref (package-properties package)
1007 (unpatched (remove (lambda (vuln)
1008 (let ((id (vulnerability-id vuln)))
1009 (or (member id patched)
1010 (member id known-safe))))
1012 (if (null? unpatched)
1017 (G_ "probably vulnerable to ~a")
1018 (list (string-join (map vulnerability-id unpatched)
1021 (define (check-for-updates package)
1022 "Check if there is an update available for PACKAGE."
1023 (match (with-networking-fail-safe
1024 (format #f (G_ "while retrieving upstream info for '~a'")
1025 (package-name package))
1027 (package-latest-release* package (force %updaters)))
1028 ((? upstream-source? source)
1029 (if (version>? (upstream-source-version source)
1030 (package-version package))
1032 (make-warning package
1033 (G_ "can be upgraded to ~a")
1034 (list (upstream-source-version source))
1037 (#f '()))) ; cannot find newer upstream release
1040 (define (check-archival package)
1041 "Check whether PACKAGE's source code is archived on Software Heritage. If
1042 it's not, and if its source code is a VCS snapshot, then send a \"save\"
1043 request to Software Heritage.
1045 Software Heritage imposes limits on the request rate per client IP address.
1046 This checker prints a notice and stops doing anything once that limit has been
1048 (define (response->warning url method response)
1049 (if (request-rate-limit-reached? url method)
1050 (list (make-warning package
1051 (G_ "Software Heritage rate limit reached; \
1054 (list (make-warning package
1055 (G_ "'~a' returned ~a")
1056 (list url (response-code response))
1059 (define skip-key (gensym "skip-archival-check"))
1061 (define (skip-when-limit-reached url method)
1062 (or (not (request-rate-limit-reached? url method))
1063 (throw skip-key #t)))
1065 (parameterize ((%allow-request? skip-when-limit-reached))
1068 (match (and (origin? (package-source package))
1069 (package-source package))
1072 ((= origin-uri (? git-reference? reference))
1074 (git-reference-url reference))
1076 (git-reference-commit reference))
1078 (match (if (commit-id? commit)
1079 (or (lookup-revision commit)
1080 (lookup-origin-revision url commit))
1081 (lookup-origin-revision url commit))
1082 ((? revision? revision)
1085 ;; Revision is missing from the archive, attempt to save it.
1088 (save-origin (git-reference-url reference) "git")
1091 ;; TRANSLATORS: "Software Heritage" is a proper noun
1092 ;; that must remain untranslated. See
1093 ;; <https://www.softwareheritage.org>.
1094 (G_ "scheduled Software Heritage archival")
1096 (lambda (key url method response . _)
1097 (cond ((= 429 (response-code response))
1100 (G_ "archival rate limit exceeded; \
1104 (response->warning url method response))))))))
1106 ;; Since "save" origins are not supported for non-VCS source, all
1107 ;; we can do is tell whether a given tarball is available or not.
1108 (if (origin-sha256 origin) ;XXX: for ungoogled-chromium
1109 (match (lookup-content (origin-sha256 origin) "sha256")
1111 (list (make-warning package
1112 (G_ "source not archived on Software \
1119 ((key url method response)
1120 (response->warning url method response))
1122 (if (eq? key skip-key)
1124 (apply throw key args)))))))
1128 ;;; Source code formatting.
1131 (define (report-tabulations package line line-number)
1132 "Warn about tabulations found in LINE."
1133 (match (string-index line #\tab)
1136 (make-warning package
1137 (G_ "tabulation on line ~a, column ~a")
1138 (list line-number index)
1140 (location (package-file package)
1144 (define (report-trailing-white-space package line line-number)
1145 "Warn about trailing white space in LINE."
1146 (and (not (or (string=? line (string-trim-right line))
1147 (string=? line (string #\page))))
1148 (make-warning package
1149 (G_ "trailing white space on line ~a")
1152 (location (package-file package)
1156 (define (report-long-line package line line-number)
1157 "Emit a warning if LINE is too long."
1158 ;; Note: We don't warn at 80 characters because sometimes hashes and URLs
1159 ;; make it hard to fit within that limit and we want to avoid making too
1161 (and (> (string-length line) 90)
1162 (make-warning package
1163 (G_ "line ~a is way too long (~a characters)")
1164 (list line-number (string-length line))
1166 (location (package-file package)
1170 (define %hanging-paren-rx
1171 (make-regexp "^[[:blank:]]*[()]+[[:blank:]]*$"))
1173 (define (report-lone-parentheses package line line-number)
1174 "Emit a warning if LINE contains hanging parentheses."
1175 (and (regexp-exec %hanging-paren-rx line)
1176 (make-warning package
1177 (G_ "parentheses feel lonely, \
1178 move to the previous or next line")
1181 (location (package-file package)
1185 (define %formatting-reporters
1186 ;; List of procedures that report formatting issues. These are not separate
1187 ;; checkers because they would need to re-read the file.
1188 (list report-tabulations
1189 report-trailing-white-space
1191 report-lone-parentheses))
1193 (define* (report-formatting-issues package file starting-line
1194 #:key (reporters %formatting-reporters))
1195 "Report white-space issues in FILE starting from STARTING-LINE, and report
1197 (define (sexp-last-line port)
1198 ;; Return the last line of the sexp read from PORT or an estimate thereof.
1199 (define &failure (list 'failure))
1201 (let ((start (ftell port))
1202 (start-line (port-line port))
1203 (sexp (catch 'read-error
1204 (lambda () (read port))
1206 (let ((line (port-line port)))
1207 (seek port start SEEK_SET)
1208 (set-port-line! port start-line)
1209 (if (eq? sexp &failure)
1210 (+ start-line 60) ;conservative estimate
1213 (call-with-input-file file
1215 (let loop ((line-number 1)
1218 (let ((line (read-line port)))
1219 (if (or (eof-object? line)
1220 (and last-line (> line-number last-line)))
1222 (if (and (= line-number starting-line)
1224 (loop (+ 1 line-number)
1225 (+ 1 (sexp-last-line port))
1227 (loop (+ 1 line-number)
1231 (if (< line-number starting-line)
1233 (filter-map (lambda (report)
1234 (report package line line-number))
1235 reporters)))))))))))
1237 (define (check-formatting package)
1238 "Check the formatting of the source code of PACKAGE."
1239 (let ((location (package-location package)))
1241 (and=> (search-path %load-path (location-file location))
1243 ;; Report issues starting from the line before the 'package'
1244 ;; form, which usually contains the 'define' form.
1245 (report-formatting-issues package file
1246 (- (location-line location) 1))))
1251 ;;; List of checkers.
1254 (define %local-checkers
1258 (description "Validate package descriptions")
1259 (check check-description-style))
1261 (name 'inputs-should-be-native)
1262 (description "Identify inputs that should be native inputs")
1263 (check check-inputs-should-be-native))
1265 (name 'inputs-should-not-be-input)
1266 (description "Identify inputs that shouldn't be inputs at all")
1267 (check check-inputs-should-not-be-an-input-at-all))
1270 ;; TRANSLATORS: <license> is the name of a data type and must not be
1272 (description "Make sure the 'license' field is a <license> \
1274 (check check-license))
1277 (description "Suggest 'mirror://' URLs")
1278 (check check-mirror-url))
1280 (name 'source-file-name)
1281 (description "Validate file names of sources")
1282 (check check-source-file-name))
1284 (name 'source-unstable-tarball)
1285 (description "Check for autogenerated tarballs")
1286 (check check-source-unstable-tarball))
1289 (description "Report failure to compile a package to a derivation")
1290 (check check-derivation))
1292 (name 'patch-file-names)
1293 (description "Validate file names and availability of patches")
1294 (check check-patch-file-names))
1297 (description "Look for formatting issues in the source")
1298 (check check-formatting))))
1300 (define %network-dependent-checkers
1304 (description "Validate package synopses")
1305 (check check-synopsis-style))
1307 (name 'gnu-description)
1308 (description "Validate synopsis & description of GNU packages")
1309 (check check-gnu-synopsis+description))
1312 (description "Validate home-page URLs")
1313 (check check-home-page))
1316 (description "Validate source URLs")
1317 (check check-source))
1320 (description "Suggest GitHub URLs")
1321 (check check-github-url))
1324 (description "Check the Common Vulnerabilities and Exposures\
1326 (check check-vulnerabilities))
1329 (description "Check the package for new upstream releases")
1330 (check check-for-updates))
1333 (description "Ensure source code archival on Software Heritage")
1334 (check check-archival))))
1336 (define %all-checkers
1337 (append %local-checkers
1338 %network-dependent-checkers))