lint.scm 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014 Cyril Roelandt <tipecaml@gmail.com>
  3. ;;; Copyright © 2014, 2015 Eric Bavier <bavier@member.fsf.org>
  4. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  5. ;;; Copyright © 2015, 2016 Mathieu Lirzin <mthl@gnu.org>
  6. ;;; Copyright © 2016 Danny Milosavljevic <dannym+a@scratchpost.org>
  7. ;;; Copyright © 2016 Hartmut Goebel <h.goebel@crazy-compilers.com>
  8. ;;; Copyright © 2017 Alex Kost <alezost@gmail.com>
  9. ;;; Copyright © 2017, 2021 Tobias Geerinckx-Rice <me@tobias.gr>
  10. ;;; Copyright © 2017, 2018, 2020 Efraim Flashner <efraim@flashner.co.il>
  11. ;;; Copyright © 2018, 2019 Arun Isaac <arunisaac@systemreboot.net>
  12. ;;; Copyright © 2020 Chris Marusich <cmmarusich@gmail.com>
  13. ;;; Copyright © 2020 Timothy Sample <samplet@ngyro.com>
  14. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  15. ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
  16. ;;;
  17. ;;; This file is part of GNU Guix.
  18. ;;;
  19. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  20. ;;; under the terms of the GNU General Public License as published by
  21. ;;; the Free Software Foundation; either version 3 of the License, or (at
  22. ;;; your option) any later version.
  23. ;;;
  24. ;;; GNU Guix is distributed in the hope that it will be useful, but
  25. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  26. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. ;;; GNU General Public License for more details.
  28. ;;;
  29. ;;; You should have received a copy of the GNU General Public License
  30. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  31. (define-module (guix lint)
  32. #:use-module (guix store)
  33. #:autoload (guix base16) (bytevector->base16-string)
  34. #:use-module (guix base32)
  35. #:use-module (guix diagnostics)
  36. #:use-module (guix download)
  37. #:use-module (guix ftp-client)
  38. #:use-module (guix http-client)
  39. #:use-module (guix packages)
  40. #:use-module (guix i18n)
  41. #:use-module ((guix gexp)
  42. #:select (local-file? local-file-absolute-file-name))
  43. #:use-module (guix licenses)
  44. #:use-module (guix records)
  45. #:use-module (guix grafts)
  46. #:use-module (guix upstream)
  47. #:use-module (guix utils)
  48. #:use-module (guix memoization)
  49. #:use-module (guix profiles)
  50. #:use-module (guix monads)
  51. #:use-module (guix scripts)
  52. #:use-module ((guix ui) #:select (texi->plain-text fill-paragraph))
  53. #:use-module (guix gnu-maintenance)
  54. #:use-module (guix cve)
  55. #:use-module ((guix swh) #:hide (origin?))
  56. #:autoload (guix git-download) (git-reference?
  57. git-reference-url git-reference-commit)
  58. #:use-module (guix import stackage)
  59. #:use-module (ice-9 match)
  60. #:use-module (ice-9 regex)
  61. #:use-module (ice-9 format)
  62. #:use-module (web client)
  63. #:use-module (web uri)
  64. #:use-module ((guix build download)
  65. #:select (maybe-expand-mirrors
  66. (open-connection-for-uri
  67. . guix:open-connection-for-uri)))
  68. #:use-module (web request)
  69. #:use-module (web response)
  70. #:use-module (srfi srfi-1)
  71. #:use-module (srfi srfi-6) ;Unicode string ports
  72. #:use-module (srfi srfi-9)
  73. #:use-module (srfi srfi-11)
  74. #:use-module (srfi srfi-26)
  75. #:use-module (srfi srfi-34)
  76. #:use-module (srfi srfi-35)
  77. #:use-module (ice-9 rdelim)
  78. #:export (check-description-style
  79. check-inputs-should-be-native
  80. check-inputs-should-not-be-an-input-at-all
  81. check-patch-file-names
  82. check-patch-headers
  83. check-synopsis-style
  84. check-derivation
  85. check-home-page
  86. check-name
  87. check-source
  88. check-source-file-name
  89. check-source-unstable-tarball
  90. check-mirror-url
  91. check-github-url
  92. check-license
  93. check-vulnerabilities
  94. check-for-updates
  95. check-formatting
  96. check-archival
  97. check-profile-collisions
  98. check-haskell-stackage
  99. check-tests-true
  100. lint-warning
  101. lint-warning?
  102. lint-warning-package
  103. lint-warning-message
  104. lint-warning-message-text
  105. lint-warning-message-data
  106. lint-warning-location
  107. %local-checkers
  108. %network-dependent-checkers
  109. %all-checkers
  110. lint-checker
  111. lint-checker?
  112. lint-checker-name
  113. lint-checker-description
  114. lint-checker-check
  115. lint-checker-requires-store?))
  116. ;;;
  117. ;;; Warnings
  118. ;;;
  119. (define-record-type* <lint-warning>
  120. lint-warning make-lint-warning
  121. lint-warning?
  122. (package lint-warning-package)
  123. (message-text lint-warning-message-text)
  124. (message-data lint-warning-message-data
  125. (default '()))
  126. (location lint-warning-location
  127. (default #f)))
  128. (define (lint-warning-message warning)
  129. (apply format #f
  130. (G_ (lint-warning-message-text warning))
  131. (lint-warning-message-data warning)))
  132. (define (package-file package)
  133. (location-file
  134. (package-location package)))
  135. (define* (%make-warning package message-text
  136. #:optional (message-data '())
  137. #:key field location)
  138. (make-lint-warning
  139. package
  140. message-text
  141. message-data
  142. (or location
  143. (and field (package-field-location package field))
  144. (package-location package))))
  145. (define-syntax make-warning
  146. (syntax-rules (G_)
  147. ((_ package (G_ message) rest ...)
  148. (%make-warning package message rest ...))))
  149. ;;;
  150. ;;; Checkers
  151. ;;;
  152. (define-record-type* <lint-checker>
  153. lint-checker make-lint-checker
  154. lint-checker?
  155. ;; TODO: add a 'certainty' field that shows how confident we are in the
  156. ;; checker. Then allow users to only run checkers that have a certain
  157. ;; 'certainty' level.
  158. (name lint-checker-name)
  159. (description lint-checker-description)
  160. (check lint-checker-check)
  161. (requires-store? lint-checker-requires-store?
  162. (default #f)))
  163. (define (check-name package)
  164. "Check whether PACKAGE's name matches our guidelines."
  165. (let ((name (package-name package)))
  166. (cond
  167. ;; Currently checks only whether the name is too short.
  168. ((and (<= (string-length name) 1)
  169. (not (string=? name "r"))) ; common-sense exception
  170. (list
  171. (make-warning package
  172. (G_ "name should be longer than a single character")
  173. #:field 'name)))
  174. ((string-index name #\_)
  175. (list
  176. (make-warning package
  177. (G_ "name should use hyphens instead of underscores")
  178. #:field 'name)))
  179. (else '()))))
  180. (define (check-tests-true package)
  181. "Check whether PACKAGE explicitly requests to run tests, which is
  182. superfluous when building natively and incorrect when cross-compiling."
  183. (define (tests-explicitly-enabled?)
  184. (apply (lambda* (#:key tests? #:allow-other-keys)
  185. (eq? tests? #t))
  186. (package-arguments package)))
  187. (if (and (tests-explicitly-enabled?)
  188. ;; Some packages, e.g. gnutls, set #:tests?
  189. ;; differently depending on whether it is being
  190. ;; cross-compiled.
  191. (parameterize ((%current-target-system "aarch64-linux-gnu"))
  192. (tests-explicitly-enabled?)))
  193. (list (make-warning package
  194. ;; TRANSLATORS: #:tests? and #t are Scheme constants
  195. ;; and must not be translated.
  196. (G_ "#:tests? must not be explicitly set to #t")
  197. #:field 'arguments))
  198. '()))
  199. (define (properly-starts-sentence? s)
  200. (string-match "^[(\"'`[:upper:][:digit:]]" s))
  201. (define (starts-with-abbreviation? s)
  202. "Return #t if S starts with what looks like an abbreviation or acronym."
  203. (string-match "^[A-Z][A-Z0-9]+\\>" s))
  204. (define %quoted-identifier-rx
  205. ;; A quoted identifier, like 'this'.
  206. (make-regexp "['`][[:graph:]]+'"))
  207. (define (check-description-style package)
  208. ;; Emit a warning if stylistic issues are found in the description of PACKAGE.
  209. (define (check-not-empty description)
  210. (if (string-null? description)
  211. (list
  212. (make-warning package
  213. (G_ "description should not be empty")
  214. #:field 'description))
  215. '()))
  216. (define (check-texinfo-markup description)
  217. "Check that DESCRIPTION can be parsed as a Texinfo fragment. If the
  218. markup is valid return a plain-text version of DESCRIPTION, otherwise #f."
  219. (catch #t
  220. (lambda () (texi->plain-text description))
  221. (lambda (keys . args)
  222. (make-warning package
  223. (G_ "Texinfo markup in description is invalid")
  224. #:field 'description))))
  225. (define (check-trademarks description)
  226. "Check that DESCRIPTION does not contain '™' or '®' characters. See
  227. http://www.gnu.org/prep/standards/html_node/Trademarks.html."
  228. (match (string-index description (char-set #\™ #\®))
  229. ((and (? number?) index)
  230. (list
  231. (make-warning package
  232. (G_ "description should not contain ~
  233. trademark sign '~a' at ~d")
  234. (list (string-ref description index) index)
  235. #:field 'description)))
  236. (else '())))
  237. (define (check-quotes description)
  238. "Check whether DESCRIPTION contains single quotes and suggest @code."
  239. (if (regexp-exec %quoted-identifier-rx description)
  240. (list
  241. (make-warning package
  242. ;; TRANSLATORS: '@code' is Texinfo markup and must be kept
  243. ;; as is.
  244. (G_ "use @code or similar ornament instead of quotes")
  245. #:field 'description))
  246. '()))
  247. (define (check-proper-start description)
  248. (if (or (string-null? description)
  249. (properly-starts-sentence? description)
  250. (string-prefix-ci? (package-name package) description))
  251. '()
  252. (list
  253. (make-warning
  254. package
  255. (G_ "description should start with an upper-case letter or digit")
  256. #:field 'description))))
  257. (define (check-end-of-sentence-space description)
  258. "Check that an end-of-sentence period is followed by two spaces."
  259. (let ((infractions
  260. (reverse (fold-matches
  261. "\\. [A-Z]" description '()
  262. (lambda (m r)
  263. ;; Filter out matches of common abbreviations.
  264. (if (find (lambda (s)
  265. (string-suffix-ci? s (match:prefix m)))
  266. '("i.e" "e.g" "a.k.a" "resp"))
  267. r (cons (match:start m) r)))))))
  268. (if (null? infractions)
  269. '()
  270. (list
  271. (make-warning package
  272. (G_ "sentences in description should be followed ~
  273. by two spaces; possible infraction~p at ~{~a~^, ~}")
  274. (list (length infractions)
  275. infractions)
  276. #:field 'description)))))
  277. (let ((description (package-description package)))
  278. (if (string? description)
  279. (append
  280. (check-not-empty description)
  281. (check-quotes description)
  282. (check-trademarks description)
  283. ;; Use raw description for this because Texinfo rendering
  284. ;; automatically fixes end of sentence space.
  285. (check-end-of-sentence-space description)
  286. (match (check-texinfo-markup description)
  287. ((and warning (? lint-warning?)) (list warning))
  288. (plain-description
  289. (check-proper-start plain-description))))
  290. (list
  291. (make-warning package
  292. (G_ "invalid description: ~s")
  293. (list description)
  294. #:field 'description)))))
  295. (define (package-input-intersection inputs-to-check input-names)
  296. "Return the intersection between INPUTS-TO-CHECK, the list of input tuples
  297. of a package, and INPUT-NAMES, a list of package specifications such as
  298. \"glib:bin\"."
  299. (match inputs-to-check
  300. (((labels packages . outputs) ...)
  301. (filter-map (lambda (package output)
  302. (and (package? package)
  303. (let ((input (string-append
  304. (package-name package)
  305. (if (> (length output) 0)
  306. (string-append ":" (car output))
  307. ""))))
  308. (and (member input input-names)
  309. input))))
  310. packages outputs))))
  311. (define (check-inputs-should-be-native package)
  312. ;; Emit a warning if some inputs of PACKAGE are likely to belong to its
  313. ;; native inputs.
  314. (let ((inputs (append (package-inputs package)
  315. (package-propagated-inputs package)))
  316. (input-names
  317. '("pkg-config"
  318. "autoconf"
  319. "automake"
  320. "bison"
  321. "cmake"
  322. "dejagnu"
  323. "desktop-file-utils"
  324. "doxygen"
  325. "extra-cmake-modules"
  326. "flex"
  327. "gettext"
  328. "glib:bin"
  329. "gobject-introspection"
  330. "googletest-source"
  331. "groff"
  332. "gtk-doc"
  333. "help2man"
  334. "intltool"
  335. "itstool"
  336. "libtool"
  337. "m4"
  338. "qttools"
  339. "yasm" "nasm" "fasm"
  340. "python-coverage" "python2-coverage"
  341. "python-cython" "python2-cython"
  342. "python-docutils" "python2-docutils"
  343. "python-mock" "python2-mock"
  344. "python-nose" "python2-nose"
  345. "python-pbr" "python2-pbr"
  346. "python-pytest" "python2-pytest"
  347. "python-pytest-cov" "python2-pytest-cov"
  348. "python-setuptools-scm" "python2-setuptools-scm"
  349. "python-sphinx" "python2-sphinx"
  350. "scdoc"
  351. "swig"
  352. "qmake"
  353. "qttools"
  354. "texinfo"
  355. "xorg-server-for-tests"
  356. "yelp-tools")))
  357. (map (lambda (input)
  358. (make-warning
  359. package
  360. (G_ "'~a' should probably be a native input")
  361. (list input)
  362. #:field 'inputs))
  363. (package-input-intersection inputs input-names))))
  364. (define (check-inputs-should-not-be-an-input-at-all package)
  365. ;; Emit a warning if some inputs of PACKAGE are likely to should not be
  366. ;; an input at all.
  367. (let ((input-names '("python-setuptools"
  368. "python2-setuptools"
  369. "python-pip"
  370. "python2-pip")))
  371. (map (lambda (input)
  372. (make-warning
  373. package
  374. (G_ "'~a' should probably not be an input at all")
  375. (list input)
  376. #:field 'inputs))
  377. (package-input-intersection (package-direct-inputs package)
  378. input-names))))
  379. (define (package-name-regexp package)
  380. "Return a regexp that matches PACKAGE's name as a word at the beginning of a
  381. line."
  382. (make-regexp (string-append "^" (regexp-quote (package-name package))
  383. "\\>")
  384. regexp/icase))
  385. (define (check-synopsis-style package)
  386. ;; Emit a warning if stylistic issues are found in the synopsis of PACKAGE.
  387. (define (check-final-period synopsis)
  388. ;; Synopsis should not end with a period, except for some special cases.
  389. (if (and (string-suffix? "." synopsis)
  390. (not (string-suffix? "etc." synopsis)))
  391. (list
  392. (make-warning package
  393. (G_ "no period allowed at the end of the synopsis")
  394. #:field 'synopsis))
  395. '()))
  396. (define check-start-article
  397. ;; Skip this check for GNU packages, as suggested by Karl Berry's reply to
  398. ;; <http://lists.gnu.org/archive/html/bug-womb/2014-11/msg00000.html>.
  399. (if (false-if-exception (gnu-package? package))
  400. (const '())
  401. (lambda (synopsis)
  402. (if (or (string-prefix-ci? "A " synopsis)
  403. (string-prefix-ci? "An " synopsis))
  404. (list
  405. (make-warning package
  406. (G_ "no article allowed at the beginning of \
  407. the synopsis")
  408. #:field 'synopsis))
  409. '()))))
  410. (define (check-synopsis-length synopsis)
  411. (if (>= (string-length synopsis) 80)
  412. (list
  413. (make-warning package
  414. (G_ "synopsis should be less than 80 characters long")
  415. #:field 'synopsis))
  416. '()))
  417. (define (check-proper-start synopsis)
  418. (if (properly-starts-sentence? synopsis)
  419. '()
  420. (list
  421. (make-warning package
  422. (G_ "synopsis should start with an upper-case letter or digit")
  423. #:field 'synopsis))))
  424. (define (check-start-with-package-name synopsis)
  425. (if (and (regexp-exec (package-name-regexp package) synopsis)
  426. (not (starts-with-abbreviation? synopsis)))
  427. (list
  428. (make-warning package
  429. (G_ "synopsis should not start with the package name")
  430. #:field 'synopsis))
  431. '()))
  432. (define (check-texinfo-markup synopsis)
  433. "Check that SYNOPSIS can be parsed as a Texinfo fragment. If the
  434. markup is valid return a plain-text version of SYNOPSIS, otherwise #f."
  435. (catch #t
  436. (lambda ()
  437. (texi->plain-text synopsis)
  438. '())
  439. (lambda (keys . args)
  440. (list
  441. (make-warning package
  442. (G_ "Texinfo markup in synopsis is invalid")
  443. #:field 'synopsis)))))
  444. (define checks
  445. (list check-proper-start
  446. check-final-period
  447. check-start-article
  448. check-start-with-package-name
  449. check-synopsis-length
  450. check-texinfo-markup))
  451. (match (package-synopsis package)
  452. (""
  453. (list
  454. (make-warning package
  455. (G_ "synopsis should not be empty")
  456. #:field 'synopsis)))
  457. ((? string? synopsis)
  458. (append-map
  459. (lambda (proc)
  460. (proc synopsis))
  461. checks))
  462. (invalid
  463. (list
  464. (make-warning package
  465. (G_ "invalid synopsis: ~s")
  466. (list invalid)
  467. #:field 'synopsis)))))
  468. (define* (probe-uri uri #:key timeout)
  469. "Probe URI, a URI object, and return two values: a symbol denoting the
  470. probing status, such as 'http-response' when we managed to get an HTTP
  471. response from URI, and additional details, such as the actual HTTP response.
  472. TIMEOUT is the maximum number of seconds (possibly an inexact number) to wait
  473. for connections to complete; when TIMEOUT is #f, wait as long as needed."
  474. (define headers
  475. '((User-Agent . "GNU Guile")
  476. (Accept . "*/*")))
  477. (let loop ((uri uri)
  478. (visited '()))
  479. (match (uri-scheme uri)
  480. ((or 'http 'https)
  481. (catch #t
  482. (lambda ()
  483. (let ((port (guix:open-connection-for-uri
  484. uri #:timeout timeout))
  485. (request (build-request uri #:headers headers)))
  486. (define response
  487. (dynamic-wind
  488. (const #f)
  489. (lambda ()
  490. (write-request request port)
  491. (force-output port)
  492. (read-response port))
  493. (lambda ()
  494. (close-port port))))
  495. (case (response-code response)
  496. ((302 ; found (redirection)
  497. 303 ; see other
  498. 307 ; temporary redirection
  499. 308) ; permanent redirection
  500. (let ((location (response-location response)))
  501. (if (or (not location) (member location visited))
  502. (values 'http-response response)
  503. (loop location (cons location visited))))) ;follow the redirect
  504. ((301) ; moved permanently
  505. (let ((location (response-location response)))
  506. ;; Return RESPONSE, unless the final response as we follow
  507. ;; redirects is not 200.
  508. (if location
  509. (let-values (((status response2)
  510. (loop location (cons location visited))))
  511. (case status
  512. ((http-response)
  513. (values 'http-response
  514. (if (= 200 (response-code response2))
  515. response
  516. response2)))
  517. (else
  518. (values status response2))))
  519. (values 'http-response response)))) ;invalid redirect
  520. (else
  521. (values 'http-response response)))))
  522. (lambda (key . args)
  523. (case key
  524. ((bad-header bad-header-component)
  525. ;; This can happen if the server returns an invalid HTTP header,
  526. ;; as is the case with the 'Date' header at sqlite.org.
  527. (values 'invalid-http-response #f))
  528. ((getaddrinfo-error system-error
  529. gnutls-error tls-certificate-error)
  530. (values key args))
  531. (else
  532. (apply throw key args))))))
  533. ('ftp
  534. (catch #t
  535. (lambda ()
  536. (let ((conn (ftp-open (uri-host uri) #:timeout timeout)))
  537. (define response
  538. (dynamic-wind
  539. (const #f)
  540. (lambda ()
  541. (ftp-chdir conn (dirname (uri-path uri)))
  542. (ftp-size conn (basename (uri-path uri))))
  543. (lambda ()
  544. (ftp-close conn))))
  545. (values 'ftp-response '(ok))))
  546. (lambda (key . args)
  547. (case key
  548. ((ftp-error)
  549. (values 'ftp-response `(error ,@args)))
  550. ((getaddrinfo-error system-error gnutls-error)
  551. (values key args))
  552. (else
  553. (apply throw key args))))))
  554. (_
  555. (values 'unknown-protocol #f)))))
  556. (define (tls-certificate-error-string args)
  557. "Return a string explaining the 'tls-certificate-error' arguments ARGS."
  558. (call-with-output-string
  559. (lambda (port)
  560. (print-exception port #f
  561. 'tls-certificate-error args))))
  562. (define (validate-uri uri package field)
  563. "Return #t if the given URI can be reached, otherwise return a warning for
  564. PACKAGE mentioning the FIELD."
  565. (let-values (((status argument)
  566. (probe-uri uri #:timeout 3))) ;wait at most 3 seconds
  567. (case status
  568. ((http-response)
  569. (cond ((= 200 (response-code argument))
  570. (match (response-content-length argument)
  571. ((? number? length)
  572. ;; As of July 2016, SourceForge returns 200 (instead of 404)
  573. ;; with a small HTML page upon failure. Attempt to detect
  574. ;; such malicious behavior.
  575. (or (> length 1000)
  576. (make-warning package
  577. (G_ "URI ~a returned \
  578. suspiciously small file (~a bytes)")
  579. (list (uri->string uri)
  580. length)
  581. #:field field)))
  582. (_ #t)))
  583. ((= 301 (response-code argument))
  584. (if (response-location argument)
  585. (make-warning package
  586. (G_ "permanent redirect from ~a to ~a")
  587. (list (uri->string uri)
  588. (uri->string
  589. (response-location argument)))
  590. #:field field)
  591. (make-warning package
  592. (G_ "invalid permanent redirect \
  593. from ~a")
  594. (list (uri->string uri))
  595. #:field field)))
  596. (else
  597. (make-warning package
  598. (G_ "URI ~a not reachable: ~a (~s)")
  599. (list (uri->string uri)
  600. (response-code argument)
  601. (response-reason-phrase argument))
  602. #:field field))))
  603. ((ftp-response)
  604. (match argument
  605. (('ok) #t)
  606. (('error port command code message)
  607. (make-warning package
  608. (G_ "URI ~a not reachable: ~a (~s)")
  609. (list (uri->string uri)
  610. code (string-trim-both message))
  611. #:field field))))
  612. ((getaddrinfo-error)
  613. (make-warning package
  614. (G_ "URI ~a domain not found: ~a")
  615. (list (uri->string uri)
  616. (gai-strerror (car argument)))
  617. #:field field))
  618. ((system-error)
  619. (make-warning package
  620. (G_ "URI ~a unreachable: ~a")
  621. (list (uri->string uri)
  622. (strerror
  623. (system-error-errno
  624. (cons status argument))))
  625. #:field field))
  626. ((tls-certificate-error)
  627. (make-warning package
  628. (G_ "TLS certificate error: ~a")
  629. (list (tls-certificate-error-string argument))
  630. #:field field))
  631. ((invalid-http-response gnutls-error)
  632. ;; Probably a misbehaving server; ignore.
  633. #f)
  634. ((unknown-protocol) ;nothing we can do
  635. #f)
  636. (else
  637. (error "internal linter error" status)))))
  638. (define (check-home-page package)
  639. "Emit a warning if PACKAGE has an invalid 'home-page' field, or if that
  640. 'home-page' is not reachable."
  641. (let ((uri (and=> (package-home-page package) string->uri)))
  642. (cond
  643. ((uri? uri)
  644. (match (validate-uri uri package 'home-page)
  645. ((and (? lint-warning? warning) warning)
  646. (list warning))
  647. (_ '())))
  648. ((not (package-home-page package))
  649. (if (or (string-contains (package-name package) "bootstrap")
  650. (string=? (package-name package) "ld-wrapper"))
  651. '()
  652. (list
  653. (make-warning package
  654. (G_ "invalid value for home page")
  655. #:field 'home-page))))
  656. (else
  657. (list
  658. (make-warning package
  659. (G_ "invalid home page URL: ~s")
  660. (list (package-home-page package))
  661. #:field 'home-page))))))
  662. (define %distro-directory
  663. (mlambda ()
  664. (dirname (search-path %load-path "gnu.scm"))))
  665. (define (check-patch-file-names package)
  666. "Emit a warning if the patches requires by PACKAGE are badly named or if the
  667. patch could not be found."
  668. (guard (c ((formatted-message? c) ;raised by 'search-patch'
  669. (list (%make-warning package
  670. (formatted-message-string c)
  671. (formatted-message-arguments c)
  672. #:field 'source))))
  673. (define patches
  674. (match (package-source package)
  675. ((? origin? origin) (origin-patches origin))
  676. (_ '())))
  677. (define (starts-with-package-name? file-name)
  678. (and=> (string-contains file-name (package-name package))
  679. zero?))
  680. (append
  681. (if (every (match-lambda ;patch starts with package name?
  682. ((? string? patch)
  683. (starts-with-package-name? (basename patch)))
  684. ((? origin? patch)
  685. (starts-with-package-name? (origin-actual-file-name patch)))
  686. (_ #f)) ;must be some other file-like object
  687. patches)
  688. '()
  689. (list
  690. (make-warning
  691. package
  692. (G_ "file names of patches should start with the package name")
  693. #:field 'patch-file-names)))
  694. ;; Check whether we're reaching tar's maximum file name length.
  695. (let ((prefix (string-length (%distro-directory)))
  696. (margin (string-length "guix-2.0.0rc3-10000-1234567890/"))
  697. (max 99))
  698. (filter-map (match-lambda
  699. ((? string? patch)
  700. (if (> (+ margin (if (string-prefix? (%distro-directory)
  701. patch)
  702. (- (string-length patch) prefix)
  703. (string-length patch)))
  704. max)
  705. (make-warning
  706. package
  707. (G_ "~a: file name is too long")
  708. (list (basename patch))
  709. #:field 'patch-file-names)
  710. #f))
  711. (_ #f))
  712. patches)))))
  713. (define (check-patch-headers package)
  714. "Check that PACKAGE's patches start with a comment. Return a list of
  715. warnings."
  716. (define (blank? str)
  717. (string-every char-set:blank str))
  718. (define (patch-header-warnings patch)
  719. (call-with-input-file patch
  720. (lambda (port)
  721. ;; Read from PORT until a non-blank line is found or EOF is reached.
  722. (let loop ()
  723. (let ((line (read-line port)))
  724. (cond ((eof-object? line)
  725. (list (make-warning package
  726. (G_ "~a: empty patch")
  727. (list (basename patch))
  728. #:field 'source)))
  729. ((blank? line)
  730. (loop))
  731. ((or (string-prefix? "--- " line)
  732. (string-prefix? "+++ " line))
  733. (list (make-warning package
  734. (G_ "~a: patch lacks comment and \
  735. upstream status")
  736. (list (basename patch))
  737. #:field 'source)))
  738. (else
  739. '())))))))
  740. (guard (c ((formatted-message? c) ;raised by 'search-patch'
  741. (list (%make-warning package
  742. (formatted-message-string c)
  743. (formatted-message-arguments c)
  744. #:field 'source))))
  745. (let ((patches (if (origin? (package-source package))
  746. (origin-patches (package-source package))
  747. '())))
  748. (append-map (lambda (patch)
  749. ;; Dismiss PATCH if it's an origin or similar.
  750. (cond ((string? patch)
  751. (patch-header-warnings patch))
  752. ((local-file? patch)
  753. (patch-header-warnings
  754. (local-file-absolute-file-name patch)))
  755. (else
  756. '())))
  757. patches))))
  758. (define (escape-quotes str)
  759. "Replace any quote character in STR by an escaped quote character."
  760. (list->string
  761. (string-fold-right (lambda (chr result)
  762. (match chr
  763. (#\" (cons* #\\ #\"result))
  764. (_ (cons chr result))))
  765. '()
  766. str)))
  767. (define official-gnu-packages*
  768. (mlambda ()
  769. "A memoizing version of 'official-gnu-packages' that returns the empty
  770. list when something goes wrong, such as a networking issue."
  771. (let ((gnus (false-if-exception (official-gnu-packages))))
  772. (or gnus '()))))
  773. (define (check-gnu-synopsis+description package)
  774. "Make sure that, if PACKAGE is a GNU package, it uses the synopsis and
  775. descriptions maintained upstream."
  776. (match (find (lambda (descriptor)
  777. (string=? (gnu-package-name descriptor)
  778. (package-name package)))
  779. (official-gnu-packages*))
  780. (#f ;not a GNU package, so nothing to do
  781. '())
  782. (descriptor ;a genuine GNU package
  783. (append
  784. (let ((upstream (gnu-package-doc-summary descriptor))
  785. (downstream (package-synopsis package)))
  786. (if (and upstream
  787. (or (not (string? downstream))
  788. (not (string=? upstream downstream))))
  789. (list
  790. (make-warning package
  791. (G_ "proposed synopsis: ~s~%")
  792. (list upstream)
  793. #:field 'synopsis))
  794. '()))
  795. (let ((upstream (gnu-package-doc-description descriptor))
  796. (downstream (package-description package)))
  797. (if (and upstream
  798. (or (not (string? downstream))
  799. (not (string=? (fill-paragraph upstream 100)
  800. (fill-paragraph downstream 100)))))
  801. (list
  802. (make-warning
  803. package
  804. (G_ "proposed description:~% \"~a\"~%")
  805. (list (fill-paragraph (escape-quotes upstream) 77 7))
  806. #:field 'description))
  807. '()))))))
  808. (define (origin-uris origin)
  809. "Return the list of URIs (strings) for ORIGIN."
  810. (match (origin-uri origin)
  811. ((? string? uri)
  812. (list uri))
  813. ((uris ...)
  814. uris)))
  815. (define (check-source package)
  816. "Emit a warning if PACKAGE has an invalid 'source' field, or if that
  817. 'source' is not reachable."
  818. (define (warnings-for-uris uris)
  819. (let loop ((uris uris)
  820. (warnings '()))
  821. (match uris
  822. (()
  823. (reverse warnings))
  824. ((uri rest ...)
  825. (match (validate-uri uri package 'source)
  826. (#t
  827. ;; We found a working URL, so stop right away.
  828. '())
  829. (#f
  830. ;; Unsupported URL or other error, skip.
  831. (loop rest warnings))
  832. ((? lint-warning? warning)
  833. (loop rest (cons warning warnings))))))))
  834. (let ((origin (package-source package)))
  835. (if (origin? origin)
  836. (cond
  837. ((eq? (origin-method origin) url-fetch)
  838. (let* ((uris (append-map (cut maybe-expand-mirrors <> %mirrors)
  839. (map string->uri (origin-uris origin))))
  840. (warnings (warnings-for-uris uris)))
  841. ;; Just make sure that at least one of the URIs is valid.
  842. (if (= (length uris) (length warnings))
  843. ;; When everything fails, report all of WARNINGS, otherwise don't
  844. ;; report anything.
  845. ;;
  846. ;; XXX: Ideally we'd still allow warnings to be raised if *some*
  847. ;; URIs are unreachable, but distinguish that from the error case
  848. ;; where *all* the URIs are unreachable.
  849. (cons*
  850. (make-warning package
  851. (G_ "all the source URIs are unreachable:")
  852. #:field 'source)
  853. warnings)
  854. '())))
  855. ((git-reference? (origin-uri origin))
  856. (warnings-for-uris
  857. (list (string->uri (git-reference-url (origin-uri origin))))))
  858. (else
  859. '()))
  860. '())))
  861. (define (check-source-file-name package)
  862. "Emit a warning if PACKAGE's origin has no meaningful file name."
  863. (define (origin-file-name-valid? origin)
  864. ;; Return #f if the source file name contains only a version or is #f;
  865. ;; indicates that the origin needs a 'file-name' field.
  866. (let ((file-name (origin-actual-file-name origin))
  867. (version (package-version package)))
  868. (and file-name
  869. ;; Common in many projects is for the filename to start
  870. ;; with a "v" followed by the version,
  871. ;; e.g. "v3.2.0.tar.gz".
  872. (not (string-match (string-append "^v?" version) file-name)))))
  873. (let ((origin (package-source package)))
  874. (if (or (not (origin? origin)) (origin-file-name-valid? origin))
  875. '()
  876. (list
  877. (make-warning package
  878. (G_ "the source file name should contain the package name")
  879. #:field 'source)))))
  880. (define (check-source-unstable-tarball package)
  881. "Emit a warning if PACKAGE's source is an autogenerated tarball."
  882. (define (check-source-uri uri)
  883. (if (and (string=? (uri-host (string->uri uri)) "github.com")
  884. (match (split-and-decode-uri-path
  885. (uri-path (string->uri uri)))
  886. ((_ _ "archive" _ ...) #t)
  887. (_ #f)))
  888. (make-warning package
  889. (G_ "the source URI should not be an autogenerated tarball")
  890. #:field 'source)
  891. #f))
  892. (let ((origin (package-source package)))
  893. (if (and (origin? origin)
  894. (eqv? (origin-method origin) url-fetch))
  895. (filter-map check-source-uri
  896. (origin-uris origin))
  897. '())))
  898. (define (check-mirror-url package)
  899. "Check whether PACKAGE uses source URLs that should be 'mirror://'."
  900. (define (check-mirror-uri uri) ;XXX: could be optimized
  901. (let loop ((mirrors %mirrors))
  902. (match mirrors
  903. (()
  904. #f)
  905. (((mirror-id mirror-urls ...) rest ...)
  906. (match (find (cut string-prefix? <> uri) mirror-urls)
  907. (#f
  908. (loop rest))
  909. (prefix
  910. (make-warning package
  911. (G_ "URL should be \
  912. 'mirror://~a/~a'")
  913. (list mirror-id
  914. (string-drop uri (string-length prefix)))
  915. #:field 'source)))))))
  916. (let ((origin (package-source package)))
  917. (if (and (origin? origin)
  918. (eqv? (origin-method origin) url-fetch))
  919. (let ((uris (origin-uris origin)))
  920. (filter-map check-mirror-uri uris))
  921. '())))
  922. (define* (check-github-url package #:key (timeout 3))
  923. "Check whether PACKAGE uses source URLs that redirect to GitHub."
  924. (define (follow-redirect url)
  925. (let* ((uri (string->uri url))
  926. (port (guix:open-connection-for-uri uri #:timeout timeout))
  927. (response (http-head uri #:port port)))
  928. (close-port port)
  929. (case (response-code response)
  930. ((301 302)
  931. (uri->string (assoc-ref (response-headers response) 'location)))
  932. (else #f))))
  933. (define (follow-redirects-to-github uri)
  934. (cond
  935. ((string-prefix? "https://github.com/" uri) uri)
  936. ((string-prefix? "http" uri)
  937. (and=> (follow-redirect uri) follow-redirects-to-github))
  938. ;; Do not attempt to follow redirects on URIs other than http and https
  939. ;; (such as mirror, file)
  940. (else #f)))
  941. (let ((origin (package-source package)))
  942. (if (and (origin? origin)
  943. (eqv? (origin-method origin) url-fetch))
  944. (filter-map
  945. (lambda (uri)
  946. (and=> (follow-redirects-to-github uri)
  947. (lambda (github-uri)
  948. (if (string=? github-uri uri)
  949. #f
  950. (make-warning
  951. package
  952. (G_ "URL should be '~a'")
  953. (list github-uri)
  954. #:field 'source)))))
  955. (origin-uris origin))
  956. '())))
  957. ;; Guile 3.0.0 does not export this predicate.
  958. (define exception-with-kind-and-args?
  959. (exception-predicate &exception-with-kind-and-args))
  960. (define* (check-derivation package #:key store)
  961. "Emit a warning if we fail to compile PACKAGE to a derivation."
  962. (define (try store system)
  963. (guard (c ((store-protocol-error? c)
  964. (make-warning package
  965. (G_ "failed to create ~a derivation: ~a")
  966. (list system
  967. (store-protocol-error-message c))))
  968. ((exception-with-kind-and-args? c)
  969. (make-warning package
  970. (G_ "failed to create ~a derivation: ~s")
  971. (list system
  972. (cons (exception-kind c)
  973. (exception-args c)))))
  974. ((message-condition? c)
  975. (make-warning package
  976. (G_ "failed to create ~a derivation: ~a")
  977. (list system
  978. (condition-message c))))
  979. ((formatted-message? c)
  980. (let ((str (apply format #f
  981. (formatted-message-string c)
  982. (formatted-message-arguments c))))
  983. (make-warning package
  984. (G_ "failed to create ~a derivation: ~a")
  985. (list system str)))))
  986. (parameterize ((%graft? #f))
  987. (package-derivation store package system #:graft? #f)
  988. ;; If there's a replacement, make sure we can compute its
  989. ;; derivation.
  990. (match (package-replacement package)
  991. (#f #t)
  992. (replacement
  993. (package-derivation store replacement system
  994. #:graft? #f))))))
  995. (define (check-with-store store)
  996. (filter lint-warning?
  997. (map (cut try store <>) (package-supported-systems package))))
  998. ;; For backwards compatability, don't rely on store being set
  999. (or (and=> store check-with-store)
  1000. (with-store store
  1001. (check-with-store store))))
  1002. (define* (check-profile-collisions package #:key store)
  1003. "Check for collisions that would occur when installing PACKAGE as a result
  1004. of the propagated inputs it pulls in."
  1005. (define (do-check store)
  1006. (guard (c ((profile-collision-error? c)
  1007. (let ((first (profile-collision-error-entry c))
  1008. (second (profile-collision-error-conflict c)))
  1009. (define format
  1010. (if (string=? (manifest-entry-version first)
  1011. (manifest-entry-version second))
  1012. manifest-entry-item
  1013. (lambda (entry)
  1014. (string-append (manifest-entry-name entry) "@"
  1015. (manifest-entry-version entry)))))
  1016. (list (make-warning package
  1017. (G_ "propagated inputs ~a and ~a collide")
  1018. (list (format first)
  1019. (format second)))))))
  1020. ;; Disable grafts to avoid building PACKAGE and its dependencies.
  1021. (parameterize ((%graft? #f))
  1022. (run-with-store store
  1023. (mbegin %store-monad
  1024. (check-for-collisions (packages->manifest (list package))
  1025. (%current-system))
  1026. (return '()))))))
  1027. (if store
  1028. (do-check store)
  1029. (with-store store
  1030. (do-check store))))
  1031. (define (check-license package)
  1032. "Warn about type errors of the 'license' field of PACKAGE."
  1033. (match (package-license package)
  1034. ((or (? license?)
  1035. ((? license?) ...))
  1036. '())
  1037. (x
  1038. (list
  1039. (make-warning package (G_ "invalid license field")
  1040. #:field 'license)))))
  1041. (define (call-with-networking-fail-safe message error-value proc)
  1042. "Call PROC catching any network-related errors. Upon a networking error,
  1043. display a message including MESSAGE and return ERROR-VALUE."
  1044. (guard (c ((http-get-error? c)
  1045. (warning (G_ "~a: HTTP GET error for ~a: ~a (~s)~%")
  1046. message
  1047. (uri->string (http-get-error-uri c))
  1048. (http-get-error-code c)
  1049. (http-get-error-reason c))
  1050. error-value))
  1051. (catch #t
  1052. proc
  1053. (match-lambda*
  1054. (('getaddrinfo-error errcode)
  1055. (warning (G_ "~a: host lookup failure: ~a~%")
  1056. message
  1057. (gai-strerror errcode))
  1058. error-value)
  1059. (('tls-certificate-error args ...)
  1060. (warning (G_ "~a: TLS certificate error: ~a")
  1061. message
  1062. (tls-certificate-error-string args))
  1063. error-value)
  1064. ((and ('system-error _ ...) args)
  1065. (let ((errno (system-error-errno args)))
  1066. (if (member errno (list ECONNRESET ECONNABORTED ECONNREFUSED))
  1067. (let ((details (call-with-output-string
  1068. (lambda (port)
  1069. (print-exception port #f (car args)
  1070. (cdr args))))))
  1071. (warning (G_ "~a: ~a~%") message details)
  1072. error-value)
  1073. (apply throw args))))
  1074. (args
  1075. (apply throw args))))))
  1076. (define-syntax-rule (with-networking-fail-safe message error-value exp ...)
  1077. (call-with-networking-fail-safe message error-value
  1078. (lambda () exp ...)))
  1079. (define (current-vulnerabilities*)
  1080. "Like 'current-vulnerabilities', but return the empty list upon networking
  1081. or HTTP errors. This allows network-less operation and makes problems with
  1082. the NIST server non-fatal."
  1083. (with-networking-fail-safe (G_ "while retrieving CVE vulnerabilities")
  1084. '()
  1085. (current-vulnerabilities #:timeout 4)))
  1086. (define package-vulnerabilities
  1087. (let ((lookup (delay (vulnerabilities->lookup-proc
  1088. (current-vulnerabilities*)))))
  1089. (lambda (package)
  1090. "Return a list of vulnerabilities affecting PACKAGE."
  1091. ;; First we retrieve the Common Platform Enumeration (CPE) name and
  1092. ;; version for PACKAGE, then we can pass them to LOOKUP.
  1093. (let ((name (or (assoc-ref (package-properties package)
  1094. 'cpe-name)
  1095. (package-name package)))
  1096. (version (or (assoc-ref (package-properties package)
  1097. 'cpe-version)
  1098. (package-version package))))
  1099. ((force lookup) name version)))))
  1100. (define* (check-vulnerabilities package
  1101. #:optional (package-vulnerabilities
  1102. package-vulnerabilities))
  1103. "Check for known vulnerabilities for PACKAGE. Obtain the list of
  1104. vulnerability records for PACKAGE by calling PACKAGE-VULNERABILITIES."
  1105. (let ((package (or (package-replacement package) package)))
  1106. (match (package-vulnerabilities package)
  1107. (()
  1108. '())
  1109. ((vulnerabilities ...)
  1110. (let* ((patched (package-patched-vulnerabilities package))
  1111. (known-safe (or (assq-ref (package-properties package)
  1112. 'lint-hidden-cve)
  1113. '()))
  1114. (unpatched (remove (lambda (vuln)
  1115. (let ((id (vulnerability-id vuln)))
  1116. (or (member id patched)
  1117. (member id known-safe))))
  1118. vulnerabilities)))
  1119. (if (null? unpatched)
  1120. '()
  1121. (list
  1122. (make-warning
  1123. package
  1124. (G_ "probably vulnerable to ~a")
  1125. (list (string-join (map vulnerability-id unpatched)
  1126. ", "))))))))))
  1127. (define (check-for-updates package)
  1128. "Check if there is an update available for PACKAGE."
  1129. (match (lookup-updater package)
  1130. (#f
  1131. (list (make-warning package (G_ "no updater for ~a")
  1132. (list (package-name package))
  1133. #:field 'source)))
  1134. ((? upstream-updater? updater)
  1135. (match (with-networking-fail-safe
  1136. (format #f (G_ "while retrieving upstream info for '~a'")
  1137. (package-name package))
  1138. #f
  1139. (package-latest-release package))
  1140. ((? upstream-source? source)
  1141. (if (version>? (upstream-source-version source)
  1142. (package-version package))
  1143. (list
  1144. (make-warning package
  1145. (G_ "can be upgraded to ~a")
  1146. (list (upstream-source-version source))
  1147. #:field 'version))
  1148. '()))
  1149. (#f ;cannot find upstream release
  1150. (list (make-warning package
  1151. (G_ "updater '~a' failed to find \
  1152. upstream releases")
  1153. (list (upstream-updater-name updater))
  1154. #:field 'source)))))))
  1155. (define (lookup-disarchive-spec hash)
  1156. "If Disarchive mirrors have a spec for HASH, return the list of SWH
  1157. directory identifiers the spec refers to. Otherwise return #f."
  1158. (define (extract-swh-id spec)
  1159. ;; Return the list of SWH directory identifiers SPEC refers to, where SPEC
  1160. ;; is a Disarchive sexp. Instead of attempting to parse it, traverse it
  1161. ;; in a pretty unintelligent fashion.
  1162. (let loop ((sexp spec)
  1163. (ids '()))
  1164. (match sexp
  1165. ((? string? str)
  1166. (let ((prefix "swh:1:dir:"))
  1167. (if (string-prefix? prefix str)
  1168. (cons (string-drop str (string-length prefix)) ids)
  1169. ids)))
  1170. ((head tail ...)
  1171. (loop tail (loop head ids)))
  1172. (_ ids))))
  1173. (any (lambda (mirror)
  1174. (with-networking-fail-safe
  1175. (format #f (G_ "failed to access Disarchive database at ~a")
  1176. mirror)
  1177. #f
  1178. (guard (c ((http-get-error? c) #f))
  1179. (let* ((url (string-append mirror
  1180. (symbol->string
  1181. (content-hash-algorithm hash))
  1182. "/"
  1183. (bytevector->base16-string
  1184. (content-hash-value hash))))
  1185. (port (http-fetch (string->uri url) #:text? #t))
  1186. (spec (read port)))
  1187. (close-port port)
  1188. (extract-swh-id spec)))))
  1189. %disarchive-mirrors))
  1190. (define (check-archival package)
  1191. "Check whether PACKAGE's source code is archived on Software Heritage. If
  1192. it's not, and if its source code is a VCS snapshot, then send a \"save\"
  1193. request to Software Heritage.
  1194. Software Heritage imposes limits on the request rate per client IP address.
  1195. This checker prints a notice and stops doing anything once that limit has been
  1196. reached."
  1197. (define (response->warning url method response)
  1198. (if (request-rate-limit-reached? url method)
  1199. (list (make-warning package
  1200. (G_ "Software Heritage rate limit reached; \
  1201. try again later")
  1202. #:field 'source))
  1203. (list (make-warning package
  1204. (G_ "'~a' returned ~a")
  1205. (list url (response-code response))
  1206. #:field 'source))))
  1207. (define skip-key (gensym "skip-archival-check"))
  1208. (define (skip-when-limit-reached url method)
  1209. (or (not (request-rate-limit-reached? url method))
  1210. (throw skip-key #t)))
  1211. (parameterize ((%allow-request? skip-when-limit-reached))
  1212. (catch #t
  1213. (lambda ()
  1214. (match (and (origin? (package-source package))
  1215. (package-source package))
  1216. (#f ;no source
  1217. '())
  1218. ((= origin-uri (? git-reference? reference))
  1219. (define url
  1220. (git-reference-url reference))
  1221. (define commit
  1222. (git-reference-commit reference))
  1223. (match (if (commit-id? commit)
  1224. (or (lookup-revision commit)
  1225. (lookup-origin-revision url commit))
  1226. (lookup-origin-revision url commit))
  1227. ((? revision? revision)
  1228. '())
  1229. (#f
  1230. ;; Revision is missing from the archive, attempt to save it.
  1231. (catch 'swh-error
  1232. (lambda ()
  1233. (save-origin (git-reference-url reference) "git")
  1234. (list (make-warning
  1235. package
  1236. ;; TRANSLATORS: "Software Heritage" is a proper noun
  1237. ;; that must remain untranslated. See
  1238. ;; <https://www.softwareheritage.org>.
  1239. (G_ "scheduled Software Heritage archival")
  1240. #:field 'source)))
  1241. (lambda (key url method response . _)
  1242. (cond ((= 429 (response-code response))
  1243. (list (make-warning
  1244. package
  1245. (G_ "archival rate limit exceeded; \
  1246. try again later")
  1247. #:field 'source)))
  1248. (else
  1249. (response->warning url method response))))))))
  1250. ((? origin? origin)
  1251. ;; Since "save" origins are not supported for non-VCS source, all
  1252. ;; we can do is tell whether a given tarball is available or not.
  1253. (if (and=> (origin-hash origin) ;XXX: for ungoogled-chromium
  1254. content-hash-value) ;& icecat
  1255. (let ((hash (origin-hash origin)))
  1256. (match (lookup-content (content-hash-value hash)
  1257. (symbol->string
  1258. (content-hash-algorithm hash)))
  1259. (#f
  1260. ;; If SWH doesn't have HASH as is, it may be because it's
  1261. ;; a hand-crafted tarball. In that case, check whether
  1262. ;; the Disarchive database has an entry for that tarball.
  1263. (match (lookup-disarchive-spec hash)
  1264. (#f
  1265. (list (make-warning package
  1266. (G_ "source not archived on Software \
  1267. Heritage and missing from the Disarchive database")
  1268. #:field 'source)))
  1269. (directory-ids
  1270. (match (find (lambda (id)
  1271. (not (lookup-directory id)))
  1272. directory-ids)
  1273. (#f '())
  1274. (id
  1275. (list (make-warning package
  1276. (G_ "
  1277. Disarchive entry refers to non-existent SWH directory '~a'")
  1278. (list id)
  1279. #:field 'source)))))))
  1280. ((? content?)
  1281. '())))
  1282. '()))))
  1283. (match-lambda*
  1284. (('swh-error url method response)
  1285. (response->warning url method response))
  1286. ((key . args)
  1287. (if (eq? key skip-key)
  1288. '()
  1289. (with-networking-fail-safe
  1290. (G_ "while connecting to Software Heritage")
  1291. '()
  1292. (apply throw key args))))))))
  1293. (define (check-haskell-stackage package)
  1294. "Check whether PACKAGE is a Haskell package ahead of the current
  1295. Stackage LTS version."
  1296. (match (with-networking-fail-safe
  1297. (format #f (G_ "while retrieving upstream info for '~a'")
  1298. (package-name package))
  1299. #f
  1300. (package-latest-release package (list %stackage-updater)))
  1301. ((? upstream-source? source)
  1302. (if (version>? (package-version package)
  1303. (upstream-source-version source))
  1304. (list
  1305. (make-warning package
  1306. (G_ "ahead of Stackage LTS version ~a")
  1307. (list (upstream-source-version source))
  1308. #:field 'version))
  1309. '()))
  1310. (#f '())))
  1311. ;;;
  1312. ;;; Source code formatting.
  1313. ;;;
  1314. (define (report-tabulations package line line-number)
  1315. "Warn about tabulations found in LINE."
  1316. (match (string-index line #\tab)
  1317. (#f #f)
  1318. (index
  1319. (make-warning package
  1320. (G_ "tabulation on line ~a, column ~a")
  1321. (list line-number index)
  1322. #:location
  1323. (location (package-file package)
  1324. line-number
  1325. index)))))
  1326. (define (report-trailing-white-space package line line-number)
  1327. "Warn about trailing white space in LINE."
  1328. (and (not (or (string=? line (string-trim-right line))
  1329. (string=? line (string #\page))))
  1330. (make-warning package
  1331. (G_ "trailing white space on line ~a")
  1332. (list line-number)
  1333. #:location
  1334. (location (package-file package)
  1335. line-number
  1336. 0))))
  1337. (define (report-long-line package line line-number)
  1338. "Emit a warning if LINE is too long."
  1339. ;; Note: We don't warn at 80 characters because sometimes hashes and URLs
  1340. ;; make it hard to fit within that limit and we want to avoid making too
  1341. ;; much noise.
  1342. (and (> (string-length line) 90)
  1343. (make-warning package
  1344. (G_ "line ~a is way too long (~a characters)")
  1345. (list line-number (string-length line))
  1346. #:location
  1347. (location (package-file package)
  1348. line-number
  1349. 0))))
  1350. (define %hanging-paren-rx
  1351. (make-regexp "^[[:blank:]]*[()]+[[:blank:]]*$"))
  1352. (define (report-lone-parentheses package line line-number)
  1353. "Emit a warning if LINE contains hanging parentheses."
  1354. (and (regexp-exec %hanging-paren-rx line)
  1355. (make-warning package
  1356. (G_ "parentheses feel lonely, \
  1357. move to the previous or next line")
  1358. (list line-number)
  1359. #:location
  1360. (location (package-file package)
  1361. line-number
  1362. 0))))
  1363. (define %formatting-reporters
  1364. ;; List of procedures that report formatting issues. These are not separate
  1365. ;; checkers because they would need to re-read the file.
  1366. (list report-tabulations
  1367. report-trailing-white-space
  1368. report-long-line
  1369. report-lone-parentheses))
  1370. (define* (report-formatting-issues package file starting-line
  1371. #:key (reporters %formatting-reporters))
  1372. "Report white-space issues in FILE starting from STARTING-LINE, and report
  1373. them for PACKAGE."
  1374. (define (sexp-last-line port)
  1375. ;; Return the last line of the sexp read from PORT or an estimate thereof.
  1376. (define &failure (list 'failure))
  1377. (let ((start (ftell port))
  1378. (start-line (port-line port))
  1379. (sexp (catch 'read-error
  1380. (lambda () (read port))
  1381. (const &failure))))
  1382. (let ((line (port-line port)))
  1383. (seek port start SEEK_SET)
  1384. (set-port-line! port start-line)
  1385. (if (eq? sexp &failure)
  1386. (+ start-line 60) ;conservative estimate
  1387. line))))
  1388. (call-with-input-file file
  1389. (lambda (port)
  1390. (let loop ((line-number 1)
  1391. (last-line #f)
  1392. (warnings '()))
  1393. (let ((line (read-line port)))
  1394. (if (or (eof-object? line)
  1395. (and last-line (> line-number last-line)))
  1396. warnings
  1397. (if (and (= line-number starting-line)
  1398. (not last-line))
  1399. (loop (+ 1 line-number)
  1400. (+ 1 (sexp-last-line port))
  1401. warnings)
  1402. (loop (+ 1 line-number)
  1403. last-line
  1404. (append
  1405. warnings
  1406. (if (< line-number starting-line)
  1407. '()
  1408. (filter-map (lambda (report)
  1409. (report package line line-number))
  1410. reporters)))))))))))
  1411. (define (check-formatting package)
  1412. "Check the formatting of the source code of PACKAGE."
  1413. (let ((location (package-location package)))
  1414. (if location
  1415. ;; Report issues starting from the line before the 'package'
  1416. ;; form, which usually contains the 'define' form.
  1417. (let ((line (- (location-line location) 1)))
  1418. (match (search-path %load-path (location-file location))
  1419. ((? string? file)
  1420. (report-formatting-issues package file line))
  1421. (#f
  1422. ;; It could be that LOCATION lists a "true" relative file
  1423. ;; name--i.e., not relative to an element of %LOAD-PATH.
  1424. (let ((file (location-file location)))
  1425. (if (file-exists? file)
  1426. (report-formatting-issues package file line)
  1427. (list (make-warning package
  1428. (G_ "source file not found"))))))))
  1429. '())))
  1430. ;;;
  1431. ;;; List of checkers.
  1432. ;;;
  1433. (define %local-checkers
  1434. (list
  1435. (lint-checker
  1436. (name 'name)
  1437. (description "Validate package names")
  1438. (check check-name))
  1439. (lint-checker
  1440. (name 'tests-true)
  1441. (description "Check if tests are explicitly enabled")
  1442. (check check-tests-true))
  1443. (lint-checker
  1444. (name 'description)
  1445. (description "Validate package descriptions")
  1446. (check check-description-style))
  1447. (lint-checker
  1448. (name 'inputs-should-be-native)
  1449. (description "Identify inputs that should be native inputs")
  1450. (check check-inputs-should-be-native))
  1451. (lint-checker
  1452. (name 'inputs-should-not-be-input)
  1453. (description "Identify inputs that shouldn't be inputs at all")
  1454. (check check-inputs-should-not-be-an-input-at-all))
  1455. (lint-checker
  1456. (name 'license)
  1457. ;; TRANSLATORS: <license> is the name of a data type and must not be
  1458. ;; translated.
  1459. (description "Make sure the 'license' field is a <license> \
  1460. or a list thereof")
  1461. (check check-license))
  1462. (lint-checker
  1463. (name 'mirror-url)
  1464. (description "Suggest 'mirror://' URLs")
  1465. (check check-mirror-url))
  1466. (lint-checker
  1467. (name 'source-file-name)
  1468. (description "Validate file names of sources")
  1469. (check check-source-file-name))
  1470. (lint-checker
  1471. (name 'source-unstable-tarball)
  1472. (description "Check for autogenerated tarballs")
  1473. (check check-source-unstable-tarball))
  1474. (lint-checker
  1475. (name 'derivation)
  1476. (description "Report failure to compile a package to a derivation")
  1477. (check check-derivation)
  1478. (requires-store? #t))
  1479. (lint-checker
  1480. (name 'profile-collisions)
  1481. (description "Report collisions that would occur due to propagated inputs")
  1482. (check check-profile-collisions)
  1483. (requires-store? #t))
  1484. (lint-checker
  1485. (name 'patch-file-names)
  1486. (description "Validate file names and availability of patches")
  1487. (check check-patch-file-names))
  1488. (lint-checker
  1489. (name 'patch-headers)
  1490. (description "Validate patch headers")
  1491. (check check-patch-headers))
  1492. (lint-checker
  1493. (name 'formatting)
  1494. (description "Look for formatting issues in the source")
  1495. (check check-formatting))))
  1496. (define %network-dependent-checkers
  1497. (list
  1498. (lint-checker
  1499. (name 'synopsis)
  1500. (description "Validate package synopses")
  1501. (check check-synopsis-style))
  1502. (lint-checker
  1503. (name 'gnu-description)
  1504. (description "Validate synopsis & description of GNU packages")
  1505. (check check-gnu-synopsis+description))
  1506. (lint-checker
  1507. (name 'home-page)
  1508. (description "Validate home-page URLs")
  1509. (check check-home-page))
  1510. (lint-checker
  1511. (name 'source)
  1512. (description "Validate source URLs")
  1513. (check check-source))
  1514. (lint-checker
  1515. (name 'github-url)
  1516. (description "Suggest GitHub URLs")
  1517. (check check-github-url))
  1518. (lint-checker
  1519. (name 'cve)
  1520. (description "Check the Common Vulnerabilities and Exposures\
  1521. (CVE) database")
  1522. (check check-vulnerabilities))
  1523. (lint-checker
  1524. (name 'refresh)
  1525. (description "Check the package for new upstream releases")
  1526. (check check-for-updates))
  1527. (lint-checker
  1528. (name 'archival)
  1529. (description "Ensure source code archival on Software Heritage")
  1530. (check check-archival))
  1531. (lint-checker
  1532. (name 'haskell-stackage)
  1533. (description "Ensure Haskell packages use Stackage LTS versions")
  1534. (check check-haskell-stackage))))
  1535. (define %all-checkers
  1536. (append %local-checkers
  1537. %network-dependent-checkers))