Parent+child golang repositories with VSCode

Hello Everyone
I have a repository that I I have split into a child package/library/repository and a parent repository. I have both in separate VSCode projects.

So when I update the child and want the parent to catchup , I currently:

  1. commit a new version of child
  2. sync (push) the commit
  3. copy the commit id
  4. switch to the parent VS Code editor
  5. use go get -u <githuburl>@<commitid>

This works (but is a bit of a pain). But more importantly sometimes my child commit doesn’t work so I have to bounce back and forth between parent and child committing new version of child until things are working again (creating spurious commits)…

What I would like to do is :

  1. save to child
  2. Run parent with local copy of child
  3. When it’s working - then do the child commit+push

N.B. I still need to run/build parent with the commit child to check deployment for other users. I might need another VSCode project just for this…that basically pulls from both latest commits…

Does anyone have a better approach that they have been using?

Thank you in advance - Andy

1 Like

Hello there. You can do it in couple of ways. The one with just go.mod file, is to add replace into the file with the path to the local copy. But my preferred option is to use go.work instead. It allows you to develop packages which are part of other projects you have and test them locally, before pushing anything into git. Official docs can be found here.

2 Likes

(eventually) that worked really well for me…

I had some tidy I had to do before it worked - in case this helps others …

  • the replace has to be after the module line and before the go version line, e.g. replace github.com/whatever/child => ../child
  • the child go.mod has to start module github.com/whatever/child
  • every import (in parent and child) also has to be of the form github.com/whatever/...

Hope this helps others…

Have you tried go work? In this case you won’t need to replace imports or change anything else in go.mod files. It will do everything for you.

I did try go work but it meant I had all my code base visible from one project. I want the child code to be separate from the parent code - I use two VSCode editors (one dark mode, the other light) when editing - it helps separate the responsibilities and my focus. I tend to work on the child (API) and then switch to/from the parent framework at need.

Thank you anyway - replace is really useful :slight_smile: - Andy

A small update for those considering a similar approach, i.e. using ‘replace’.

The VSCode ‘parent’ editor sometimes gets out of date with ‘child’ code updates. This can be fixed by either restarting the parent editor or by using (on windows) Ctrl-Shift-P then choose Developer: Reload Window (on the parent editor).

Hope this helps - Andy