简体   繁体   中英

Two version of same dependency - lower version getting ignored

I have a project in which two dependencies uses different version of same library. For instance, my project has dependency A and dependency B . A and B , both uses a common library/dependency X , but of different versions. A has v1 version of X and B has v2 version of X . So now when I add A & B as dependencies in my project, there are 2 versions of X in my project's go.sum .

I was expecting, the respective versions will be referred at run time by A and B . But it is not the case. Somehow when I run tests on my project, the A is using v2 of X , ideally it should use v1 (because in go.mod of A , explicitly specified/added v1 ). So it breaks the execution,because there are lot differences in v1 and v2 of X .

So in my project, how can I explicitly specify that to use v1 of X by A and use v2 by B ? Is there such provision in go modules?

Your B package must import X with a /v2 suffix.

Go Wiki: Modules: Semantic Import versioning:

Recall semver requires a major version change when a v1 or higher package makes a backwards incompatible change. The result of following both the import compatibility rule and semver is called Semantic Import Versioning , where the major version is included in the import path — this ensures the import path changes any time the major version increments due to a break in compatibility.

As a result of Semantic Import Versioning, code opting in to Go modules must comply with these rules:

  • If the module is version v2 or higher, the major version of the module must be included as a /vN at the end of the module paths used in go.mod files (eg, module github.com/my/mod/v2 , require github.com/my/mod/v2 v2.0.0 ) and in the package import path (eg, import "github.com/my/mod/v2/mypkg" ).

This version suffix in the import path will make them 2 "different" packages. If A and B would use the same major version of X , then there would be no 2 versions of it, the higher version would be chosen ("minimal version selection" algorithm). For details, see Version Selection .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM