Is there a way to use the pakckages under vendor folder and from module cache?

542 views
Skip to first unread message

T L

unread,
Sep 10, 2019, 8:59:28 AM9/10/19
to golang-nuts
I maintain an private old Go project, which depends many many old packages.
Before the module mode, I put all these dependency packages under the vendor folder.
In the developing process, from time to time, I modified some of dependency packages.

Meanwhile, I plan to migrate the project to modules mode and need to import some new module packages.
I found I encountered an embarrassing situation.
If I delete the vendor folder and run "go mod vendor" in modules mode to rebuild the vendor,
the command will fail. One reason is some dependency packages disappeared.
The other reason is many packages are updated and broke capabilities.
And I don't want the new download to overwrite my local modifications for some dependency packages.

So, is there a way to let me continue use the these old dependency packages and use some new modules based packages?

T L

unread,
Sep 10, 2019, 9:03:52 AM9/10/19
to golang-nuts
I mean "to use the pakckages under vendor folder and from module cache at the same time".

I have one question, is it better to add a "go mod cache-locally" command to save the dependency modules
in a "module-cache" folder in the current project, so that "go build -use-local-module-cache" can use
the packages in the old "vendor" folder and the "module-cache" folder?

T L

unread,
Sep 10, 2019, 9:11:44 AM9/10/19
to golang-nuts
Or can go commands support a new special folder: dependencies.
Its functionality is like the functionality of vendor folders,
but to avoid being messed up with the packages under vendors.

When running "go build -mod=vendor", the packages under the "dependencies" folder
have higher look-up priorities than the packages under "vendor" folder.

t hepudds

unread,
Sep 10, 2019, 12:30:56 PM9/10/19
to golang-nuts
Hello T L,

I think I might not fully understand the exact scenario, but one thing some people have done when they have something in their vendor directory that they can't otherwise find anywhere else (e.g., perhaps because it is modified, or maybe the only copy of a now-missing dependency is in your vendor directory) is to rename the vendor directory to something else ("oldvendor", or whatever name makes sense), and then set 'replace' directives in the main project's go.mod file to point into the "oldvendor" directory for whatever pieces from there are needed.

For example, something like:

   require github.com/some/dependency v0.0.0

   replace github.com/some/dependency => ./oldvendor/github.com/some/dependency

I think that would allow you to pick and choose what you want from your old vendor directory.

In terms of your other question, I think -mod=vendor is currently all or nothing. There is a proposal to perhaps one day support partial vendoring, but that is not in the current software.

Regards,
thepudds


On Tuesday, September 10, 2019 at 8:59:28 AM UTC-4, T L wrote:

T L

unread,
Sep 10, 2019, 6:46:44 PM9/10/19
to golang-nuts
Thanks for the suggestion.

I think the replace method works, but it is some tedious.
I need to add a "go.mod" file for each of the subfolders under "oldvendor",
and add a line in the "go.mod" of the main module.

But it looks this is the only way which works currently.
It would be great if there is a simpler way to support both
always-on vendor and cached vendor at the same time.
Reply all
Reply to author
Forward
0 new messages