go.scm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020 Katherine Cox-Buday <cox.katherine.e@gmail.com>
  3. ;;; Copyright © 2020 Helio Machado <0x2b3bfa0+guix@googlemail.com>
  4. ;;; Copyright © 2021 François Joulaud <francois.joulaud@radiofrance.com>
  5. ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  6. ;;; Copyright © 2021-2022 Ludovic Courtès <ludo@gnu.org>
  7. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  8. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  9. ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
  10. ;;;
  11. ;;; This file is part of GNU Guix.
  12. ;;;
  13. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  14. ;;; under the terms of the GNU General Public License as published by
  15. ;;; the Free Software Foundation; either version 3 of the License, or (at
  16. ;;; your option) any later version.
  17. ;;;
  18. ;;; GNU Guix is distributed in the hope that it will be useful, but
  19. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;;; GNU General Public License for more details.
  22. ;;;
  23. ;;; You should have received a copy of the GNU General Public License
  24. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  25. (define-module (guix import go)
  26. #:use-module (guix build-system go)
  27. #:use-module (guix git)
  28. #:use-module (guix hash)
  29. #:use-module (guix i18n)
  30. #:use-module (guix diagnostics)
  31. #:use-module (guix import utils)
  32. #:use-module (guix import json)
  33. #:use-module (guix packages)
  34. #:use-module ((guix utils) #:select (string-replace-substring))
  35. #:use-module (guix http-client)
  36. #:use-module ((guix licenses) #:prefix license:)
  37. #:use-module (guix memoization)
  38. #:autoload (htmlprag) (html->sxml) ;from Guile-Lib
  39. #:autoload (guix serialization) (write-file)
  40. #:autoload (guix base32) (bytevector->nix-base32-string)
  41. #:autoload (guix build utils) (mkdir-p)
  42. #:autoload (gcrypt hash) (hash-algorithm sha256)
  43. #:use-module (ice-9 format)
  44. #:use-module (ice-9 match)
  45. #:use-module (ice-9 peg)
  46. #:use-module (ice-9 rdelim)
  47. #:use-module (ice-9 receive)
  48. #:use-module (ice-9 regex)
  49. #:use-module (ice-9 textual-ports)
  50. #:use-module ((rnrs io ports) #:select (call-with-port))
  51. #:use-module (srfi srfi-1)
  52. #:use-module (srfi srfi-2)
  53. #:use-module (srfi srfi-9)
  54. #:use-module (srfi srfi-11)
  55. #:use-module (srfi srfi-26)
  56. #:use-module (srfi srfi-34)
  57. #:use-module (srfi srfi-35)
  58. #:use-module (sxml match)
  59. #:use-module ((sxml xpath) #:renamer (lambda (s)
  60. (if (eq? 'filter s)
  61. 'xfilter
  62. s)))
  63. #:use-module (web client)
  64. #:use-module (web response)
  65. #:use-module (web uri)
  66. #:export (go-module->guix-package
  67. go-module->guix-package*
  68. go-module-recursive-import))
  69. ;;; Commentary:
  70. ;;;
  71. ;;; (guix import go) attempts to make it easier to create Guix package
  72. ;;; declarations for Go modules.
  73. ;;;
  74. ;;; Modules in Go are a "collection of related Go packages" which are "the
  75. ;;; unit of source code interchange and versioning". Modules are generally
  76. ;;; hosted in a repository.
  77. ;;;
  78. ;;; At this point it should handle correctly modules which have only Go
  79. ;;; dependencies and are accessible from proxy.golang.org (or configured via
  80. ;;; GOPROXY).
  81. ;;;
  82. ;;; We want it to work more or less this way:
  83. ;;; - get latest version for the module from GOPROXY
  84. ;;; - infer VCS root repo from which we will check-out source by
  85. ;;; + recognising known patterns (like github.com)
  86. ;;; + or recognizing .vcs suffix
  87. ;;; + or parsing meta tag in HTML served at the URL
  88. ;;; + or (TODO) if nothing else works by using zip file served by GOPROXY
  89. ;;; - get go.mod from GOPROXY (which is able to synthetize one if needed)
  90. ;;; - extract list of dependencies from this go.mod
  91. ;;;
  92. ;;; The Go module paths are translated to a Guix package name under the
  93. ;;; assumption that there will be no collision.
  94. ;;; TODO list
  95. ;;; - get correct hash in vcs->origin for Mercurial and Subversion
  96. ;;; Code:
  97. (define http-fetch*
  98. ;; Like http-fetch, but memoized and returning the body as a string.
  99. (memoize (lambda args
  100. (call-with-port (apply http-fetch args) get-string-all))))
  101. (define json-fetch*
  102. (memoize json-fetch))
  103. (define (go-path-escape path)
  104. "Escape a module path by replacing every uppercase letter with an
  105. exclamation mark followed with its lowercase equivalent, as per the module
  106. Escaped Paths specification (see:
  107. https://godoc.org/golang.org/x/mod/module#hdr-Escaped_Paths)."
  108. (define (escape occurrence)
  109. (string-append "!" (string-downcase (match:substring occurrence))))
  110. (regexp-substitute/global #f "[A-Z]" path 'pre escape 'post))
  111. ;; Prevent inlining of this procedure, which is accessed by unit tests.
  112. (set! go-path-escape go-path-escape)
  113. (define (go.pkg.dev-info name)
  114. (http-fetch* (string-append "https://pkg.go.dev/" name)))
  115. (define* (go-module-version-string goproxy name #:key version)
  116. "Fetch the version string of the latest version for NAME from the given
  117. GOPROXY server, or for VERSION when specified."
  118. (let ((file (if version
  119. (string-append "@v/" version ".info")
  120. "@latest")))
  121. (assoc-ref (json-fetch* (format #f "~a/~a/~a"
  122. goproxy (go-path-escape name) file))
  123. "Version")))
  124. (define* (go-module-available-versions goproxy name)
  125. "Retrieve the available versions for a given module from the module proxy.
  126. Versions are being returned **unordered** and may contain different versioning
  127. styles for the same package."
  128. (let* ((url (string-append goproxy "/" (go-path-escape name) "/@v/list"))
  129. (body (http-fetch* url))
  130. (versions (remove string-null? (string-split body #\newline))))
  131. (if (null? versions)
  132. (list (go-module-version-string goproxy name)) ;latest version
  133. versions)))
  134. (define (go-package-licenses name)
  135. "Retrieve the list of licenses that apply to NAME, a Go package or module
  136. name (e.g. \"github.com/golang/protobuf/proto\")."
  137. (let* ((body (go.pkg.dev-info (string-append name "?tab=licenses")))
  138. ;; Extract the text contained in a h2 child node of any
  139. ;; element marked with a "License" class attribute.
  140. (select (sxpath `(// (* (@ (equal? (class "License"))))
  141. h2 // div // *text*))))
  142. (select (html->sxml body #:strict? #t))))
  143. (define (sxml->texi sxml-node)
  144. "A very basic SXML to Texinfo converter which attempts to preserve HTML
  145. formatting and links as text."
  146. (sxml-match sxml-node
  147. ((strong ,text)
  148. (format #f "@strong{~a}" text))
  149. ((a (@ (href ,url)) ,text)
  150. (format #f "@url{~a,~a}" url text))
  151. ((code ,text)
  152. (format #f "@code{~a}" text))
  153. (,something-else something-else)))
  154. (define (go-package-description name)
  155. "Retrieve a short description for NAME, a Go package name,
  156. e.g. \"google.golang.org/protobuf/proto\"."
  157. (let* ((body (go.pkg.dev-info name))
  158. (sxml (html->sxml body #:strict? #t))
  159. (overview ((sxpath
  160. `(//
  161. (* (@ (equal? (class "Documentation-overview"))))
  162. (p 1))) sxml))
  163. ;; Sometimes, the first paragraph just contains images/links that
  164. ;; has only "\n" for text. The following filter is designed to
  165. ;; omit it.
  166. (contains-text? (lambda (node)
  167. (remove string-null?
  168. (map string-trim-both
  169. (filter (node-typeof? '*text*)
  170. (cdr node))))))
  171. (select-content (sxpath
  172. `(//
  173. (* (@ (equal? (class "UnitReadme-content"))))
  174. div // p ,(xfilter contains-text?))))
  175. ;; Fall-back to use content; this is less desirable as it is more
  176. ;; verbose, but not every page has an overview.
  177. (description (if (not (null? overview))
  178. overview
  179. (select-content sxml)))
  180. (description* (if (not (null? description))
  181. (first description)
  182. description)))
  183. (match description*
  184. (() #f) ;nothing selected
  185. ((p elements ...)
  186. (apply string-append (filter string? (map sxml->texi elements)))))))
  187. (define (go-package-synopsis module-name)
  188. "Retrieve a short synopsis for a Go module named MODULE-NAME,
  189. e.g. \"google.golang.org/protobuf\". The data is scraped from
  190. the https://pkg.go.dev/ web site."
  191. ;; Note: Only the *module* (rather than package) page has the README title
  192. ;; used as a synopsis on the https://pkg.go.dev web site.
  193. (let* ((url (string-append "https://pkg.go.dev/" module-name))
  194. (body (http-fetch* url))
  195. ;; Extract the text contained in a h2 child node of any
  196. ;; element marked with a "License" class attribute.
  197. (select-title (sxpath
  198. `(// (div (@ (equal? (class "UnitReadme-content"))))
  199. // h3 *text*))))
  200. (match (select-title (html->sxml body #:strict? #t))
  201. (() #f) ;nothing selected
  202. ((title more ...) ;title is the first string of the list
  203. (string-trim-both title)))))
  204. (define (list->licenses licenses)
  205. "Given a list of LICENSES mostly following the SPDX conventions, return the
  206. corresponding Guix license or 'unknown-license!"
  207. (filter-map (lambda (license)
  208. (and (not (string-null? license))
  209. (not (any (cut string=? <> license)
  210. '("AND" "OR" "WITH")))
  211. ;; Adjust the license names scraped from
  212. ;; https://pkg.go.dev to an equivalent SPDX identifier,
  213. ;; if they differ (see: https://github.com/golang/pkgsite
  214. ;; /internal/licenses/licenses.go#L174).
  215. (or (spdx-string->license
  216. (match license
  217. ("BlueOak-1.0" "BlueOak-1.0.0")
  218. ("BSD-0-Clause" "0BSD")
  219. ("BSD-2-Clause" "BSD-2-Clause-FreeBSD")
  220. ("GPL2" "GPL-2.0")
  221. ("GPL3" "GPL-3.0")
  222. ("NIST" "NIST-PD")
  223. (_ license)))
  224. 'unknown-license!)))
  225. licenses))
  226. (define (fetch-go.mod goproxy module-path version)
  227. "Fetch go.mod from the given GOPROXY server for the given MODULE-PATH
  228. and VERSION and return an input port."
  229. (let ((url (format #f "~a/~a/@v/~a.mod" goproxy
  230. (go-path-escape module-path)
  231. (go-path-escape version))))
  232. (http-fetch* url)))
  233. (define (parse-go.mod content)
  234. "Parse the go.mod file CONTENT, returning a list of directives, comments,
  235. and unknown lines. Each sublist begins with a symbol (go, module, require,
  236. replace, exclude, retract, comment, or unknown) and is followed by one or more
  237. sublists. Each sublist begins with a symbol (module-path, version, file-path,
  238. comment, or unknown) and is followed by the indicated data."
  239. ;; https://golang.org/ref/mod#go-mod-file-grammar
  240. (define-peg-pattern NL none "\n")
  241. (define-peg-pattern WS none (or " " "\t" "\r"))
  242. (define-peg-pattern => none (and (* WS) "=>"))
  243. (define-peg-pattern punctuation none (or "," "=>" "[" "]" "(" ")"))
  244. (define-peg-pattern comment all
  245. (and (ignore "//") (* WS) (* (and (not-followed-by NL) peg-any))))
  246. (define-peg-pattern EOL body (and (* WS) (? comment) NL))
  247. (define-peg-pattern block-start none (and (* WS) "(" EOL))
  248. (define-peg-pattern block-end none (and (* WS) ")" EOL))
  249. (define-peg-pattern any-line body
  250. (and (* WS) (* (and (not-followed-by NL) peg-any)) EOL))
  251. ;; Strings and identifiers
  252. (define-peg-pattern identifier body
  253. (+ (and (not-followed-by (or NL WS punctuation)) peg-any)))
  254. (define-peg-pattern string-raw body
  255. (and (ignore "`") (+ (and (not-followed-by "`") peg-any)) (ignore "`")))
  256. (define-peg-pattern string-quoted body
  257. (and (ignore "\"")
  258. (+ (or (and (ignore "\\") peg-any)
  259. (and (not-followed-by "\"") peg-any)))
  260. (ignore "\"")))
  261. (define-peg-pattern string-or-ident body
  262. (and (* WS) (or string-raw string-quoted identifier)))
  263. (define-peg-pattern version all string-or-ident)
  264. (define-peg-pattern module-path all string-or-ident)
  265. (define-peg-pattern file-path all string-or-ident)
  266. ;; Non-directive lines
  267. (define-peg-pattern unknown all any-line)
  268. (define-peg-pattern block-line body
  269. (or EOL (and (not-followed-by block-end) unknown)))
  270. ;; GoDirective = "go" GoVersion newline .
  271. (define-peg-pattern go all (and (ignore "go") version EOL))
  272. ;; ModuleDirective = "module" ( ModulePath | "(" newline ModulePath newline ")" ) newline .
  273. (define-peg-pattern module all
  274. (and (ignore "module") (or (and block-start module-path EOL block-end)
  275. (and module-path EOL))))
  276. ;; The following directives may all be used solo or in a block
  277. ;; RequireSpec = ModulePath Version newline .
  278. (define-peg-pattern require all (and module-path version EOL))
  279. (define-peg-pattern require-top body
  280. (and (ignore "require")
  281. (or (and block-start (* (or require block-line)) block-end) require)))
  282. ;; ExcludeSpec = ModulePath Version newline .
  283. (define-peg-pattern exclude all (and module-path version EOL))
  284. (define-peg-pattern exclude-top body
  285. (and (ignore "exclude")
  286. (or (and block-start (* (or exclude block-line)) block-end) exclude)))
  287. ;; ReplaceSpec = ModulePath [ Version ] "=>" FilePath newline
  288. ;; | ModulePath [ Version ] "=>" ModulePath Version newline .
  289. (define-peg-pattern original all (or (and module-path version) module-path))
  290. (define-peg-pattern with all (or (and module-path version) file-path))
  291. (define-peg-pattern replace all (and original => with EOL))
  292. (define-peg-pattern replace-top body
  293. (and (ignore "replace")
  294. (or (and block-start (* (or replace block-line)) block-end) replace)))
  295. ;; RetractSpec = ( Version | "[" Version "," Version "]" ) newline .
  296. (define-peg-pattern range all
  297. (and (* WS) (ignore "[") version
  298. (* WS) (ignore ",") version (* WS) (ignore "]")))
  299. (define-peg-pattern retract all (and (or range version) EOL))
  300. (define-peg-pattern retract-top body
  301. (and (ignore "retract")
  302. (or (and block-start (* (or retract block-line)) block-end) retract)))
  303. (define-peg-pattern go-mod body
  304. (* (and (* WS) (or go module require-top exclude-top replace-top
  305. retract-top EOL unknown))))
  306. (let ((tree (peg:tree (match-pattern go-mod content)))
  307. (keywords '(go module require replace exclude retract comment unknown)))
  308. (keyword-flatten keywords tree)))
  309. ;; Prevent inlining of this procedure, which is accessed by unit tests.
  310. (set! parse-go.mod parse-go.mod)
  311. (define (go.mod-directives go.mod directive)
  312. "Return the list of top-level directive bodies in GO.MOD matching the symbol
  313. DIRECTIVE."
  314. (filter-map (match-lambda
  315. (((? (cut eq? <> directive) head) . rest) rest)
  316. (_ #f))
  317. go.mod))
  318. (define (go.mod-requirements go.mod)
  319. "Compute and return the list of requirements specified by GO.MOD."
  320. (define (replace directive requirements)
  321. (define (maybe-replace module-path new-requirement)
  322. ;; Do not allow version updates for indirect dependencies (see:
  323. ;; https://golang.org/ref/mod#go-mod-file-replace).
  324. (if (and (equal? module-path (first new-requirement))
  325. (not (assoc-ref requirements module-path)))
  326. requirements
  327. (cons new-requirement (alist-delete module-path requirements))))
  328. (match directive
  329. ((('original ('module-path module-path) . _) with . _)
  330. (match with
  331. (('with ('file-path _) . _)
  332. (alist-delete module-path requirements))
  333. (('with ('module-path new-module-path) ('version new-version) . _)
  334. (maybe-replace module-path
  335. (list new-module-path new-version)))))))
  336. (define (require directive requirements)
  337. (match directive
  338. ((('module-path module-path) ('version version) . _)
  339. (cons (list module-path version) requirements))))
  340. (let* ((requires (go.mod-directives go.mod 'require))
  341. (replaces (go.mod-directives go.mod 'replace))
  342. (requirements (fold require '() requires)))
  343. (fold replace requirements replaces)))
  344. ;; Prevent inlining of this procedure, which is accessed by unit tests.
  345. (set! go.mod-requirements go.mod-requirements)
  346. (define-record-type <vcs>
  347. (%make-vcs url-prefix root-regex type)
  348. vcs?
  349. (url-prefix vcs-url-prefix)
  350. (root-regex vcs-root-regex)
  351. (type vcs-type))
  352. (define (make-vcs prefix regexp type)
  353. (%make-vcs prefix (make-regexp regexp) type))
  354. (define known-vcs
  355. ;; See the following URL for the official Go equivalent:
  356. ;; https://github.com/golang/go/blob/846dce9d05f19a1f53465e62a304dea21b99f910/src/cmd/go/internal/vcs/vcs.go#L1026-L1087
  357. (list
  358. (make-vcs
  359. "github.com"
  360. "^(github\\.com/[A-Za-z0-9_.\\-]+/[A-Za-z0-9_.\\-]+)(/[A-Za-z0-9_.\\-]+)*$"
  361. 'git)
  362. (make-vcs
  363. "bitbucket.org"
  364. "^(bitbucket\\.org/([A-Za-z0-9_.\\-]+/[A-Za-z0-9_.\\-]+))(/[A-Za-z0-9_.\\-]+)*$"
  365. 'unknown)
  366. (make-vcs
  367. "hub.jazz.net/git/"
  368. "^(hub\\.jazz\\.net/git/[a-z0-9]+/[A-Za-z0-9_.\\-]+)(/[A-Za-z0-9_.\\-]+)*$"
  369. 'git)
  370. (make-vcs
  371. "git.apache.org"
  372. "^(git\\.apache\\.org/[a-z0-9_.\\-]+\\.git)(/[A-Za-z0-9_.\\-]+)*$"
  373. 'git)
  374. (make-vcs
  375. "git.openstack.org"
  376. "^(git\\.openstack\\.org/[A-Za-z0-9_.\\-]+/[A-Za-z0-9_.\\-]+)(\\.git)?\
  377. (/[A-Za-z0-9_.\\-]+)*$"
  378. 'git)))
  379. (define (module-path->repository-root module-path)
  380. "Infer the repository root from a module path. Go modules can be
  381. defined at any level of a repository tree, but querying for the meta tag
  382. usually can only be done from the web page at the root of the repository,
  383. hence the need to derive this information."
  384. ;; For reference, see: https://golang.org/ref/mod#vcs-find.
  385. (define vcs-qualifiers '(".bzr" ".fossil" ".git" ".hg" ".svn"))
  386. (define (vcs-qualified-module-path->root-repo-url module-path)
  387. (let* ((vcs-qualifiers-group (string-join vcs-qualifiers "|"))
  388. (pattern (format #f "^(.*(~a))(/|$)" vcs-qualifiers-group))
  389. (m (string-match pattern module-path)))
  390. (and=> m (cut match:substring <> 1))))
  391. (or (and=> (find (lambda (vcs)
  392. (string-prefix? (vcs-url-prefix vcs) module-path))
  393. known-vcs)
  394. (lambda (vcs)
  395. (match:substring (regexp-exec (vcs-root-regex vcs)
  396. module-path) 1)))
  397. (vcs-qualified-module-path->root-repo-url module-path)
  398. module-path))
  399. (define* (go-module->guix-package-name module-path #:optional version)
  400. "Converts a module's path to the canonical Guix format for Go packages.
  401. Optionally include a VERSION string to append to the name."
  402. ;; Map dot, slash, underscore and tilde characters to hyphens.
  403. (let ((module-path* (string-map (lambda (c)
  404. (if (member c '(#\. #\/ #\_ #\~))
  405. #\-
  406. c))
  407. module-path)))
  408. (string-downcase (string-append "go-" module-path*
  409. (if version
  410. (string-append "-" version)
  411. "")))))
  412. (define (strip-.git-suffix/maybe repo-url)
  413. "Strip a repository URL '.git' suffix from REPO-URL if hosted at GitHub."
  414. (match repo-url
  415. ((and (? (cut string-prefix? "https://github.com" <>))
  416. (? (cut string-suffix? ".git" <>)))
  417. (string-drop-right repo-url 4))
  418. (_ repo-url)))
  419. (define-record-type <module-meta>
  420. (make-module-meta import-prefix vcs repo-root)
  421. module-meta?
  422. (import-prefix module-meta-import-prefix)
  423. (vcs module-meta-vcs) ;a symbol
  424. (repo-root module-meta-repo-root))
  425. (define (fetch-module-meta-data module-path)
  426. "Retrieve the module meta-data from its landing page. This is necessary
  427. because goproxy servers don't currently provide all the information needed to
  428. build a package."
  429. (define (go-import->module-meta content-text)
  430. (match (string-tokenize content-text char-set:graphic)
  431. ((root-path vcs repo-url)
  432. (make-module-meta root-path (string->symbol vcs)
  433. (strip-.git-suffix/maybe repo-url)))))
  434. ;; <meta name="go-import" content="import-prefix vcs repo-root">
  435. (let* ((meta-data (http-fetch* (format #f "https://~a?go-get=1" module-path)))
  436. (select (sxpath `(// (meta (@ (equal? (name "go-import"))))
  437. // content))))
  438. (match (select (html->sxml meta-data #:strict? #t))
  439. (() #f) ;nothing selected
  440. ((('content content-text) ..1)
  441. (or
  442. (find (lambda (meta)
  443. (string-prefix? (module-meta-import-prefix meta) module-path))
  444. (map go-import->module-meta content-text))
  445. ;; Fallback to the first meta if no import prefixes match.
  446. (go-import->module-meta (first content-text)))))))
  447. (define (module-meta-data-repo-url meta-data goproxy)
  448. "Return the URL where the fetcher which will be used can download the
  449. source."
  450. (if (member (module-meta-vcs meta-data) '(fossil mod))
  451. goproxy
  452. (module-meta-repo-root meta-data)))
  453. (define* (git-checkout-hash url reference algorithm)
  454. "Return the ALGORITHM hash of the checkout of URL at REFERENCE, a commit or
  455. tag."
  456. (define cache
  457. (string-append (or (getenv "TMPDIR") "/tmp")
  458. "/guix-import-go-"
  459. (passwd:name (getpwuid (getuid)))))
  460. ;; Use a custom cache to avoid cluttering the default one under
  461. ;; ~/.cache/guix, but choose one under /tmp so that it's persistent across
  462. ;; subsequent "guix import" invocations.
  463. (mkdir-p cache)
  464. (chmod cache #o700)
  465. (let-values (((checkout commit _)
  466. (parameterize ((%repository-cache-directory cache))
  467. (update-cached-checkout url
  468. #:ref
  469. `(tag-or-commit . ,reference)))))
  470. (file-hash* checkout #:algorithm algorithm #:recursive? #true)))
  471. (define (vcs->origin vcs-type vcs-repo-url version)
  472. "Generate the `origin' block of a package depending on what type of source
  473. control system is being used."
  474. (case vcs-type
  475. ((git)
  476. (let ((plain-version? (string=? version (go-version->git-ref version)))
  477. (v-prefixed? (string-prefix? "v" version)))
  478. `(origin
  479. (method git-fetch)
  480. (uri (git-reference
  481. (url ,vcs-repo-url)
  482. ;; This is done because the version field of the package,
  483. ;; which the generated quoted expression refers to, has been
  484. ;; stripped of any 'v' prefixed.
  485. (commit ,(if (and plain-version? v-prefixed?)
  486. '(string-append "v" version)
  487. '(go-version->git-ref version)))))
  488. (file-name (git-file-name name version))
  489. (sha256
  490. (base32
  491. ,(bytevector->nix-base32-string
  492. (git-checkout-hash vcs-repo-url (go-version->git-ref version)
  493. (hash-algorithm sha256))))))))
  494. ((hg)
  495. `(origin
  496. (method hg-fetch)
  497. (uri (hg-reference
  498. (url ,vcs-repo-url)
  499. (changeset ,version)))
  500. (file-name (string-append name "-" version "-checkout"))
  501. (sha256
  502. (base32
  503. ;; FIXME: populate hash for hg repo checkout
  504. "0000000000000000000000000000000000000000000000000000"))))
  505. ((svn)
  506. `(origin
  507. (method svn-fetch)
  508. (uri (svn-reference
  509. (url ,vcs-repo-url)
  510. (revision (string->number version))))
  511. (file-name (string-append name "-" version "-checkout"))
  512. (sha256
  513. (base32
  514. ;; FIXME: populate hash for svn repo checkout
  515. "0000000000000000000000000000000000000000000000000000"))))
  516. (else
  517. (raise
  518. (formatted-message (G_ "unsupported vcs type '~a' for package '~a'")
  519. vcs-type vcs-repo-url)))))
  520. (define (strip-v-prefix version)
  521. "Strip from VERSION the \"v\" prefix that Go uses."
  522. (string-trim version #\v))
  523. (define (ensure-v-prefix version)
  524. "Add a \"v\" prefix to VERSION if it does not already have one."
  525. (if (string-prefix? "v" version)
  526. version
  527. (string-append "v" version)))
  528. (define (validate-version version available-versions module-path)
  529. "Raise an error if VERSION is not among AVAILABLE-VERSIONS, unless VERSION
  530. is a pseudo-version. Return VERSION."
  531. ;; Pseudo-versions do not appear in the versions list; skip the
  532. ;; following check.
  533. (if (or (go-pseudo-version? version)
  534. (member version available-versions))
  535. version
  536. (raise
  537. (make-compound-condition
  538. (formatted-message (G_ "version ~a of ~a is not available~%")
  539. version module-path available-versions)
  540. (condition (&fix-hint
  541. (hint (format #f (G_ "Pick one of the following \
  542. available versions:~{ ~a~}.")
  543. (map strip-v-prefix
  544. available-versions)))))))))
  545. (define* (go-module->guix-package module-path #:key
  546. (goproxy "https://proxy.golang.org")
  547. version
  548. pin-versions?)
  549. "Return the package S-expression corresponding to MODULE-PATH at VERSION, a Go package.
  550. The meta-data is fetched from the GOPROXY server and https://pkg.go.dev/.
  551. When VERSION is unspecified, the latest version available is used."
  552. (let* ((available-versions (go-module-available-versions goproxy module-path))
  553. (version* (validate-version
  554. (or (and version (ensure-v-prefix version))
  555. (go-module-version-string goproxy module-path)) ;latest
  556. available-versions
  557. module-path))
  558. (content (fetch-go.mod goproxy module-path version*))
  559. (dependencies+versions (go.mod-requirements (parse-go.mod content)))
  560. (dependencies (if pin-versions?
  561. dependencies+versions
  562. (map car dependencies+versions)))
  563. (module-path-sans-suffix
  564. (match:prefix (string-match "([\\./]v[0-9]+)?$" module-path)))
  565. (guix-name (go-module->guix-package-name module-path))
  566. (root-module-path (module-path->repository-root module-path))
  567. ;; The VCS type and URL are not included in goproxy information. For
  568. ;; this we need to fetch it from the official module page.
  569. (meta-data (fetch-module-meta-data root-module-path))
  570. (vcs-type (module-meta-vcs meta-data))
  571. (vcs-repo-url (module-meta-data-repo-url meta-data goproxy))
  572. (synopsis (go-package-synopsis module-path))
  573. (description (go-package-description module-path))
  574. (licenses (go-package-licenses module-path)))
  575. (values
  576. `(package
  577. (name ,guix-name)
  578. (version ,(strip-v-prefix version*))
  579. (source
  580. ,(vcs->origin vcs-type vcs-repo-url version*))
  581. (build-system go-build-system)
  582. (arguments
  583. '(#:import-path ,module-path
  584. ,@(if (string=? module-path-sans-suffix root-module-path)
  585. '()
  586. `(#:unpack-path ,root-module-path))))
  587. ,@(maybe-propagated-inputs
  588. (map (match-lambda
  589. ((name version)
  590. (go-module->guix-package-name name (strip-v-prefix version)))
  591. (name
  592. (go-module->guix-package-name name)))
  593. dependencies))
  594. (home-page ,(format #f "https://~a" root-module-path))
  595. (synopsis ,synopsis)
  596. (description ,(and=> description beautify-description))
  597. (license ,(match (list->licenses licenses)
  598. (() #f) ;unknown license
  599. ((license) ;a single license
  600. license)
  601. ((license ...) ;a list of licenses
  602. `(list ,@license)))))
  603. (if pin-versions?
  604. dependencies+versions
  605. dependencies))))
  606. (define go-module->guix-package*
  607. (lambda args
  608. ;; Disable output buffering so that the following warning gets printed
  609. ;; consistently.
  610. (setvbuf (current-error-port) 'none)
  611. (let ((package-name (match args ((name _ ...) name))))
  612. (guard (c ((http-get-error? c)
  613. (warning (G_ "Failed to import package ~s.
  614. reason: ~s could not be fetched: HTTP error ~a (~s).
  615. This package and its dependencies won't be imported.~%")
  616. package-name
  617. (uri->string (http-get-error-uri c))
  618. (http-get-error-code c)
  619. (http-get-error-reason c))
  620. (values #f '())))
  621. (apply go-module->guix-package args)))))
  622. (define* (go-module-recursive-import package-name
  623. #:key (goproxy "https://proxy.golang.org")
  624. version
  625. pin-versions?)
  626. (recursive-import
  627. package-name
  628. #:repo->guix-package
  629. (memoize
  630. (lambda* (name #:key version repo)
  631. (receive (package-sexp dependencies)
  632. (go-module->guix-package* name #:goproxy goproxy
  633. #:version version
  634. #:pin-versions? pin-versions?)
  635. (values package-sexp dependencies))))
  636. #:guix-name go-module->guix-package-name
  637. #:version version))