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, 2021 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>
13 ;;; Copyright © 2020 Timothy Sample <samplet@ngyro.com>
15 ;;; This file is part of GNU Guix.
17 ;;; GNU Guix is free software; you can redistribute it and/or modify it
18 ;;; under the terms of the GNU General Public License as published by
19 ;;; the Free Software Foundation; either version 3 of the License, or (at
20 ;;; your option) any later version.
22 ;;; GNU Guix is distributed in the hope that it will be useful, but
23 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;;; GNU General Public License for more details.
27 ;;; You should have received a copy of the GNU General Public License
28 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
30 (define-module (guix lint)
31 #:use-module (guix store)
32 #:use-module (guix base32)
33 #:use-module (guix diagnostics)
34 #:use-module (guix download)
35 #:use-module (guix ftp-client)
36 #:use-module (guix http-client)
37 #:use-module (guix packages)
38 #:use-module (guix i18n)
39 #:use-module ((guix gexp)
40 #:select (local-file? local-file-absolute-file-name))
41 #:use-module (guix licenses)
42 #:use-module (guix records)
43 #:use-module (guix grafts)
44 #:use-module (guix upstream)
45 #:use-module (guix utils)
46 #:use-module (guix memoization)
47 #:use-module (guix profiles)
48 #:use-module (guix monads)
49 #:use-module (guix scripts)
50 #:use-module ((guix ui) #:select (texi->plain-text fill-paragraph))
51 #:use-module (guix gnu-maintenance)
52 #:use-module (guix cve)
53 #:use-module ((guix swh) #:hide (origin?))
54 #:autoload (guix git-download) (git-reference?
55 git-reference-url git-reference-commit)
56 #:use-module (guix import stackage)
57 #:use-module (ice-9 match)
58 #:use-module (ice-9 regex)
59 #:use-module (ice-9 format)
60 #:use-module (web client)
61 #:use-module (web uri)
62 #:use-module ((guix build download)
63 #:select (maybe-expand-mirrors
64 (open-connection-for-uri
65 . guix:open-connection-for-uri)))
66 #:use-module (web request)
67 #:use-module (web response)
68 #:use-module (srfi srfi-1)
69 #:use-module (srfi srfi-6) ;Unicode string ports
70 #:use-module (srfi srfi-9)
71 #:use-module (srfi srfi-11)
72 #:use-module (srfi srfi-26)
73 #:use-module (srfi srfi-34)
74 #:use-module (srfi srfi-35)
75 #:use-module (ice-9 rdelim)
76 #:export (check-description-style
77 check-inputs-should-be-native
78 check-inputs-should-not-be-an-input-at-all
79 check-patch-file-names
85 check-source-file-name
86 check-source-unstable-tarball
94 check-profile-collisions
95 check-haskell-stackage
101 lint-warning-message-text
102 lint-warning-message-data
103 lint-warning-location
106 %network-dependent-checkers
112 lint-checker-description
114 lint-checker-requires-store?))
121 (define-record-type* <lint-warning>
122 lint-warning make-lint-warning
124 (package lint-warning-package)
125 (message-text lint-warning-message-text)
126 (message-data lint-warning-message-data
128 (location lint-warning-location
131 (define (lint-warning-message warning)
133 (G_ (lint-warning-message-text warning))
134 (lint-warning-message-data warning)))
136 (define (package-file package)
138 (package-location package)))
140 (define* (%make-warning package message-text
141 #:optional (message-data '())
142 #:key field location)
148 (and field (package-field-location package field))
149 (package-location package))))
151 (define-syntax make-warning
153 ((_ package (G_ message) rest ...)
154 (%make-warning package message rest ...))))
161 (define-record-type* <lint-checker>
162 lint-checker make-lint-checker
164 ;; TODO: add a 'certainty' field that shows how confident we are in the
165 ;; checker. Then allow users to only run checkers that have a certain
166 ;; 'certainty' level.
167 (name lint-checker-name)
168 (description lint-checker-description)
169 (check lint-checker-check)
170 (requires-store? lint-checker-requires-store?
173 (define (properly-starts-sentence? s)
174 (string-match "^[(\"'`[:upper:][:digit:]]" s))
176 (define (starts-with-abbreviation? s)
177 "Return #t if S starts with what looks like an abbreviation or acronym."
178 (string-match "^[A-Z][A-Z0-9]+\\>" s))
180 (define %quoted-identifier-rx
181 ;; A quoted identifier, like 'this'.
182 (make-regexp "['`][[:graph:]]+'"))
184 (define (check-description-style package)
185 ;; Emit a warning if stylistic issues are found in the description of PACKAGE.
186 (define (check-not-empty description)
187 (if (string-null? description)
189 (make-warning package
190 (G_ "description should not be empty")
191 #:field 'description))
194 (define (check-texinfo-markup description)
195 "Check that DESCRIPTION can be parsed as a Texinfo fragment. If the
196 markup is valid return a plain-text version of DESCRIPTION, otherwise #f."
198 (lambda () (texi->plain-text description))
199 (lambda (keys . args)
200 (make-warning package
201 (G_ "Texinfo markup in description is invalid")
202 #:field 'description))))
204 (define (check-trademarks description)
205 "Check that DESCRIPTION does not contain '™' or '®' characters. See
206 http://www.gnu.org/prep/standards/html_node/Trademarks.html."
207 (match (string-index description (char-set #\™ #\®))
208 ((and (? number?) index)
210 (make-warning package
211 (G_ "description should not contain ~
212 trademark sign '~a' at ~d")
213 (list (string-ref description index) index)
214 #:field 'description)))
217 (define (check-quotes description)
218 "Check whether DESCRIPTION contains single quotes and suggest @code."
219 (if (regexp-exec %quoted-identifier-rx description)
221 (make-warning package
222 ;; TRANSLATORS: '@code' is Texinfo markup and must be kept
224 (G_ "use @code or similar ornament instead of quotes")
225 #:field 'description))
228 (define (check-proper-start description)
229 (if (or (string-null? description)
230 (properly-starts-sentence? description)
231 (string-prefix-ci? (package-name package) description))
236 (G_ "description should start with an upper-case letter or digit")
237 #:field 'description))))
239 (define (check-end-of-sentence-space description)
240 "Check that an end-of-sentence period is followed by two spaces."
242 (reverse (fold-matches
243 "\\. [A-Z]" description '()
245 ;; Filter out matches of common abbreviations.
246 (if (find (lambda (s)
247 (string-suffix-ci? s (match:prefix m)))
248 '("i.e" "e.g" "a.k.a" "resp"))
249 r (cons (match:start m) r)))))))
250 (if (null? infractions)
253 (make-warning package
254 (G_ "sentences in description should be followed ~
255 by two spaces; possible infraction~p at ~{~a~^, ~}")
256 (list (length infractions)
258 #:field 'description)))))
260 (let ((description (package-description package)))
261 (if (string? description)
263 (check-not-empty description)
264 (check-quotes description)
265 (check-trademarks description)
266 ;; Use raw description for this because Texinfo rendering
267 ;; automatically fixes end of sentence space.
268 (check-end-of-sentence-space description)
269 (match (check-texinfo-markup description)
270 ((and warning (? lint-warning?)) (list warning))
272 (check-proper-start plain-description))))
274 (make-warning package
275 (G_ "invalid description: ~s")
277 #:field 'description)))))
279 (define (package-input-intersection inputs-to-check input-names)
280 "Return the intersection between INPUTS-TO-CHECK, the list of input tuples
281 of a package, and INPUT-NAMES, a list of package specifications such as
283 (match inputs-to-check
284 (((labels packages . outputs) ...)
285 (filter-map (lambda (package output)
286 (and (package? package)
287 (let ((input (string-append
288 (package-name package)
289 (if (> (length output) 0)
290 (string-append ":" (car output))
292 (and (member input input-names)
296 (define (check-inputs-should-be-native package)
297 ;; Emit a warning if some inputs of PACKAGE are likely to belong to its
299 (let ((inputs (append (package-inputs package)
300 (package-propagated-inputs package)))
310 "extra-cmake-modules"
314 "gobject-introspection"
325 "python-coverage" "python2-coverage"
326 "python-cython" "python2-cython"
327 "python-docutils" "python2-docutils"
328 "python-mock" "python2-mock"
329 "python-nose" "python2-nose"
330 "python-pbr" "python2-pbr"
331 "python-pytest" "python2-pytest"
332 "python-pytest-cov" "python2-pytest-cov"
333 "python-setuptools-scm" "python2-setuptools-scm"
334 "python-sphinx" "python2-sphinx"
340 "xorg-server-for-tests"
345 (G_ "'~a' should probably be a native input")
348 (package-input-intersection inputs input-names))))
350 (define (check-inputs-should-not-be-an-input-at-all package)
351 ;; Emit a warning if some inputs of PACKAGE are likely to should not be
353 (let ((input-names '("python-setuptools"
360 (G_ "'~a' should probably not be an input at all")
363 (package-input-intersection (package-direct-inputs package)
366 (define (package-name-regexp package)
367 "Return a regexp that matches PACKAGE's name as a word at the beginning of a
369 (make-regexp (string-append "^" (regexp-quote (package-name package))
373 (define (check-synopsis-style package)
374 ;; Emit a warning if stylistic issues are found in the synopsis of PACKAGE.
375 (define (check-final-period synopsis)
376 ;; Synopsis should not end with a period, except for some special cases.
377 (if (and (string-suffix? "." synopsis)
378 (not (string-suffix? "etc." synopsis)))
380 (make-warning package
381 (G_ "no period allowed at the end of the synopsis")
385 (define check-start-article
386 ;; Skip this check for GNU packages, as suggested by Karl Berry's reply to
387 ;; <http://lists.gnu.org/archive/html/bug-womb/2014-11/msg00000.html>.
388 (if (false-if-exception (gnu-package? package))
391 (if (or (string-prefix-ci? "A " synopsis)
392 (string-prefix-ci? "An " synopsis))
394 (make-warning package
395 (G_ "no article allowed at the beginning of \
400 (define (check-synopsis-length synopsis)
401 (if (>= (string-length synopsis) 80)
403 (make-warning package
404 (G_ "synopsis should be less than 80 characters long")
408 (define (check-proper-start synopsis)
409 (if (properly-starts-sentence? synopsis)
412 (make-warning package
413 (G_ "synopsis should start with an upper-case letter or digit")
414 #:field 'synopsis))))
416 (define (check-start-with-package-name synopsis)
417 (if (and (regexp-exec (package-name-regexp package) synopsis)
418 (not (starts-with-abbreviation? synopsis)))
420 (make-warning package
421 (G_ "synopsis should not start with the package name")
425 (define (check-texinfo-markup synopsis)
426 "Check that SYNOPSIS can be parsed as a Texinfo fragment. If the
427 markup is valid return a plain-text version of SYNOPSIS, otherwise #f."
430 (texi->plain-text synopsis)
432 (lambda (keys . args)
434 (make-warning package
435 (G_ "Texinfo markup in synopsis is invalid")
436 #:field 'synopsis)))))
439 (list check-proper-start
442 check-start-with-package-name
443 check-synopsis-length
444 check-texinfo-markup))
446 (match (package-synopsis package)
449 (make-warning package
450 (G_ "synopsis should not be empty")
452 ((? string? synopsis)
459 (make-warning package
460 (G_ "invalid synopsis: ~s")
462 #:field 'synopsis)))))
464 (define* (probe-uri uri #:key timeout)
465 "Probe URI, a URI object, and return two values: a symbol denoting the
466 probing status, such as 'http-response' when we managed to get an HTTP
467 response from URI, and additional details, such as the actual HTTP response.
469 TIMEOUT is the maximum number of seconds (possibly an inexact number) to wait
470 for connections to complete; when TIMEOUT is #f, wait as long as needed."
472 '((User-Agent . "GNU Guile")
477 (match (uri-scheme uri)
481 (let ((port (guix:open-connection-for-uri
482 uri #:timeout timeout))
483 (request (build-request uri #:headers headers)))
488 (write-request request port)
490 (read-response port))
494 (case (response-code response)
495 ((302 ; found (redirection)
497 307 ; temporary redirection
498 308) ; permanent redirection
499 (let ((location (response-location response)))
500 (if (or (not location) (member location visited))
501 (values 'http-response response)
502 (loop location (cons location visited))))) ;follow the redirect
503 ((301) ; moved permanently
504 (let ((location (response-location response)))
505 ;; Return RESPONSE, unless the final response as we follow
506 ;; redirects is not 200.
508 (let-values (((status response2)
509 (loop location (cons location visited))))
512 (values 'http-response
513 (if (= 200 (response-code response2))
517 (values status response2))))
518 (values 'http-response response)))) ;invalid redirect
520 (values 'http-response response)))))
523 ((bad-header bad-header-component)
524 ;; This can happen if the server returns an invalid HTTP header,
525 ;; as is the case with the 'Date' header at sqlite.org.
526 (values 'invalid-http-response #f))
527 ((getaddrinfo-error system-error
528 gnutls-error tls-certificate-error)
531 (apply throw key args))))))
535 (let ((conn (ftp-open (uri-host uri) #:timeout timeout)))
540 (ftp-chdir conn (dirname (uri-path uri)))
541 (ftp-size conn (basename (uri-path uri))))
544 (values 'ftp-response '(ok))))
548 (values 'ftp-response `(error ,@args)))
549 ((getaddrinfo-error system-error gnutls-error)
552 (apply throw key args))))))
554 (values 'unknown-protocol #f)))))
556 (define (tls-certificate-error-string args)
557 "Return a string explaining the 'tls-certificate-error' arguments ARGS."
558 (call-with-output-string
560 (print-exception port #f
561 'tls-certificate-error args))))
563 (define (validate-uri uri package field)
564 "Return #t if the given URI can be reached, otherwise return a warning for
565 PACKAGE mentioning the FIELD."
566 (let-values (((status argument)
567 (probe-uri uri #:timeout 3))) ;wait at most 3 seconds
570 (cond ((= 200 (response-code argument))
571 (match (response-content-length argument)
573 ;; As of July 2016, SourceForge returns 200 (instead of 404)
574 ;; with a small HTML page upon failure. Attempt to detect
575 ;; such malicious behavior.
577 (make-warning package
578 (G_ "URI ~a returned \
579 suspiciously small file (~a bytes)")
580 (list (uri->string uri)
584 ((= 301 (response-code argument))
585 (if (response-location argument)
586 (make-warning package
587 (G_ "permanent redirect from ~a to ~a")
588 (list (uri->string uri)
590 (response-location argument)))
592 (make-warning package
593 (G_ "invalid permanent redirect \
595 (list (uri->string uri))
598 (make-warning package
599 (G_ "URI ~a not reachable: ~a (~s)")
600 (list (uri->string uri)
601 (response-code argument)
602 (response-reason-phrase argument))
607 (('error port command code message)
608 (make-warning package
609 (G_ "URI ~a not reachable: ~a (~s)")
610 (list (uri->string uri)
611 code (string-trim-both message))
614 (make-warning package
615 (G_ "URI ~a domain not found: ~a")
616 (list (uri->string uri)
617 (gai-strerror (car argument)))
620 (make-warning package
621 (G_ "URI ~a unreachable: ~a")
622 (list (uri->string uri)
625 (cons status argument))))
627 ((tls-certificate-error)
628 (make-warning package
629 (G_ "TLS certificate error: ~a")
630 (list (tls-certificate-error-string argument))
632 ((invalid-http-response gnutls-error)
633 ;; Probably a misbehaving server; ignore.
635 ((unknown-protocol) ;nothing we can do
638 (error "internal linter error" status)))))
640 (define (check-home-page package)
641 "Emit a warning if PACKAGE has an invalid 'home-page' field, or if that
642 'home-page' is not reachable."
643 (let ((uri (and=> (package-home-page package) string->uri)))
646 (match (validate-uri uri package 'home-page)
647 ((and (? lint-warning? warning) warning)
650 ((not (package-home-page package))
651 (if (or (string-contains (package-name package) "bootstrap")
652 (string=? (package-name package) "ld-wrapper"))
655 (make-warning package
656 (G_ "invalid value for home page")
657 #:field 'home-page))))
660 (make-warning package
661 (G_ "invalid home page URL: ~s")
662 (list (package-home-page package))
663 #:field 'home-page))))))
665 (define %distro-directory
667 (dirname (search-path %load-path "gnu.scm"))))
669 (define (check-patch-file-names package)
670 "Emit a warning if the patches requires by PACKAGE are badly named or if the
671 patch could not be found."
672 (guard (c ((formatted-message? c) ;raised by 'search-patch'
673 (list (%make-warning package
674 (formatted-message-string c)
675 (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 (check-patch-headers package)
722 "Check that PACKAGE's patches start with a comment. Return a list of
725 (string-every char-set:blank str))
727 (define (patch-header-warnings patch)
728 (call-with-input-file patch
730 ;; Read from PORT until a non-blank line is found or EOF is reached.
732 (let ((line (read-line port)))
733 (cond ((eof-object? line)
734 (list (make-warning package
735 (G_ "~a: empty patch")
736 (list (basename patch))
740 ((or (string-prefix? "--- " line)
741 (string-prefix? "+++ " line))
742 (list (make-warning package
743 (G_ "~a: patch lacks comment and \
745 (list (basename patch))
750 (guard (c ((formatted-message? c) ;raised by 'search-patch'
751 (list (%make-warning package
752 (formatted-message-string c)
753 (formatted-message-arguments c)
755 (let ((patches (if (origin? (package-source package))
756 (origin-patches (package-source package))
758 (append-map (lambda (patch)
759 ;; Dismiss PATCH if it's an origin or similar.
760 (cond ((string? patch)
761 (patch-header-warnings patch))
763 (patch-header-warnings
764 (local-file-absolute-file-name patch)))
769 (define (escape-quotes str)
770 "Replace any quote character in STR by an escaped quote character."
772 (string-fold-right (lambda (chr result)
774 (#\" (cons* #\\ #\"result))
775 (_ (cons chr result))))
779 (define official-gnu-packages*
781 "A memoizing version of 'official-gnu-packages' that returns the empty
782 list when something goes wrong, such as a networking issue."
783 (let ((gnus (false-if-exception (official-gnu-packages))))
786 (define (check-gnu-synopsis+description package)
787 "Make sure that, if PACKAGE is a GNU package, it uses the synopsis and
788 descriptions maintained upstream."
789 (match (find (lambda (descriptor)
790 (string=? (gnu-package-name descriptor)
791 (package-name package)))
792 (official-gnu-packages*))
793 (#f ;not a GNU package, so nothing to do
795 (descriptor ;a genuine GNU package
797 (let ((upstream (gnu-package-doc-summary descriptor))
798 (downstream (package-synopsis package)))
800 (or (not (string? downstream))
801 (not (string=? upstream downstream))))
803 (make-warning package
804 (G_ "proposed synopsis: ~s~%")
809 (let ((upstream (gnu-package-doc-description descriptor))
810 (downstream (package-description package)))
812 (or (not (string? downstream))
813 (not (string=? (fill-paragraph upstream 100)
814 (fill-paragraph downstream 100)))))
818 (G_ "proposed description:~% \"~a\"~%")
819 (list (fill-paragraph (escape-quotes upstream) 77 7))
820 #:field 'description))
823 (define (origin-uris origin)
824 "Return the list of URIs (strings) for ORIGIN."
825 (match (origin-uri origin)
831 (define (check-source package)
832 "Emit a warning if PACKAGE has an invalid 'source' field, or if that
833 'source' is not reachable."
834 (define (warnings-for-uris uris)
835 (let loop ((uris uris)
841 (match (validate-uri uri package 'source)
843 ;; We found a working URL, so stop right away.
846 ;; Unsupported URL or other error, skip.
847 (loop rest warnings))
848 ((? lint-warning? warning)
849 (loop rest (cons warning warnings))))))))
851 (let ((origin (package-source package)))
854 ((eq? (origin-method origin) url-fetch)
855 (let* ((uris (append-map (cut maybe-expand-mirrors <> %mirrors)
856 (map string->uri (origin-uris origin))))
857 (warnings (warnings-for-uris uris)))
859 ;; Just make sure that at least one of the URIs is valid.
860 (if (= (length uris) (length warnings))
861 ;; When everything fails, report all of WARNINGS, otherwise don't
864 ;; XXX: Ideally we'd still allow warnings to be raised if *some*
865 ;; URIs are unreachable, but distinguish that from the error case
866 ;; where *all* the URIs are unreachable.
868 (make-warning package
869 (G_ "all the source URIs are unreachable:")
873 ((git-reference? (origin-uri origin))
875 (list (string->uri (git-reference-url (origin-uri origin))))))
880 (define (check-source-file-name package)
881 "Emit a warning if PACKAGE's origin has no meaningful file name."
882 (define (origin-file-name-valid? origin)
883 ;; Return #f if the source file name contains only a version or is #f;
884 ;; indicates that the origin needs a 'file-name' field.
885 (let ((file-name (origin-actual-file-name origin))
886 (version (package-version package)))
888 ;; Common in many projects is for the filename to start
889 ;; with a "v" followed by the version,
890 ;; e.g. "v3.2.0.tar.gz".
891 (not (string-match (string-append "^v?" version) file-name)))))
893 (let ((origin (package-source package)))
894 (if (or (not (origin? origin)) (origin-file-name-valid? origin))
897 (make-warning package
898 (G_ "the source file name should contain the package name")
901 (define (check-source-unstable-tarball package)
902 "Emit a warning if PACKAGE's source is an autogenerated tarball."
903 (define (check-source-uri uri)
904 (if (and (string=? (uri-host (string->uri uri)) "github.com")
905 (match (split-and-decode-uri-path
906 (uri-path (string->uri uri)))
907 ((_ _ "archive" _ ...) #t)
909 (make-warning package
910 (G_ "the source URI should not be an autogenerated tarball")
914 (let ((origin (package-source package)))
915 (if (and (origin? origin)
916 (eqv? (origin-method origin) url-fetch))
917 (filter-map check-source-uri
918 (origin-uris origin))
921 (define (check-mirror-url package)
922 "Check whether PACKAGE uses source URLs that should be 'mirror://'."
923 (define (check-mirror-uri uri) ;XXX: could be optimized
924 (let loop ((mirrors %mirrors))
928 (((mirror-id mirror-urls ...) rest ...)
929 (match (find (cut string-prefix? <> uri) mirror-urls)
933 (make-warning package
937 (string-drop uri (string-length prefix)))
938 #:field 'source)))))))
940 (let ((origin (package-source package)))
941 (if (and (origin? origin)
942 (eqv? (origin-method origin) url-fetch))
943 (let ((uris (origin-uris origin)))
944 (filter-map check-mirror-uri uris))
947 (define* (check-github-url package #:key (timeout 3))
948 "Check whether PACKAGE uses source URLs that redirect to GitHub."
949 (define (follow-redirect url)
950 (let* ((uri (string->uri url))
951 (port (guix:open-connection-for-uri uri #:timeout timeout))
952 (response (http-head uri #:port port)))
954 (case (response-code response)
956 (uri->string (assoc-ref (response-headers response) 'location)))
959 (define (follow-redirects-to-github uri)
961 ((string-prefix? "https://github.com/" uri) uri)
962 ((string-prefix? "http" uri)
963 (and=> (follow-redirect uri) follow-redirects-to-github))
964 ;; Do not attempt to follow redirects on URIs other than http and https
965 ;; (such as mirror, file)
968 (let ((origin (package-source package)))
969 (if (and (origin? origin)
970 (eqv? (origin-method origin) url-fetch))
973 (and=> (follow-redirects-to-github uri)
975 (if (string=? github-uri uri)
979 (G_ "URL should be '~a'")
982 (origin-uris origin))
987 ;; Guile 3.0.0 does not export this predicate.
988 (define exception-with-kind-and-args?
989 (exception-predicate &exception-with-kind-and-args)))
991 (define exception-with-kind-and-args?
994 (define* (check-derivation package #:key store)
995 "Emit a warning if we fail to compile PACKAGE to a derivation."
996 (define (try store system)
997 (catch #t ;TODO: Remove 'catch' when Guile 2.x is no longer supported.
999 (guard (c ((store-protocol-error? c)
1000 (make-warning package
1001 (G_ "failed to create ~a derivation: ~a")
1003 (store-protocol-error-message c))))
1004 ((exception-with-kind-and-args? c)
1005 (make-warning package
1006 (G_ "failed to create ~a derivation: ~s")
1008 (cons (exception-kind c)
1009 (exception-args c)))))
1010 ((message-condition? c)
1011 (make-warning package
1012 (G_ "failed to create ~a derivation: ~a")
1014 (condition-message c))))
1015 ((formatted-message? c)
1016 (let ((str (apply format #f
1017 (formatted-message-string c)
1018 (formatted-message-arguments c))))
1019 (make-warning package
1020 (G_ "failed to create ~a derivation: ~a")
1021 (list system str)))))
1022 (parameterize ((%graft? #f))
1023 (package-derivation store package system #:graft? #f)
1025 ;; If there's a replacement, make sure we can compute its
1027 (match (package-replacement package)
1030 (package-derivation store replacement system
1033 (make-warning package
1034 (G_ "failed to create ~a derivation: ~s")
1035 (list system args)))))
1037 (define (check-with-store store)
1038 (filter lint-warning?
1039 (map (cut try store <>) (package-supported-systems package))))
1041 ;; For backwards compatability, don't rely on store being set
1042 (or (and=> store check-with-store)
1044 (check-with-store store))))
1046 (define* (check-profile-collisions package #:key store)
1047 "Check for collisions that would occur when installing PACKAGE as a result
1048 of the propagated inputs it pulls in."
1049 (define (do-check store)
1050 (guard (c ((profile-collision-error? c)
1051 (let ((first (profile-collision-error-entry c))
1052 (second (profile-collision-error-conflict c)))
1054 (if (string=? (manifest-entry-version first)
1055 (manifest-entry-version second))
1058 (string-append (manifest-entry-name entry) "@"
1059 (manifest-entry-version entry)))))
1061 (list (make-warning package
1062 (G_ "propagated inputs ~a and ~a collide")
1063 (list (format first)
1064 (format second)))))))
1065 ;; Disable grafts to avoid building PACKAGE and its dependencies.
1066 (parameterize ((%graft? #f))
1067 (run-with-store store
1068 (mbegin %store-monad
1069 (check-for-collisions (packages->manifest (list package))
1078 (define (check-license package)
1079 "Warn about type errors of the 'license' field of PACKAGE."
1080 (match (package-license package)
1086 (make-warning package (G_ "invalid license field")
1087 #:field 'license)))))
1089 (define (call-with-networking-fail-safe message error-value proc)
1090 "Call PROC catching any network-related errors. Upon a networking error,
1091 display a message including MESSAGE and return ERROR-VALUE."
1092 (guard (c ((http-get-error? c)
1093 (warning (G_ "~a: HTTP GET error for ~a: ~a (~s)~%")
1095 (uri->string (http-get-error-uri c))
1096 (http-get-error-code c)
1097 (http-get-error-reason c))
1102 (('getaddrinfo-error errcode)
1103 (warning (G_ "~a: host lookup failure: ~a~%")
1105 (gai-strerror errcode))
1107 (('tls-certificate-error args ...)
1108 (warning (G_ "~a: TLS certificate error: ~a")
1110 (tls-certificate-error-string args))
1112 ((and ('system-error _ ...) args)
1113 (let ((errno (system-error-errno args)))
1114 (if (member errno (list ECONNRESET ECONNABORTED ECONNREFUSED))
1115 (let ((details (call-with-output-string
1117 (print-exception port #f (car args)
1119 (warning (G_ "~a: ~a~%") message details)
1121 (apply throw args))))
1123 (apply throw args))))))
1125 (define-syntax-rule (with-networking-fail-safe message error-value exp ...)
1126 (call-with-networking-fail-safe message error-value
1127 (lambda () exp ...)))
1129 (define (current-vulnerabilities*)
1130 "Like 'current-vulnerabilities', but return the empty list upon networking
1131 or HTTP errors. This allows network-less operation and makes problems with
1132 the NIST server non-fatal."
1133 (with-networking-fail-safe (G_ "while retrieving CVE vulnerabilities")
1135 (current-vulnerabilities #:timeout 4)))
1137 (define package-vulnerabilities
1138 (let ((lookup (delay (vulnerabilities->lookup-proc
1139 (current-vulnerabilities*)))))
1141 "Return a list of vulnerabilities affecting PACKAGE."
1142 ;; First we retrieve the Common Platform Enumeration (CPE) name and
1143 ;; version for PACKAGE, then we can pass them to LOOKUP.
1144 (let ((name (or (assoc-ref (package-properties package)
1146 (package-name package)))
1147 (version (or (assoc-ref (package-properties package)
1149 (package-version package))))
1150 ((force lookup) name version)))))
1152 (define* (check-vulnerabilities package
1153 #:optional (package-vulnerabilities
1154 package-vulnerabilities))
1155 "Check for known vulnerabilities for PACKAGE. Obtain the list of
1156 vulnerability records for PACKAGE by calling PACKAGE-VULNERABILITIES."
1157 (let ((package (or (package-replacement package) package)))
1158 (match (package-vulnerabilities package)
1161 ((vulnerabilities ...)
1162 (let* ((patched (package-patched-vulnerabilities package))
1163 (known-safe (or (assq-ref (package-properties package)
1166 (unpatched (remove (lambda (vuln)
1167 (let ((id (vulnerability-id vuln)))
1168 (or (member id patched)
1169 (member id known-safe))))
1171 (if (null? unpatched)
1176 (G_ "probably vulnerable to ~a")
1177 (list (string-join (map vulnerability-id unpatched)
1180 (define (check-for-updates package)
1181 "Check if there is an update available for PACKAGE."
1182 (match (with-networking-fail-safe
1183 (format #f (G_ "while retrieving upstream info for '~a'")
1184 (package-name package))
1186 (package-latest-release* package))
1187 ((? upstream-source? source)
1188 (if (version>? (upstream-source-version source)
1189 (package-version package))
1191 (make-warning package
1192 (G_ "can be upgraded to ~a")
1193 (list (upstream-source-version source))
1196 (#f '()))) ; cannot find newer upstream release
1199 (define (check-archival package)
1200 "Check whether PACKAGE's source code is archived on Software Heritage. If
1201 it's not, and if its source code is a VCS snapshot, then send a \"save\"
1202 request to Software Heritage.
1204 Software Heritage imposes limits on the request rate per client IP address.
1205 This checker prints a notice and stops doing anything once that limit has been
1207 (define (response->warning url method response)
1208 (if (request-rate-limit-reached? url method)
1209 (list (make-warning package
1210 (G_ "Software Heritage rate limit reached; \
1213 (list (make-warning package
1214 (G_ "'~a' returned ~a")
1215 (list url (response-code response))
1218 (define skip-key (gensym "skip-archival-check"))
1220 (define (skip-when-limit-reached url method)
1221 (or (not (request-rate-limit-reached? url method))
1222 (throw skip-key #t)))
1224 (parameterize ((%allow-request? skip-when-limit-reached))
1227 (match (and (origin? (package-source package))
1228 (package-source package))
1231 ((= origin-uri (? git-reference? reference))
1233 (git-reference-url reference))
1235 (git-reference-commit reference))
1237 (match (if (commit-id? commit)
1238 (or (lookup-revision commit)
1239 (lookup-origin-revision url commit))
1240 (lookup-origin-revision url commit))
1241 ((? revision? revision)
1244 ;; Revision is missing from the archive, attempt to save it.
1247 (save-origin (git-reference-url reference) "git")
1250 ;; TRANSLATORS: "Software Heritage" is a proper noun
1251 ;; that must remain untranslated. See
1252 ;; <https://www.softwareheritage.org>.
1253 (G_ "scheduled Software Heritage archival")
1255 (lambda (key url method response . _)
1256 (cond ((= 429 (response-code response))
1259 (G_ "archival rate limit exceeded; \
1263 (response->warning url method response))))))))
1265 ;; Since "save" origins are not supported for non-VCS source, all
1266 ;; we can do is tell whether a given tarball is available or not.
1267 (if (origin-hash origin) ;XXX: for ungoogled-chromium
1268 (let ((hash (origin-hash origin)))
1269 (match (lookup-content (content-hash-value hash)
1271 (content-hash-algorithm hash)))
1273 (list (make-warning package
1274 (G_ "source not archived on Software \
1281 (('swh-error url method response)
1282 (response->warning url method response))
1284 (if (eq? key skip-key)
1286 (with-networking-fail-safe
1287 (G_ "while connecting to Software Heritage")
1289 (apply throw key args))))))))
1291 (define (check-haskell-stackage package)
1292 "Check whether PACKAGE is a Haskell package ahead of the current
1293 Stackage LTS version."
1294 (match (with-networking-fail-safe
1295 (format #f (G_ "while retrieving upstream info for '~a'")
1296 (package-name package))
1298 (package-latest-release package (list %stackage-updater)))
1299 ((? upstream-source? source)
1300 (if (version>? (package-version package)
1301 (upstream-source-version source))
1303 (make-warning package
1304 (G_ "ahead of Stackage LTS version ~a")
1305 (list (upstream-source-version source))
1312 ;;; Source code formatting.
1315 (define (report-tabulations package line line-number)
1316 "Warn about tabulations found in LINE."
1317 (match (string-index line #\tab)
1320 (make-warning package
1321 (G_ "tabulation on line ~a, column ~a")
1322 (list line-number index)
1324 (location (package-file package)
1328 (define (report-trailing-white-space package line line-number)
1329 "Warn about trailing white space in LINE."
1330 (and (not (or (string=? line (string-trim-right line))
1331 (string=? line (string #\page))))
1332 (make-warning package
1333 (G_ "trailing white space on line ~a")
1336 (location (package-file package)
1340 (define (report-long-line package line line-number)
1341 "Emit a warning if LINE is too long."
1342 ;; Note: We don't warn at 80 characters because sometimes hashes and URLs
1343 ;; make it hard to fit within that limit and we want to avoid making too
1345 (and (> (string-length line) 90)
1346 (make-warning package
1347 (G_ "line ~a is way too long (~a characters)")
1348 (list line-number (string-length line))
1350 (location (package-file package)
1354 (define %hanging-paren-rx
1355 (make-regexp "^[[:blank:]]*[()]+[[:blank:]]*$"))
1357 (define (report-lone-parentheses package line line-number)
1358 "Emit a warning if LINE contains hanging parentheses."
1359 (and (regexp-exec %hanging-paren-rx line)
1360 (make-warning package
1361 (G_ "parentheses feel lonely, \
1362 move to the previous or next line")
1365 (location (package-file package)
1369 (define %formatting-reporters
1370 ;; List of procedures that report formatting issues. These are not separate
1371 ;; checkers because they would need to re-read the file.
1372 (list report-tabulations
1373 report-trailing-white-space
1375 report-lone-parentheses))
1377 (define* (report-formatting-issues package file starting-line
1378 #:key (reporters %formatting-reporters))
1379 "Report white-space issues in FILE starting from STARTING-LINE, and report
1381 (define (sexp-last-line port)
1382 ;; Return the last line of the sexp read from PORT or an estimate thereof.
1383 (define &failure (list 'failure))
1385 (let ((start (ftell port))
1386 (start-line (port-line port))
1387 (sexp (catch 'read-error
1388 (lambda () (read port))
1390 (let ((line (port-line port)))
1391 (seek port start SEEK_SET)
1392 (set-port-line! port start-line)
1393 (if (eq? sexp &failure)
1394 (+ start-line 60) ;conservative estimate
1397 (call-with-input-file file
1399 (let loop ((line-number 1)
1402 (let ((line (read-line port)))
1403 (if (or (eof-object? line)
1404 (and last-line (> line-number last-line)))
1406 (if (and (= line-number starting-line)
1408 (loop (+ 1 line-number)
1409 (+ 1 (sexp-last-line port))
1411 (loop (+ 1 line-number)
1415 (if (< line-number starting-line)
1417 (filter-map (lambda (report)
1418 (report package line line-number))
1419 reporters)))))))))))
1421 (define (check-formatting package)
1422 "Check the formatting of the source code of PACKAGE."
1423 (let ((location (package-location package)))
1425 ;; Report issues starting from the line before the 'package'
1426 ;; form, which usually contains the 'define' form.
1427 (let ((line (- (location-line location) 1)))
1428 (match (search-path %load-path (location-file location))
1430 (report-formatting-issues package file line))
1432 ;; It could be that LOCATION lists a "true" relative file
1433 ;; name--i.e., not relative to an element of %LOAD-PATH.
1434 (let ((file (location-file location)))
1435 (if (file-exists? file)
1436 (report-formatting-issues package file line)
1437 (list (make-warning package
1438 (G_ "source file not found"))))))))
1443 ;;; List of checkers.
1446 (define %local-checkers
1450 (description "Validate package descriptions")
1451 (check check-description-style))
1453 (name 'inputs-should-be-native)
1454 (description "Identify inputs that should be native inputs")
1455 (check check-inputs-should-be-native))
1457 (name 'inputs-should-not-be-input)
1458 (description "Identify inputs that shouldn't be inputs at all")
1459 (check check-inputs-should-not-be-an-input-at-all))
1462 ;; TRANSLATORS: <license> is the name of a data type and must not be
1464 (description "Make sure the 'license' field is a <license> \
1466 (check check-license))
1469 (description "Suggest 'mirror://' URLs")
1470 (check check-mirror-url))
1472 (name 'source-file-name)
1473 (description "Validate file names of sources")
1474 (check check-source-file-name))
1476 (name 'source-unstable-tarball)
1477 (description "Check for autogenerated tarballs")
1478 (check check-source-unstable-tarball))
1481 (description "Report failure to compile a package to a derivation")
1482 (check check-derivation)
1483 (requires-store? #t))
1485 (name 'profile-collisions)
1486 (description "Report collisions that would occur due to propagated inputs")
1487 (check check-profile-collisions)
1488 (requires-store? #t))
1490 (name 'patch-file-names)
1491 (description "Validate file names and availability of patches")
1492 (check check-patch-file-names))
1494 (name 'patch-headers)
1495 (description "Validate patch headers")
1496 (check check-patch-headers))
1499 (description "Look for formatting issues in the source")
1500 (check check-formatting))))
1502 (define %network-dependent-checkers
1506 (description "Validate package synopses")
1507 (check check-synopsis-style))
1509 (name 'gnu-description)
1510 (description "Validate synopsis & description of GNU packages")
1511 (check check-gnu-synopsis+description))
1514 (description "Validate home-page URLs")
1515 (check check-home-page))
1518 (description "Validate source URLs")
1519 (check check-source))
1522 (description "Suggest GitHub URLs")
1523 (check check-github-url))
1526 (description "Check the Common Vulnerabilities and Exposures\
1528 (check check-vulnerabilities))
1531 (description "Check the package for new upstream releases")
1532 (check check-for-updates))
1535 (description "Ensure source code archival on Software Heritage")
1536 (check check-archival))
1538 (name 'haskell-stackage)
1539 (description "Ensure Haskell packages use Stackage LTS versions")
1540 (check check-haskell-stackage))))
1542 (define %all-checkers
1543 (append %local-checkers
1544 %network-dependent-checkers))