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, 2021 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>
14 ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
15 ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
17 ;;; This file is part of GNU Guix.
19 ;;; GNU Guix is free software; you can redistribute it and/or modify it
20 ;;; under the terms of the GNU General Public License as published by
21 ;;; the Free Software Foundation; either version 3 of the License, or (at
22 ;;; your option) any later version.
24 ;;; GNU Guix is distributed in the hope that it will be useful, but
25 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;;; GNU General Public License for more details.
29 ;;; You should have received a copy of the GNU General Public License
30 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
32 (define-module (guix lint)
33 #:use-module (guix store)
34 #:autoload (guix base16) (bytevector->base16-string)
35 #:use-module (guix base32)
36 #:use-module (guix diagnostics)
37 #:use-module (guix download)
38 #:use-module (guix ftp-client)
39 #:use-module (guix http-client)
40 #:use-module (guix packages)
41 #:use-module (guix i18n)
42 #:use-module ((guix gexp)
43 #:select (local-file? local-file-absolute-file-name))
44 #:use-module (guix licenses)
45 #:use-module (guix records)
46 #:use-module (guix grafts)
47 #:use-module (guix upstream)
48 #:use-module (guix utils)
49 #:use-module (guix memoization)
50 #:use-module (guix profiles)
51 #:use-module (guix monads)
52 #:use-module (guix scripts)
53 #:use-module ((guix ui) #:select (texi->plain-text fill-paragraph))
54 #:use-module (guix gnu-maintenance)
55 #:use-module (guix cve)
56 #:use-module ((guix swh) #:hide (origin?))
57 #:autoload (guix git-download) (git-reference?
58 git-reference-url git-reference-commit)
59 #:use-module (guix import stackage)
60 #:use-module (ice-9 match)
61 #:use-module (ice-9 regex)
62 #:use-module (ice-9 format)
63 #:use-module (web client)
64 #:use-module (web uri)
65 #:use-module ((guix build download)
66 #:select (maybe-expand-mirrors
67 (open-connection-for-uri
68 . guix:open-connection-for-uri)))
69 #:use-module (web request)
70 #:use-module (web response)
71 #:use-module (srfi srfi-1)
72 #:use-module (srfi srfi-6) ;Unicode string ports
73 #:use-module (srfi srfi-9)
74 #:use-module (srfi srfi-11)
75 #:use-module (srfi srfi-26)
76 #:use-module (srfi srfi-34)
77 #:use-module (srfi srfi-35)
78 #:use-module (ice-9 rdelim)
79 #:export (check-description-style
80 check-inputs-should-be-native
81 check-inputs-should-not-be-an-input-at-all
82 check-patch-file-names
89 check-source-file-name
90 check-source-unstable-tarball
98 check-profile-collisions
99 check-haskell-stackage
106 lint-warning-message-text
107 lint-warning-message-data
108 lint-warning-location
111 %network-dependent-checkers
117 lint-checker-description
119 lint-checker-requires-store?))
126 (define-record-type* <lint-warning>
127 lint-warning make-lint-warning
129 (package lint-warning-package)
130 (message-text lint-warning-message-text)
131 (message-data lint-warning-message-data
133 (location lint-warning-location
136 (define (lint-warning-message warning)
138 (G_ (lint-warning-message-text warning))
139 (lint-warning-message-data warning)))
141 (define (package-file package)
143 (package-location package)))
145 (define* (%make-warning package message-text
146 #:optional (message-data '())
147 #:key field location)
153 (and field (package-field-location package field))
154 (package-location package))))
156 (define-syntax make-warning
158 ((_ package (G_ message) rest ...)
159 (%make-warning package message rest ...))))
166 (define-record-type* <lint-checker>
167 lint-checker make-lint-checker
169 ;; TODO: add a 'certainty' field that shows how confident we are in the
170 ;; checker. Then allow users to only run checkers that have a certain
171 ;; 'certainty' level.
172 (name lint-checker-name)
173 (description lint-checker-description)
174 (check lint-checker-check)
175 (requires-store? lint-checker-requires-store?
178 (define (check-name package)
179 "Check whether PACKAGE's name matches our guidelines."
180 (let ((name (package-name package)))
182 ;; Currently checks only whether the name is too short.
183 ((and (<= (string-length name) 1)
184 (not (string=? name "r"))) ; common-sense exception
186 (make-warning package
187 (G_ "name should be longer than a single character")
189 ((string-index name #\_)
191 (make-warning package
192 (G_ "name should use hyphens instead of underscores")
196 (define (check-tests-true package)
197 "Check whether PACKAGE explicitly requests to run tests, which is
198 superfluous when building natively and incorrect when cross-compiling."
199 (define (tests-explicitly-enabled?)
200 (apply (lambda* (#:key tests? #:allow-other-keys)
202 (package-arguments package)))
203 (if (and (tests-explicitly-enabled?)
204 ;; Some packages, e.g. gnutls, set #:tests?
205 ;; differently depending on whether it is being
207 (parameterize ((%current-target-system "aarch64-linux-gnu"))
208 (tests-explicitly-enabled?)))
209 (list (make-warning package
210 ;; TRANSLATORS: #:tests? and #t are Scheme constants
211 ;; and must not be translated.
212 (G_ "#:tests? must not be explicitly set to #t")
216 (define (properly-starts-sentence? s)
217 (string-match "^[(\"'`[:upper:][:digit:]]" s))
219 (define (starts-with-abbreviation? s)
220 "Return #t if S starts with what looks like an abbreviation or acronym."
221 (string-match "^[A-Z][A-Z0-9]+\\>" s))
223 (define %quoted-identifier-rx
224 ;; A quoted identifier, like 'this'.
225 (make-regexp "['`][[:graph:]]+'"))
227 (define (check-description-style package)
228 ;; Emit a warning if stylistic issues are found in the description of PACKAGE.
229 (define (check-not-empty description)
230 (if (string-null? description)
232 (make-warning package
233 (G_ "description should not be empty")
234 #:field 'description))
237 (define (check-texinfo-markup description)
238 "Check that DESCRIPTION can be parsed as a Texinfo fragment. If the
239 markup is valid return a plain-text version of DESCRIPTION, otherwise #f."
241 (lambda () (texi->plain-text description))
242 (lambda (keys . args)
243 (make-warning package
244 (G_ "Texinfo markup in description is invalid")
245 #:field 'description))))
247 (define (check-trademarks description)
248 "Check that DESCRIPTION does not contain '™' or '®' characters. See
249 http://www.gnu.org/prep/standards/html_node/Trademarks.html."
250 (match (string-index description (char-set #\™ #\®))
251 ((and (? number?) index)
253 (make-warning package
254 (G_ "description should not contain ~
255 trademark sign '~a' at ~d")
256 (list (string-ref description index) index)
257 #:field 'description)))
260 (define (check-quotes description)
261 "Check whether DESCRIPTION contains single quotes and suggest @code."
262 (if (regexp-exec %quoted-identifier-rx description)
264 (make-warning package
265 ;; TRANSLATORS: '@code' is Texinfo markup and must be kept
267 (G_ "use @code or similar ornament instead of quotes")
268 #:field 'description))
271 (define (check-proper-start description)
272 (if (or (string-null? description)
273 (properly-starts-sentence? description)
274 (string-prefix-ci? (package-name package) description))
279 (G_ "description should start with an upper-case letter or digit")
280 #:field 'description))))
282 (define (check-end-of-sentence-space description)
283 "Check that an end-of-sentence period is followed by two spaces."
285 (reverse (fold-matches
286 "\\. [A-Z]" description '()
288 ;; Filter out matches of common abbreviations.
289 (if (find (lambda (s)
290 (string-suffix-ci? s (match:prefix m)))
291 '("i.e" "e.g" "a.k.a" "resp"))
292 r (cons (match:start m) r)))))))
293 (if (null? infractions)
296 (make-warning package
297 (G_ "sentences in description should be followed ~
298 by two spaces; possible infraction~p at ~{~a~^, ~}")
299 (list (length infractions)
301 #:field 'description)))))
303 (let ((description (package-description package)))
304 (if (string? description)
306 (check-not-empty description)
307 (check-quotes description)
308 (check-trademarks description)
309 ;; Use raw description for this because Texinfo rendering
310 ;; automatically fixes end of sentence space.
311 (check-end-of-sentence-space description)
312 (match (check-texinfo-markup description)
313 ((and warning (? lint-warning?)) (list warning))
315 (check-proper-start plain-description))))
317 (make-warning package
318 (G_ "invalid description: ~s")
320 #:field 'description)))))
322 (define (package-input-intersection inputs-to-check input-names)
323 "Return the intersection between INPUTS-TO-CHECK, the list of input tuples
324 of a package, and INPUT-NAMES, a list of package specifications such as
326 (match inputs-to-check
327 (((labels packages . outputs) ...)
328 (filter-map (lambda (package output)
329 (and (package? package)
330 (let ((input (string-append
331 (package-name package)
332 (if (> (length output) 0)
333 (string-append ":" (car output))
335 (and (member input input-names)
339 (define (check-inputs-should-be-native package)
340 ;; Emit a warning if some inputs of PACKAGE are likely to belong to its
342 (let ((inputs (append (package-inputs package)
343 (package-propagated-inputs package)))
353 "extra-cmake-modules"
357 "gobject-introspection"
368 "python-coverage" "python2-coverage"
369 "python-cython" "python2-cython"
370 "python-docutils" "python2-docutils"
371 "python-mock" "python2-mock"
372 "python-nose" "python2-nose"
373 "python-pbr" "python2-pbr"
374 "python-pytest" "python2-pytest"
375 "python-pytest-cov" "python2-pytest-cov"
376 "python-setuptools-scm" "python2-setuptools-scm"
377 "python-sphinx" "python2-sphinx"
383 "xorg-server-for-tests"
388 (G_ "'~a' should probably be a native input")
391 (package-input-intersection inputs input-names))))
393 (define (check-inputs-should-not-be-an-input-at-all package)
394 ;; Emit a warning if some inputs of PACKAGE are likely to should not be
396 (let ((input-names '("python-setuptools"
403 (G_ "'~a' should probably not be an input at all")
406 (package-input-intersection (package-direct-inputs package)
409 (define (package-name-regexp package)
410 "Return a regexp that matches PACKAGE's name as a word at the beginning of a
412 (make-regexp (string-append "^" (regexp-quote (package-name package))
416 (define (check-synopsis-style package)
417 ;; Emit a warning if stylistic issues are found in the synopsis of PACKAGE.
418 (define (check-final-period synopsis)
419 ;; Synopsis should not end with a period, except for some special cases.
420 (if (and (string-suffix? "." synopsis)
421 (not (string-suffix? "etc." synopsis)))
423 (make-warning package
424 (G_ "no period allowed at the end of the synopsis")
428 (define check-start-article
429 ;; Skip this check for GNU packages, as suggested by Karl Berry's reply to
430 ;; <http://lists.gnu.org/archive/html/bug-womb/2014-11/msg00000.html>.
431 (if (false-if-exception (gnu-package? package))
434 (if (or (string-prefix-ci? "A " synopsis)
435 (string-prefix-ci? "An " synopsis))
437 (make-warning package
438 (G_ "no article allowed at the beginning of \
443 (define (check-synopsis-length synopsis)
444 (if (>= (string-length synopsis) 80)
446 (make-warning package
447 (G_ "synopsis should be less than 80 characters long")
451 (define (check-proper-start synopsis)
452 (if (properly-starts-sentence? synopsis)
455 (make-warning package
456 (G_ "synopsis should start with an upper-case letter or digit")
457 #:field 'synopsis))))
459 (define (check-start-with-package-name synopsis)
460 (if (and (regexp-exec (package-name-regexp package) synopsis)
461 (not (starts-with-abbreviation? synopsis)))
463 (make-warning package
464 (G_ "synopsis should not start with the package name")
468 (define (check-texinfo-markup synopsis)
469 "Check that SYNOPSIS can be parsed as a Texinfo fragment. If the
470 markup is valid return a plain-text version of SYNOPSIS, otherwise #f."
473 (texi->plain-text synopsis)
475 (lambda (keys . args)
477 (make-warning package
478 (G_ "Texinfo markup in synopsis is invalid")
479 #:field 'synopsis)))))
482 (list check-proper-start
485 check-start-with-package-name
486 check-synopsis-length
487 check-texinfo-markup))
489 (match (package-synopsis package)
492 (make-warning package
493 (G_ "synopsis should not be empty")
495 ((? string? synopsis)
502 (make-warning package
503 (G_ "invalid synopsis: ~s")
505 #:field 'synopsis)))))
507 (define* (probe-uri uri #:key timeout)
508 "Probe URI, a URI object, and return two values: a symbol denoting the
509 probing status, such as 'http-response' when we managed to get an HTTP
510 response from URI, and additional details, such as the actual HTTP response.
512 TIMEOUT is the maximum number of seconds (possibly an inexact number) to wait
513 for connections to complete; when TIMEOUT is #f, wait as long as needed."
515 '((User-Agent . "GNU Guile")
520 (match (uri-scheme uri)
524 (let ((port (guix:open-connection-for-uri
525 uri #:timeout timeout))
526 (request (build-request uri #:headers headers)))
531 (write-request request port)
533 (read-response port))
537 (case (response-code response)
538 ((302 ; found (redirection)
540 307 ; temporary redirection
541 308) ; permanent redirection
542 (let ((location (response-location response)))
543 (if (or (not location) (member location visited))
544 (values 'http-response response)
545 (loop location (cons location visited))))) ;follow the redirect
546 ((301) ; moved permanently
547 (let ((location (response-location response)))
548 ;; Return RESPONSE, unless the final response as we follow
549 ;; redirects is not 200.
551 (let-values (((status response2)
552 (loop location (cons location visited))))
555 (values 'http-response
556 (if (= 200 (response-code response2))
560 (values status response2))))
561 (values 'http-response response)))) ;invalid redirect
563 (values 'http-response response)))))
566 ((bad-header bad-header-component)
567 ;; This can happen if the server returns an invalid HTTP header,
568 ;; as is the case with the 'Date' header at sqlite.org.
569 (values 'invalid-http-response #f))
570 ((getaddrinfo-error system-error
571 gnutls-error tls-certificate-error)
574 (apply throw key args))))))
578 (let ((conn (ftp-open (uri-host uri) #:timeout timeout)))
583 (ftp-chdir conn (dirname (uri-path uri)))
584 (ftp-size conn (basename (uri-path uri))))
587 (values 'ftp-response '(ok))))
591 (values 'ftp-response `(error ,@args)))
592 ((getaddrinfo-error system-error gnutls-error)
595 (apply throw key args))))))
597 (values 'unknown-protocol #f)))))
599 (define (tls-certificate-error-string args)
600 "Return a string explaining the 'tls-certificate-error' arguments ARGS."
601 (call-with-output-string
603 (print-exception port #f
604 'tls-certificate-error args))))
606 (define (validate-uri uri package field)
607 "Return #t if the given URI can be reached, otherwise return a warning for
608 PACKAGE mentioning the FIELD."
609 (let-values (((status argument)
610 (probe-uri uri #:timeout 3))) ;wait at most 3 seconds
613 (cond ((= 200 (response-code argument))
614 (match (response-content-length argument)
616 ;; As of July 2016, SourceForge returns 200 (instead of 404)
617 ;; with a small HTML page upon failure. Attempt to detect
618 ;; such malicious behavior.
620 (make-warning package
621 (G_ "URI ~a returned \
622 suspiciously small file (~a bytes)")
623 (list (uri->string uri)
627 ((= 301 (response-code argument))
628 (if (response-location argument)
629 (make-warning package
630 (G_ "permanent redirect from ~a to ~a")
631 (list (uri->string uri)
633 (response-location argument)))
635 (make-warning package
636 (G_ "invalid permanent redirect \
638 (list (uri->string uri))
641 (make-warning package
642 (G_ "URI ~a not reachable: ~a (~s)")
643 (list (uri->string uri)
644 (response-code argument)
645 (response-reason-phrase argument))
650 (('error port command code message)
651 (make-warning package
652 (G_ "URI ~a not reachable: ~a (~s)")
653 (list (uri->string uri)
654 code (string-trim-both message))
657 (make-warning package
658 (G_ "URI ~a domain not found: ~a")
659 (list (uri->string uri)
660 (gai-strerror (car argument)))
663 (make-warning package
664 (G_ "URI ~a unreachable: ~a")
665 (list (uri->string uri)
668 (cons status argument))))
670 ((tls-certificate-error)
671 (make-warning package
672 (G_ "TLS certificate error: ~a")
673 (list (tls-certificate-error-string argument))
675 ((invalid-http-response gnutls-error)
676 ;; Probably a misbehaving server; ignore.
678 ((unknown-protocol) ;nothing we can do
681 (error "internal linter error" status)))))
683 (define (check-home-page package)
684 "Emit a warning if PACKAGE has an invalid 'home-page' field, or if that
685 'home-page' is not reachable."
686 (let ((uri (and=> (package-home-page package) string->uri)))
689 (match (validate-uri uri package 'home-page)
690 ((and (? lint-warning? warning) warning)
693 ((not (package-home-page package))
694 (if (or (string-contains (package-name package) "bootstrap")
695 (string=? (package-name package) "ld-wrapper"))
698 (make-warning package
699 (G_ "invalid value for home page")
700 #:field 'home-page))))
703 (make-warning package
704 (G_ "invalid home page URL: ~s")
705 (list (package-home-page package))
706 #:field 'home-page))))))
708 (define %distro-directory
710 (dirname (search-path %load-path "gnu.scm"))))
712 (define (check-patch-file-names package)
713 "Emit a warning if the patches requires by PACKAGE are badly named or if the
714 patch could not be found."
715 (guard (c ((formatted-message? c) ;raised by 'search-patch'
716 (list (%make-warning package
717 (formatted-message-string c)
718 (formatted-message-arguments c)
721 (match (package-source package)
722 ((? origin? origin) (origin-patches origin))
725 (define (starts-with-package-name? file-name)
726 (and=> (string-contains file-name (package-name package))
730 (if (every (match-lambda ;patch starts with package name?
732 (starts-with-package-name? (basename patch)))
734 (starts-with-package-name? (origin-actual-file-name patch)))
735 (_ #f)) ;must be some other file-like object
741 (G_ "file names of patches should start with the package name")
742 #:field 'patch-file-names)))
744 ;; Check whether we're reaching tar's maximum file name length.
745 (let ((prefix (string-length (%distro-directory)))
746 (margin (string-length "guix-2.0.0rc3-10000-1234567890/"))
748 (filter-map (match-lambda
750 (if (> (+ margin (if (string-prefix? (%distro-directory)
752 (- (string-length patch) prefix)
753 (string-length patch)))
757 (G_ "~a: file name is too long")
758 (list (basename patch))
759 #:field 'patch-file-names)
764 (define (check-patch-headers package)
765 "Check that PACKAGE's patches start with a comment. Return a list of
768 (string-every char-set:blank str))
770 (define (patch-header-warnings patch)
771 (call-with-input-file patch
773 ;; Read from PORT until a non-blank line is found or EOF is reached.
775 (let ((line (read-line port)))
776 (cond ((eof-object? line)
777 (list (make-warning package
778 (G_ "~a: empty patch")
779 (list (basename patch))
783 ((or (string-prefix? "--- " line)
784 (string-prefix? "+++ " line))
785 (list (make-warning package
786 (G_ "~a: patch lacks comment and \
788 (list (basename patch))
793 (guard (c ((formatted-message? c) ;raised by 'search-patch'
794 (list (%make-warning package
795 (formatted-message-string c)
796 (formatted-message-arguments c)
798 (let ((patches (if (origin? (package-source package))
799 (origin-patches (package-source package))
801 (append-map (lambda (patch)
802 ;; Dismiss PATCH if it's an origin or similar.
803 (cond ((string? patch)
804 (patch-header-warnings patch))
806 (patch-header-warnings
807 (local-file-absolute-file-name patch)))
812 (define (escape-quotes str)
813 "Replace any quote character in STR by an escaped quote character."
815 (string-fold-right (lambda (chr result)
817 (#\" (cons* #\\ #\"result))
818 (_ (cons chr result))))
822 (define official-gnu-packages*
824 "A memoizing version of 'official-gnu-packages' that returns the empty
825 list when something goes wrong, such as a networking issue."
826 (let ((gnus (false-if-exception (official-gnu-packages))))
829 (define (check-gnu-synopsis+description package)
830 "Make sure that, if PACKAGE is a GNU package, it uses the synopsis and
831 descriptions maintained upstream."
832 (match (find (lambda (descriptor)
833 (string=? (gnu-package-name descriptor)
834 (package-name package)))
835 (official-gnu-packages*))
836 (#f ;not a GNU package, so nothing to do
838 (descriptor ;a genuine GNU package
840 (let ((upstream (gnu-package-doc-summary descriptor))
841 (downstream (package-synopsis package)))
843 (or (not (string? downstream))
844 (not (string=? upstream downstream))))
846 (make-warning package
847 (G_ "proposed synopsis: ~s~%")
852 (let ((upstream (gnu-package-doc-description descriptor))
853 (downstream (package-description package)))
855 (or (not (string? downstream))
856 (not (string=? (fill-paragraph upstream 100)
857 (fill-paragraph downstream 100)))))
861 (G_ "proposed description:~% \"~a\"~%")
862 (list (fill-paragraph (escape-quotes upstream) 77 7))
863 #:field 'description))
866 (define (origin-uris origin)
867 "Return the list of URIs (strings) for ORIGIN."
868 (match (origin-uri origin)
874 (define (check-source package)
875 "Emit a warning if PACKAGE has an invalid 'source' field, or if that
876 'source' is not reachable."
877 (define (warnings-for-uris uris)
878 (let loop ((uris uris)
884 (match (validate-uri uri package 'source)
886 ;; We found a working URL, so stop right away.
889 ;; Unsupported URL or other error, skip.
890 (loop rest warnings))
891 ((? lint-warning? warning)
892 (loop rest (cons warning warnings))))))))
894 (let ((origin (package-source package)))
897 ((eq? (origin-method origin) url-fetch)
898 (let* ((uris (append-map (cut maybe-expand-mirrors <> %mirrors)
899 (map string->uri (origin-uris origin))))
900 (warnings (warnings-for-uris uris)))
902 ;; Just make sure that at least one of the URIs is valid.
903 (if (= (length uris) (length warnings))
904 ;; When everything fails, report all of WARNINGS, otherwise don't
907 ;; XXX: Ideally we'd still allow warnings to be raised if *some*
908 ;; URIs are unreachable, but distinguish that from the error case
909 ;; where *all* the URIs are unreachable.
911 (make-warning package
912 (G_ "all the source URIs are unreachable:")
916 ((git-reference? (origin-uri origin))
918 (list (string->uri (git-reference-url (origin-uri origin))))))
923 (define (check-source-file-name package)
924 "Emit a warning if PACKAGE's origin has no meaningful file name."
925 (define (origin-file-name-valid? origin)
926 ;; Return #f if the source file name contains only a version or is #f;
927 ;; indicates that the origin needs a 'file-name' field.
928 (let ((file-name (origin-actual-file-name origin))
929 (version (package-version package)))
931 ;; Common in many projects is for the filename to start
932 ;; with a "v" followed by the version,
933 ;; e.g. "v3.2.0.tar.gz".
934 (not (string-match (string-append "^v?" version) file-name)))))
936 (let ((origin (package-source package)))
937 (if (or (not (origin? origin)) (origin-file-name-valid? origin))
940 (make-warning package
941 (G_ "the source file name should contain the package name")
944 (define (check-source-unstable-tarball package)
945 "Emit a warning if PACKAGE's source is an autogenerated tarball."
946 (define (check-source-uri uri)
947 (if (and (string=? (uri-host (string->uri uri)) "github.com")
948 (match (split-and-decode-uri-path
949 (uri-path (string->uri uri)))
950 ((_ _ "archive" _ ...) #t)
952 (make-warning package
953 (G_ "the source URI should not be an autogenerated tarball")
957 (let ((origin (package-source package)))
958 (if (and (origin? origin)
959 (eqv? (origin-method origin) url-fetch))
960 (filter-map check-source-uri
961 (origin-uris origin))
964 (define (check-mirror-url package)
965 "Check whether PACKAGE uses source URLs that should be 'mirror://'."
966 (define (check-mirror-uri uri) ;XXX: could be optimized
967 (let loop ((mirrors %mirrors))
971 (((mirror-id mirror-urls ...) rest ...)
972 (match (find (cut string-prefix? <> uri) mirror-urls)
976 (make-warning package
980 (string-drop uri (string-length prefix)))
981 #:field 'source)))))))
983 (let ((origin (package-source package)))
984 (if (and (origin? origin)
985 (eqv? (origin-method origin) url-fetch))
986 (let ((uris (origin-uris origin)))
987 (filter-map check-mirror-uri uris))
990 (define* (check-github-url package #:key (timeout 3))
991 "Check whether PACKAGE uses source URLs that redirect to GitHub."
992 (define (follow-redirect url)
993 (let* ((uri (string->uri url))
994 (port (guix:open-connection-for-uri uri #:timeout timeout))
995 (response (http-head uri #:port port)))
997 (case (response-code response)
999 (uri->string (assoc-ref (response-headers response) 'location)))
1002 (define (follow-redirects-to-github uri)
1004 ((string-prefix? "https://github.com/" uri) uri)
1005 ((string-prefix? "http" uri)
1006 (and=> (follow-redirect uri) follow-redirects-to-github))
1007 ;; Do not attempt to follow redirects on URIs other than http and https
1008 ;; (such as mirror, file)
1011 (let ((origin (package-source package)))
1012 (if (and (origin? origin)
1013 (eqv? (origin-method origin) url-fetch))
1016 (and=> (follow-redirects-to-github uri)
1017 (lambda (github-uri)
1018 (if (string=? github-uri uri)
1022 (G_ "URL should be '~a'")
1024 #:field 'source)))))
1025 (origin-uris origin))
1028 ;; Guile 3.0.0 does not export this predicate.
1029 (define exception-with-kind-and-args?
1030 (exception-predicate &exception-with-kind-and-args))
1032 (define* (check-derivation package #:key store)
1033 "Emit a warning if we fail to compile PACKAGE to a derivation."
1034 (define (try store system)
1035 (guard (c ((store-protocol-error? c)
1036 (make-warning package
1037 (G_ "failed to create ~a derivation: ~a")
1039 (store-protocol-error-message c))))
1040 ((exception-with-kind-and-args? c)
1041 (make-warning package
1042 (G_ "failed to create ~a derivation: ~s")
1044 (cons (exception-kind c)
1045 (exception-args c)))))
1046 ((message-condition? c)
1047 (make-warning package
1048 (G_ "failed to create ~a derivation: ~a")
1050 (condition-message c))))
1051 ((formatted-message? c)
1052 (let ((str (apply format #f
1053 (formatted-message-string c)
1054 (formatted-message-arguments c))))
1055 (make-warning package
1056 (G_ "failed to create ~a derivation: ~a")
1057 (list system str)))))
1058 (parameterize ((%graft? #f))
1059 (package-derivation store package system #:graft? #f)
1061 ;; If there's a replacement, make sure we can compute its
1063 (match (package-replacement package)
1066 (package-derivation store replacement system
1069 (define (check-with-store store)
1070 (filter lint-warning?
1071 (map (cut try store <>) (package-supported-systems package))))
1073 ;; For backwards compatability, don't rely on store being set
1074 (or (and=> store check-with-store)
1076 (check-with-store store))))
1078 (define* (check-profile-collisions package #:key store)
1079 "Check for collisions that would occur when installing PACKAGE as a result
1080 of the propagated inputs it pulls in."
1081 (define (do-check store)
1082 (guard (c ((profile-collision-error? c)
1083 (let ((first (profile-collision-error-entry c))
1084 (second (profile-collision-error-conflict c)))
1086 (if (string=? (manifest-entry-version first)
1087 (manifest-entry-version second))
1090 (string-append (manifest-entry-name entry) "@"
1091 (manifest-entry-version entry)))))
1093 (list (make-warning package
1094 (G_ "propagated inputs ~a and ~a collide")
1095 (list (format first)
1096 (format second)))))))
1097 ;; Disable grafts to avoid building PACKAGE and its dependencies.
1098 (parameterize ((%graft? #f))
1099 (run-with-store store
1100 (mbegin %store-monad
1101 (check-for-collisions (packages->manifest (list package))
1110 (define (check-license package)
1111 "Warn about type errors of the 'license' field of PACKAGE."
1112 (match (package-license package)
1118 (make-warning package (G_ "invalid license field")
1119 #:field 'license)))))
1121 (define (call-with-networking-fail-safe message error-value proc)
1122 "Call PROC catching any network-related errors. Upon a networking error,
1123 display a message including MESSAGE and return ERROR-VALUE."
1124 (guard (c ((http-get-error? c)
1125 (warning (G_ "~a: HTTP GET error for ~a: ~a (~s)~%")
1127 (uri->string (http-get-error-uri c))
1128 (http-get-error-code c)
1129 (http-get-error-reason c))
1134 (('getaddrinfo-error errcode)
1135 (warning (G_ "~a: host lookup failure: ~a~%")
1137 (gai-strerror errcode))
1139 (('tls-certificate-error args ...)
1140 (warning (G_ "~a: TLS certificate error: ~a")
1142 (tls-certificate-error-string args))
1144 ((and ('system-error _ ...) args)
1145 (let ((errno (system-error-errno args)))
1146 (if (member errno (list ECONNRESET ECONNABORTED ECONNREFUSED))
1147 (let ((details (call-with-output-string
1149 (print-exception port #f (car args)
1151 (warning (G_ "~a: ~a~%") message details)
1153 (apply throw args))))
1155 (apply throw args))))))
1157 (define-syntax-rule (with-networking-fail-safe message error-value exp ...)
1158 (call-with-networking-fail-safe message error-value
1159 (lambda () exp ...)))
1161 (define (current-vulnerabilities*)
1162 "Like 'current-vulnerabilities', but return the empty list upon networking
1163 or HTTP errors. This allows network-less operation and makes problems with
1164 the NIST server non-fatal."
1165 (with-networking-fail-safe (G_ "while retrieving CVE vulnerabilities")
1167 (current-vulnerabilities #:timeout 4)))
1169 (define package-vulnerabilities
1170 (let ((lookup (delay (vulnerabilities->lookup-proc
1171 (current-vulnerabilities*)))))
1173 "Return a list of vulnerabilities affecting PACKAGE."
1174 ;; First we retrieve the Common Platform Enumeration (CPE) name and
1175 ;; version for PACKAGE, then we can pass them to LOOKUP.
1176 (let ((name (or (assoc-ref (package-properties package)
1178 (package-name package)))
1179 (version (or (assoc-ref (package-properties package)
1181 (package-version package))))
1182 ((force lookup) name version)))))
1184 (define* (check-vulnerabilities package
1185 #:optional (package-vulnerabilities
1186 package-vulnerabilities))
1187 "Check for known vulnerabilities for PACKAGE. Obtain the list of
1188 vulnerability records for PACKAGE by calling PACKAGE-VULNERABILITIES."
1189 (let ((package (or (package-replacement package) package)))
1190 (match (package-vulnerabilities package)
1193 ((vulnerabilities ...)
1194 (let* ((patched (package-patched-vulnerabilities package))
1195 (known-safe (or (assq-ref (package-properties package)
1198 (unpatched (remove (lambda (vuln)
1199 (let ((id (vulnerability-id vuln)))
1200 (or (member id patched)
1201 (member id known-safe))))
1203 (if (null? unpatched)
1208 (G_ "probably vulnerable to ~a")
1209 (list (string-join (map vulnerability-id unpatched)
1212 (define (check-for-updates package)
1213 "Check if there is an update available for PACKAGE."
1214 (match (lookup-updater package)
1216 (list (make-warning package (G_ "no updater for ~a")
1217 (list (package-name package))
1219 ((? upstream-updater? updater)
1220 (match (with-networking-fail-safe
1221 (format #f (G_ "while retrieving upstream info for '~a'")
1222 (package-name package))
1224 (package-latest-release package))
1225 ((? upstream-source? source)
1226 (if (version>? (upstream-source-version source)
1227 (package-version package))
1229 (make-warning package
1230 (G_ "can be upgraded to ~a")
1231 (list (upstream-source-version source))
1234 (#f ;cannot find upstream release
1235 (list (make-warning package
1236 (G_ "updater '~a' failed to find \
1238 (list (upstream-updater-name updater))
1239 #:field 'source)))))))
1242 (define (lookup-disarchive-spec hash)
1243 "If Disarchive mirrors have a spec for HASH, return the list of SWH
1244 directory identifiers the spec refers to. Otherwise return #f."
1245 (define (extract-swh-id spec)
1246 ;; Return the list of SWH directory identifiers SPEC refers to, where SPEC
1247 ;; is a Disarchive sexp. Instead of attempting to parse it, traverse it
1248 ;; in a pretty unintelligent fashion.
1249 (let loop ((sexp spec)
1253 (let ((prefix "swh:1:dir:"))
1254 (if (string-prefix? prefix str)
1255 (cons (string-drop str (string-length prefix)) ids)
1258 (loop tail (loop head ids)))
1261 (any (lambda (mirror)
1262 (with-networking-fail-safe
1263 (format #f (G_ "failed to access Disarchive database at ~a")
1266 (guard (c ((http-get-error? c) #f))
1267 (let* ((url (string-append mirror
1269 (content-hash-algorithm hash))
1271 (bytevector->base16-string
1272 (content-hash-value hash))))
1273 (port (http-fetch (string->uri url) #:text? #t))
1276 (extract-swh-id spec)))))
1277 %disarchive-mirrors))
1279 (define (check-archival package)
1280 "Check whether PACKAGE's source code is archived on Software Heritage. If
1281 it's not, and if its source code is a VCS snapshot, then send a \"save\"
1282 request to Software Heritage.
1284 Software Heritage imposes limits on the request rate per client IP address.
1285 This checker prints a notice and stops doing anything once that limit has been
1287 (define (response->warning url method response)
1288 (if (request-rate-limit-reached? url method)
1289 (list (make-warning package
1290 (G_ "Software Heritage rate limit reached; \
1293 (list (make-warning package
1294 (G_ "'~a' returned ~a")
1295 (list url (response-code response))
1298 (define skip-key (gensym "skip-archival-check"))
1300 (define (skip-when-limit-reached url method)
1301 (or (not (request-rate-limit-reached? url method))
1302 (throw skip-key #t)))
1304 (parameterize ((%allow-request? skip-when-limit-reached))
1307 (match (and (origin? (package-source package))
1308 (package-source package))
1311 ((= origin-uri (? git-reference? reference))
1313 (git-reference-url reference))
1315 (git-reference-commit reference))
1317 (match (if (commit-id? commit)
1318 (or (lookup-revision commit)
1319 (lookup-origin-revision url commit))
1320 (lookup-origin-revision url commit))
1321 ((? revision? revision)
1324 ;; Revision is missing from the archive, attempt to save it.
1327 (save-origin (git-reference-url reference) "git")
1330 ;; TRANSLATORS: "Software Heritage" is a proper noun
1331 ;; that must remain untranslated. See
1332 ;; <https://www.softwareheritage.org>.
1333 (G_ "scheduled Software Heritage archival")
1335 (lambda (key url method response . _)
1336 (cond ((= 429 (response-code response))
1339 (G_ "archival rate limit exceeded; \
1343 (response->warning url method response))))))))
1345 ;; Since "save" origins are not supported for non-VCS source, all
1346 ;; we can do is tell whether a given tarball is available or not.
1347 (if (and=> (origin-hash origin) ;XXX: for ungoogled-chromium
1348 content-hash-value) ;& icecat
1349 (let ((hash (origin-hash origin)))
1350 (match (lookup-content (content-hash-value hash)
1352 (content-hash-algorithm hash)))
1354 ;; If SWH doesn't have HASH as is, it may be because it's
1355 ;; a hand-crafted tarball. In that case, check whether
1356 ;; the Disarchive database has an entry for that tarball.
1357 (match (lookup-disarchive-spec hash)
1359 (list (make-warning package
1360 (G_ "source not archived on Software \
1361 Heritage and missing from the Disarchive database")
1364 (match (find (lambda (id)
1365 (not (lookup-directory id)))
1369 (list (make-warning package
1371 Disarchive entry refers to non-existent SWH directory '~a'")
1373 #:field 'source)))))))
1378 (('swh-error url method response)
1379 (response->warning url method response))
1381 (if (eq? key skip-key)
1383 (with-networking-fail-safe
1384 (G_ "while connecting to Software Heritage")
1386 (apply throw key args))))))))
1388 (define (check-haskell-stackage package)
1389 "Check whether PACKAGE is a Haskell package ahead of the current
1390 Stackage LTS version."
1391 (match (with-networking-fail-safe
1392 (format #f (G_ "while retrieving upstream info for '~a'")
1393 (package-name package))
1395 (package-latest-release package (list %stackage-updater)))
1396 ((? upstream-source? source)
1397 (if (version>? (package-version package)
1398 (upstream-source-version source))
1400 (make-warning package
1401 (G_ "ahead of Stackage LTS version ~a")
1402 (list (upstream-source-version source))
1409 ;;; Source code formatting.
1412 (define (report-tabulations package line line-number)
1413 "Warn about tabulations found in LINE."
1414 (match (string-index line #\tab)
1417 (make-warning package
1418 (G_ "tabulation on line ~a, column ~a")
1419 (list line-number index)
1421 (location (package-file package)
1425 (define (report-trailing-white-space package line line-number)
1426 "Warn about trailing white space in LINE."
1427 (and (not (or (string=? line (string-trim-right line))
1428 (string=? line (string #\page))))
1429 (make-warning package
1430 (G_ "trailing white space on line ~a")
1433 (location (package-file package)
1437 (define (report-long-line package line line-number)
1438 "Emit a warning if LINE is too long."
1439 ;; Note: We don't warn at 80 characters because sometimes hashes and URLs
1440 ;; make it hard to fit within that limit and we want to avoid making too
1442 (and (> (string-length line) 90)
1443 (make-warning package
1444 (G_ "line ~a is way too long (~a characters)")
1445 (list line-number (string-length line))
1447 (location (package-file package)
1451 (define %hanging-paren-rx
1452 (make-regexp "^[[:blank:]]*[()]+[[:blank:]]*$"))
1454 (define (report-lone-parentheses package line line-number)
1455 "Emit a warning if LINE contains hanging parentheses."
1456 (and (regexp-exec %hanging-paren-rx line)
1457 (make-warning package
1458 (G_ "parentheses feel lonely, \
1459 move to the previous or next line")
1462 (location (package-file package)
1466 (define %formatting-reporters
1467 ;; List of procedures that report formatting issues. These are not separate
1468 ;; checkers because they would need to re-read the file.
1469 (list report-tabulations
1470 report-trailing-white-space
1472 report-lone-parentheses))
1474 (define* (report-formatting-issues package file starting-line
1475 #:key (reporters %formatting-reporters))
1476 "Report white-space issues in FILE starting from STARTING-LINE, and report
1478 (define (sexp-last-line port)
1479 ;; Return the last line of the sexp read from PORT or an estimate thereof.
1480 (define &failure (list 'failure))
1482 (let ((start (ftell port))
1483 (start-line (port-line port))
1484 (sexp (catch 'read-error
1485 (lambda () (read port))
1487 (let ((line (port-line port)))
1488 (seek port start SEEK_SET)
1489 (set-port-line! port start-line)
1490 (if (eq? sexp &failure)
1491 (+ start-line 60) ;conservative estimate
1494 (call-with-input-file file
1496 (let loop ((line-number 1)
1499 (let ((line (read-line port)))
1500 (if (or (eof-object? line)
1501 (and last-line (> line-number last-line)))
1503 (if (and (= line-number starting-line)
1505 (loop (+ 1 line-number)
1506 (+ 1 (sexp-last-line port))
1508 (loop (+ 1 line-number)
1512 (if (< line-number starting-line)
1514 (filter-map (lambda (report)
1515 (report package line line-number))
1516 reporters)))))))))))
1518 (define (check-formatting package)
1519 "Check the formatting of the source code of PACKAGE."
1520 (let ((location (package-location package)))
1522 ;; Report issues starting from the line before the 'package'
1523 ;; form, which usually contains the 'define' form.
1524 (let ((line (- (location-line location) 1)))
1525 (match (search-path %load-path (location-file location))
1527 (report-formatting-issues package file line))
1529 ;; It could be that LOCATION lists a "true" relative file
1530 ;; name--i.e., not relative to an element of %LOAD-PATH.
1531 (let ((file (location-file location)))
1532 (if (file-exists? file)
1533 (report-formatting-issues package file line)
1534 (list (make-warning package
1535 (G_ "source file not found"))))))))
1540 ;;; List of checkers.
1543 (define %local-checkers
1547 (description "Validate package names")
1551 (description "Check if tests are explicitly enabled")
1552 (check check-tests-true))
1555 (description "Validate package descriptions")
1556 (check check-description-style))
1558 (name 'inputs-should-be-native)
1559 (description "Identify inputs that should be native inputs")
1560 (check check-inputs-should-be-native))
1562 (name 'inputs-should-not-be-input)
1563 (description "Identify inputs that shouldn't be inputs at all")
1564 (check check-inputs-should-not-be-an-input-at-all))
1567 ;; TRANSLATORS: <license> is the name of a data type and must not be
1569 (description "Make sure the 'license' field is a <license> \
1571 (check check-license))
1574 (description "Suggest 'mirror://' URLs")
1575 (check check-mirror-url))
1577 (name 'source-file-name)
1578 (description "Validate file names of sources")
1579 (check check-source-file-name))
1581 (name 'source-unstable-tarball)
1582 (description "Check for autogenerated tarballs")
1583 (check check-source-unstable-tarball))
1586 (description "Report failure to compile a package to a derivation")
1587 (check check-derivation)
1588 (requires-store? #t))
1590 (name 'profile-collisions)
1591 (description "Report collisions that would occur due to propagated inputs")
1592 (check check-profile-collisions)
1593 (requires-store? #t))
1595 (name 'patch-file-names)
1596 (description "Validate file names and availability of patches")
1597 (check check-patch-file-names))
1599 (name 'patch-headers)
1600 (description "Validate patch headers")
1601 (check check-patch-headers))
1604 (description "Look for formatting issues in the source")
1605 (check check-formatting))))
1607 (define %network-dependent-checkers
1611 (description "Validate package synopses")
1612 (check check-synopsis-style))
1614 (name 'gnu-description)
1615 (description "Validate synopsis & description of GNU packages")
1616 (check check-gnu-synopsis+description))
1619 (description "Validate home-page URLs")
1620 (check check-home-page))
1623 (description "Validate source URLs")
1624 (check check-source))
1627 (description "Suggest GitHub URLs")
1628 (check check-github-url))
1631 (description "Check the Common Vulnerabilities and Exposures\
1633 (check check-vulnerabilities))
1636 (description "Check the package for new upstream releases")
1637 (check check-for-updates))
1640 (description "Ensure source code archival on Software Heritage")
1641 (check check-archival))
1643 (name 'haskell-stackage)
1644 (description "Ensure Haskell packages use Stackage LTS versions")
1645 (check check-haskell-stackage))))
1647 (define %all-checkers
1648 (append %local-checkers
1649 %network-dependent-checkers))