lint.scm 79 KB

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