lint.scm 60 KB

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