lint.scm 72 KB

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