Using a local copy of an external package?

315 views
Skip to first unread message

Paul S. R. Chisholm

unread,
Aug 26, 2021, 6:22:54 PM8/26/21
to golan...@googlegroups.com
Hypothetical example: Say I'm writing an application that uses "rsc.io/quote" and I discover a bug in that package that breaks my software. I would of course clone the quote repository, add a test that demonstrates the bug, fix the bug, and submit a patch to the maintainer.

After making the fix but before submitting the patch, I'd like to first import my local copy of the quote package to ensure my change prevents the problem in my application. How could I do that?

Back in the GOPATH days, I could presumably have temporarily used a relative import path:
I'm not sure how that would work in a post-GOPATH environment. (That page also talks about programs in a "work space" and not in a "work space." Apparently "work space" is a technical term in this context, but I don't know what it means. "go help gopath" doesn't help. What is and isn't a "work space"?)

Could I clone the quote repository in a vendor/ directory inside my application directory structure? Are vendor/ directories still used in post-GOPATH environments, or are they considered deprecated or obsolete?

Thanks. --PSRC

Dan Kortschak

unread,
Aug 26, 2021, 6:30:36 PM8/26/21
to golan...@googlegroups.com

Connor Kuehl

unread,
Aug 27, 2021, 2:37:55 PM8/27/21
to Paul S. R. Chisholm, golan...@googlegroups.com


> On Aug 26, 2021, at 5:21 PM, Paul S. R. Chisholm <psrch...@gmail.com> wrote:
>
> Hypothetical example: Say I'm writing an application that uses "rsc.io/quote" and I discover a bug in that package that breaks my software. I would of course clone the quote repository, add a test that demonstrates the bug, fix the bug, and submit a patch to the maintainer.
>
> After making the fix but before submitting the patch, I'd like to first import my local copy of the quote package to ensure my change prevents the problem in my application. How could I do that?

I recently did something like this:

1. Fork the repository
2. Clone it, fix it up
3. Push your changes to your fork
4. Then, in your project:

go mod edit -replace github.com/upstream_org/repo=github.com/your_user/repo@branch

The thing I don’t like about this approach is having to push to the remote each time I want to iterate.

However, after writing this all out, I recently did a quick experiment to see if I could just operate on a local copy and it seems to have worked!

1. Clone the repository
2. Fix it up locally
3. Then, in your project:

go mod edit -replace GitHub.com/upstream_org/repo=/path/to/your/clone/of/the/patched-project

Connor

Connor Kuehl

unread,
Aug 27, 2021, 2:40:17 PM8/27/21
to Paul S. R. Chisholm, golan...@googlegroups.com
Oops, I see another reply to this thread linked to a document that has a better description of what I stumbled through. :-)

Connor

Nikhilesh Susarla

unread,
Feb 8, 2022, 5:43:36 AM2/8/22
to golang-nuts
The replace works. But somehow lately I'm facing issue with replace. 
In my go.mod file I wrote replace for the package referring it to local package using ../package name

But when I go do go mod tidy in that package, it brings back that package referring to the github.com in the require. 
I'm not sure what to do.
Even tried the go mod edit -replace command too, but if I do go mod tidy it doesn't create a local reference with 00000.00, instead it refers to the original github.com pkg. 

Thank you

Manlio Perillo

unread,
Feb 8, 2022, 8:45:17 AM2/8/22
to golang-nuts
On Friday, August 27, 2021 at 12:22:54 AM UTC+2 Paul S. R. Chisholm wrote:
Hypothetical example: Say I'm writing an application that uses "rsc.io/quote" and I discover a bug in that package that breaks my software. I would of course clone the quote repository, add a test that demonstrates the bug, fix the bug, and submit a patch to the maintainer.

After making the fix but before submitting the patch, I'd like to first import my local copy of the quote package to ensure my change prevents the problem in my application. How could I do that?


Nikhilesh Susarla

unread,
Feb 9, 2022, 3:42:49 AM2/9/22
to golan...@googlegroups.com, Manlio Perillo
I was trying to init the work file as instructed, but I have a package which still doesn't work in 1.18

image.png
So, is there any other alternative?

Thank you
Susarla Nikhilesh

Manlio Perillo

unread,
Feb 9, 2022, 5:18:43 AM2/9/22
to golang-nuts

Manlio 

Nikhilesh Susarla

unread,
Feb 9, 2022, 7:00:02 AM2/9/22
to Manlio Perillo, golang-nuts
I updated the project. 
ran go work init cmd
then added 
use ../go-bitswap

replace github.com/ipfs/go-bitswap v0.5.1 => ../go-bitswap`
and comment in my go.mod file. 

But when I run go mod tidy it comes back in the go.mod file. 
using go 1.18v 

workspace : D:\test\workspace
hierarchy : workspace\src\github.com\ipfs\go-bitswap
                  workspace\src\github.com\ipfs\go-ipfs

So, in the go-ipfs folder go.mod file I am trying to refer to the go-bitswap local directory.

Thank you
Susarla Nikhilesh
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/3c3m4anlzR8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f5ee2267-4cbf-4652-8b17-c4f7a47f5badn%40googlegroups.com.

Manlio Perillo

unread,
Feb 9, 2022, 8:34:49 AM2/9/22
to golang-nuts
The `use` directive must be added in the go.work file, not in the go.mod file.
Also, you can use  `go work use` command, instead of manually editing the go.work file.

Manlio

Nikhilesh Susarla

unread,
Feb 9, 2022, 9:49:48 AM2/9/22
to Manlio Perillo, golang-nuts
I did place them in go.work file only. 
I commented the package in go.mod file.

Manlio Perillo

unread,
Feb 9, 2022, 11:52:12 AM2/9/22
to golang-nuts
I tried to reproduce the problem and indeed `go mod tidy` seems to ignore the go.work file.
This seems to be a bug; a opened the issue #51106.


Manlio
Reply all
Reply to author
Forward
0 new messages