Go module and local dependencies

352 views
Skip to first unread message

Guillaume Lescure

unread,
Aug 31, 2019, 2:35:58 PM8/31/19
to golang-nuts
Hi,

I spend my day trying to build a new project in Go using the new Go module system.
I didn't succeed so I'm kind of upset because I never waste that much time for something that simple before.

The project : I try to use Go module only so no environment variable anywhere.
There are 1 library and 1 executable (both in Go) in 2 different local folders.

The library In /home/angryBoy/myLib :
- go.mod
- myLib.go
- otherPkg
    |- otherPkg.go

go.mod :
module xxx/myLib

myLib.go : its package name is "myLib" and it doesn't import "otherPkg"
otherPkg.go : its package name is "otherPkg" and it does import "myLib"

At this point if I build it : "$ cd myLib && go build"
it's seems to be ok but in fact, it doesn't compile otherPkg.go (I add a compilation error inside to be sure)

1) Why doesn't it compile everthing like before ? How can I fix that ?

The executable In /home/angryBoy/myBin :
- go.mod
- myBin.go

go.mod :
module yyy/myBin 

myBin.go : its package name is "myBin" and it does import "xxx/myLib" and "xxx/myLib/otherPkg"

But it fail at compilation ("$ cd myBin && go build") because it doesn't reconize "xxx/myLib". So, after a long time, I find something about "replace" keyword in go.mod so :

go.mod :
module yyy/myBin 
require xxx/myLib v1.0.0
replace xxx/myLib =>  /home/angryBoy/myLib

And this workaround kind of works for this specific import but it doesn't work for otherPkg. Moreover, if I add :
require xxx/myLib/otherPkg v1.0.0
replace xxx/myLib/otherPkg =>  /home/angryBoy/myLib/otherPkg

It doesn't find the go.mod (of course there is not). But if I understand correctly it shouldn't have more than 1 go.mod in a repository.
2) Is the "replace" workaround the good way to allow local compilation ? If yes, how can I access my sub-package ?

Thanks in advance,

t hepudds

unread,
Aug 31, 2019, 3:05:51 PM8/31/19
to golang-nuts
A few quick comments:

1. Try 'go build ./...' from the root directory of the module to build all the packages in the module. 'go build' without any arguments is the same as 'go build .' which means just build the current directory/package.

2. With only one go.mod, you should not need a 'replace' to find local code in your own module.

3. Make sure you use the full import path in any import statements in your code when importing other packages, even when importing other packages in the same module. The import path should always start with the module path. (The module path what you see on the 'module' line of your go.mod).

If that doesn’t help, you might need to share the exact error messages, along with the exact go.mod and exact import statements in question.

(I am on mobile, so sorry this is brief).

Regards,
thepudds

Guillaume Lescure

unread,
Sep 1, 2019, 6:02:21 AM9/1/19
to golang-nuts
Hi,

Thanks for the comments and the ideas :)


1. Try 'go build ./...'  from the root directory of the module to build all the packages in the module.  'go build'  without any arguments is the same as 'go build .'  which means just build the current directory/package. 

I didn't know the command "go build ./..." and that directly answer my 1st question, thanks a lot. Now my library is totally built.
Thanks a lot for the tips and the explanation (maybe it sould be documented ?).


2.  With only one go.mod, you should not need a 'replace'  to find local code in your own module. 

I  have 2 go.mod, 1 in myLib folder and 1 other in myBin folder and I don't want to merge them.
The idea behind that is to simulate 2 repositories, 1 for the library and 1 for a random application that use that library.

3.  Make sure you use the full import path in any import statements in your code when importing other packages, even when importing other packages in the same module.  The import path should always start with the module path. (The module path what you see on the 'module' line of your go.mod).
 If that doesn’t help, you might need to share the exact error messages, along with the exact go.mod and exact import statements in question.

My code is attached.

Regards,
testGo.tar.gz

Igor Maznitsa

unread,
Sep 2, 2019, 5:56:22 PM9/2/19
to golang-nuts
maven golang plugin allows to process such cases automatically and organize mix from local parts of project and modules, but it needs knowledge of maven buid tool

t hepudds

unread,
Sep 4, 2019, 9:57:51 PM9/4/19
to golang-nuts
Hello Guillaume,

I haven't had a chance to look at your example closely, but it seems you have two modules defined on your local filesystem, with two go.mod files total.

If that is what you are trying to do, one approach is to use a `replace` directive to let one module know about the on-disk location of the other module. Otherwise, they don't know how to find each other.

Also, you usually want to name your modules as if you will publish them someday, which means the `module` line in a go.mod would read something like `module github.com/some/repo`, rather I think you might have something like `module xxx/myLib`, which can be problematic.

In general, when importing code with an import statement, you should always use the full import path, which will start with the module path, such as `import "github.com/some/repo/some/pkg"`. You should always use the full import path when importing packages inside the same modules, as well as when importing packages from a different module.

You can read more about how to use `replace` to let modules find each other on your local filesystem in these two FAQs on the modules wiki:

    FAQ: When should I use the `replace` directive?
  
    FAQ: Can I work entirely outside of VCS on my local filesystem?

Here is a link to a small runnable example that shows two modules side-by-side on the filesystem that use `replace` to make things work. I think that is at least somewhat similar to what you are trying to do, if I followed:


Hope that helps at least somewhat. Please don't hesitate to post more questions or comments.

Regards,
thepudds

Guillaume Lescure

unread,
Sep 6, 2019, 10:24:24 AM9/6/19
to golang-nuts
Hi t hepudds

Yeah this is exactly what I was looking for, thanks a lot.

I search everywhere on the Go website and the Go blog but I didn't go on the Github, my bad.

With this help, I'm able to build it but in an other more complicated exemple I have an issue : if I add a 2nd library that need the 1st, I have a weird error :

go: xxx/my...@v0.0.0-00010101000000-000000000000: unrecognized import path "xxx/myLib" (import path does not begin with hostname)
go: error loading module requirements

Can I use the "replace design" with any number of project like :
* myLIb
* myWrapper => need myLib
* myBin => need myWrapper

Source code is attached.
testGo.tar.gz
Reply all
Reply to author
Forward
0 new messages