lint.scm 56 KB

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