From c1c898c1df14f931be31151713ec4204adee04eb Mon Sep 17 00:00:00 2001 Message-Id: In-Reply-To: References: From: Sarah Morgensen Date: Thu, 5 Aug 2021 21:19:58 -0700 Subject: [PATCH 2/2] import: go: Check version for replacements. --- guix/import/go.scm | 48 +++++++++++++++++++++++++++++----------------- tests/go.scm | 2 ++ 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/guix/import/go.scm b/guix/import/go.scm index 5c1a251677..04546cb9e9 100644 --- a/guix/import/go.scm +++ b/guix/import/go.scm @@ -348,25 +348,37 @@ DIRECTIVE." (define (go.mod-requirements go.mod) "Compute and return the list of requirements specified by GO.MOD." (define (replace directive requirements) - (define (maybe-replace module-path new-requirement) - ;; Since only one go.mod is considered at a time and hence not all the - ;; transitive requirements are known, we honor all the replacements, - ;; contrary to the upstream specification where only dependencies actually - ;; *required* get replaced. - ;; - ;; Notably, allow version pinning/updating for indirect dependencies. It - ;; is rare in practice, may be useful with --pin-versions, and at worst - ;; adds an extra direct input (which would be transitively included anyway). - (cons new-requirement (alist-delete module-path requirements))) - + ;; Since only one go.mod is considered at a time and hence not all the + ;; transitive requirements are known, we honor all the replacements, + ;; contrary to the upstream specification where only dependencies actually + ;; *required* get replaced. However, if a version is specified and the + ;; module is required in this go.mod, the version must match in order to + ;; replace. + ;; + ;; Notably, allow version pinning/updating for indirect dependencies. It + ;; is rare in practice, may be useful with --pin-versions, and at worst + ;; adds an extra direct input (which would be transitively included anyway). + (define (replace* module-path version + to-module-path to-version + requirements) + "Perform the replacement unless VERSION is non-false, MODULE-PATH is found +in REQUIREMENTS, and its version does not match VERSION. Return the updated +REQUIREMENTS." + (let* ((current-version (and=> (assoc-ref requirements module-path) first)) + (version-matches? (equal? version current-version))) + (if (and version current-version (not version-matches?)) + requirements + (cons (list to-module-path to-version) + (alist-delete module-path requirements))))) (match directive - ((('original ('module-path module-path) . _) with . _) - (match with - (('with ('file-path _) . _) - (alist-delete module-path requirements)) - (('with ('module-path new-module-path) ('version new-version) . _) - (maybe-replace module-path - (list new-module-path new-version))))))) + ((('original ('module-path module-path) . _) ('with ('file-path _)) . _) + (alist-delete module-path requirements)) + ((('original ('module-path module-path) ('version version) . _) + ('with ('module-path new-module-path) ('version new-version) . _) . _) + (replace* module-path version new-module-path new-version requirements)) + ((('original ('module-path module-path) . _) + ('with ('module-path new-module-path) ('version new-version) . _) . _) + (replace* module-path #f new-module-path new-version requirements)))) (define (require directive requirements) (match directive diff --git a/tests/go.scm b/tests/go.scm index 6749f4585f..ec92e3fce4 100644 --- a/tests/go.scm +++ b/tests/go.scm @@ -238,6 +238,8 @@ require github.com/kr/pretty v0.2.1 '(("github.com/corp/arbitrary-repo" "v0.0.2") ("quoted.example.com/abitrary/repo" "v0.0.2") ("one.example.com/abitrary/repo" "v1.1.111") + ("golang.org/x/sys" "v0.0.0-20190813064441-fde4db37ae7a") + ("golang.org/x/tools" "v0.0.0-20190821162956-65e3620a7ae7") ("hub.jazz.net/git/user/project/sub/directory" "v1.1.19") ("hub.jazz.net/git/user/project" "v1.1.18") ("launchpad.net/~user/project/branch/sub/directory" "v1.1.17") -- 2.31.1