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>
12 ;;; Copyright © 2020 Chris Marusich <cmmarusich@gmail.com>
14 ;;; This file is part of GNU Guix.
16 ;;; GNU Guix is free software; you can redistribute it and/or modify it
17 ;;; under the terms of the GNU General Public License as published by
18 ;;; the Free Software Foundation; either version 3 of the License, or (at
19 ;;; your option) any later version.
21 ;;; GNU Guix is distributed in the hope that it will be useful, but
22 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;;; GNU General Public License for more details.
26 ;;; You should have received a copy of the GNU General Public License
27 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
29 (define-module (guix lint)
30 #:use-module (guix store)
31 #:use-module (guix base32)
32 #:use-module (guix diagnostics)
33 #:use-module (guix download)
34 #:use-module (guix ftp-client)
35 #:use-module (guix http-client)
36 #:use-module (guix packages)
37 #:use-module (guix i18n)
38 #:use-module (guix licenses)
39 #:use-module (guix records)
40 #:use-module (guix grafts)
41 #:use-module (guix upstream)
42 #:use-module (guix utils)
43 #:use-module (guix memoization)
44 #:use-module (guix profiles)
45 #:use-module (guix monads)
46 #:use-module (guix scripts)
47 #:use-module ((guix ui) #:select (texi->plain-text fill-paragraph))
48 #:use-module (guix gnu-maintenance)
49 #:use-module (guix cve)
50 #:use-module ((guix swh) #:hide (origin?))
51 #:autoload (guix git-download) (git-reference?
52 git-reference-url git-reference-commit)
53 #:use-module (ice-9 match)
54 #:use-module (ice-9 regex)
55 #:use-module (ice-9 format)
56 #:use-module (web client)
57 #:use-module (web uri)
58 #:use-module ((guix build download)
59 #:select (maybe-expand-mirrors
60 (open-connection-for-uri
61 . guix:open-connection-for-uri)))
62 #:use-module (web request)
63 #:use-module (web response)
64 #:use-module (srfi srfi-1)
65 #:use-module (srfi srfi-6) ;Unicode string ports
66 #:use-module (srfi srfi-9)
67 #:use-module (srfi srfi-11)
68 #:use-module (srfi srfi-26)
69 #:use-module (srfi srfi-34)
70 #:use-module (srfi srfi-35)
71 #:use-module (ice-9 rdelim)
72 #:export (check-description-style
73 check-inputs-should-be-native
74 check-inputs-should-not-be-an-input-at-all
75 check-patch-file-names
80 check-source-file-name
81 check-source-unstable-tarball
89 check-profile-collisions
95 lint-warning-message-text
96 lint-warning-message-data
100 %network-dependent-checkers
106 lint-checker-description
108 lint-checker-requires-store?))
115 (define-record-type* <lint-warning>
116 lint-warning make-lint-warning
118 (package lint-warning-package)
119 (message-text lint-warning-message-text)
120 (message-data lint-warning-message-data
122 (location lint-warning-location
125 (define (lint-warning-message warning)
127 (G_ (lint-warning-message-text warning))
128 (lint-warning-message-data warning)))
130 (define (package-file package)
132 (package-location package)))
134 (define* (%make-warning package message-text
135 #:optional (message-data '())
136 #:key field location)
142 (and field (package-field-location package field))
143 (package-location package))))
145 (define-syntax make-warning
147 ((_ package (G_ message) rest ...)
148 (%make-warning package message rest ...))))
155 (define-record-type* <lint-checker>
156 lint-checker make-lint-checker
158 ;; TODO: add a 'certainty' field that shows how confident we are in the
159 ;; checker. Then allow users to only run checkers that have a certain
160 ;; 'certainty' level.
161 (name lint-checker-name)
162 (description lint-checker-description)
163 (check lint-checker-check)
164 (requires-store? lint-checker-requires-store?
167 (define (properly-starts-sentence? s)
168 (string-match "^[(\"'`[:upper:][:digit:]]" s))
170 (define (starts-with-abbreviation? s)
171 "Return #t if S starts with what looks like an abbreviation or acronym."
172 (string-match "^[A-Z][A-Z0-9]+\\>" s))
174 (define %quoted-identifier-rx
175 ;; A quoted identifier, like 'this'.
176 (make-regexp "['`][[:graph:]]+'"))
178 (define (check-description-style package)
179 ;; Emit a warning if stylistic issues are found in the description of PACKAGE.
180 (define (check-not-empty description)
181 (if (string-null? description)
183 (make-warning package
184 (G_ "description should not be empty")
185 #:field 'description))
188 (define (check-texinfo-markup description)
189 "Check that DESCRIPTION can be parsed as a Texinfo fragment. If the
190 markup is valid return a plain-text version of DESCRIPTION, otherwise #f."
192 (lambda () (texi->plain-text description))
193 (lambda (keys . args)
194 (make-warning package
195 (G_ "Texinfo markup in description is invalid")
196 #:field 'description))))
198 (define (check-trademarks description)
199 "Check that DESCRIPTION does not contain '™' or '®' characters. See
200 http://www.gnu.org/prep/standards/html_node/Trademarks.html."
201 (match (string-index description (char-set #\™ #\®))
202 ((and (? number?) index)
204 (make-warning package
205 (G_ "description should not contain ~
206 trademark sign '~a' at ~d")
207 (list (string-ref description index) index)
208 #:field 'description)))
211 (define (check-quotes description)
212 "Check whether DESCRIPTION contains single quotes and suggest @code."
213 (if (regexp-exec %quoted-identifier-rx description)
215 (make-warning package
216 ;; TRANSLATORS: '@code' is Texinfo markup and must be kept
218 (G_ "use @code or similar ornament instead of quotes")
219 #:field 'description))
222 (define (check-proper-start description)
223 (if (or (string-null? description)
224 (properly-starts-sentence? description)
225 (string-prefix-ci? (package-name package) description))
230 (G_ "description should start with an upper-case letter or digit")
231 #:field 'description))))
233 (define (check-end-of-sentence-space description)
234 "Check that an end-of-sentence period is followed by two spaces."
236 (reverse (fold-matches
237 "\\. [A-Z]" description '()
239 ;; Filter out matches of common abbreviations.
240 (if (find (lambda (s)
241 (string-suffix-ci? s (match:prefix m)))
242 '("i.e" "e.g" "a.k.a" "resp"))
243 r (cons (match:start m) r)))))))
244 (if (null? infractions)
247 (make-warning package
248 (G_ "sentences in description should be followed ~
249 by two spaces; possible infraction~p at ~{~a~^, ~}")
250 (list (length infractions)
252 #:field 'description)))))
254 (let ((description (package-description package)))
255 (if (string? description)
257 (check-not-empty description)
258 (check-quotes description)
259 (check-trademarks description)
260 ;; Use raw description for this because Texinfo rendering
261 ;; automatically fixes end of sentence space.
262 (check-end-of-sentence-space description)
263 (match (check-texinfo-markup description)
264 ((and warning (? lint-warning?)) (list warning))
266 (check-proper-start plain-description))))
268 (make-warning package
269 (G_ "invalid description: ~s")
271 #:field 'description)))))
273 (define (package-input-intersection inputs-to-check input-names)
274 "Return the intersection between INPUTS-TO-CHECK, the list of input tuples
275 of a package, and INPUT-NAMES, a list of package specifications such as
277 (match inputs-to-check
278 (((labels packages . outputs) ...)
279 (filter-map (lambda (package output)
280 (and (package? package)
281 (let ((input (string-append
282 (package-name package)
283 (if (> (length output) 0)
284 (string-append ":" (car output))
286 (and (member input input-names)
290 (define (check-inputs-should-be-native package)
291 ;; Emit a warning if some inputs of PACKAGE are likely to belong to its
293 (let ((inputs (append (package-inputs package)
294 (package-propagated-inputs package)))
304 "extra-cmake-modules"
308 "gobject-introspection"
319 "python-coverage" "python2-coverage"
320 "python-cython" "python2-cython"
321 "python-docutils" "python2-docutils"
322 "python-mock" "python2-mock"
323 "python-nose" "python2-nose"
324 "python-pbr" "python2-pbr"
325 "python-pytest" "python2-pytest"
326 "python-pytest-cov" "python2-pytest-cov"
327 "python-setuptools-scm" "python2-setuptools-scm"
328 "python-sphinx" "python2-sphinx"
334 "xorg-server-for-tests"
339 (G_ "'~a' should probably be a native input")
342 (package-input-intersection inputs input-names))))
344 (define (check-inputs-should-not-be-an-input-at-all package)
345 ;; Emit a warning if some inputs of PACKAGE are likely to should not be
347 (let ((input-names '("python-setuptools"
354 (G_ "'~a' should probably not be an input at all")
357 (package-input-intersection (package-direct-inputs package)
360 (define (package-name-regexp package)
361 "Return a regexp that matches PACKAGE's name as a word at the beginning of a
363 (make-regexp (string-append "^" (regexp-quote (package-name package))
367 (define (check-synopsis-style package)
368 ;; Emit a warning if stylistic issues are found in the synopsis of PACKAGE.
369 (define (check-final-period synopsis)
370 ;; Synopsis should not end with a period, except for some special cases.
371 (if (and (string-suffix? "." synopsis)
372 (not (string-suffix? "etc." synopsis)))
374 (make-warning package
375 (G_ "no period allowed at the end of the synopsis")
379 (define check-start-article
380 ;; Skip this check for GNU packages, as suggested by Karl Berry's reply to
381 ;; <http://lists.gnu.org/archive/html/bug-womb/2014-11/msg00000.html>.
382 (if (false-if-exception (gnu-package? package))
385 (if (or (string-prefix-ci? "A " synopsis)
386 (string-prefix-ci? "An " synopsis))
388 (make-warning package
389 (G_ "no article allowed at the beginning of \
394 (define (check-synopsis-length synopsis)
395 (if (>= (string-length synopsis) 80)
397 (make-warning package
398 (G_ "synopsis should be less than 80 characters long")
402 (define (check-proper-start synopsis)
403 (if (properly-starts-sentence? synopsis)
406 (make-warning package
407 (G_ "synopsis should start with an upper-case letter or digit")
408 #:field 'synopsis))))
410 (define (check-start-with-package-name synopsis)
411 (if (and (regexp-exec (package-name-regexp package) synopsis)
412 (not (starts-with-abbreviation? synopsis)))
414 (make-warning package
415 (G_ "synopsis should not start with the package name")
419 (define (check-texinfo-markup synopsis)
420 "Check that SYNOPSIS can be parsed as a Texinfo fragment. If the
421 markup is valid return a plain-text version of SYNOPSIS, otherwise #f."
424 (texi->plain-text synopsis)
426 (lambda (keys . args)
428 (make-warning package
429 (G_ "Texinfo markup in synopsis is invalid")
430 #:field 'synopsis)))))
433 (list check-proper-start
436 check-start-with-package-name
437 check-synopsis-length
438 check-texinfo-markup))
440 (match (package-synopsis package)
443 (make-warning package
444 (G_ "synopsis should not be empty")
446 ((? string? synopsis)
453 (make-warning package
454 (G_ "invalid synopsis: ~s")
456 #:field 'synopsis)))))
458 (define* (probe-uri uri #:key timeout)
459 "Probe URI, a URI object, and return two values: a symbol denoting the
460 probing status, such as 'http-response' when we managed to get an HTTP
461 response from URI, and additional details, such as the actual HTTP response.
463 TIMEOUT is the maximum number of seconds (possibly an inexact number) to wait
464 for connections to complete; when TIMEOUT is #f, wait as long as needed."
466 '((User-Agent . "GNU Guile")
471 (match (uri-scheme uri)
475 (let ((port (guix:open-connection-for-uri
476 uri #:timeout timeout))
477 (request (build-request uri #:headers headers)))
482 (write-request request port)
484 (read-response port))
488 (case (response-code response)
489 ((302 ; found (redirection)
491 307 ; temporary redirection
492 308) ; permanent redirection
493 (let ((location (response-location response)))
494 (if (or (not location) (member location visited))
495 (values 'http-response response)
496 (loop location (cons location visited))))) ;follow the redirect
497 ((301) ; moved permanently
498 (let ((location (response-location response)))
499 ;; Return RESPONSE, unless the final response as we follow
500 ;; redirects is not 200.
502 (let-values (((status response2)
503 (loop location (cons location visited))))
506 (values 'http-response
507 (if (= 200 (response-code response2))
511 (values status response2))))
512 (values 'http-response response)))) ;invalid redirect
514 (values 'http-response response)))))
517 ((bad-header bad-header-component)
518 ;; This can happen if the server returns an invalid HTTP header,
519 ;; as is the case with the 'Date' header at sqlite.org.
520 (values 'invalid-http-response #f))
521 ((getaddrinfo-error system-error
522 gnutls-error tls-certificate-error)
525 (apply throw key args))))))
529 (let ((conn (ftp-open (uri-host uri) #:timeout timeout)))
534 (ftp-chdir conn (dirname (uri-path uri)))
535 (ftp-size conn (basename (uri-path uri))))
538 (values 'ftp-response '(ok))))
542 (values 'ftp-response `(error ,@args)))
543 ((getaddrinfo-error system-error gnutls-error)
546 (apply throw key args))))))
548 (values 'unknown-protocol #f)))))
550 (define (tls-certificate-error-string args)
551 "Return a string explaining the 'tls-certificate-error' arguments ARGS."
552 (call-with-output-string
554 (print-exception port #f
555 'tls-certificate-error args))))
557 (define (validate-uri uri package field)
558 "Return #t if the given URI can be reached, otherwise return a warning for
559 PACKAGE mentioning the FIELD."
560 (let-values (((status argument)
561 (probe-uri uri #:timeout 3))) ;wait at most 3 seconds
564 (cond ((= 200 (response-code argument))
565 (match (response-content-length argument)
567 ;; As of July 2016, SourceForge returns 200 (instead of 404)
568 ;; with a small HTML page upon failure. Attempt to detect
569 ;; such malicious behavior.
571 (make-warning package
572 (G_ "URI ~a returned \
573 suspiciously small file (~a bytes)")
574 (list (uri->string uri)
578 ((= 301 (response-code argument))
579 (if (response-location argument)
580 (make-warning package
581 (G_ "permanent redirect from ~a to ~a")
582 (list (uri->string uri)
584 (response-location argument)))
586 (make-warning package
587 (G_ "invalid permanent redirect \
589 (list (uri->string uri))
592 (make-warning package
593 (G_ "URI ~a not reachable: ~a (~s)")
594 (list (uri->string uri)
595 (response-code argument)
596 (response-reason-phrase argument))
601 (('error port command code message)
602 (make-warning package
603 (G_ "URI ~a not reachable: ~a (~s)")
604 (list (uri->string uri)
605 code (string-trim-both message))
608 (make-warning package
609 (G_ "URI ~a domain not found: ~a")
610 (list (uri->string uri)
611 (gai-strerror (car argument)))
614 (make-warning package
615 (G_ "URI ~a unreachable: ~a")
616 (list (uri->string uri)
619 (cons status argument))))
621 ((tls-certificate-error)
622 (make-warning package
623 (G_ "TLS certificate error: ~a")
624 (list (tls-certificate-error-string argument))
626 ((invalid-http-response gnutls-error)
627 ;; Probably a misbehaving server; ignore.
629 ((unknown-protocol) ;nothing we can do
632 (error "internal linter error" status)))))
634 (define (check-home-page package)
635 "Emit a warning if PACKAGE has an invalid 'home-page' field, or if that
636 'home-page' is not reachable."
637 (let ((uri (and=> (package-home-page package) string->uri)))
640 (match (validate-uri uri package 'home-page)
641 ((and (? lint-warning? warning) warning)
644 ((not (package-home-page package))
645 (if (or (string-contains (package-name package) "bootstrap")
646 (string=? (package-name package) "ld-wrapper"))
649 (make-warning package
650 (G_ "invalid value for home page")
651 #:field 'home-page))))
654 (make-warning package
655 (G_ "invalid home page URL: ~s")
656 (list (package-home-page package))
657 #:field 'home-page))))))
659 (define %distro-directory
661 (dirname (search-path %load-path "gnu.scm"))))
663 (define (check-patch-file-names package)
664 "Emit a warning if the patches requires by PACKAGE are badly named or if the
665 patch could not be found."
666 (guard (c ((message-condition? c) ;raised by 'search-patch'
668 ;; Use %make-warning, as condition-mesasge is already
670 (%make-warning package (condition-message c)
671 #:field 'patch-file-names)))
672 ((formatted-message? c)
673 (list (%make-warning package
675 (G_ (formatted-message-string c))
676 (formatted-message-arguments c))))))
678 (match (package-source package)
679 ((? origin? origin) (origin-patches origin))
682 (define (starts-with-package-name? file-name)
683 (and=> (string-contains file-name (package-name package))
687 (if (every (match-lambda ;patch starts with package name?
689 (starts-with-package-name? (basename patch)))
691 (starts-with-package-name? (origin-actual-file-name patch)))
692 (_ #f)) ;must be some other file-like object
698 (G_ "file names of patches should start with the package name")
699 #:field 'patch-file-names)))
701 ;; Check whether we're reaching tar's maximum file name length.
702 (let ((prefix (string-length (%distro-directory)))
703 (margin (string-length "guix-2.0.0rc3-10000-1234567890/"))
705 (filter-map (match-lambda
707 (if (> (+ margin (if (string-prefix? (%distro-directory)
709 (- (string-length patch) prefix)
710 (string-length patch)))
714 (G_ "~a: file name is too long")
715 (list (basename patch))
716 #:field 'patch-file-names)
721 (define (escape-quotes str)
722 "Replace any quote character in STR by an escaped quote character."
724 (string-fold-right (lambda (chr result)
726 (#\" (cons* #\\ #\"result))
727 (_ (cons chr result))))
731 (define official-gnu-packages*
733 "A memoizing version of 'official-gnu-packages' that returns the empty
734 list when something goes wrong, such as a networking issue."
735 (let ((gnus (false-if-exception (official-gnu-packages))))
738 (define (check-gnu-synopsis+description package)
739 "Make sure that, if PACKAGE is a GNU package, it uses the synopsis and
740 descriptions maintained upstream."
741 (match (find (lambda (descriptor)
742 (string=? (gnu-package-name descriptor)
743 (package-name package)))
744 (official-gnu-packages*))
745 (#f ;not a GNU package, so nothing to do
747 (descriptor ;a genuine GNU package
749 (let ((upstream (gnu-package-doc-summary descriptor))
750 (downstream (package-synopsis package)))
752 (or (not (string? downstream))
753 (not (string=? upstream downstream))))
755 (make-warning package
756 (G_ "proposed synopsis: ~s~%")
761 (let ((upstream (gnu-package-doc-description descriptor))
762 (downstream (package-description package)))
764 (or (not (string? downstream))
765 (not (string=? (fill-paragraph upstream 100)
766 (fill-paragraph downstream 100)))))
770 (G_ "proposed description:~% \"~a\"~%")
771 (list (fill-paragraph (escape-quotes upstream) 77 7))
772 #:field 'description))
775 (define (origin-uris origin)
776 "Return the list of URIs (strings) for ORIGIN."
777 (match (origin-uri origin)
783 (define (check-source package)
784 "Emit a warning if PACKAGE has an invalid 'source' field, or if that
785 'source' is not reachable."
786 (define (warnings-for-uris uris)
787 (let loop ((uris uris)
793 (match (validate-uri uri package 'source)
795 ;; We found a working URL, so stop right away.
798 ;; Unsupported URL or other error, skip.
799 (loop rest warnings))
800 ((? lint-warning? warning)
801 (loop rest (cons warning warnings))))))))
803 (let ((origin (package-source package)))
806 ((eq? (origin-method origin) url-fetch)
807 (let* ((uris (append-map (cut maybe-expand-mirrors <> %mirrors)
808 (map string->uri (origin-uris origin))))
809 (warnings (warnings-for-uris uris)))
811 ;; Just make sure that at least one of the URIs is valid.
812 (if (= (length uris) (length warnings))
813 ;; When everything fails, report all of WARNINGS, otherwise don't
816 ;; XXX: Ideally we'd still allow warnings to be raised if *some*
817 ;; URIs are unreachable, but distinguish that from the error case
818 ;; where *all* the URIs are unreachable.
820 (make-warning package
821 (G_ "all the source URIs are unreachable:")
825 ((git-reference? (origin-uri origin))
827 (list (string->uri (git-reference-url (origin-uri origin))))))
832 (define (check-source-file-name package)
833 "Emit a warning if PACKAGE's origin has no meaningful file name."
834 (define (origin-file-name-valid? origin)
835 ;; Return #f if the source file name contains only a version or is #f;
836 ;; indicates that the origin needs a 'file-name' field.
837 (let ((file-name (origin-actual-file-name origin))
838 (version (package-version package)))
840 ;; Common in many projects is for the filename to start
841 ;; with a "v" followed by the version,
842 ;; e.g. "v3.2.0.tar.gz".
843 (not (string-match (string-append "^v?" version) file-name)))))
845 (let ((origin (package-source package)))
846 (if (or (not (origin? origin)) (origin-file-name-valid? origin))
849 (make-warning package
850 (G_ "the source file name should contain the package name")
853 (define (check-source-unstable-tarball package)
854 "Emit a warning if PACKAGE's source is an autogenerated tarball."
855 (define (check-source-uri uri)
856 (if (and (string=? (uri-host (string->uri uri)) "github.com")
857 (match (split-and-decode-uri-path
858 (uri-path (string->uri uri)))
859 ((_ _ "archive" _ ...) #t)
861 (make-warning package
862 (G_ "the source URI should not be an autogenerated tarball")
866 (let ((origin (package-source package)))
867 (if (and (origin? origin)
868 (eqv? (origin-method origin) url-fetch))
869 (filter-map check-source-uri
870 (origin-uris origin))
873 (define (check-mirror-url package)
874 "Check whether PACKAGE uses source URLs that should be 'mirror://'."
875 (define (check-mirror-uri uri) ;XXX: could be optimized
876 (let loop ((mirrors %mirrors))
880 (((mirror-id mirror-urls ...) rest ...)
881 (match (find (cut string-prefix? <> uri) mirror-urls)
885 (make-warning package
889 (string-drop uri (string-length prefix)))
890 #:field 'source)))))))
892 (let ((origin (package-source package)))
893 (if (and (origin? origin)
894 (eqv? (origin-method origin) url-fetch))
895 (let ((uris (origin-uris origin)))
896 (filter-map check-mirror-uri uris))
899 (define* (check-github-url package #:key (timeout 3))
900 "Check whether PACKAGE uses source URLs that redirect to GitHub."
901 (define (follow-redirect url)
902 (let* ((uri (string->uri url))
903 (port (guix:open-connection-for-uri uri #:timeout timeout))
904 (response (http-head uri #:port port)))
906 (case (response-code response)
908 (uri->string (assoc-ref (response-headers response) 'location)))
911 (define (follow-redirects-to-github uri)
913 ((string-prefix? "https://github.com/" uri) uri)
914 ((string-prefix? "http" uri)
915 (and=> (follow-redirect uri) follow-redirects-to-github))
916 ;; Do not attempt to follow redirects on URIs other than http and https
917 ;; (such as mirror, file)
920 (let ((origin (package-source package)))
921 (if (and (origin? origin)
922 (eqv? (origin-method origin) url-fetch))
925 (and=> (follow-redirects-to-github uri)
927 (if (string=? github-uri uri)
931 (G_ "URL should be '~a'")
934 (origin-uris origin))
939 ;; Guile 3.0.0 does not export this predicate.
940 (define exception-with-kind-and-args?
941 (exception-predicate &exception-with-kind-and-args)))
943 (define exception-with-kind-and-args?
946 (define* (check-derivation package #:key store)
947 "Emit a warning if we fail to compile PACKAGE to a derivation."
948 (define (try store system)
949 (catch #t ;TODO: Remove 'catch' when Guile 2.x is no longer supported.
951 (guard (c ((store-protocol-error? c)
952 (make-warning package
953 (G_ "failed to create ~a derivation: ~a")
955 (store-protocol-error-message c))))
956 ((exception-with-kind-and-args? c)
957 (make-warning package
958 (G_ "failed to create ~a derivation: ~s")
960 (cons (exception-kind c)
961 (exception-args c)))))
962 ((message-condition? c)
963 (make-warning package
964 (G_ "failed to create ~a derivation: ~a")
966 (condition-message c))))
967 ((formatted-message? c)
968 (let ((str (apply format #f
969 (formatted-message-string c)
970 (formatted-message-arguments c))))
971 (make-warning package
972 (G_ "failed to create ~a derivation: ~a")
973 (list system str)))))
974 (parameterize ((%graft? #f))
975 (package-derivation store package system #:graft? #f)
977 ;; If there's a replacement, make sure we can compute its
979 (match (package-replacement package)
982 (package-derivation store replacement system
985 (make-warning package
986 (G_ "failed to create ~a derivation: ~s")
987 (list system args)))))
989 (define (check-with-store store)
990 (filter lint-warning?
991 (map (cut try store <>) (package-supported-systems package))))
993 ;; For backwards compatability, don't rely on store being set
994 (or (and=> store check-with-store)
996 (check-with-store store))))
998 (define* (check-profile-collisions package #:key store)
999 "Check for collisions that would occur when installing PACKAGE as a result
1000 of the propagated inputs it pulls in."
1001 (define (do-check store)
1002 (guard (c ((profile-collision-error? c)
1003 (let ((first (profile-collision-error-entry c))
1004 (second (profile-collision-error-conflict c)))
1006 (if (string=? (manifest-entry-version first)
1007 (manifest-entry-version second))
1010 (string-append (manifest-entry-name entry) "@"
1011 (manifest-entry-version entry)))))
1013 (list (make-warning package
1014 (G_ "propagated inputs ~a and ~a collide")
1015 (list (format first)
1016 (format second)))))))
1017 ;; Disable grafts to avoid building PACKAGE and its dependencies.
1018 (parameterize ((%graft? #f))
1019 (run-with-store store
1020 (mbegin %store-monad
1021 (check-for-collisions (packages->manifest (list package))
1030 (define (check-license package)
1031 "Warn about type errors of the 'license' field of PACKAGE."
1032 (match (package-license package)
1038 (make-warning package (G_ "invalid license field")
1039 #:field 'license)))))
1041 (define (call-with-networking-fail-safe message error-value proc)
1042 "Call PROC catching any network-related errors. Upon a networking error,
1043 display a message including MESSAGE and return ERROR-VALUE."
1044 (guard (c ((http-get-error? c)
1045 (warning (G_ "~a: HTTP GET error for ~a: ~a (~s)~%")
1047 (uri->string (http-get-error-uri c))
1048 (http-get-error-code c)
1049 (http-get-error-reason c))
1054 (('getaddrinfo-error errcode)
1055 (warning (G_ "~a: host lookup failure: ~a~%")
1057 (gai-strerror errcode))
1059 (('tls-certificate-error args ...)
1060 (warning (G_ "~a: TLS certificate error: ~a")
1062 (tls-certificate-error-string args))
1064 ((and ('system-error _ ...) args)
1065 (let ((errno (system-error-errno args)))
1066 (if (member errno (list ECONNRESET ECONNABORTED ECONNREFUSED))
1067 (let ((details (call-with-output-string
1069 (print-exception port #f (car args)
1071 (warning (G_ "~a: ~a~%") message details)
1073 (apply throw args))))
1075 (apply throw args))))))
1077 (define-syntax-rule (with-networking-fail-safe message error-value exp ...)
1078 (call-with-networking-fail-safe message error-value
1079 (lambda () exp ...)))
1081 (define (current-vulnerabilities*)
1082 "Like 'current-vulnerabilities', but return the empty list upon networking
1083 or HTTP errors. This allows network-less operation and makes problems with
1084 the NIST server non-fatal."
1085 (with-networking-fail-safe (G_ "while retrieving CVE vulnerabilities")
1087 (current-vulnerabilities #:timeout 4)))
1089 (define package-vulnerabilities
1090 (let ((lookup (delay (vulnerabilities->lookup-proc
1091 (current-vulnerabilities*)))))
1093 "Return a list of vulnerabilities affecting PACKAGE."
1094 ;; First we retrieve the Common Platform Enumeration (CPE) name and
1095 ;; version for PACKAGE, then we can pass them to LOOKUP.
1096 (let ((name (or (assoc-ref (package-properties package)
1098 (package-name package)))
1099 (version (or (assoc-ref (package-properties package)
1101 (package-version package))))
1102 ((force lookup) name version)))))
1104 (define* (check-vulnerabilities package
1105 #:optional (package-vulnerabilities
1106 package-vulnerabilities))
1107 "Check for known vulnerabilities for PACKAGE. Obtain the list of
1108 vulnerability records for PACKAGE by calling PACKAGE-VULNERABILITIES."
1109 (let ((package (or (package-replacement package) package)))
1110 (match (package-vulnerabilities package)
1113 ((vulnerabilities ...)
1114 (let* ((patched (package-patched-vulnerabilities package))
1115 (known-safe (or (assq-ref (package-properties package)
1118 (unpatched (remove (lambda (vuln)
1119 (let ((id (vulnerability-id vuln)))
1120 (or (member id patched)
1121 (member id known-safe))))
1123 (if (null? unpatched)
1128 (G_ "probably vulnerable to ~a")
1129 (list (string-join (map vulnerability-id unpatched)
1132 (define (check-for-updates package)
1133 "Check if there is an update available for PACKAGE."
1134 (match (with-networking-fail-safe
1135 (format #f (G_ "while retrieving upstream info for '~a'")
1136 (package-name package))
1138 (package-latest-release* package (force %updaters)))
1139 ((? upstream-source? source)
1140 (if (version>? (upstream-source-version source)
1141 (package-version package))
1143 (make-warning package
1144 (G_ "can be upgraded to ~a")
1145 (list (upstream-source-version source))
1148 (#f '()))) ; cannot find newer upstream release
1151 (define (check-archival package)
1152 "Check whether PACKAGE's source code is archived on Software Heritage. If
1153 it's not, and if its source code is a VCS snapshot, then send a \"save\"
1154 request to Software Heritage.
1156 Software Heritage imposes limits on the request rate per client IP address.
1157 This checker prints a notice and stops doing anything once that limit has been
1159 (define (response->warning url method response)
1160 (if (request-rate-limit-reached? url method)
1161 (list (make-warning package
1162 (G_ "Software Heritage rate limit reached; \
1165 (list (make-warning package
1166 (G_ "'~a' returned ~a")
1167 (list url (response-code response))
1170 (define skip-key (gensym "skip-archival-check"))
1172 (define (skip-when-limit-reached url method)
1173 (or (not (request-rate-limit-reached? url method))
1174 (throw skip-key #t)))
1176 (parameterize ((%allow-request? skip-when-limit-reached))
1179 (match (and (origin? (package-source package))
1180 (package-source package))
1183 ((= origin-uri (? git-reference? reference))
1185 (git-reference-url reference))
1187 (git-reference-commit reference))
1189 (match (if (commit-id? commit)
1190 (or (lookup-revision commit)
1191 (lookup-origin-revision url commit))
1192 (lookup-origin-revision url commit))
1193 ((? revision? revision)
1196 ;; Revision is missing from the archive, attempt to save it.
1199 (save-origin (git-reference-url reference) "git")
1202 ;; TRANSLATORS: "Software Heritage" is a proper noun
1203 ;; that must remain untranslated. See
1204 ;; <https://www.softwareheritage.org>.
1205 (G_ "scheduled Software Heritage archival")
1207 (lambda (key url method response . _)
1208 (cond ((= 429 (response-code response))
1211 (G_ "archival rate limit exceeded; \
1215 (response->warning url method response))))))))
1217 ;; Since "save" origins are not supported for non-VCS source, all
1218 ;; we can do is tell whether a given tarball is available or not.
1219 (if (origin-hash origin) ;XXX: for ungoogled-chromium
1220 (let ((hash (origin-hash origin)))
1221 (match (lookup-content (content-hash-value hash)
1223 (content-hash-algorithm hash)))
1225 (list (make-warning package
1226 (G_ "source not archived on Software \
1233 (('swh-error url method response)
1234 (response->warning url method response))
1236 (if (eq? key skip-key)
1238 (with-networking-fail-safe
1239 (G_ "while connecting to Software Heritage")
1241 (apply throw key args))))))))
1245 ;;; Source code formatting.
1248 (define (report-tabulations package line line-number)
1249 "Warn about tabulations found in LINE."
1250 (match (string-index line #\tab)
1253 (make-warning package
1254 (G_ "tabulation on line ~a, column ~a")
1255 (list line-number index)
1257 (location (package-file package)
1261 (define (report-trailing-white-space package line line-number)
1262 "Warn about trailing white space in LINE."
1263 (and (not (or (string=? line (string-trim-right line))
1264 (string=? line (string #\page))))
1265 (make-warning package
1266 (G_ "trailing white space on line ~a")
1269 (location (package-file package)
1273 (define (report-long-line package line line-number)
1274 "Emit a warning if LINE is too long."
1275 ;; Note: We don't warn at 80 characters because sometimes hashes and URLs
1276 ;; make it hard to fit within that limit and we want to avoid making too
1278 (and (> (string-length line) 90)
1279 (make-warning package
1280 (G_ "line ~a is way too long (~a characters)")
1281 (list line-number (string-length line))
1283 (location (package-file package)
1287 (define %hanging-paren-rx
1288 (make-regexp "^[[:blank:]]*[()]+[[:blank:]]*$"))
1290 (define (report-lone-parentheses package line line-number)
1291 "Emit a warning if LINE contains hanging parentheses."
1292 (and (regexp-exec %hanging-paren-rx line)
1293 (make-warning package
1294 (G_ "parentheses feel lonely, \
1295 move to the previous or next line")
1298 (location (package-file package)
1302 (define %formatting-reporters
1303 ;; List of procedures that report formatting issues. These are not separate
1304 ;; checkers because they would need to re-read the file.
1305 (list report-tabulations
1306 report-trailing-white-space
1308 report-lone-parentheses))
1310 (define* (report-formatting-issues package file starting-line
1311 #:key (reporters %formatting-reporters))
1312 "Report white-space issues in FILE starting from STARTING-LINE, and report
1314 (define (sexp-last-line port)
1315 ;; Return the last line of the sexp read from PORT or an estimate thereof.
1316 (define &failure (list 'failure))
1318 (let ((start (ftell port))
1319 (start-line (port-line port))
1320 (sexp (catch 'read-error
1321 (lambda () (read port))
1323 (let ((line (port-line port)))
1324 (seek port start SEEK_SET)
1325 (set-port-line! port start-line)
1326 (if (eq? sexp &failure)
1327 (+ start-line 60) ;conservative estimate
1330 (call-with-input-file file
1332 (let loop ((line-number 1)
1335 (let ((line (read-line port)))
1336 (if (or (eof-object? line)
1337 (and last-line (> line-number last-line)))
1339 (if (and (= line-number starting-line)
1341 (loop (+ 1 line-number)
1342 (+ 1 (sexp-last-line port))
1344 (loop (+ 1 line-number)
1348 (if (< line-number starting-line)
1350 (filter-map (lambda (report)
1351 (report package line line-number))
1352 reporters)))))))))))
1354 (define (check-formatting package)
1355 "Check the formatting of the source code of PACKAGE."
1356 (let ((location (package-location package)))
1358 ;; Report issues starting from the line before the 'package'
1359 ;; form, which usually contains the 'define' form.
1360 (let ((line (- (location-line location) 1)))
1361 (match (search-path %load-path (location-file location))
1363 (report-formatting-issues package file line))
1365 ;; It could be that LOCATION lists a "true" relative file
1366 ;; name--i.e., not relative to an element of %LOAD-PATH.
1367 (let ((file (location-file location)))
1368 (if (file-exists? file)
1369 (report-formatting-issues package file line)
1370 (list (make-warning package
1371 (G_ "source file not found"))))))))
1376 ;;; List of checkers.
1379 (define %local-checkers
1383 (description "Validate package descriptions")
1384 (check check-description-style))
1386 (name 'inputs-should-be-native)
1387 (description "Identify inputs that should be native inputs")
1388 (check check-inputs-should-be-native))
1390 (name 'inputs-should-not-be-input)
1391 (description "Identify inputs that shouldn't be inputs at all")
1392 (check check-inputs-should-not-be-an-input-at-all))
1395 ;; TRANSLATORS: <license> is the name of a data type and must not be
1397 (description "Make sure the 'license' field is a <license> \
1399 (check check-license))
1402 (description "Suggest 'mirror://' URLs")
1403 (check check-mirror-url))
1405 (name 'source-file-name)
1406 (description "Validate file names of sources")
1407 (check check-source-file-name))
1409 (name 'source-unstable-tarball)
1410 (description "Check for autogenerated tarballs")
1411 (check check-source-unstable-tarball))
1414 (description "Report failure to compile a package to a derivation")
1415 (check check-derivation)
1416 (requires-store? #t))
1418 (name 'profile-collisions)
1419 (description "Report collisions that would occur due to propagated inputs")
1420 (check check-profile-collisions)
1421 (requires-store? #t))
1423 (name 'patch-file-names)
1424 (description "Validate file names and availability of patches")
1425 (check check-patch-file-names))
1428 (description "Look for formatting issues in the source")
1429 (check check-formatting))))
1431 (define %network-dependent-checkers
1435 (description "Validate package synopses")
1436 (check check-synopsis-style))
1438 (name 'gnu-description)
1439 (description "Validate synopsis & description of GNU packages")
1440 (check check-gnu-synopsis+description))
1443 (description "Validate home-page URLs")
1444 (check check-home-page))
1447 (description "Validate source URLs")
1448 (check check-source))
1451 (description "Suggest GitHub URLs")
1452 (check check-github-url))
1455 (description "Check the Common Vulnerabilities and Exposures\
1457 (check check-vulnerabilities))
1460 (description "Check the package for new upstream releases")
1461 (check check-for-updates))
1464 (description "Ensure source code archival on Software Heritage")
1465 (check check-archival))))
1467 (define %all-checkers
1468 (append %local-checkers
1469 %network-dependent-checkers))