lint: source: Add check for <svn-reference> over HTTP(S).
[jackhill/guix/guix.git] / guix / lint.scm
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>
17 ;;;
18 ;;; This file is part of GNU Guix.
19 ;;;
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.
24 ;;;
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.
29 ;;;
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/>.
32
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 grafts)
51 #:use-module (guix upstream)
52 #:use-module (guix utils)
53 #:use-module (guix memoization)
54 #:use-module (guix profiles)
55 #:use-module (guix monads)
56 #:use-module (guix scripts)
57 #:use-module ((guix ui) #:select (texi->plain-text fill-paragraph))
58 #:use-module (guix gnu-maintenance)
59 #:use-module (guix cve)
60 #:use-module ((guix swh) #:hide (origin?))
61 #:autoload (guix git-download) (git-reference?
62 git-reference-url git-reference-commit)
63 #:autoload (guix svn-download) (svn-reference?
64 svn-reference-url
65 svn-reference-user-name
66 svn-reference-password)
67 #:use-module (guix import stackage)
68 #:use-module (ice-9 match)
69 #:use-module (ice-9 regex)
70 #:use-module (ice-9 format)
71 #:autoload (rnrs bytevectors) (string->utf8)
72 #:use-module (web client)
73 #:use-module (web uri)
74 #:use-module ((guix build download)
75 #:select (maybe-expand-mirrors
76 (open-connection-for-uri
77 . guix:open-connection-for-uri)))
78 #:use-module (web request)
79 #:use-module (web response)
80 #:autoload (gnutls) (error->string)
81 #:use-module (srfi srfi-1)
82 #:use-module (srfi srfi-6) ;Unicode string ports
83 #:use-module (srfi srfi-9)
84 #:use-module (srfi srfi-11)
85 #:use-module (srfi srfi-26)
86 #:use-module (srfi srfi-34)
87 #:use-module (srfi srfi-35)
88 #:use-module (ice-9 rdelim)
89 #:export (check-description-style
90 check-inputs-should-be-native
91 check-inputs-should-not-be-an-input-at-all
92 check-input-labels
93 check-wrapper-inputs
94 check-patch-file-names
95 check-patch-headers
96 check-synopsis-style
97 check-derivation
98 check-home-page
99 check-name
100 check-source
101 check-source-file-name
102 check-source-unstable-tarball
103 check-optional-tests
104 check-mirror-url
105 check-github-url
106 check-license
107 check-vulnerabilities
108 check-for-updates
109 check-formatting
110 check-archival
111 check-profile-collisions
112 check-haskell-stackage
113 check-tests-true
114
115 lint-warning
116 lint-warning?
117 lint-warning-package
118 lint-warning-message
119 lint-warning-message-text
120 lint-warning-message-data
121 lint-warning-location
122
123 %local-checkers
124 %network-dependent-checkers
125 %all-checkers
126
127 lint-checker
128 lint-checker?
129 lint-checker-name
130 lint-checker-description
131 lint-checker-check
132 lint-checker-requires-store?))
133
134 \f
135 ;;;
136 ;;; Warnings
137 ;;;
138
139 (define-record-type* <lint-warning>
140 lint-warning make-lint-warning
141 lint-warning?
142 (package lint-warning-package)
143 (message-text lint-warning-message-text)
144 (message-data lint-warning-message-data
145 (default '()))
146 (location lint-warning-location
147 (default #f)))
148
149 (define (lint-warning-message warning)
150 (apply format #f
151 (G_ (lint-warning-message-text warning))
152 (lint-warning-message-data warning)))
153
154 (define (package-file package)
155 (location-file
156 (package-location package)))
157
158 (define* (%make-warning package message-text
159 #:optional (message-data '())
160 #:key field location)
161 (make-lint-warning
162 package
163 message-text
164 message-data
165 (or location
166 (and field (package-field-location package field))
167 (package-location package))))
168
169 (define-syntax make-warning
170 (syntax-rules (G_)
171 ((_ package (G_ message) rest ...)
172 (%make-warning package message rest ...))))
173
174 \f
175 ;;;
176 ;;; Procedures for analysing Scheme code in package definitions
177 ;;;
178
179 (define* (find-procedure-body expression found
180 #:key (not-found (const '())))
181 "Try to find the body of the procedure defined inline by EXPRESSION.
182 If it was found, call FOUND with its body. If it wasn't, call
183 the thunk NOT-FOUND."
184 (match expression
185 (`(,(or 'let 'let*) . ,_)
186 (find-procedure-body (car (last-pair expression)) found
187 #:not-found not-found))
188 (`(,(or 'lambda 'lambda*) ,_ . ,code)
189 (found code))
190 (_ (not-found))))
191
192 (define* (report-bogus-phase-deltas package bogus-deltas)
193 "Report a bogus invocation of ‘modify-phases’."
194 (list (make-warning package
195 ;; TRANSLATORS: 'modify-phases' is a Scheme syntax
196 ;; and should not be translated.
197 (G_ "incorrect call to ‘modify-phases’")
198 #:field 'arguments)))
199
200 (define* (find-phase-deltas package found
201 #:key (not-found (const '()))
202 (bogus
203 (cut report-bogus-phase-deltas package <>)))
204 "Try to find the clauses of the ‘modify-phases’ form in the phases
205 specification of PACKAGE. If they were found, all FOUND with a list
206 of the clauses. If they weren't (e.g. because ‘modify-phases’ wasn't
207 used at all), call the thunk NOT-FOUND instead. If ‘modify-phases’
208 was used, but the clauses don't form a list, call BOGUS with the
209 not-a-list."
210 (apply (lambda* (#:key phases #:allow-other-keys)
211 (define phases/sexp
212 (if (gexp? phases)
213 (gexp->approximate-sexp phases)
214 phases))
215 (match phases/sexp
216 (`(modify-phases ,_ . ,changes)
217 ((if (list? changes) found bogus) changes))
218 (_ (not-found))))
219 (package-arguments package)))
220
221 (define (report-bogus-phase-procedure package)
222 "Report a syntactically-invalid phase clause."
223 (list (make-warning package
224 ;; TRANSLATORS: See ‘modify-phases’ in the manual.
225 (G_ "invalid phase clause")
226 #:field 'arguments)))
227
228 (define* (find-phase-procedure package expression found
229 #:key (not-found (const '()))
230 (bogus (cut report-bogus-phase-procedure
231 package)))
232 "Try to find the procedure in the phase clause EXPRESSION. If it was
233 found, call FOUND with the procedure expression. If EXPRESSION isn't
234 actually a phase clause, call the thunk BOGUS. If the phase form doesn't
235 have a procedure, call the thunk NOT-FOUND."
236 (match expression
237 (('add-after before after proc-expr)
238 (found proc-expr))
239 (('add-before after before proc-expr)
240 (found proc-expr))
241 (('replace _ proc-expr)
242 (found proc-expr))
243 (('delete _) (not-found))
244 (_ (bogus))))
245
246 \f
247 ;;;
248 ;;; Checkers
249 ;;;
250
251 (define-record-type* <lint-checker>
252 lint-checker make-lint-checker
253 lint-checker?
254 ;; TODO: add a 'certainty' field that shows how confident we are in the
255 ;; checker. Then allow users to only run checkers that have a certain
256 ;; 'certainty' level.
257 (name lint-checker-name)
258 (description lint-checker-description)
259 (check lint-checker-check)
260 (requires-store? lint-checker-requires-store?
261 (default #f)))
262
263 (define (check-name package)
264 "Check whether PACKAGE's name matches our guidelines."
265 (let ((name (package-name package)))
266 (cond
267 ;; Currently checks only whether the name is too short.
268 ((and (<= (string-length name) 1)
269 (not (string=? name "r"))) ; common-sense exception
270 (list
271 (make-warning package
272 (G_ "name should be longer than a single character")
273 #:field 'name)))
274 ((string-index name #\_)
275 (list
276 (make-warning package
277 (G_ "name should use hyphens instead of underscores")
278 #:field 'name)))
279 (else '()))))
280
281 (define (check-tests-true package)
282 "Check whether PACKAGE explicitly requests to run tests, which is
283 superfluous when building natively and incorrect when cross-compiling."
284 (define (tests-explicitly-enabled?)
285 (apply (lambda* (#:key tests? #:allow-other-keys)
286 (eq? tests? #t))
287 (package-arguments package)))
288 (if (and (tests-explicitly-enabled?)
289 ;; emacs-build-system sets #:tests? #f by default, therefore
290 ;; writing #:tests? #t in package definitions using
291 ;; emacs-build-system is reasonable. Likewise for
292 ;; texlive-build-system.
293 ;;
294 ;; Compare the name of the build system instead of the build system
295 ;; itself to avoid loading unnecessary modules when only a few
296 ;; modules are linted.
297 (not (memq (build-system-name (package-build-system package))
298 '(emacs texlive)))
299 ;; Some packages, e.g. gnutls, set #:tests?
300 ;; differently depending on whether it is being
301 ;; cross-compiled.
302 (parameterize ((%current-target-system "aarch64-linux-gnu"))
303 (tests-explicitly-enabled?)))
304 (list (make-warning package
305 ;; TRANSLATORS: #:tests? and #t are Scheme constants
306 ;; and must not be translated.
307 (G_ "#:tests? must not be explicitly set to #t")
308 #:field 'arguments))
309 '()))
310
311 (define (properly-starts-sentence? s)
312 (string-match "^[(\"'`[:upper:][:digit:]]" s))
313
314 (define (starts-with-abbreviation? s)
315 "Return #t if S starts with what looks like an abbreviation or acronym."
316 (string-match "^[A-Z][A-Z0-9]+\\>" s))
317
318 (define %quoted-identifier-rx
319 ;; A quoted identifier, like 'this'.
320 (make-regexp "['`][[:graph:]]+'"))
321
322 (define (check-description-style package)
323 ;; Emit a warning if stylistic issues are found in the description of PACKAGE.
324 (define (check-not-empty description)
325 (if (string-null? description)
326 (list
327 (make-warning package
328 (G_ "description should not be empty")
329 #:field 'description))
330 '()))
331
332 (define (check-texinfo-markup description)
333 "Check that DESCRIPTION can be parsed as a Texinfo fragment. If the
334 markup is valid return a plain-text version of DESCRIPTION, otherwise #f."
335 (catch #t
336 (lambda () (texi->plain-text description))
337 (lambda (keys . args)
338 (make-warning package
339 (G_ "Texinfo markup in description is invalid")
340 #:field 'description))))
341
342 (define (check-description-typo description typo-corrections)
343 "Check that DESCRIPTION does not contain typo, with optional correction"
344 (append-map
345 (match-lambda
346 ((typo . correction)
347 (if (string-contains description typo)
348 (list
349 (make-warning package
350 (G_
351 (format #false
352 "description contains typo '~a'~@[, should be '~a'~]"
353 typo correction))))
354 '())))
355 typo-corrections))
356
357 (define (check-trademarks description)
358 "Check that DESCRIPTION does not contain '™' or '®' characters. See
359 http://www.gnu.org/prep/standards/html_node/Trademarks.html."
360 (match (string-index description (char-set #\™ #\®))
361 ((and (? number?) index)
362 (list
363 (make-warning package
364 (G_ "description should not contain ~
365 trademark sign '~a' at ~d")
366 (list (string-ref description index) index)
367 #:field 'description)))
368 (else '())))
369
370 (define (check-quotes description)
371 "Check whether DESCRIPTION contains single quotes and suggest @code."
372 (if (regexp-exec %quoted-identifier-rx description)
373 (list
374 (make-warning package
375 ;; TRANSLATORS: '@code' is Texinfo markup and must be kept
376 ;; as is.
377 (G_ "use @code or similar ornament instead of quotes")
378 #:field 'description))
379 '()))
380
381 (define (check-proper-start description)
382 (if (or (string-null? description)
383 (properly-starts-sentence? description)
384 (string-prefix-ci? (package-name package) description))
385 '()
386 (list
387 (make-warning
388 package
389 (G_ "description should start with an upper-case letter or digit")
390 #:field 'description))))
391
392 (define (check-end-of-sentence-space description)
393 "Check that an end-of-sentence period is followed by two spaces."
394 (let ((infractions
395 (reverse (fold-matches
396 "\\. [A-Z]" description '()
397 (lambda (m r)
398 ;; Filter out matches of common abbreviations.
399 (if (find (lambda (s)
400 (string-suffix-ci? s (match:prefix m)))
401 '("i.e" "e.g" "a.k.a" "resp"))
402 r (cons (match:start m) r)))))))
403 (if (null? infractions)
404 '()
405 (list
406 (make-warning package
407 (G_ "sentences in description should be followed ~
408 by two spaces; possible infraction~p at ~{~a~^, ~}")
409 (list (length infractions)
410 infractions)
411 #:field 'description)))))
412
413 (define (check-no-leading-whitespace description)
414 "Check that DESCRIPTION doesn't have trailing whitespace."
415 (if (string-prefix? " " description)
416 (list
417 (make-warning package
418 (G_ "description contains leading whitespace")
419 #:field 'description))
420 '()))
421
422 (define (check-no-trailing-whitespace description)
423 "Check that DESCRIPTION doesn't have trailing whitespace."
424 (if (string-suffix? " " description)
425 (list
426 (make-warning package
427 (G_ "description contains trailing whitespace")
428 #:field 'description))
429 '()))
430
431 (let ((description (package-description package)))
432 (if (string? description)
433 (append
434 (check-not-empty description)
435 (check-quotes description)
436 (check-trademarks description)
437 (check-description-typo description '(("This packages" . "This package")
438 ("This modules" . "This module")
439 ("allows to" . #f)
440 ("permits to" . #f)))
441 ;; Use raw description for this because Texinfo rendering
442 ;; automatically fixes end of sentence space.
443 (check-end-of-sentence-space description)
444 (check-no-leading-whitespace description)
445 (check-no-trailing-whitespace description)
446 (match (check-texinfo-markup description)
447 ((and warning (? lint-warning?)) (list warning))
448 (plain-description
449 (check-proper-start plain-description))))
450 (list
451 (make-warning package
452 (G_ "invalid description: ~s")
453 (list description)
454 #:field 'description)))))
455
456 (define (package-input-intersection inputs-to-check input-names)
457 "Return the intersection between INPUTS-TO-CHECK, the list of input tuples
458 of a package, and INPUT-NAMES, a list of package specifications such as
459 \"glib:bin\"."
460 (match inputs-to-check
461 (((labels packages . outputs) ...)
462 (filter-map (lambda (package output)
463 (and (package? package)
464 (let ((input (string-append
465 (package-name package)
466 (if (> (length output) 0)
467 (string-append ":" (car output))
468 ""))))
469 (and (member input input-names)
470 input))))
471 packages outputs))))
472
473 (define (check-inputs-should-be-native package)
474 ;; Emit a warning if some inputs of PACKAGE are likely to belong to its
475 ;; native inputs.
476 (let ((inputs (append (package-inputs package)
477 (package-propagated-inputs package)))
478 (input-names
479 '("pkg-config"
480 "autoconf"
481 "automake"
482 "bison"
483 "cmake"
484 "dejagnu"
485 "desktop-file-utils"
486 "doxygen"
487 "extra-cmake-modules"
488 "flex"
489 "gettext"
490 "glib:bin"
491 "gobject-introspection"
492 "googletest-source"
493 "groff"
494 "gtk-doc"
495 "help2man"
496 "intltool"
497 "itstool"
498 "kdoctools"
499 "libtool"
500 "m4"
501 "qttools-5"
502 "yasm" "nasm" "fasm"
503 "python-coverage"
504 "python-cython"
505 "python-docutils"
506 "python-mock"
507 "python-nose"
508 "python-pbr"
509 "python-pytest"
510 "python-pytest-cov"
511 "python-setuptools-scm"
512 "python-sphinx"
513 "scdoc"
514 "swig"
515 "qmake"
516 "qttools-5"
517 "texinfo"
518 "xorg-server-for-tests"
519 "yelp-tools")))
520 (map (lambda (input)
521 (make-warning
522 package
523 (G_ "'~a' should probably be a native input")
524 (list input)
525 #:field 'inputs))
526 (package-input-intersection inputs input-names))))
527
528 (define (check-inputs-should-not-be-an-input-at-all package)
529 ;; Emit a warning if some inputs of PACKAGE are likely to should not be
530 ;; an input at all.
531 (let ((input-names '("python-setuptools"
532 "python-pip")))
533 (map (lambda (input)
534 (make-warning
535 package
536 (G_ "'~a' should probably not be an input at all")
537 (list input)
538 #:field 'inputs))
539 (package-input-intersection (package-direct-inputs package)
540 input-names))))
541
542 (define (check-input-labels package)
543 "Emit a warning for labels that differ from the corresponding package name."
544 (define (check input-kind package-inputs)
545 (define (warning label name)
546 (make-warning package
547 (G_ "label '~a' does not match package name '~a'")
548 (list label name)
549 #:field input-kind))
550
551 (append-map (match-lambda
552 (((? string? label) (? package? dependency))
553 (if (string=? label (package-name dependency))
554 '()
555 (list (warning label (package-name dependency)))))
556 (((? string? label) (? package? dependency) output)
557 (let ((expected (string-append (package-name dependency)
558 ":" output)))
559 (if (string=? label expected)
560 '()
561 (list (warning label expected)))))
562 (_
563 '()))
564 (package-inputs package)))
565
566 (append-map (match-lambda
567 ((kind proc)
568 (check kind proc)))
569 `((native-inputs ,package-native-inputs)
570 (inputs ,package-inputs)
571 (propagated-inputs ,package-propagated-inputs))))
572
573 (define (report-wrap-program-error package wrapper-name)
574 "Warn that \"bash-minimal\" is missing from 'inputs', while WRAPPER-NAME
575 requires it."
576 (make-warning package
577 (G_ "\"bash-minimal\" should be in 'inputs' when '~a' is used")
578 (list wrapper-name)))
579
580 (define (check-wrapper-inputs package)
581 "Emit a warning if PACKAGE uses 'wrap-program' or similar, but \"bash\"
582 or \"bash-minimal\" is not in its inputs. 'wrap-script' is not supported."
583 (define input-names '("bash" "bash-minimal"))
584 (define has-bash-input?
585 (pair? (package-input-intersection (package-inputs package)
586 input-names)))
587 (define (check-procedure-body body)
588 (match body
589 ;; Explicitely setting an interpreter is acceptable.
590 (('wrap-program _ '#:sh . _) '())
591 (('wrap-program _ . _)
592 (list (report-wrap-program-error package 'wrap-program)))
593 ;; Wrapper of 'wrap-program' for Qt programs.
594 ;; TODO #:sh is not yet supported but probably will be.
595 (('wrap-qt-program _ '#:sh . _) '())
596 (('wrap-qt-program _ . _)
597 (list (report-wrap-program-error package 'wrap-qt-program)))
598 ((x . y)
599 (append (check-procedure-body x) (check-procedure-body y)))
600 (_ '())))
601 (define (check-phase-procedure expression)
602 (find-procedure-body expression check-procedure-body))
603 (define (check-delta expression)
604 (find-phase-procedure package expression check-phase-procedure))
605 (define (check-deltas deltas)
606 (append-map check-delta deltas))
607 (if has-bash-input?
608 ;; "bash" (or "bash-minimal") is in 'inputs', so everything seems ok.
609 '()
610 ;; "bash" is not in 'inputs'. Verify 'wrap-program' and friends
611 ;; are unused
612 (find-phase-deltas package check-deltas)))
613
614 (define (package-name-regexp package)
615 "Return a regexp that matches PACKAGE's name as a word at the beginning of a
616 line."
617 (make-regexp (string-append "^" (regexp-quote (package-name package))
618 "\\>")
619 regexp/icase))
620
621 (define (check-synopsis-style package)
622 ;; Emit a warning if stylistic issues are found in the synopsis of PACKAGE.
623 (define (check-final-period synopsis)
624 ;; Synopsis should not end with a period, except for some special cases.
625 (if (and (string-suffix? "." synopsis)
626 (not (string-suffix? "etc." synopsis)))
627 (list
628 (make-warning package
629 (G_ "no period allowed at the end of the synopsis")
630 #:field 'synopsis))
631 '()))
632
633 (define check-start-article
634 ;; Skip this check for GNU packages, as suggested by Karl Berry's reply to
635 ;; <http://lists.gnu.org/archive/html/bug-womb/2014-11/msg00000.html>.
636 (if (false-if-exception (gnu-package? package))
637 (const '())
638 (lambda (synopsis)
639 (if (or (string-prefix-ci? "A " synopsis)
640 (string-prefix-ci? "An " synopsis))
641 (list
642 (make-warning package
643 (G_ "no article allowed at the beginning of \
644 the synopsis")
645 #:field 'synopsis))
646 '()))))
647
648 (define (check-synopsis-length synopsis)
649 (if (>= (string-length synopsis) 80)
650 (list
651 (make-warning package
652 (G_ "synopsis should be less than 80 characters long")
653 #:field 'synopsis))
654 '()))
655
656 (define (check-proper-start synopsis)
657 (if (properly-starts-sentence? synopsis)
658 '()
659 (list
660 (make-warning package
661 (G_ "synopsis should start with an upper-case letter or digit")
662 #:field 'synopsis))))
663
664 (define (check-start-with-package-name synopsis)
665 (if (and (regexp-exec (package-name-regexp package) synopsis)
666 (not (starts-with-abbreviation? synopsis)))
667 (list
668 (make-warning package
669 (G_ "synopsis should not start with the package name")
670 #:field 'synopsis))
671 '()))
672
673 (define (check-texinfo-markup synopsis)
674 "Check that SYNOPSIS can be parsed as a Texinfo fragment. If the
675 markup is valid return a plain-text version of SYNOPSIS, otherwise #f."
676 (catch #t
677 (lambda ()
678 (texi->plain-text synopsis)
679 '())
680 (lambda (keys . args)
681 (list
682 (make-warning package
683 (G_ "Texinfo markup in synopsis is invalid")
684 #:field 'synopsis)))))
685
686 (define (check-no-trailing-whitespace synopsis)
687 "Check that SYNOPSIS doesn't have trailing whitespace."
688 (if (string-suffix? " " synopsis)
689 (list
690 (make-warning package
691 (G_ "synopsis contains trailing whitespace")
692 #:field 'synopsis))
693 '()))
694
695 (define checks
696 (list check-proper-start
697 check-final-period
698 check-start-article
699 check-start-with-package-name
700 check-synopsis-length
701 check-texinfo-markup
702 check-no-trailing-whitespace))
703
704 (match (package-synopsis package)
705 (""
706 (list
707 (make-warning package
708 (G_ "synopsis should not be empty")
709 #:field 'synopsis)))
710 ((? string? synopsis)
711 (append-map
712 (lambda (proc)
713 (proc synopsis))
714 checks))
715 (invalid
716 (list
717 (make-warning package
718 (G_ "invalid synopsis: ~s")
719 (list invalid)
720 #:field 'synopsis)))))
721
722 (define* (probe-uri uri #:key timeout)
723 "Probe URI, a URI object, and return two values: a symbol denoting the
724 probing status, such as 'http-response' when we managed to get an HTTP
725 response from URI, and additional details, such as the actual HTTP response.
726
727 TIMEOUT is the maximum number of seconds (possibly an inexact number) to wait
728 for connections to complete; when TIMEOUT is #f, wait as long as needed."
729 (define headers
730 `((User-Agent . "GNU Guile")
731 (Accept . "*/*")
732 ,@(match (uri-userinfo uri)
733 ((? string? str) ;"basic authentication"
734 `((Authorization . ,(string-append "Basic "
735 (base64-encode
736 (string->utf8 str))))))
737 (_ '()))))
738
739 (let loop ((uri uri)
740 (visited '()))
741 (match (uri-scheme uri)
742 ((or 'http 'https)
743 (catch #t
744 (lambda ()
745 (let ((port (guix:open-connection-for-uri
746 uri #:timeout timeout))
747 (request (build-request uri #:headers headers)))
748 (define response
749 (dynamic-wind
750 (const #f)
751 (lambda ()
752 (write-request request port)
753 (force-output port)
754 (read-response port))
755 (lambda ()
756 (close-port port))))
757
758 (case (response-code response)
759 ((302 ; found (redirection)
760 303 ; see other
761 307 ; temporary redirection
762 308) ; permanent redirection
763 (let ((location (response-location response)))
764 (if (or (not location) (member location visited))
765 (values 'http-response response)
766 (loop location (cons location visited))))) ;follow the redirect
767 ((301) ; moved permanently
768 (let ((location (response-location response)))
769 ;; Return RESPONSE, unless the final response as we follow
770 ;; redirects is not 200.
771 (if location
772 (let-values (((status response2)
773 (loop location (cons location visited))))
774 (case status
775 ((http-response)
776 (values 'http-response
777 (if (= 200 (response-code response2))
778 response
779 response2)))
780 (else
781 (values status response2))))
782 (values 'http-response response)))) ;invalid redirect
783 (else
784 (values 'http-response response)))))
785 (lambda (key . args)
786 (case key
787 ((bad-header bad-header-component)
788 ;; This can happen if the server returns an invalid HTTP header,
789 ;; as is the case with the 'Date' header at sqlite.org.
790 (values 'invalid-http-response #f))
791 ((getaddrinfo-error system-error
792 gnutls-error tls-certificate-error)
793 (values key args))
794 (else
795 (apply throw key args))))))
796 ('ftp
797 (catch #t
798 (lambda ()
799 (let ((conn (ftp-open (uri-host uri) #:timeout timeout)))
800 (define response
801 (dynamic-wind
802 (const #f)
803 (lambda ()
804 (ftp-chdir conn (dirname (uri-path uri)))
805 (ftp-size conn (basename (uri-path uri))))
806 (lambda ()
807 (ftp-close conn))))
808 (values 'ftp-response '(ok))))
809 (lambda (key . args)
810 (case key
811 ((ftp-error)
812 (values 'ftp-response `(error ,@args)))
813 ((getaddrinfo-error system-error gnutls-error)
814 (values key args))
815 (else
816 (apply throw key args))))))
817 (_
818 (values 'unknown-protocol #f)))))
819
820 (define (call-with-networking-fail-safe message error-value proc)
821 "Call PROC catching any network-related errors. Upon a networking error,
822 display a message including MESSAGE and return ERROR-VALUE."
823 (guard (c ((http-get-error? c)
824 (warning (G_ "~a: HTTP GET error for ~a: ~a (~s)~%")
825 message
826 (uri->string (http-get-error-uri c))
827 (http-get-error-code c)
828 (http-get-error-reason c))
829 error-value))
830 (catch #t
831 proc
832 (match-lambda*
833 (('getaddrinfo-error errcode)
834 (warning (G_ "~a: host lookup failure: ~a~%")
835 message
836 (gai-strerror errcode))
837 error-value)
838 (('tls-certificate-error args ...)
839 (warning (G_ "~a: TLS certificate error: ~a")
840 message
841 (tls-certificate-error-string args))
842 error-value)
843 (('gnutls-error error function _ ...)
844 (warning (G_ "~a: TLS error in '~a': ~a~%")
845 message
846 function (error->string error))
847 error-value)
848 ((and ('system-error _ ...) args)
849 (let ((errno (system-error-errno args)))
850 (if (member errno (list ECONNRESET ECONNABORTED ECONNREFUSED))
851 (let ((details (call-with-output-string
852 (lambda (port)
853 (print-exception port #f (car args)
854 (cdr args))))))
855 (warning (G_ "~a: ~a~%") message details)
856 error-value)
857 (apply throw args))))
858 (args
859 (apply throw args))))))
860
861 (define-syntax-rule (with-networking-fail-safe message error-value exp ...)
862 (call-with-networking-fail-safe message error-value
863 (lambda () exp ...)))
864
865 (define (tls-certificate-error-string args)
866 "Return a string explaining the 'tls-certificate-error' arguments ARGS."
867 (call-with-output-string
868 (lambda (port)
869 (print-exception port #f
870 'tls-certificate-error args))))
871
872 (define (validate-uri uri package field)
873 "Return #t if the given URI can be reached, otherwise return a warning for
874 PACKAGE mentioning the FIELD."
875 (let-values (((status argument)
876 (probe-uri uri #:timeout 3))) ;wait at most 3 seconds
877 (case status
878 ((http-response)
879 (cond ((= 200 (response-code argument))
880 (match (response-content-length argument)
881 ((? number? length)
882 ;; As of July 2016, SourceForge returns 200 (instead of 404)
883 ;; with a small HTML page upon failure. Attempt to detect
884 ;; such malicious behavior.
885 (or (> length 1000)
886 (make-warning package
887 (G_ "URI ~a returned \
888 suspiciously small file (~a bytes)")
889 (list (uri->string uri)
890 length)
891 #:field field)))
892 (_ #t)))
893 ((= 301 (response-code argument))
894 (if (response-location argument)
895 (make-warning package
896 (G_ "permanent redirect from ~a to ~a")
897 (list (uri->string uri)
898 (uri->string
899 (response-location argument)))
900 #:field field)
901 (make-warning package
902 (G_ "invalid permanent redirect \
903 from ~a")
904 (list (uri->string uri))
905 #:field field)))
906 (else
907 (make-warning package
908 (G_ "URI ~a not reachable: ~a (~s)")
909 (list (uri->string uri)
910 (response-code argument)
911 (response-reason-phrase argument))
912 #:field field))))
913 ((ftp-response)
914 (match argument
915 (('ok) #t)
916 (('error port command code message)
917 (make-warning package
918 (G_ "URI ~a not reachable: ~a (~s)")
919 (list (uri->string uri)
920 code (string-trim-both message))
921 #:field field))))
922 ((getaddrinfo-error)
923 (make-warning package
924 (G_ "URI ~a domain not found: ~a")
925 (list (uri->string uri)
926 (gai-strerror (car argument)))
927 #:field field))
928 ((system-error)
929 (make-warning package
930 (G_ "URI ~a unreachable: ~a")
931 (list (uri->string uri)
932 (strerror
933 (system-error-errno
934 (cons status argument))))
935 #:field field))
936 ((tls-certificate-error)
937 (make-warning package
938 (G_ "TLS certificate error: ~a")
939 (list (tls-certificate-error-string argument))
940 #:field field))
941 ((invalid-http-response gnutls-error)
942 ;; Probably a misbehaving server; ignore.
943 #f)
944 ((unknown-protocol) ;nothing we can do
945 #f)
946 (else
947 (error "internal linter error" status)))))
948
949 (define (check-home-page package)
950 "Emit a warning if PACKAGE has an invalid 'home-page' field, or if that
951 'home-page' is not reachable."
952 (let ((uri (and=> (package-home-page package) string->uri)))
953 (cond
954 ((uri? uri)
955 (match (validate-uri uri package 'home-page)
956 ((and (? lint-warning? warning) warning)
957 (list warning))
958 (_ '())))
959 ((not (package-home-page package))
960 (if (or (string-contains (package-name package) "bootstrap")
961 (string=? (package-name package) "ld-wrapper"))
962 '()
963 (list
964 (make-warning package
965 (G_ "invalid value for home page")
966 #:field 'home-page))))
967 (else
968 (list
969 (make-warning package
970 (G_ "invalid home page URL: ~s")
971 (list (package-home-page package))
972 #:field 'home-page))))))
973
974 (define %distro-directory
975 (mlambda ()
976 (dirname (search-path %load-path "gnu.scm"))))
977
978 (define (check-patch-file-names package)
979 "Emit a warning if the patches requires by PACKAGE are badly named or if the
980 patch could not be found."
981 (guard (c ((formatted-message? c) ;raised by 'search-patch'
982 (list (%make-warning package
983 (formatted-message-string c)
984 (formatted-message-arguments c)
985 #:field 'source))))
986 (define patches
987 (match (package-source package)
988 ((? origin? origin) (origin-patches origin))
989 (_ '())))
990
991 (define (starts-with-package-name? file-name)
992 (and=> (string-contains file-name (package-name package))
993 zero?))
994
995 (append
996 (if (every (match-lambda ;patch starts with package name?
997 ((? string? patch)
998 (starts-with-package-name? (basename patch)))
999 ((? origin? patch)
1000 (starts-with-package-name? (origin-actual-file-name patch)))
1001 (_ #f)) ;must be some other file-like object
1002 patches)
1003 '()
1004 (list
1005 (make-warning
1006 package
1007 (G_ "file names of patches should start with the package name")
1008 #:field 'patch-file-names)))
1009
1010 ;; Check whether we're reaching tar's maximum file name length.
1011 (let ((prefix (string-length (%distro-directory)))
1012 ;; Margin approximating the largest path that "make dist" might
1013 ;; create, with a release candidate version, 123456 commits, and
1014 ;; git commit hash abcde0.
1015 (margin (string-length "guix-92.0.0rc3-123456-abcde0/"))
1016 ;; Tested maximum patch file length for ustar format.
1017 (max 151))
1018 (filter-map (match-lambda
1019 ((? string? patch)
1020 (if (> (+ margin (if (string-prefix? (%distro-directory)
1021 patch)
1022 (- (string-length patch) prefix)
1023 (string-length patch)))
1024 max)
1025 (make-warning
1026 package
1027 (G_ "~a: file name is too long, which may break 'make dist'")
1028 (list (basename patch))
1029 #:field 'patch-file-names)
1030 #f))
1031 (_ #f))
1032 patches)))))
1033
1034 (define (check-patch-headers package)
1035 "Check that PACKAGE's patches start with a comment. Return a list of
1036 warnings."
1037 (define (blank? str)
1038 (string-every char-set:blank str))
1039
1040 (define (patch-header-warnings patch)
1041 (call-with-input-file patch
1042 (lambda (port)
1043 ;; Read from PORT until a non-blank line is found or EOF is reached.
1044 (let loop ()
1045 (let ((line (read-line port)))
1046 (cond ((eof-object? line)
1047 (list (make-warning package
1048 (G_ "~a: empty patch")
1049 (list (basename patch))
1050 #:field 'source)))
1051 ((blank? line)
1052 (loop))
1053 ((or (string-prefix? "--- " line)
1054 (string-prefix? "+++ " line)
1055 (string-prefix? "diff --git " line))
1056 (list (make-warning package
1057 (G_ "~a: patch lacks comment and \
1058 upstream status")
1059 (list (basename patch))
1060 #:field 'source)))
1061 (else
1062 '())))))))
1063
1064 (guard (c ((formatted-message? c) ;raised by 'search-patch'
1065 (list (%make-warning package
1066 (formatted-message-string c)
1067 (formatted-message-arguments c)
1068 #:field 'source))))
1069 (let ((patches (if (origin? (package-source package))
1070 (origin-patches (package-source package))
1071 '())))
1072 (append-map (lambda (patch)
1073 ;; Dismiss PATCH if it's an origin or similar.
1074 (cond ((string? patch)
1075 (patch-header-warnings patch))
1076 ((local-file? patch)
1077 (patch-header-warnings
1078 (local-file-absolute-file-name patch)))
1079 (else
1080 '())))
1081 patches))))
1082
1083 (define (escape-quotes str)
1084 "Replace any quote character in STR by an escaped quote character."
1085 (list->string
1086 (string-fold-right (lambda (chr result)
1087 (match chr
1088 (#\" (cons* #\\ #\"result))
1089 (_ (cons chr result))))
1090 '()
1091 str)))
1092
1093 (define official-gnu-packages*
1094 (mlambda ()
1095 "A memoizing version of 'official-gnu-packages' that returns the empty
1096 list when something goes wrong, such as a networking issue."
1097 (let ((gnus (false-if-exception (official-gnu-packages))))
1098 (or gnus '()))))
1099
1100 (define (check-gnu-synopsis+description package)
1101 "Make sure that, if PACKAGE is a GNU package, it uses the synopsis and
1102 descriptions maintained upstream."
1103 (match (find (lambda (descriptor)
1104 (string=? (gnu-package-name descriptor)
1105 (package-name package)))
1106 (official-gnu-packages*))
1107 (#f ;not a GNU package, so nothing to do
1108 '())
1109 (descriptor ;a genuine GNU package
1110 (append
1111 (let ((upstream (gnu-package-doc-summary descriptor))
1112 (downstream (package-synopsis package)))
1113 (if (and upstream
1114 (or (not (string? downstream))
1115 (not (string=? upstream downstream))))
1116 (list
1117 (make-warning package
1118 (G_ "proposed synopsis: ~s~%")
1119 (list upstream)
1120 #:field 'synopsis))
1121 '()))
1122
1123 (let ((upstream (gnu-package-doc-description descriptor))
1124 (downstream (package-description package)))
1125 (if (and upstream
1126 (or (not (string? downstream))
1127 (not (string=? (fill-paragraph upstream 100)
1128 (fill-paragraph downstream 100)))))
1129 (list
1130 (make-warning
1131 package
1132 (G_ "proposed description:~% \"~a\"~%")
1133 (list (fill-paragraph (escape-quotes upstream) 77 7))
1134 #:field 'description))
1135 '()))))))
1136
1137 (define (origin-uris origin)
1138 "Return the list of URIs (strings) for ORIGIN."
1139 (match (origin-uri origin)
1140 ((? string? uri)
1141 (list uri))
1142 ((uris ...)
1143 uris)))
1144
1145 (define (svn-reference-uri-with-userinfo ref)
1146 "Return the URI of REF, an <svn-reference> object, but with an additional
1147 'userinfo' part corresponding to REF's user name and password, provided REF's
1148 URI is HTTP or HTTPS."
1149 (let ((uri (string->uri (svn-reference-url ref))))
1150 (if (and (svn-reference-user-name ref)
1151 (memq (uri-scheme uri) '(http https)))
1152 (build-uri (uri-scheme uri)
1153 #:userinfo
1154 (string-append (svn-reference-user-name ref)
1155 (if (svn-reference-password ref)
1156 (string-append
1157 ":" (svn-reference-password ref))
1158 ""))
1159 #:host (uri-host uri)
1160 #:port (uri-port uri)
1161 #:query (uri-query uri)
1162 #:fragment (uri-fragment uri))
1163 uri)))
1164
1165 (define (check-source package)
1166 "Emit a warning if PACKAGE has an invalid 'source' field, or if that
1167 'source' is not reachable."
1168 (define (warnings-for-uris uris)
1169 (let loop ((uris uris)
1170 (warnings '()))
1171 (match uris
1172 (()
1173 (reverse warnings))
1174 ((uri rest ...)
1175 (match (validate-uri uri package 'source)
1176 (#t
1177 ;; We found a working URL, so stop right away.
1178 '())
1179 (#f
1180 ;; Unsupported URL or other error, skip.
1181 (loop rest warnings))
1182 ((? lint-warning? warning)
1183 (loop rest (cons warning warnings))))))))
1184
1185 (let ((origin (package-source package)))
1186 (if (origin? origin)
1187 (cond
1188 ((eq? (origin-method origin) url-fetch)
1189 (let* ((uris (append-map (cut maybe-expand-mirrors <> %mirrors)
1190 (map string->uri (origin-uris origin))))
1191 (warnings (warnings-for-uris uris)))
1192
1193 ;; Just make sure that at least one of the URIs is valid.
1194 (if (= (length uris) (length warnings))
1195 ;; When everything fails, report all of WARNINGS, otherwise don't
1196 ;; report anything.
1197 ;;
1198 ;; XXX: Ideally we'd still allow warnings to be raised if *some*
1199 ;; URIs are unreachable, but distinguish that from the error case
1200 ;; where *all* the URIs are unreachable.
1201 (cons*
1202 (make-warning package
1203 (G_ "all the source URIs are unreachable:")
1204 #:field 'source)
1205 warnings)
1206 '())))
1207 ((git-reference? (origin-uri origin))
1208 (warnings-for-uris
1209 (list (string->uri (git-reference-url (origin-uri origin))))))
1210 ((svn-reference? (origin-uri origin))
1211 (let ((uri (svn-reference-uri-with-userinfo (origin-uri origin))))
1212 (if (memq (uri-scheme uri) '(http https))
1213 (warnings-for-uris (list uri))
1214 '()))) ;TODO: handle svn:// URLs
1215 (else
1216 '()))
1217 '())))
1218
1219 (define (check-source-file-name package)
1220 "Emit a warning if PACKAGE's origin has no meaningful file name."
1221 (define (origin-file-name-valid? origin)
1222 ;; Return #f if the source file name contains only a version or is #f;
1223 ;; indicates that the origin needs a 'file-name' field.
1224 (let ((file-name (origin-actual-file-name origin))
1225 (version (package-version package)))
1226 (and file-name
1227 ;; Common in many projects is for the filename to start
1228 ;; with a "v" followed by the version,
1229 ;; e.g. "v3.2.0.tar.gz".
1230 (not (string-match (string-append "^v?" version) file-name)))))
1231
1232 (let ((origin (package-source package)))
1233 (if (or (not (origin? origin)) (origin-file-name-valid? origin))
1234 '()
1235 (list
1236 (make-warning package
1237 (G_ "the source file name should contain the package name")
1238 #:field 'source)))))
1239
1240 (define (check-source-unstable-tarball package)
1241 "Emit a warning if PACKAGE's source is an autogenerated tarball."
1242 (define (check-source-uri uri)
1243 (if (and (string=? (uri-host (string->uri uri)) "github.com")
1244 (match (split-and-decode-uri-path
1245 (uri-path (string->uri uri)))
1246 ((_ _ "archive" _ ...) #t)
1247 (_ #f)))
1248 (make-warning package
1249 (G_ "the source URI should not be an autogenerated tarball")
1250 #:field 'source)
1251 #f))
1252
1253 (let ((origin (package-source package)))
1254 (if (and (origin? origin)
1255 (eqv? (origin-method origin) url-fetch))
1256 (filter-map check-source-uri
1257 (origin-uris origin))
1258 '())))
1259
1260 (define (check-mirror-url package)
1261 "Check whether PACKAGE uses source URLs that should be 'mirror://'."
1262 (define (check-mirror-uri uri)
1263 (define rewritten-uri
1264 (uri-mirror-rewrite uri))
1265
1266 (and (not (string=? uri rewritten-uri))
1267 (make-warning package (G_ "URL should be '~a'")
1268 (list rewritten-uri)
1269 #:field 'source)))
1270
1271 (let ((origin (package-source package)))
1272 (if (and (origin? origin)
1273 (eqv? (origin-method origin) url-fetch))
1274 (let ((uris (origin-uris origin)))
1275 (filter-map check-mirror-uri uris))
1276 '())))
1277
1278 (define* (check-github-url package #:key (timeout 3))
1279 "Check whether PACKAGE uses source URLs that redirect to GitHub."
1280 (define (follow-redirect url)
1281 (let* ((uri (string->uri url))
1282 (port (guix:open-connection-for-uri uri #:timeout timeout))
1283 (response (http-head uri #:port port)))
1284 (close-port port)
1285 (case (response-code response)
1286 ((301 302)
1287 (uri->string (assoc-ref (response-headers response) 'location)))
1288 (else #f))))
1289
1290 (define (follow-redirects-to-github uri)
1291 (cond
1292 ((string-prefix? "https://github.com/" uri) uri)
1293 ((string-prefix? "http" uri)
1294 (and=> (follow-redirect uri) follow-redirects-to-github))
1295 ;; Do not attempt to follow redirects on URIs other than http and https
1296 ;; (such as mirror, file)
1297 (else #f)))
1298
1299 (let ((origin (package-source package)))
1300 (if (and (origin? origin)
1301 (eqv? (origin-method origin) url-fetch))
1302 (filter-map
1303 (lambda (uri)
1304 (and=> (with-networking-fail-safe
1305 (format #f (G_ "while accessing '~a'") uri)
1306 #f
1307 (follow-redirects-to-github uri))
1308 (lambda (github-uri)
1309 (and (not (string=? github-uri uri))
1310 (make-warning
1311 package
1312 (G_ "URL should be '~a'")
1313 (list github-uri)
1314 #:field 'source)))))
1315 (origin-uris origin))
1316 '())))
1317
1318 ;; Guile 3.0.0 does not export this predicate.
1319 (define exception-with-kind-and-args?
1320 (exception-predicate &exception-with-kind-and-args))
1321
1322 (define (check-optional-tests package)
1323 "Emit a warning if the test suite is run unconditionally."
1324 (define (sexp-contains-atom? sexp atom)
1325 "Test if SEXP contains ATOM."
1326 (if (pair? sexp)
1327 (or (sexp-contains-atom? (car sexp) atom)
1328 (sexp-contains-atom? (cdr sexp) atom))
1329 (eq? sexp atom)))
1330 (define (sexp-uses-tests?? sexp)
1331 "Test if SEXP contains the symbol 'tests?'."
1332 (sexp-contains-atom? sexp 'tests?))
1333 (define (check-procedure-body code)
1334 (if (sexp-uses-tests?? code)
1335 '()
1336 (list (make-warning package
1337 ;; TRANSLATORS: check and #:tests? are a
1338 ;; Scheme symbol and keyword respectively
1339 ;; and should not be translated.
1340 (G_ "the 'check' phase should respect #:tests?")
1341 #:field 'arguments))))
1342 (define (check-check-procedure expression)
1343 (find-procedure-body expression check-procedure-body))
1344 (define (check-phases-delta delta)
1345 (match delta
1346 (`(replace 'check ,expression)
1347 (check-check-procedure expression))
1348 (_ '())))
1349 (define (check-phases-deltas deltas)
1350 (append-map check-phases-delta deltas))
1351 (find-phase-deltas package check-phases-deltas))
1352
1353 (define* (check-derivation package #:key store)
1354 "Emit a warning if we fail to compile PACKAGE to a derivation."
1355 (define (try store system)
1356 (guard (c ((store-protocol-error? c)
1357 (make-warning package
1358 (G_ "failed to create ~a derivation: ~a")
1359 (list system
1360 (store-protocol-error-message c))))
1361 ((exception-with-kind-and-args? c)
1362 (make-warning package
1363 (G_ "failed to create ~a derivation: ~s")
1364 (list system
1365 (cons (exception-kind c)
1366 (exception-args c)))))
1367 ((message-condition? c)
1368 (make-warning package
1369 (G_ "failed to create ~a derivation: ~a")
1370 (list system
1371 (condition-message c))))
1372 ((formatted-message? c)
1373 (let ((str (apply format #f
1374 (formatted-message-string c)
1375 (formatted-message-arguments c))))
1376 (make-warning package
1377 (G_ "failed to create ~a derivation: ~a")
1378 (list system str))))
1379 (else
1380 (make-warning package
1381 (G_ "failed to create ~a derivation: ~a")
1382 (list system c))))
1383 (parameterize ((%graft? #f))
1384 (package-derivation store package system #:graft? #f)
1385
1386 ;; If there's a replacement, make sure we can compute its
1387 ;; derivation.
1388 (match (package-replacement package)
1389 (#f #t)
1390 (replacement
1391 (package-derivation store replacement system
1392 #:graft? #f))))))
1393
1394 (define (check-with-store store)
1395 (filter lint-warning?
1396 (map (cut try store <>) (package-supported-systems package))))
1397
1398 ;; For backwards compatability, don't rely on store being set
1399 (or (and=> store check-with-store)
1400 (with-store store
1401 (check-with-store store))))
1402
1403 (define* (check-profile-collisions package #:key store)
1404 "Check for collisions that would occur when installing PACKAGE as a result
1405 of the propagated inputs it pulls in."
1406 (define (do-check store)
1407 (guard (c ((profile-collision-error? c)
1408 (let ((first (profile-collision-error-entry c))
1409 (second (profile-collision-error-conflict c)))
1410 (define format
1411 (if (string=? (manifest-entry-version first)
1412 (manifest-entry-version second))
1413 manifest-entry-item
1414 (lambda (entry)
1415 (string-append (manifest-entry-name entry) "@"
1416 (manifest-entry-version entry)))))
1417
1418 (list (make-warning package
1419 (G_ "propagated inputs ~a and ~a collide")
1420 (list (format first)
1421 (format second)))))))
1422 ;; Disable grafts to avoid building PACKAGE and its dependencies.
1423 (parameterize ((%graft? #f))
1424 (run-with-store store
1425 (mbegin %store-monad
1426 (check-for-collisions (packages->manifest (list package))
1427 (%current-system))
1428 (return '()))))))
1429
1430 (if store
1431 (do-check store)
1432 (with-store store
1433 (do-check store))))
1434
1435 (define (check-license package)
1436 "Warn about type errors of the 'license' field of PACKAGE."
1437 (match (package-license package)
1438 ((or (? license?)
1439 ((? license?) ...))
1440 '())
1441 (x
1442 (list
1443 (make-warning package (G_ "invalid license field")
1444 #:field 'license)))))
1445
1446 (define (current-vulnerabilities*)
1447 "Like 'current-vulnerabilities', but return the empty list upon networking
1448 or HTTP errors. This allows network-less operation and makes problems with
1449 the NIST server non-fatal."
1450 (with-networking-fail-safe (G_ "while retrieving CVE vulnerabilities")
1451 '()
1452 (current-vulnerabilities #:timeout 4)))
1453
1454 (define package-vulnerabilities
1455 (let ((lookup (delay (vulnerabilities->lookup-proc
1456 (current-vulnerabilities*)))))
1457 (lambda (package)
1458 "Return a list of vulnerabilities affecting PACKAGE."
1459 ;; First we retrieve the Common Platform Enumeration (CPE) name and
1460 ;; version for PACKAGE, then we can pass them to LOOKUP.
1461 (let ((name (or (assoc-ref (package-properties package)
1462 'cpe-name)
1463 (package-name package)))
1464 (version (or (assoc-ref (package-properties package)
1465 'cpe-version)
1466 (package-version package))))
1467 ((force lookup) name version)))))
1468
1469 (define* (check-vulnerabilities package
1470 #:optional (package-vulnerabilities
1471 package-vulnerabilities))
1472 "Check for known vulnerabilities for PACKAGE. Obtain the list of
1473 vulnerability records for PACKAGE by calling PACKAGE-VULNERABILITIES."
1474 (let ((package (or (package-replacement package) package)))
1475 (match (package-vulnerabilities package)
1476 (()
1477 '())
1478 ((vulnerabilities ...)
1479 (let* ((patched (package-patched-vulnerabilities package))
1480 (known-safe (or (assq-ref (package-properties package)
1481 'lint-hidden-cve)
1482 '()))
1483 (unpatched (remove (lambda (vuln)
1484 (let ((id (vulnerability-id vuln)))
1485 (or (member id patched)
1486 (member id known-safe))))
1487 vulnerabilities)))
1488 (if (null? unpatched)
1489 '()
1490 (list
1491 (make-warning
1492 package
1493 (G_ "probably vulnerable to ~a")
1494 (list (string-join (map vulnerability-id unpatched)
1495 ", "))))))))))
1496
1497 (define (check-for-updates package)
1498 "Check if there is an update available for PACKAGE."
1499 (match (lookup-updater package)
1500 (#f
1501 (list (make-warning package (G_ "no updater for ~a")
1502 (list (package-name package))
1503 #:field 'source)))
1504 ((? upstream-updater? updater)
1505 (match (with-networking-fail-safe
1506 (format #f (G_ "while retrieving upstream info for '~a'")
1507 (package-name package))
1508 #f
1509 (package-latest-release package))
1510 ((? upstream-source? source)
1511 (if (version>? (upstream-source-version source)
1512 (package-version package))
1513 (list
1514 (make-warning package
1515 (G_ "can be upgraded to ~a")
1516 (list (upstream-source-version source))
1517 #:field 'version))
1518 '()))
1519 (#f ;cannot find upstream release
1520 (list (make-warning package
1521 (G_ "updater '~a' failed to find \
1522 upstream releases")
1523 (list (upstream-updater-name updater))
1524 #:field 'source)))))))
1525
1526
1527 (define (lookup-disarchive-spec hash)
1528 "If Disarchive mirrors have a spec for HASH, return the list of SWH
1529 directory identifiers the spec refers to. Otherwise return #f."
1530 (define (extract-swh-id spec)
1531 ;; Return the list of SWH directory identifiers SPEC refers to, where SPEC
1532 ;; is a Disarchive sexp. Instead of attempting to parse it, traverse it
1533 ;; in a pretty unintelligent fashion.
1534 (let loop ((sexp spec)
1535 (ids '()))
1536 (match sexp
1537 ((? string? str)
1538 (let ((prefix "swh:1:dir:"))
1539 (if (string-prefix? prefix str)
1540 (cons (string-drop str (string-length prefix)) ids)
1541 ids)))
1542 ((head tail ...)
1543 (loop tail (loop head ids)))
1544 (_ ids))))
1545
1546 (any (lambda (mirror)
1547 (with-networking-fail-safe
1548 (format #f (G_ "failed to access Disarchive database at ~a")
1549 mirror)
1550 #f
1551 (guard (c ((http-get-error? c) #f))
1552 (let* ((url (string-append mirror
1553 (symbol->string
1554 (content-hash-algorithm hash))
1555 "/"
1556 (bytevector->base16-string
1557 (content-hash-value hash))))
1558 (port (http-fetch (string->uri url) #:text? #t))
1559 (spec (read port)))
1560 (close-port port)
1561 (extract-swh-id spec)))))
1562 %disarchive-mirrors))
1563
1564 (define (check-archival package)
1565 "Check whether PACKAGE's source code is archived on Software Heritage. If
1566 it's not, and if its source code is a VCS snapshot, then send a \"save\"
1567 request to Software Heritage.
1568
1569 Software Heritage imposes limits on the request rate per client IP address.
1570 This checker prints a notice and stops doing anything once that limit has been
1571 reached."
1572 (define (response->warning url method response)
1573 (if (request-rate-limit-reached? url method)
1574 (list (make-warning package
1575 (G_ "Software Heritage rate limit reached; \
1576 try again later")
1577 #:field 'source))
1578 (list (make-warning package
1579 (G_ "'~a' returned ~a")
1580 (list url (response-code response))
1581 #:field 'source))))
1582
1583 (define skip-key (gensym "skip-archival-check"))
1584
1585 (define (skip-when-limit-reached url method)
1586 (or (not (request-rate-limit-reached? url method))
1587 (throw skip-key #t)))
1588
1589 (parameterize ((%allow-request? skip-when-limit-reached))
1590 (catch #t
1591 (lambda ()
1592 (match (and (origin? (package-source package))
1593 (package-source package))
1594 (#f ;no source
1595 '())
1596 ((= origin-uri (? git-reference? reference))
1597 (define url
1598 (git-reference-url reference))
1599 (define commit
1600 (git-reference-commit reference))
1601
1602 (match (if (commit-id? commit)
1603 (or (lookup-revision commit)
1604 (lookup-origin-revision url commit))
1605 (lookup-origin-revision url commit))
1606 ((? revision? revision)
1607 '())
1608 (#f
1609 ;; Revision is missing from the archive, attempt to save it.
1610 (catch 'swh-error
1611 (lambda ()
1612 (save-origin (git-reference-url reference) "git")
1613 (list (make-warning
1614 package
1615 ;; TRANSLATORS: "Software Heritage" is a proper noun
1616 ;; that must remain untranslated. See
1617 ;; <https://www.softwareheritage.org>.
1618 (G_ "scheduled Software Heritage archival")
1619 #:field 'source)))
1620 (lambda (key url method response . _)
1621 (cond ((= 429 (response-code response))
1622 (list (make-warning
1623 package
1624 (G_ "archival rate limit exceeded; \
1625 try again later")
1626 #:field 'source)))
1627 (else
1628 (response->warning url method response))))))))
1629 ((? origin? origin)
1630 ;; Since "save" origins are not supported for non-VCS source, all
1631 ;; we can do is tell whether a given tarball is available or not.
1632 (if (and=> (origin-hash origin) ;XXX: for ungoogled-chromium
1633 content-hash-value) ;& icecat
1634 (let ((hash (origin-hash origin)))
1635 (match (lookup-content (content-hash-value hash)
1636 (symbol->string
1637 (content-hash-algorithm hash)))
1638 (#f
1639 ;; If SWH doesn't have HASH as is, it may be because it's
1640 ;; a hand-crafted tarball. In that case, check whether
1641 ;; the Disarchive database has an entry for that tarball.
1642 (match (lookup-disarchive-spec hash)
1643 (#f
1644 (list (make-warning package
1645 (G_ "source not archived on Software \
1646 Heritage and missing from the Disarchive database")
1647 #:field 'source)))
1648 (directory-ids
1649 (match (find (lambda (id)
1650 (not (lookup-directory id)))
1651 directory-ids)
1652 (#f '())
1653 (id
1654 (list (make-warning package
1655 (G_ "\
1656 Disarchive entry refers to non-existent SWH directory '~a'")
1657 (list id)
1658 #:field 'source)))))))
1659 ((? content?)
1660 '())))
1661 '()))
1662 (_
1663 (list (make-warning package
1664 (G_ "unsupported source type")
1665 #:field 'source)))))
1666 (match-lambda*
1667 (('swh-error url method response)
1668 (response->warning url method response))
1669 ((key . args)
1670 (if (eq? key skip-key)
1671 '()
1672 (with-networking-fail-safe
1673 (G_ "while connecting to Software Heritage")
1674 '()
1675 (apply throw key args))))))))
1676
1677 (define (check-haskell-stackage package)
1678 "Check whether PACKAGE is a Haskell package ahead of the current
1679 Stackage LTS version."
1680 (match (with-networking-fail-safe
1681 (format #f (G_ "while retrieving upstream info for '~a'")
1682 (package-name package))
1683 #f
1684 (package-latest-release package (list %stackage-updater)))
1685 ((? upstream-source? source)
1686 (if (version>? (package-version package)
1687 (upstream-source-version source))
1688 (list
1689 (make-warning package
1690 (G_ "ahead of Stackage LTS version ~a")
1691 (list (upstream-source-version source))
1692 #:field 'version))
1693 '()))
1694 (#f '())))
1695
1696 \f
1697 ;;;
1698 ;;; Source code formatting.
1699 ;;;
1700
1701 (define (report-tabulations package line line-number)
1702 "Warn about tabulations found in LINE."
1703 (match (string-index line #\tab)
1704 (#f #f)
1705 (index
1706 (make-warning package
1707 (G_ "tabulation on line ~a, column ~a")
1708 (list line-number index)
1709 #:location
1710 (location (package-file package)
1711 line-number
1712 index)))))
1713
1714 (define (report-trailing-white-space package line line-number)
1715 "Warn about trailing white space in LINE."
1716 (and (not (or (string=? line (string-trim-right line))
1717 (string=? line (string #\page))))
1718 (make-warning package
1719 (G_ "trailing white space on line ~a")
1720 (list line-number)
1721 #:location
1722 (location (package-file package)
1723 line-number
1724 0))))
1725
1726 (define (report-long-line package line line-number)
1727 "Emit a warning if LINE is too long."
1728 ;; Note: We don't warn at 80 characters because sometimes hashes and URLs
1729 ;; make it hard to fit within that limit and we want to avoid making too
1730 ;; much noise.
1731 (and (> (string-length line) 90)
1732 (make-warning package
1733 (G_ "line ~a is way too long (~a characters)")
1734 (list line-number (string-length line))
1735 #:location
1736 (location (package-file package)
1737 line-number
1738 0))))
1739
1740 (define %hanging-paren-rx
1741 (make-regexp "^[[:blank:]]*[()]+[[:blank:]]*$"))
1742
1743 (define (report-lone-parentheses package line line-number)
1744 "Emit a warning if LINE contains hanging parentheses."
1745 (and (regexp-exec %hanging-paren-rx line)
1746 (make-warning package
1747 (G_ "parentheses feel lonely, \
1748 move to the previous or next line")
1749 (list line-number)
1750 #:location
1751 (location (package-file package)
1752 line-number
1753 0))))
1754
1755 (define %formatting-reporters
1756 ;; List of procedures that report formatting issues. These are not separate
1757 ;; checkers because they would need to re-read the file.
1758 (list report-tabulations
1759 report-trailing-white-space
1760 report-long-line
1761 report-lone-parentheses))
1762
1763 (define* (report-formatting-issues package file starting-line
1764 #:key (reporters %formatting-reporters))
1765 "Report white-space issues in FILE starting from STARTING-LINE, and report
1766 them for PACKAGE."
1767 (define (sexp-last-line port)
1768 ;; Return the last line of the sexp read from PORT or an estimate thereof.
1769 (define &failure (list 'failure))
1770
1771 (let ((start (ftell port))
1772 (start-line (port-line port))
1773 (sexp (catch 'read-error
1774 (lambda () (read port))
1775 (const &failure))))
1776 (let ((line (port-line port)))
1777 (seek port start SEEK_SET)
1778 (set-port-line! port start-line)
1779 (if (eq? sexp &failure)
1780 (+ start-line 60) ;conservative estimate
1781 line))))
1782
1783 (call-with-input-file file
1784 (lambda (port)
1785 (let loop ((line-number 1)
1786 (last-line #f)
1787 (warnings '()))
1788 (let ((line (read-line port)))
1789 (if (or (eof-object? line)
1790 (and last-line (> line-number last-line)))
1791 warnings
1792 (if (and (= line-number starting-line)
1793 (not last-line))
1794 (loop (+ 1 line-number)
1795 (+ 1 (sexp-last-line port))
1796 warnings)
1797 (loop (+ 1 line-number)
1798 last-line
1799 (append
1800 warnings
1801 (if (< line-number starting-line)
1802 '()
1803 (filter-map (lambda (report)
1804 (report package line line-number))
1805 reporters)))))))))))
1806
1807 (define (check-formatting package)
1808 "Check the formatting of the source code of PACKAGE."
1809 (let ((location (package-location package)))
1810 (if location
1811 ;; Report issues starting from the line before the 'package'
1812 ;; form, which usually contains the 'define' form.
1813 (let ((line (- (location-line location) 1)))
1814 (match (search-path %load-path (location-file location))
1815 ((? string? file)
1816 (report-formatting-issues package file line))
1817 (#f
1818 ;; It could be that LOCATION lists a "true" relative file
1819 ;; name--i.e., not relative to an element of %LOAD-PATH.
1820 (let ((file (location-file location)))
1821 (if (file-exists? file)
1822 (report-formatting-issues package file line)
1823 (list (make-warning package
1824 (G_ "source file not found"))))))))
1825 '())))
1826
1827 \f
1828 ;;;
1829 ;;; List of checkers.
1830 ;;;
1831
1832 (define %local-checkers
1833 (list
1834 (lint-checker
1835 (name 'name)
1836 (description "Validate package names")
1837 (check check-name))
1838 (lint-checker
1839 (name 'tests-true)
1840 (description "Check if tests are explicitly enabled")
1841 (check check-tests-true))
1842 (lint-checker
1843 (name 'description)
1844 (description "Validate package descriptions")
1845 (check check-description-style))
1846 (lint-checker
1847 (name 'inputs-should-be-native)
1848 (description "Identify inputs that should be native inputs")
1849 (check check-inputs-should-be-native))
1850 (lint-checker
1851 (name 'inputs-should-not-be-input)
1852 (description "Identify inputs that shouldn't be inputs at all")
1853 (check check-inputs-should-not-be-an-input-at-all))
1854 (lint-checker
1855 (name 'input-labels)
1856 (description "Identify input labels that do not match package names")
1857 (check check-input-labels))
1858 (lint-checker
1859 (name 'wrapper-inputs)
1860 (description "Make sure 'wrap-program' can finds its interpreter.")
1861 (check check-wrapper-inputs))
1862 (lint-checker
1863 (name 'license)
1864 ;; TRANSLATORS: <license> is the name of a data type and must not be
1865 ;; translated.
1866 (description "Make sure the 'license' field is a <license> \
1867 or a list thereof")
1868 (check check-license))
1869 (lint-checker
1870 (name 'optional-tests)
1871 (description "Make sure tests are only run when requested")
1872 (check check-optional-tests))
1873 (lint-checker
1874 (name 'mirror-url)
1875 (description "Suggest 'mirror://' URLs")
1876 (check check-mirror-url))
1877 (lint-checker
1878 (name 'source-file-name)
1879 (description "Validate file names of sources")
1880 (check check-source-file-name))
1881 (lint-checker
1882 (name 'source-unstable-tarball)
1883 (description "Check for autogenerated tarballs")
1884 (check check-source-unstable-tarball))
1885 (lint-checker
1886 (name 'derivation)
1887 (description "Report failure to compile a package to a derivation")
1888 (check check-derivation)
1889 (requires-store? #t))
1890 (lint-checker
1891 (name 'profile-collisions)
1892 (description "Report collisions that would occur due to propagated inputs")
1893 (check check-profile-collisions)
1894 (requires-store? #t))
1895 (lint-checker
1896 (name 'patch-file-names)
1897 (description "Validate file names and availability of patches")
1898 (check check-patch-file-names))
1899 (lint-checker
1900 (name 'patch-headers)
1901 (description "Validate patch headers")
1902 (check check-patch-headers))
1903 (lint-checker
1904 (name 'formatting)
1905 (description "Look for formatting issues in the source")
1906 (check check-formatting))))
1907
1908 (define %network-dependent-checkers
1909 (list
1910 (lint-checker
1911 (name 'synopsis)
1912 (description "Validate package synopses")
1913 (check check-synopsis-style))
1914 (lint-checker
1915 (name 'gnu-description)
1916 (description "Validate synopsis & description of GNU packages")
1917 (check check-gnu-synopsis+description))
1918 (lint-checker
1919 (name 'home-page)
1920 (description "Validate home-page URLs")
1921 (check check-home-page))
1922 (lint-checker
1923 (name 'source)
1924 (description "Validate source URLs")
1925 (check check-source))
1926 (lint-checker
1927 (name 'github-url)
1928 (description "Suggest GitHub URLs")
1929 (check check-github-url))
1930 (lint-checker
1931 (name 'cve)
1932 (description "Check the Common Vulnerabilities and Exposures\
1933 (CVE) database")
1934 (check check-vulnerabilities))
1935 (lint-checker
1936 (name 'refresh)
1937 (description "Check the package for new upstream releases")
1938 (check check-for-updates))
1939 (lint-checker
1940 (name 'archival)
1941 (description "Ensure source code archival on Software Heritage")
1942 (check check-archival))
1943 (lint-checker
1944 (name 'haskell-stackage)
1945 (description "Ensure Haskell packages use Stackage LTS versions")
1946 (check check-haskell-stackage))))
1947
1948 (define %all-checkers
1949 (append %local-checkers
1950 %network-dependent-checkers))