#Splitting go.mod File into Children

25 messages · Page 1 of 1 (latest)

limber valve
#

I have a project that has a structure like so:

root
|
go.mod
sub-dir-a
sub-dir-b

I want to break up my go.mod file so that sub-dir-a/b each have their own mod. The sub dirs have executables and I would like to be able to package each independently.

This is what I'd want ideally.

root
|
sub-dir-a
|
go.mod
sub-dir-b
|
go.mod

I've tried doing this manually, but when I go mod tidy on each of my sub-dir mod files I get ambiguous import errors.

If someone who's run into this issue could give some advice, it'd be much appreciated.

glad pumice
#

post the full error here

#

post the go.mod path for a and b dirs

#

generally it's easier to just not try to split it

#

for example if something in subdir-a imports subdir-b
go will actually fetch it online, rather then refer to local directories

#

if you are doing this for a concern of sorts maybe ask in #go-chat if that's warranted

limber valve
#

Okay, reason I was doing this was because I was containerizing the executable in each dir. In my mind, the mod files (which contains it's dependencies), should live next to it

glad pumice
#

can you just compile the executable, before doing what you need to?
because go.mod is only used for compiling, if you have contained executable without go.mod next to it, the executable will still work
but i think my response missed something else crucial here

limber valve
#

No that makes sense but my dockerfile had a go mod download step in it to pull deps (will skip if nothing changed from previous image).

#

Here's the full error I was seeing though.

github.com/rmarken5/cfcs/client/client: ambiguous import: found package github.com/rmarken5/cfcs/client/client in multiple modules:
        github.com/rmarken5/cfcs v0.0.0-20230127023914-1ebc79a1ea4a (/home/ryan/go/pkg/mod/github.com/rmarken5/[email protected]/client/client)
        github.com/rmarken5/cfcs/client (/home/ryan/programming/go-programs/cfcs/client/client)
github.com/rmarken5/cfcs/client/file-manager: ambiguous import: found package github.com/rmarken5/cfcs/client/file-manager in multiple modules:
        github.com/rmarken5/cfcs v0.0.0-20230127023914-1ebc79a1ea4a (/home/ryan/go/pkg/mod/github.com/rmarken5/[email protected]/client/file-manager)
        github.com/rmarken5/cfcs/client (/home/ryan/programming/go-programs/cfcs/client/file-manager)
github.com/rmarken5/cfcs/client/file-manager/file_manager_test: ambiguous import: found package github.com/rmarken5/cfcs/client/file-manager/file_manager_test in multiple modules:
        github.com/rmarken5/cfcs v0.0.0-20230127023914-1ebc79a1ea4a (/home/ryan/go/pkg/mod/github.com/rmarken5/[email protected]/client/file-manager/file_manager_test)
        github.com/rmarken5/cfcs/client (/home/ryan/programming/go-programs/cfcs/client/file-manager/file_manager_test)
github.com/rmarken5/cfcs/client/main: ambiguous import: found package github.com/rmarken5/cfcs/client/main in multiple modules:
        github.com/rmarken5/cfcs v0.0.0-20230127023914-1ebc79a1ea4a (/home/ryan/go/pkg/mod/github.com/rmarken5/[email protected]/client/main)
        github.com/rmarken5/cfcs/client (/home/ryan/programming/go-programs/cfcs/client/main)
#

So it's confused if it wants to pull from an existing module or my new "child" module.

Steps that I used to try and resolve this:

  1. deleted the parent module
  2. Remove the package from GOROOT (/home/ryan/go/pkg/mod/github.com/rmarken5/[email protected]/client/main)
  3. go mod tidy in child dir.
glad pumice
#

did the steps you run fixed it?
i would try clearing all the cache in /home/ryan/go/pkg/mod/github.com/rmarken5/cfcs

glad pumice
limber valve
#

I had a docker file in each sub directory

#

And the steps I took did not fix it. It kept wanting to pull it back.

#

If I have a module published, could go's tooling be trying to pull from the internet? I may have published this at some point.

glad pumice
#

but yes go tooling will be pulling from the internet

#

because you have
/a/go.mod
/b/go.mod
they are no longer "related"

#

unlike having

/a/
/b/
/go.mod
```which go will know to use local files
#

you could use go workspace to use a/ and b/ to stop go from fetching remotely though

#

but this is like duct tape ontop of duct tapes IMO

limber valve
#

No worries! I appreciate the help. What I've opted for is: 2 dockerfiles in dockerfiles dir at the root of my project. Then I'll run a build as such: docker build . -f ./dockerfiles/server.dockerfile -t rmarken5/test-cfs

-f lets you point at a file while maintaining docker's context

The server file looks like:

FROM golang:alpine3.16

RUN mkdir "dummy"

WORKDIR /app

# copy Go modules and dependencies to image
COPY go.mod .

# download Go modules and dependencies
RUN go mod download

COPY common ./common
COPY server ./server


RUN go mod tidy

RUN ls

# compile application
RUN go build -o fs ./server/main

RUN ls

# tells Docker that the container listens on specified network ports at runtime
EXPOSE 8999

ENTRYPOINT [ "./fs",  "--directory", "../dummy", "--port", "8999"]
#

That seems to have done it.

glad pumice