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-2022 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, 2022 Maxime Devos <maximedevos@telenet.be>
16 ;;; Copyright © 2021 Brice Waegeneire <brice@waegenei.re>
18 ;;; This file is part of GNU Guix.
20 ;;; GNU Guix is free software; you can redistribute it and/or modify it
21 ;;; under the terms of the GNU General Public License as published by
22 ;;; the Free Software Foundation; either version 3 of the License, or (at
23 ;;; your option) any later version.
25 ;;; GNU Guix is distributed in the hope that it will be useful, but
26 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
27 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ;;; GNU General Public License for more details.
30 ;;; You should have received a copy of the GNU General Public License
31 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
33 (define-module (guix lint)
34 #:use-module (guix store)
35 #:autoload (guix base16) (bytevector->base16-string)
36 #:use-module (guix base32)
37 #:autoload (guix base64) (base64-encode)
38 #:use-module (guix build-system)
39 #:use-module (guix diagnostics)
40 #:use-module (guix download)
41 #:use-module (guix ftp-client)
42 #:use-module (guix http-client)
43 #:use-module (guix packages)
44 #:use-module (guix i18n)
45 #:use-module ((guix gexp)
46 #:select (gexp? local-file? local-file-absolute-file-name
47 gexp->approximate-sexp))
48 #:use-module (guix licenses)
49 #:use-module (guix records)
50 #:use-module (guix upstream)
51 #:use-module (guix utils)
52 #:use-module (guix memoization)
53 #:use-module (guix profiles)
54 #:use-module (guix monads)
55 #:use-module (guix scripts)
56 #:use-module ((guix ui) #:select (texi->plain-text fill-paragraph))
57 #:use-module (guix gnu-maintenance)
58 #:use-module (guix cve)
59 #:use-module ((guix swh) #:hide (origin?))
60 #:autoload (guix git-download) (git-reference?
61 git-reference-url git-reference-commit)
62 #:autoload (guix svn-download) (svn-reference?
64 svn-reference-user-name
65 svn-reference-password
68 svn-multi-reference-url
69 svn-multi-reference-user-name
70 svn-multi-reference-password)
71 #:use-module (guix import stackage)
72 #:use-module (ice-9 match)
73 #:use-module (ice-9 regex)
74 #:use-module (ice-9 format)
75 #:autoload (rnrs bytevectors) (string->utf8)
76 #:use-module (web client)
77 #:use-module (web uri)
78 #:use-module ((guix build download)
79 #:select (maybe-expand-mirrors
80 (open-connection-for-uri
81 . guix:open-connection-for-uri)))
82 #:use-module (web request)
83 #:use-module (web response)
84 #:autoload (gnutls) (error->string)
85 #:use-module (srfi srfi-1)
86 #:use-module (srfi srfi-6) ;Unicode string ports
87 #:use-module (srfi srfi-9)
88 #:use-module (srfi srfi-11)
89 #:use-module (srfi srfi-26)
90 #:use-module (srfi srfi-34)
91 #:use-module (srfi srfi-35)
92 #:use-module (ice-9 rdelim)
93 #:export (check-description-style
94 check-inputs-should-be-native
95 check-inputs-should-not-be-an-input-at-all
98 check-patch-file-names
105 check-source-file-name
106 check-source-unstable-tarball
111 check-vulnerabilities
115 check-profile-collisions
116 check-haskell-stackage
123 lint-warning-message-text
124 lint-warning-message-data
125 lint-warning-location
128 %network-dependent-checkers
134 lint-checker-description
136 lint-checker-requires-store?))
143 (define-record-type* <lint-warning>
144 lint-warning make-lint-warning
146 (package lint-warning-package)
147 (message-text lint-warning-message-text)
148 (message-data lint-warning-message-data
150 (location lint-warning-location
153 (define (lint-warning-message warning)
155 (G_ (lint-warning-message-text warning))
156 (lint-warning-message-data warning)))
158 (define (package-file package)
160 (package-location package)))
162 (define* (%make-warning package message-text
163 #:optional (message-data '())
164 #:key field location)
170 (and field (package-field-location package field))
171 (package-location package))))
173 (define-syntax make-warning
175 ((_ package (G_ message) rest ...)
176 (%make-warning package message rest ...))))
180 ;;; Procedures for analysing Scheme code in package definitions
183 (define* (find-procedure-body expression found
184 #:key (not-found (const '())))
185 "Try to find the body of the procedure defined inline by EXPRESSION.
186 If it was found, call FOUND with its body. If it wasn't, call
187 the thunk NOT-FOUND."
189 (`(,(or 'let 'let*) . ,_)
190 (find-procedure-body (car (last-pair expression)) found
191 #:not-found not-found))
192 (`(,(or 'lambda 'lambda*) ,_ . ,code)
196 (define* (report-bogus-phase-deltas package bogus-deltas)
197 "Report a bogus invocation of ‘modify-phases’."
198 (list (make-warning package
199 ;; TRANSLATORS: 'modify-phases' is a Scheme syntax
200 ;; and should not be translated.
201 (G_ "incorrect call to ‘modify-phases’")
202 #:field 'arguments)))
204 (define* (find-phase-deltas package found
205 #:key (not-found (const '()))
207 (cut report-bogus-phase-deltas package <>)))
208 "Try to find the clauses of the ‘modify-phases’ form in the phases
209 specification of PACKAGE. If they were found, all FOUND with a list
210 of the clauses. If they weren't (e.g. because ‘modify-phases’ wasn't
211 used at all), call the thunk NOT-FOUND instead. If ‘modify-phases’
212 was used, but the clauses don't form a list, call BOGUS with the
214 (apply (lambda* (#:key phases #:allow-other-keys)
217 (gexp->approximate-sexp phases)
220 (`(modify-phases ,_ . ,changes)
221 ((if (list? changes) found bogus) changes))
223 (package-arguments package)))
225 (define (report-bogus-phase-procedure package)
226 "Report a syntactically-invalid phase clause."
227 (list (make-warning package
228 ;; TRANSLATORS: See ‘modify-phases’ in the manual.
229 (G_ "invalid phase clause")
230 #:field 'arguments)))
232 (define* (find-phase-procedure package expression found
233 #:key (not-found (const '()))
234 (bogus (cut report-bogus-phase-procedure
236 "Try to find the procedure in the phase clause EXPRESSION. If it was
237 found, call FOUND with the procedure expression. If EXPRESSION isn't
238 actually a phase clause, call the thunk BOGUS. If the phase form doesn't
239 have a procedure, call the thunk NOT-FOUND."
241 (('add-after before after proc-expr)
243 (('add-before after before proc-expr)
245 (('replace _ proc-expr)
247 (('delete _) (not-found))
255 (define-record-type* <lint-checker>
256 lint-checker make-lint-checker
258 ;; TODO: add a 'certainty' field that shows how confident we are in the
259 ;; checker. Then allow users to only run checkers that have a certain
260 ;; 'certainty' level.
261 (name lint-checker-name)
262 (description lint-checker-description)
263 (check lint-checker-check)
264 (requires-store? lint-checker-requires-store?
267 (define (check-name package)
268 "Check whether PACKAGE's name matches our guidelines."
269 (let ((name (package-name package)))
271 ;; Currently checks only whether the name is too short.
272 ((and (<= (string-length name) 1)
273 (not (string=? name "r"))) ; common-sense exception
275 (make-warning package
276 (G_ "name should be longer than a single character")
278 ((string-index name #\_)
280 (make-warning package
281 (G_ "name should use hyphens instead of underscores")
285 (define (check-tests-true package)
286 "Check whether PACKAGE explicitly requests to run tests, which is
287 superfluous when building natively and incorrect when cross-compiling."
288 (define (tests-explicitly-enabled?)
289 (apply (lambda* (#:key tests? #:allow-other-keys)
291 (package-arguments package)))
292 (if (and (tests-explicitly-enabled?)
293 ;; emacs-build-system sets #:tests? #f by default, therefore
294 ;; writing #:tests? #t in package definitions using
295 ;; emacs-build-system is reasonable. Likewise for
296 ;; texlive-build-system.
298 ;; Compare the name of the build system instead of the build system
299 ;; itself to avoid loading unnecessary modules when only a few
300 ;; modules are linted.
301 (not (memq (build-system-name (package-build-system package))
303 ;; Some packages, e.g. gnutls, set #:tests?
304 ;; differently depending on whether it is being
306 (parameterize ((%current-target-system "aarch64-linux-gnu"))
307 (tests-explicitly-enabled?)))
308 (list (make-warning package
309 ;; TRANSLATORS: #:tests? and #t are Scheme constants
310 ;; and must not be translated.
311 (G_ "#:tests? must not be explicitly set to #t")
315 (define (properly-starts-sentence? s)
316 (string-match "^[(\"'`[:upper:][:digit:]]" s))
318 (define (starts-with-abbreviation? s)
319 "Return #t if S starts with what looks like an abbreviation or acronym."
320 (string-match "^[A-Z][A-Z0-9]+\\>" s))
322 (define %quoted-identifier-rx
323 ;; A quoted identifier, like 'this'.
324 (make-regexp "['`][[:graph:]]+'"))
326 (define (check-description-style package)
327 ;; Emit a warning if stylistic issues are found in the description of PACKAGE.
328 (define (check-not-empty description)
329 (if (string-null? description)
331 (make-warning package
332 (G_ "description should not be empty")
333 #:field 'description))
336 (define (check-texinfo-markup description)
337 "Check that DESCRIPTION can be parsed as a Texinfo fragment. If the
338 markup is valid return a plain-text version of DESCRIPTION, otherwise #f."
340 (lambda () (texi->plain-text description))
341 (lambda (keys . args)
342 (make-warning package
343 (G_ "Texinfo markup in description is invalid")
344 #:field 'description))))
346 (define (check-description-typo description typo-corrections)
347 "Check that DESCRIPTION does not contain typo, with optional correction"
351 (if (string-contains description typo)
353 (make-warning package
356 "description contains typo '~a'~@[, should be '~a'~]"
361 (define (check-trademarks description)
362 "Check that DESCRIPTION does not contain '™' or '®' characters. See
363 http://www.gnu.org/prep/standards/html_node/Trademarks.html."
364 (match (string-index description (char-set #\™ #\®))
365 ((and (? number?) index)
367 (make-warning package
368 (G_ "description should not contain ~
369 trademark sign '~a' at ~d")
370 (list (string-ref description index) index)
371 #:field 'description)))
374 (define (check-quotes description)
375 "Check whether DESCRIPTION contains single quotes and suggest @code."
376 (if (regexp-exec %quoted-identifier-rx description)
378 (make-warning package
379 ;; TRANSLATORS: '@code' is Texinfo markup and must be kept
381 (G_ "use @code or similar ornament instead of quotes")
382 #:field 'description))
385 (define (check-proper-start description)
386 (if (or (string-null? description)
387 (properly-starts-sentence? description)
388 (string-prefix-ci? (package-name package) description))
393 (G_ "description should start with an upper-case letter or digit")
394 #:field 'description))))
396 (define (check-end-of-sentence-space description)
397 "Check that an end-of-sentence period is followed by two spaces."
399 (reverse (fold-matches
400 "\\. [A-Z]" description '()
402 ;; Filter out matches of common abbreviations.
403 (if (find (lambda (s)
404 (string-suffix-ci? s (match:prefix m)))
405 '("i.e" "e.g" "a.k.a" "resp"))
406 r (cons (match:start m) r)))))))
407 (if (null? infractions)
410 (make-warning package
411 (G_ "sentences in description should be followed ~
412 by two spaces; possible infraction~p at ~{~a~^, ~}")
413 (list (length infractions)
415 #:field 'description)))))
417 (define (check-no-leading-whitespace description)
418 "Check that DESCRIPTION doesn't have trailing whitespace."
419 (if (string-prefix? " " description)
421 (make-warning package
422 (G_ "description contains leading whitespace")
423 #:field 'description))
426 (define (check-no-trailing-whitespace description)
427 "Check that DESCRIPTION doesn't have trailing whitespace."
428 (if (string-suffix? " " description)
430 (make-warning package
431 (G_ "description contains trailing whitespace")
432 #:field 'description))
435 (let ((description (package-description package)))
436 (if (string? description)
438 (check-not-empty description)
439 (check-quotes description)
440 (check-trademarks description)
441 (check-description-typo description '(("This packages" . "This package")
442 ("This modules" . "This module")
444 ("permits to" . #f)))
445 ;; Use raw description for this because Texinfo rendering
446 ;; automatically fixes end of sentence space.
447 (check-end-of-sentence-space description)
448 (check-no-leading-whitespace description)
449 (check-no-trailing-whitespace description)
450 (match (check-texinfo-markup description)
451 ((and warning (? lint-warning?)) (list warning))
453 (check-proper-start plain-description))))
455 (make-warning package
456 (G_ "invalid description: ~s")
458 #:field 'description)))))
460 (define (package-input-intersection inputs-to-check input-names)
461 "Return the intersection between INPUTS-TO-CHECK, the list of input tuples
462 of a package, and INPUT-NAMES, a list of package specifications such as
464 (match inputs-to-check
465 (((labels packages . outputs) ...)
466 (filter-map (lambda (package output)
467 (and (package? package)
468 (let ((input (string-append
469 (package-name package)
470 (if (> (length output) 0)
471 (string-append ":" (car output))
473 (and (member input input-names)
477 (define (check-inputs-should-be-native package)
478 ;; Emit a warning if some inputs of PACKAGE are likely to belong to its
480 (let ((inputs (append (package-inputs package)
481 (package-propagated-inputs package)))
491 "extra-cmake-modules"
495 "gobject-introspection"
515 "python-setuptools-scm"
522 "xorg-server-for-tests"
527 (G_ "'~a' should probably be a native input")
530 (package-input-intersection inputs input-names))))
532 (define (check-inputs-should-not-be-an-input-at-all package)
533 ;; Emit a warning if some inputs of PACKAGE are likely to should not be
535 (let ((input-names '("python-setuptools"
540 (G_ "'~a' should probably not be an input at all")
543 (package-input-intersection (package-direct-inputs package)
546 (define (check-input-labels package)
547 "Emit a warning for labels that differ from the corresponding package name."
548 (define (check input-kind package-inputs)
549 (define (warning label name)
550 (make-warning package
551 (G_ "label '~a' does not match package name '~a'")
555 (append-map (match-lambda
556 (((? string? label) (? package? dependency))
557 (if (string=? label (package-name dependency))
559 (list (warning label (package-name dependency)))))
560 (((? string? label) (? package? dependency) output)
561 (let ((expected (string-append (package-name dependency)
563 (if (string=? label expected)
565 (list (warning label expected)))))
568 (package-inputs package)))
570 (append-map (match-lambda
573 `((native-inputs ,package-native-inputs)
574 (inputs ,package-inputs)
575 (propagated-inputs ,package-propagated-inputs))))
577 (define (report-wrap-program-error package wrapper-name)
578 "Warn that \"bash-minimal\" is missing from 'inputs', while WRAPPER-NAME
580 (make-warning package
581 (G_ "\"bash-minimal\" should be in 'inputs' when '~a' is used")
582 (list wrapper-name)))
584 (define (check-wrapper-inputs package)
585 "Emit a warning if PACKAGE uses 'wrap-program' or similar, but \"bash\"
586 or \"bash-minimal\" is not in its inputs. 'wrap-script' is not supported."
587 (define input-names '("bash" "bash-minimal"))
588 (define has-bash-input?
589 (pair? (package-input-intersection (package-inputs package)
591 (define (check-procedure-body body)
593 ;; Explicitely setting an interpreter is acceptable.
594 (('wrap-program _ '#:sh . _) '())
595 (('wrap-program _ . _)
596 (list (report-wrap-program-error package 'wrap-program)))
597 ;; Wrapper of 'wrap-program' for Qt programs.
598 ;; TODO #:sh is not yet supported but probably will be.
599 (('wrap-qt-program _ '#:sh . _) '())
600 (('wrap-qt-program _ . _)
601 (list (report-wrap-program-error package 'wrap-qt-program)))
603 (append (check-procedure-body x) (check-procedure-body y)))
605 (define (check-phase-procedure expression)
606 (find-procedure-body expression check-procedure-body))
607 (define (check-delta expression)
608 (find-phase-procedure package expression check-phase-procedure))
609 (define (check-deltas deltas)
610 (append-map check-delta deltas))
612 ;; "bash" (or "bash-minimal") is in 'inputs', so everything seems ok.
614 ;; "bash" is not in 'inputs'. Verify 'wrap-program' and friends
616 (find-phase-deltas package check-deltas)))
618 (define (package-name-regexp package)
619 "Return a regexp that matches PACKAGE's name as a word at the beginning of a
621 (make-regexp (string-append "^" (regexp-quote (package-name package))
625 (define (check-synopsis-style package)
626 ;; Emit a warning if stylistic issues are found in the synopsis of PACKAGE.
627 (define (check-final-period synopsis)
628 ;; Synopsis should not end with a period, except for some special cases.
629 (if (and (string-suffix? "." synopsis)
630 (not (string-suffix? "etc." synopsis)))
632 (make-warning package
633 (G_ "no period allowed at the end of the synopsis")
637 (define check-start-article
638 ;; Skip this check for GNU packages, as suggested by Karl Berry's reply to
639 ;; <http://lists.gnu.org/archive/html/bug-womb/2014-11/msg00000.html>.
640 (if (false-if-exception (gnu-package? package))
643 (if (or (string-prefix-ci? "A " synopsis)
644 (string-prefix-ci? "An " synopsis))
646 (make-warning package
647 (G_ "no article allowed at the beginning of \
652 (define (check-synopsis-length synopsis)
653 (if (>= (string-length synopsis) 80)
655 (make-warning package
656 (G_ "synopsis should be less than 80 characters long")
660 (define (check-proper-start synopsis)
661 (if (properly-starts-sentence? synopsis)
664 (make-warning package
665 (G_ "synopsis should start with an upper-case letter or digit")
666 #:field 'synopsis))))
668 (define (check-start-with-package-name synopsis)
669 (if (and (regexp-exec (package-name-regexp package) synopsis)
670 (not (starts-with-abbreviation? synopsis)))
672 (make-warning package
673 (G_ "synopsis should not start with the package name")
677 (define (check-texinfo-markup synopsis)
678 "Check that SYNOPSIS can be parsed as a Texinfo fragment. If the
679 markup is valid return a plain-text version of SYNOPSIS, otherwise #f."
682 (texi->plain-text synopsis)
684 (lambda (keys . args)
686 (make-warning package
687 (G_ "Texinfo markup in synopsis is invalid")
688 #:field 'synopsis)))))
690 (define (check-no-trailing-whitespace synopsis)
691 "Check that SYNOPSIS doesn't have trailing whitespace."
692 (if (string-suffix? " " synopsis)
694 (make-warning package
695 (G_ "synopsis contains trailing whitespace")
700 (list check-proper-start
703 check-start-with-package-name
704 check-synopsis-length
706 check-no-trailing-whitespace))
708 (match (package-synopsis package)
711 (make-warning package
712 (G_ "synopsis should not be empty")
714 ((? string? synopsis)
721 (make-warning package
722 (G_ "invalid synopsis: ~s")
724 #:field 'synopsis)))))
726 (define* (probe-uri uri #:key timeout)
727 "Probe URI, a URI object, and return two values: a symbol denoting the
728 probing status, such as 'http-response' when we managed to get an HTTP
729 response from URI, and additional details, such as the actual HTTP response.
731 TIMEOUT is the maximum number of seconds (possibly an inexact number) to wait
732 for connections to complete; when TIMEOUT is #f, wait as long as needed."
734 `((User-Agent . "GNU Guile")
736 ,@(match (uri-userinfo uri)
737 ((? string? str) ;"basic authentication"
738 `((Authorization . ,(string-append "Basic "
740 (string->utf8 str))))))
745 (match (uri-scheme uri)
749 (let ((port (guix:open-connection-for-uri
750 uri #:timeout timeout))
751 (request (build-request uri #:headers headers)))
756 (write-request request port)
758 (read-response port))
762 (case (response-code response)
763 ((302 ; found (redirection)
765 307 ; temporary redirection
766 308) ; permanent redirection
767 (let ((location (response-location response)))
768 (if (or (not location) (member location visited))
769 (values 'http-response response)
770 (loop location (cons location visited))))) ;follow the redirect
771 ((301) ; moved permanently
772 (let ((location (response-location response)))
773 ;; Return RESPONSE, unless the final response as we follow
774 ;; redirects is not 200.
776 (let-values (((status response2)
777 (loop location (cons location visited))))
780 (values 'http-response
781 (if (= 200 (response-code response2))
785 (values status response2))))
786 (values 'http-response response)))) ;invalid redirect
788 (values 'http-response response)))))
791 ((bad-header bad-header-component)
792 ;; This can happen if the server returns an invalid HTTP header,
793 ;; as is the case with the 'Date' header at sqlite.org.
794 (values 'invalid-http-response #f))
795 ((getaddrinfo-error system-error
796 gnutls-error tls-certificate-error)
799 (apply throw key args))))))
803 (let ((conn (ftp-open (uri-host uri) #:timeout timeout)))
808 (ftp-chdir conn (dirname (uri-path uri)))
809 (ftp-size conn (basename (uri-path uri))))
812 (values 'ftp-response '(ok))))
816 (values 'ftp-response `(error ,@args)))
817 ((getaddrinfo-error system-error gnutls-error)
820 (apply throw key args))))))
822 (values 'unknown-protocol #f)))))
824 (define (call-with-networking-fail-safe message error-value proc)
825 "Call PROC catching any network-related errors. Upon a networking error,
826 display a message including MESSAGE and return ERROR-VALUE."
827 (guard (c ((http-get-error? c)
828 (warning (G_ "~a: HTTP GET error for ~a: ~a (~s)~%")
830 (uri->string (http-get-error-uri c))
831 (http-get-error-code c)
832 (http-get-error-reason c))
837 (('getaddrinfo-error errcode)
838 (warning (G_ "~a: host lookup failure: ~a~%")
840 (gai-strerror errcode))
842 (('tls-certificate-error args ...)
843 (warning (G_ "~a: TLS certificate error: ~a")
845 (tls-certificate-error-string args))
847 (('gnutls-error error function _ ...)
848 (warning (G_ "~a: TLS error in '~a': ~a~%")
850 function (error->string error))
852 ((and ('system-error _ ...) args)
853 (let ((errno (system-error-errno args)))
854 (if (member errno (list ECONNRESET ECONNABORTED ECONNREFUSED))
855 (let ((details (call-with-output-string
857 (print-exception port #f (car args)
859 (warning (G_ "~a: ~a~%") message details)
861 (apply throw args))))
863 (apply throw args))))))
865 (define-syntax-rule (with-networking-fail-safe message error-value exp ...)
866 (call-with-networking-fail-safe message error-value
867 (lambda () exp ...)))
869 (define (tls-certificate-error-string args)
870 "Return a string explaining the 'tls-certificate-error' arguments ARGS."
871 (call-with-output-string
873 (print-exception port #f
874 'tls-certificate-error args))))
876 (define (validate-uri uri package field)
877 "Return #t if the given URI can be reached, otherwise return a warning for
878 PACKAGE mentioning the FIELD."
879 (let-values (((status argument)
880 (probe-uri uri #:timeout 3))) ;wait at most 3 seconds
883 (cond ((= 200 (response-code argument))
884 (match (response-content-length argument)
886 ;; As of July 2016, SourceForge returns 200 (instead of 404)
887 ;; with a small HTML page upon failure. Attempt to detect
888 ;; such malicious behavior.
890 (make-warning package
891 (G_ "URI ~a returned \
892 suspiciously small file (~a bytes)")
893 (list (uri->string uri)
897 ((= 301 (response-code argument))
898 (if (response-location argument)
899 (make-warning package
900 (G_ "permanent redirect from ~a to ~a")
901 (list (uri->string uri)
903 (response-location argument)))
905 (make-warning package
906 (G_ "invalid permanent redirect \
908 (list (uri->string uri))
911 (make-warning package
912 (G_ "URI ~a not reachable: ~a (~s)")
913 (list (uri->string uri)
914 (response-code argument)
915 (response-reason-phrase argument))
920 (('error port command code message)
921 (make-warning package
922 (G_ "URI ~a not reachable: ~a (~s)")
923 (list (uri->string uri)
924 code (string-trim-both message))
927 (make-warning package
928 (G_ "URI ~a domain not found: ~a")
929 (list (uri->string uri)
930 (gai-strerror (car argument)))
933 (make-warning package
934 (G_ "URI ~a unreachable: ~a")
935 (list (uri->string uri)
938 (cons status argument))))
940 ((tls-certificate-error)
941 (make-warning package
942 (G_ "TLS certificate error: ~a")
943 (list (tls-certificate-error-string argument))
945 ((invalid-http-response gnutls-error)
946 ;; Probably a misbehaving server; ignore.
948 ((unknown-protocol) ;nothing we can do
951 (error "internal linter error" status)))))
953 (define (check-home-page package)
954 "Emit a warning if PACKAGE has an invalid 'home-page' field, or if that
955 'home-page' is not reachable."
956 (let ((uri (and=> (package-home-page package) string->uri)))
959 (match (validate-uri uri package 'home-page)
960 ((and (? lint-warning? warning) warning)
963 ((not (package-home-page package))
964 (if (or (string-contains (package-name package) "bootstrap")
965 (string=? (package-name package) "ld-wrapper"))
968 (make-warning package
969 (G_ "invalid value for home page")
970 #:field 'home-page))))
973 (make-warning package
974 (G_ "invalid home page URL: ~s")
975 (list (package-home-page package))
976 #:field 'home-page))))))
978 (define %distro-directory
980 (dirname (search-path %load-path "gnu.scm"))))
982 (define (check-patch-file-names package)
983 "Emit a warning if the patches requires by PACKAGE are badly named or if the
984 patch could not be found."
985 (guard (c ((formatted-message? c) ;raised by 'search-patch'
986 (list (%make-warning package
987 (formatted-message-string c)
988 (formatted-message-arguments c)
991 (match (package-source package)
992 ((? origin? origin) (origin-patches origin))
995 (define (starts-with-package-name? file-name)
996 (and=> (string-contains file-name (package-name package))
1000 (if (every (match-lambda ;patch starts with package name?
1002 (starts-with-package-name? (basename patch)))
1004 (starts-with-package-name? (origin-actual-file-name patch)))
1005 (_ #f)) ;must be some other file-like object
1011 (G_ "file names of patches should start with the package name")
1012 #:field 'patch-file-names)))
1014 ;; Check whether we're reaching tar's maximum file name length.
1015 (let ((prefix (string-length (%distro-directory)))
1016 ;; Margin approximating the largest path that "make dist" might
1017 ;; create, with a release candidate version, 123456 commits, and
1018 ;; git commit hash abcde0.
1019 (margin (string-length "guix-92.0.0rc3-123456-abcde0/"))
1020 ;; Tested maximum patch file length for ustar format.
1022 (filter-map (match-lambda
1024 (if (> (+ margin (if (string-prefix? (%distro-directory)
1026 (- (string-length patch) prefix)
1027 (string-length patch)))
1031 (G_ "~a: file name is too long, which may break 'make dist'")
1032 (list (basename patch))
1033 #:field 'patch-file-names)
1038 (define (check-patch-headers package)
1039 "Check that PACKAGE's patches start with a comment. Return a list of
1041 (define (blank? str)
1042 (string-every char-set:blank str))
1044 (define (patch-header-warnings patch)
1045 (call-with-input-file patch
1047 ;; Read from PORT until a non-blank line is found or EOF is reached.
1049 (let ((line (read-line port)))
1050 (cond ((eof-object? line)
1051 (list (make-warning package
1052 (G_ "~a: empty patch")
1053 (list (basename patch))
1057 ((or (string-prefix? "--- " line)
1058 (string-prefix? "+++ " line)
1059 (string-prefix? "diff --git " line))
1060 (list (make-warning package
1061 (G_ "~a: patch lacks comment and \
1063 (list (basename patch))
1068 (guard (c ((formatted-message? c) ;raised by 'search-patch'
1069 (list (%make-warning package
1070 (formatted-message-string c)
1071 (formatted-message-arguments c)
1073 (let ((patches (if (origin? (package-source package))
1074 (origin-patches (package-source package))
1076 (append-map (lambda (patch)
1077 ;; Dismiss PATCH if it's an origin or similar.
1078 (cond ((string? patch)
1079 (patch-header-warnings patch))
1080 ((local-file? patch)
1081 (patch-header-warnings
1082 (local-file-absolute-file-name patch)))
1087 (define (escape-quotes str)
1088 "Replace any quote character in STR by an escaped quote character."
1090 (string-fold-right (lambda (chr result)
1092 (#\" (cons* #\\ #\"result))
1093 (_ (cons chr result))))
1097 (define official-gnu-packages*
1099 "A memoizing version of 'official-gnu-packages' that returns the empty
1100 list when something goes wrong, such as a networking issue."
1101 (let ((gnus (false-if-exception (official-gnu-packages))))
1104 (define (check-gnu-synopsis+description package)
1105 "Make sure that, if PACKAGE is a GNU package, it uses the synopsis and
1106 descriptions maintained upstream."
1107 (match (find (lambda (descriptor)
1108 (string=? (gnu-package-name descriptor)
1109 (package-name package)))
1110 (official-gnu-packages*))
1111 (#f ;not a GNU package, so nothing to do
1113 (descriptor ;a genuine GNU package
1115 (let ((upstream (gnu-package-doc-summary descriptor))
1116 (downstream (package-synopsis package)))
1118 (or (not (string? downstream))
1119 (not (string=? upstream downstream))))
1121 (make-warning package
1122 (G_ "proposed synopsis: ~s~%")
1127 (let ((upstream (gnu-package-doc-description descriptor))
1128 (downstream (package-description package)))
1130 (or (not (string? downstream))
1131 (not (string=? (fill-paragraph upstream 100)
1132 (fill-paragraph downstream 100)))))
1136 (G_ "proposed description:~% \"~a\"~%")
1137 (list (fill-paragraph (escape-quotes upstream) 77 7))
1138 #:field 'description))
1141 (define (origin-uris origin)
1142 "Return the list of URIs (strings) for ORIGIN."
1143 (match (origin-uri origin)
1149 (define (svn-reference-uri-with-userinfo ref)
1150 "Return the URI of REF, an <svn-reference> or <svn-multi-reference> object,
1151 but with an additional 'userinfo' part corresponding to REF's user name and
1152 password, provided REF's URI is HTTP or HTTPS."
1153 ;; XXX: For lack of record type inheritance.
1155 (if (svn-reference? ref)
1157 svn-multi-reference-url))
1159 (if (svn-reference? ref)
1160 svn-reference-user-name
1161 svn-multi-reference-user-name))
1163 (if (svn-reference? ref)
1164 svn-reference-password
1165 svn-multi-reference-password))
1167 (let ((uri (string->uri (->url ref))))
1168 (if (and (->user-name ref)
1169 (memq (uri-scheme uri) '(http https)))
1170 (build-uri (uri-scheme uri)
1172 (string-append (->user-name ref)
1173 (if (->password ref)
1175 ":" (->password ref))
1177 #:host (uri-host uri)
1178 #:port (uri-port uri)
1179 #:query (uri-query uri)
1180 #:fragment (uri-fragment uri))
1183 (define (check-source package)
1184 "Emit a warning if PACKAGE has an invalid 'source' field, or if that
1185 'source' is not reachable."
1186 (define (warnings-for-uris uris)
1187 (let loop ((uris uris)
1193 (match (validate-uri uri package 'source)
1195 ;; We found a working URL, so stop right away.
1198 ;; Unsupported URL or other error, skip.
1199 (loop rest warnings))
1200 ((? lint-warning? warning)
1201 (loop rest (cons warning warnings))))))))
1203 (let ((origin (package-source package)))
1204 (if (origin? origin)
1206 ((eq? (origin-method origin) url-fetch)
1207 (let* ((uris (append-map (cut maybe-expand-mirrors <> %mirrors)
1208 (map string->uri (origin-uris origin))))
1209 (warnings (warnings-for-uris uris)))
1211 ;; Just make sure that at least one of the URIs is valid.
1212 (if (= (length uris) (length warnings))
1213 ;; When everything fails, report all of WARNINGS, otherwise don't
1216 ;; XXX: Ideally we'd still allow warnings to be raised if *some*
1217 ;; URIs are unreachable, but distinguish that from the error case
1218 ;; where *all* the URIs are unreachable.
1220 (make-warning package
1221 (G_ "all the source URIs are unreachable:")
1225 ((git-reference? (origin-uri origin))
1227 (list (string->uri (git-reference-url (origin-uri origin))))))
1228 ((or (svn-reference? (origin-uri origin))
1229 (svn-multi-reference? (origin-uri origin)))
1230 (let ((uri (svn-reference-uri-with-userinfo (origin-uri origin))))
1231 (if (memq (uri-scheme uri) '(http https))
1232 (warnings-for-uris (list uri))
1233 '()))) ;TODO: handle svn:// URLs
1238 (define (check-source-file-name package)
1239 "Emit a warning if PACKAGE's origin has no meaningful file name."
1240 (define (origin-file-name-valid? origin)
1241 ;; Return #f if the source file name contains only a version or is #f;
1242 ;; indicates that the origin needs a 'file-name' field.
1243 (let ((file-name (origin-actual-file-name origin))
1244 (version (package-version package)))
1246 ;; Common in many projects is for the filename to start
1247 ;; with a "v" followed by the version,
1248 ;; e.g. "v3.2.0.tar.gz".
1249 (not (string-match (string-append "^v?" version) file-name)))))
1251 (let ((origin (package-source package)))
1252 (if (or (not (origin? origin)) (origin-file-name-valid? origin))
1255 (make-warning package
1256 (G_ "the source file name should contain the package name")
1257 #:field 'source)))))
1259 (define (check-source-unstable-tarball package)
1260 "Emit a warning if PACKAGE's source is an autogenerated tarball."
1261 (define (check-source-uri uri)
1262 (if (and (string=? (uri-host (string->uri uri)) "github.com")
1263 (match (split-and-decode-uri-path
1264 (uri-path (string->uri uri)))
1265 ((_ _ "archive" _ ...) #t)
1267 (make-warning package
1268 (G_ "the source URI should not be an autogenerated tarball")
1272 (let ((origin (package-source package)))
1273 (if (and (origin? origin)
1274 (eqv? (origin-method origin) url-fetch))
1275 (filter-map check-source-uri
1276 (origin-uris origin))
1279 (define (check-mirror-url package)
1280 "Check whether PACKAGE uses source URLs that should be 'mirror://'."
1281 (define (check-mirror-uri uri)
1282 (define rewritten-uri
1283 (uri-mirror-rewrite uri))
1285 (and (not (string=? uri rewritten-uri))
1286 (make-warning package (G_ "URL should be '~a'")
1287 (list rewritten-uri)
1290 (let ((origin (package-source package)))
1291 (if (and (origin? origin)
1292 (eqv? (origin-method origin) url-fetch))
1293 (let ((uris (origin-uris origin)))
1294 (filter-map check-mirror-uri uris))
1297 (define* (check-github-url package #:key (timeout 3))
1298 "Check whether PACKAGE uses source URLs that redirect to GitHub."
1299 (define (follow-redirect url)
1300 (let* ((uri (string->uri url))
1301 (port (guix:open-connection-for-uri uri #:timeout timeout))
1302 (response (http-head uri #:port port)))
1304 (case (response-code response)
1306 (uri->string (assoc-ref (response-headers response) 'location)))
1309 (define (follow-redirects-to-github uri)
1311 ((string-prefix? "https://github.com/" uri) uri)
1312 ((string-prefix? "http" uri)
1313 (and=> (follow-redirect uri) follow-redirects-to-github))
1314 ;; Do not attempt to follow redirects on URIs other than http and https
1315 ;; (such as mirror, file)
1318 (let ((origin (package-source package)))
1319 (if (and (origin? origin)
1320 (eqv? (origin-method origin) url-fetch))
1323 (and=> (with-networking-fail-safe
1324 (format #f (G_ "while accessing '~a'") uri)
1326 (follow-redirects-to-github uri))
1327 (lambda (github-uri)
1328 (and (not (string=? github-uri uri))
1331 (G_ "URL should be '~a'")
1333 #:field 'source)))))
1334 (origin-uris origin))
1337 ;; Guile 3.0.0 does not export this predicate.
1338 (define exception-with-kind-and-args?
1339 (exception-predicate &exception-with-kind-and-args))
1341 (define (check-optional-tests package)
1342 "Emit a warning if the test suite is run unconditionally."
1343 (define (sexp-contains-atom? sexp atom)
1344 "Test if SEXP contains ATOM."
1346 (or (sexp-contains-atom? (car sexp) atom)
1347 (sexp-contains-atom? (cdr sexp) atom))
1349 (define (sexp-uses-tests?? sexp)
1350 "Test if SEXP contains the symbol 'tests?'."
1351 (sexp-contains-atom? sexp 'tests?))
1352 (define (check-procedure-body code)
1353 (if (sexp-uses-tests?? code)
1355 (list (make-warning package
1356 ;; TRANSLATORS: check and #:tests? are a
1357 ;; Scheme symbol and keyword respectively
1358 ;; and should not be translated.
1359 (G_ "the 'check' phase should respect #:tests?")
1360 #:field 'arguments))))
1361 (define (check-check-procedure expression)
1362 (find-procedure-body expression check-procedure-body))
1363 (define (check-phases-delta delta)
1365 (`(replace 'check ,expression)
1366 (check-check-procedure expression))
1368 (define (check-phases-deltas deltas)
1369 (append-map check-phases-delta deltas))
1370 (find-phase-deltas package check-phases-deltas))
1372 (define* (check-derivation package #:key store)
1373 "Emit a warning if we fail to compile PACKAGE to a derivation."
1374 (define (try store system)
1375 (guard (c ((store-protocol-error? c)
1376 (make-warning package
1377 (G_ "failed to create ~a derivation: ~a")
1379 (store-protocol-error-message c))))
1380 ((exception-with-kind-and-args? c)
1381 (make-warning package
1382 (G_ "failed to create ~a derivation: ~s")
1384 (cons (exception-kind c)
1385 (exception-args c)))))
1386 ((message-condition? c)
1387 (make-warning package
1388 (G_ "failed to create ~a derivation: ~a")
1390 (condition-message c))))
1391 ((formatted-message? c)
1392 (let ((str (apply format #f
1393 (formatted-message-string c)
1394 (formatted-message-arguments c))))
1395 (make-warning package
1396 (G_ "failed to create ~a derivation: ~a")
1397 (list system str))))
1399 (make-warning package
1400 (G_ "failed to create ~a derivation: ~a")
1402 (parameterize ((%graft? #f))
1403 (package-derivation store package system #:graft? #f)
1405 ;; If there's a replacement, make sure we can compute its
1407 (match (package-replacement package)
1410 (package-derivation store replacement system
1413 (define (check-with-store store)
1414 (filter lint-warning?
1415 (map (cut try store <>) (package-supported-systems package))))
1417 ;; For backwards compatability, don't rely on store being set
1418 (or (and=> store check-with-store)
1420 (check-with-store store))))
1422 (define* (check-profile-collisions package #:key store)
1423 "Check for collisions that would occur when installing PACKAGE as a result
1424 of the propagated inputs it pulls in."
1425 (define (do-check store)
1426 (guard (c ((profile-collision-error? c)
1427 (let ((first (profile-collision-error-entry c))
1428 (second (profile-collision-error-conflict c)))
1430 (if (string=? (manifest-entry-version first)
1431 (manifest-entry-version second))
1434 (string-append (manifest-entry-name entry) "@"
1435 (manifest-entry-version entry)))))
1437 (list (make-warning package
1438 (G_ "propagated inputs ~a and ~a collide")
1439 (list (format first)
1440 (format second)))))))
1441 ;; Disable grafts to avoid building PACKAGE and its dependencies.
1442 (parameterize ((%graft? #f))
1443 (run-with-store store
1444 (mbegin %store-monad
1445 (check-for-collisions (packages->manifest (list package))
1454 (define (check-license package)
1455 "Warn about type errors of the 'license' field of PACKAGE."
1456 (match (package-license package)
1462 (make-warning package (G_ "invalid license field")
1463 #:field 'license)))))
1465 (define (current-vulnerabilities*)
1466 "Like 'current-vulnerabilities', but return the empty list upon networking
1467 or HTTP errors. This allows network-less operation and makes problems with
1468 the NIST server non-fatal."
1469 (with-networking-fail-safe (G_ "while retrieving CVE vulnerabilities")
1471 (current-vulnerabilities #:timeout 4)))
1473 (define package-vulnerabilities
1474 (let ((lookup (delay (vulnerabilities->lookup-proc
1475 (current-vulnerabilities*)))))
1477 "Return a list of vulnerabilities affecting PACKAGE."
1478 ;; First we retrieve the Common Platform Enumeration (CPE) name and
1479 ;; version for PACKAGE, then we can pass them to LOOKUP.
1480 (let ((name (or (assoc-ref (package-properties package)
1482 (package-name package)))
1483 (version (or (assoc-ref (package-properties package)
1485 (package-version package))))
1486 ((force lookup) name version)))))
1488 (define* (check-vulnerabilities package
1489 #:optional (package-vulnerabilities
1490 package-vulnerabilities))
1491 "Check for known vulnerabilities for PACKAGE. Obtain the list of
1492 vulnerability records for PACKAGE by calling PACKAGE-VULNERABILITIES."
1493 (let ((package (or (package-replacement package) package)))
1494 (match (package-vulnerabilities package)
1497 ((vulnerabilities ...)
1498 (let* ((patched (package-patched-vulnerabilities package))
1499 (known-safe (or (assq-ref (package-properties package)
1502 (unpatched (remove (lambda (vuln)
1503 (let ((id (vulnerability-id vuln)))
1504 (or (member id patched)
1505 (member id known-safe))))
1507 (if (null? unpatched)
1512 (G_ "probably vulnerable to ~a")
1513 (list (string-join (map vulnerability-id unpatched)
1516 (define (check-for-updates package)
1517 "Check if there is an update available for PACKAGE."
1518 (match (lookup-updater package)
1520 (list (make-warning package (G_ "no updater for ~a")
1521 (list (package-name package))
1523 ((? upstream-updater? updater)
1524 (match (with-networking-fail-safe
1525 (format #f (G_ "while retrieving upstream info for '~a'")
1526 (package-name package))
1528 (package-latest-release package))
1529 ((? upstream-source? source)
1530 (if (version>? (upstream-source-version source)
1531 (package-version package))
1533 (make-warning package
1534 (G_ "can be upgraded to ~a")
1535 (list (upstream-source-version source))
1538 (#f ;cannot find upstream release
1539 (list (make-warning package
1540 (G_ "updater '~a' failed to find \
1542 (list (upstream-updater-name updater))
1543 #:field 'source)))))))
1546 (define (lookup-disarchive-spec hash)
1547 "If Disarchive mirrors have a spec for HASH, return the list of SWH
1548 directory identifiers the spec refers to. Otherwise return #f."
1549 (define (extract-swh-id spec)
1550 ;; Return the list of SWH directory identifiers SPEC refers to, where SPEC
1551 ;; is a Disarchive sexp. Instead of attempting to parse it, traverse it
1552 ;; in a pretty unintelligent fashion.
1553 (let loop ((sexp spec)
1557 (let ((prefix "swh:1:dir:"))
1558 (if (string-prefix? prefix str)
1559 (cons (string-drop str (string-length prefix)) ids)
1562 (loop tail (loop head ids)))
1565 (any (lambda (mirror)
1566 (with-networking-fail-safe
1567 (format #f (G_ "failed to access Disarchive database at ~a")
1570 (guard (c ((http-get-error? c) #f))
1571 (let* ((url (string-append mirror
1573 (content-hash-algorithm hash))
1575 (bytevector->base16-string
1576 (content-hash-value hash))))
1577 (port (http-fetch (string->uri url) #:text? #t))
1580 (extract-swh-id spec)))))
1581 %disarchive-mirrors))
1583 (define (check-archival package)
1584 "Check whether PACKAGE's source code is archived on Software Heritage. If
1585 it's not, and if its source code is a VCS snapshot, then send a \"save\"
1586 request to Software Heritage.
1588 Software Heritage imposes limits on the request rate per client IP address.
1589 This checker prints a notice and stops doing anything once that limit has been
1591 (define (response->warning url method response)
1592 (if (request-rate-limit-reached? url method)
1593 (list (make-warning package
1594 (G_ "Software Heritage rate limit reached; \
1597 (list (make-warning package
1598 (G_ "'~a' returned ~a")
1599 (list url (response-code response))
1602 (define skip-key (gensym "skip-archival-check"))
1604 (define (skip-when-limit-reached url method)
1605 (or (not (request-rate-limit-reached? url method))
1606 (throw skip-key #t)))
1608 (parameterize ((%allow-request? skip-when-limit-reached))
1611 (match (and (origin? (package-source package))
1612 (package-source package))
1615 ((= origin-uri (? git-reference? reference))
1617 (git-reference-url reference))
1619 (git-reference-commit reference))
1621 (match (if (commit-id? commit)
1622 (or (lookup-revision commit)
1623 (lookup-origin-revision url commit))
1624 (lookup-origin-revision url commit))
1625 ((? revision? revision)
1628 ;; Revision is missing from the archive, attempt to save it.
1631 (save-origin (git-reference-url reference) "git")
1634 ;; TRANSLATORS: "Software Heritage" is a proper noun
1635 ;; that must remain untranslated. See
1636 ;; <https://www.softwareheritage.org>.
1637 (G_ "scheduled Software Heritage archival")
1639 (lambda (key url method response . _)
1640 (cond ((= 429 (response-code response))
1643 (G_ "archival rate limit exceeded; \
1647 (response->warning url method response))))))))
1649 ;; Since "save" origins are not supported for non-VCS source, all
1650 ;; we can do is tell whether a given tarball is available or not.
1651 (if (and=> (origin-hash origin) ;XXX: for ungoogled-chromium
1652 content-hash-value) ;& icecat
1653 (let ((hash (origin-hash origin)))
1654 (match (lookup-content (content-hash-value hash)
1656 (content-hash-algorithm hash)))
1658 ;; If SWH doesn't have HASH as is, it may be because it's
1659 ;; a hand-crafted tarball. In that case, check whether
1660 ;; the Disarchive database has an entry for that tarball.
1661 (match (lookup-disarchive-spec hash)
1663 (list (make-warning package
1664 (G_ "source not archived on Software \
1665 Heritage and missing from the Disarchive database")
1668 (match (find (lambda (id)
1669 (not (lookup-directory id)))
1673 (list (make-warning package
1675 Disarchive entry refers to non-existent SWH directory '~a'")
1677 #:field 'source)))))))
1682 (list (make-warning package
1683 (G_ "unsupported source type")
1684 #:field 'source)))))
1686 (('swh-error url method response)
1687 (response->warning url method response))
1689 (if (eq? key skip-key)
1691 (with-networking-fail-safe
1692 (G_ "while connecting to Software Heritage")
1694 (apply throw key args))))))))
1696 (define (check-haskell-stackage package)
1697 "Check whether PACKAGE is a Haskell package ahead of the current
1698 Stackage LTS version."
1699 (match (with-networking-fail-safe
1700 (format #f (G_ "while retrieving upstream info for '~a'")
1701 (package-name package))
1703 (package-latest-release package (list %stackage-updater)))
1704 ((? upstream-source? source)
1705 (if (version>? (package-version package)
1706 (upstream-source-version source))
1708 (make-warning package
1709 (G_ "ahead of Stackage LTS version ~a")
1710 (list (upstream-source-version source))
1717 ;;; Source code formatting.
1720 (define (report-tabulations package line line-number)
1721 "Warn about tabulations found in LINE."
1722 (match (string-index line #\tab)
1725 (make-warning package
1726 (G_ "tabulation on line ~a, column ~a")
1727 (list line-number index)
1729 (location (package-file package)
1733 (define (report-trailing-white-space package line line-number)
1734 "Warn about trailing white space in LINE."
1735 (and (not (or (string=? line (string-trim-right line))
1736 (string=? line (string #\page))))
1737 (make-warning package
1738 (G_ "trailing white space on line ~a")
1741 (location (package-file package)
1745 (define (report-long-line package line line-number)
1746 "Emit a warning if LINE is too long."
1747 ;; Note: We don't warn at 80 characters because sometimes hashes and URLs
1748 ;; make it hard to fit within that limit and we want to avoid making too
1750 (and (> (string-length line) 90)
1751 (make-warning package
1752 (G_ "line ~a is way too long (~a characters)")
1753 (list line-number (string-length line))
1755 (location (package-file package)
1759 (define %hanging-paren-rx
1760 (make-regexp "^[[:blank:]]*[()]+[[:blank:]]*$"))
1762 (define (report-lone-parentheses package line line-number)
1763 "Emit a warning if LINE contains hanging parentheses."
1764 (and (regexp-exec %hanging-paren-rx line)
1765 (make-warning package
1766 (G_ "parentheses feel lonely, \
1767 move to the previous or next line")
1770 (location (package-file package)
1774 (define %formatting-reporters
1775 ;; List of procedures that report formatting issues. These are not separate
1776 ;; checkers because they would need to re-read the file.
1777 (list report-tabulations
1778 report-trailing-white-space
1780 report-lone-parentheses))
1782 (define* (report-formatting-issues package file starting-line
1783 #:key (reporters %formatting-reporters))
1784 "Report white-space issues in FILE starting from STARTING-LINE, and report
1786 (define (sexp-last-line port)
1787 ;; Return the last line of the sexp read from PORT or an estimate thereof.
1788 (define &failure (list 'failure))
1790 (let ((start (ftell port))
1791 (start-line (port-line port))
1792 (sexp (catch 'read-error
1793 (lambda () (read port))
1795 (let ((line (port-line port)))
1796 (seek port start SEEK_SET)
1797 (set-port-line! port start-line)
1798 (if (eq? sexp &failure)
1799 (+ start-line 60) ;conservative estimate
1802 (call-with-input-file file
1804 (let loop ((line-number 1)
1807 (let ((line (read-line port)))
1808 (if (or (eof-object? line)
1809 (and last-line (> line-number last-line)))
1811 (if (and (= line-number starting-line)
1813 (loop (+ 1 line-number)
1814 (+ 1 (sexp-last-line port))
1816 (loop (+ 1 line-number)
1820 (if (< line-number starting-line)
1822 (filter-map (lambda (report)
1823 (report package line line-number))
1824 reporters)))))))))))
1826 (define (check-formatting package)
1827 "Check the formatting of the source code of PACKAGE."
1828 (let ((location (package-location package)))
1830 ;; Report issues starting from the line before the 'package'
1831 ;; form, which usually contains the 'define' form.
1832 (let ((line (- (location-line location) 1)))
1833 (match (search-path %load-path (location-file location))
1835 (report-formatting-issues package file line))
1837 ;; It could be that LOCATION lists a "true" relative file
1838 ;; name--i.e., not relative to an element of %LOAD-PATH.
1839 (let ((file (location-file location)))
1840 (if (file-exists? file)
1841 (report-formatting-issues package file line)
1842 (list (make-warning package
1843 (G_ "source file not found"))))))))
1848 ;;; List of checkers.
1851 (define %local-checkers
1855 (description "Validate package names")
1859 (description "Check if tests are explicitly enabled")
1860 (check check-tests-true))
1863 (description "Validate package descriptions")
1864 (check check-description-style))
1866 (name 'inputs-should-be-native)
1867 (description "Identify inputs that should be native inputs")
1868 (check check-inputs-should-be-native))
1870 (name 'inputs-should-not-be-input)
1871 (description "Identify inputs that shouldn't be inputs at all")
1872 (check check-inputs-should-not-be-an-input-at-all))
1874 (name 'input-labels)
1875 (description "Identify input labels that do not match package names")
1876 (check check-input-labels))
1878 (name 'wrapper-inputs)
1879 (description "Make sure 'wrap-program' can finds its interpreter.")
1880 (check check-wrapper-inputs))
1883 ;; TRANSLATORS: <license> is the name of a data type and must not be
1885 (description "Make sure the 'license' field is a <license> \
1887 (check check-license))
1889 (name 'optional-tests)
1890 (description "Make sure tests are only run when requested")
1891 (check check-optional-tests))
1894 (description "Suggest 'mirror://' URLs")
1895 (check check-mirror-url))
1897 (name 'source-file-name)
1898 (description "Validate file names of sources")
1899 (check check-source-file-name))
1901 (name 'source-unstable-tarball)
1902 (description "Check for autogenerated tarballs")
1903 (check check-source-unstable-tarball))
1906 (description "Report failure to compile a package to a derivation")
1907 (check check-derivation)
1908 (requires-store? #t))
1910 (name 'profile-collisions)
1911 (description "Report collisions that would occur due to propagated inputs")
1912 (check check-profile-collisions)
1913 (requires-store? #t))
1915 (name 'patch-file-names)
1916 (description "Validate file names and availability of patches")
1917 (check check-patch-file-names))
1919 (name 'patch-headers)
1920 (description "Validate patch headers")
1921 (check check-patch-headers))
1924 (description "Look for formatting issues in the source")
1925 (check check-formatting))))
1927 (define %network-dependent-checkers
1931 (description "Validate package synopses")
1932 (check check-synopsis-style))
1934 (name 'gnu-description)
1935 (description "Validate synopsis & description of GNU packages")
1936 (check check-gnu-synopsis+description))
1939 (description "Validate home-page URLs")
1940 (check check-home-page))
1943 (description "Validate source URLs")
1944 (check check-source))
1947 (description "Suggest GitHub URLs")
1948 (check check-github-url))
1951 (description "Check the Common Vulnerabilities and Exposures\
1953 (check check-vulnerabilities))
1956 (description "Check the package for new upstream releases")
1957 (check check-for-updates))
1960 (description "Ensure source code archival on Software Heritage")
1961 (check check-archival))
1963 (name 'haskell-stackage)
1964 (description "Ensure Haskell packages use Stackage LTS versions")
1965 (check check-haskell-stackage))))
1967 (define %all-checkers
1968 (append %local-checkers
1969 %network-dependent-checkers))