Do I need go.mod to build one of the example projects?

I’m trying to build reflect in a Docker container and got an error:

appuser@64e303774d45:/app$ go build
go: go.mod file not found in current directory or any parent directory; see 'go help modules'

I see go.mod exists in the top-level of the project, but I only want to build reflect. Is there a way to build reflect portably that doesn’t depend on the whole tree?

I tried to copy a build from my Linux computer into the container, but that didn’t work because the resulting dynamic links don’t match the golang source container I’m trying to use to run reflect.

I forgot to mention that the reflect/ dir is a mounted volume on the container’s /app/.

appuser@64e303774d45:/app$ ls -l
total 20
-rw-rw-r-- 1 appuser appgroup  709 Aug 13 17:28 Dockerfile
lrwxrwxrwx 1 appuser appgroup   49 Aug 11 21:23 KenReflectSrv1.json -> /home/kbingham/.ziti-edge-tunnel/Reflect1748.json
drwx------ 2 appuser appgroup 4096 Aug  3 15:17 cmd
-rw-rw-r-- 1 appuser appgroup  556 Aug 13 17:23 docker-compose.yml
-rw-rw-r-- 1 appuser appgroup 1188 Aug 11 21:31 k8s-deployment.yml
-rw-rw-r-- 1 appuser appgroup 1912 Aug 13 17:13 main.go

When I do go build on the computer that has the full repo working copy it succeeds, presumably because it can “see” go.mod file in a parent directory.

I tried to create a suitable go.mod file like this.

❯ go mod init github.com/openziti/ziti-sdk-golang/example/reflect
go: creating new go.mod: module github.com/openziti/ziti-sdk-golang/example/reflect
go: to add module requirements and sums:
        go mod tidy

That gets us a go.mod file like this.

module github.com/openziti/ziti-sdk-golang/example/reflect

go 1.16

And then I re-tried the build in the container like this.

appuser@8149f7f00d74:/app$ go build
main.go:4:2: cannot find package "." in:
        /app/vendor/github.com/michaelquigley/pfxlog
main.go:5:2: cannot find package "." in:
        /app/vendor/github.com/openziti/sdk-golang/example/reflect/cmd
main.go:6:2: cannot find package "." in:
        /app/vendor/github.com/openziti/sdk-golang/ziti/config
main.go:7:2: cannot find package "." in:
        /app/vendor/github.com/sirupsen/logrus
main.go:8:2: cannot find package "." in:
        /app/vendor/github.com/spf13/cobra

I’m new to Go and Go Modules so I’m probably missing something obvious.

Next I tried running go build with the full repo working copy mounted on /app in the container and got a different error:

inconsistent vendoring

appuser@37731163b0a0:/app/example/reflect$ go build
go: inconsistent vendoring in /app:
        github.com/Jeffail/gabs@v1.4.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/StackExchange/wmi@v1.2.1: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/cenkalti/backoff/v4@v4.1.1: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/fullsailor/pkcs7@v0.0.0-20190404230743-d7302db945fa: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/go-ole/go-ole@v1.2.5: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/golang-jwt/jwt@v3.2.2+incompatible: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/google/uuid@v1.3.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/influxdata/influxdb1-client@v0.0.0-20200827194710-b269163b24ab: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/mattn/go-colorable@v0.1.8: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/mattn/go-isatty@v0.0.13: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/michaelquigley/pfxlog@v0.6.1: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/mitchellh/go-ps@v1.0.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/mitchellh/mapstructure@v1.4.1: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/netfoundry/secretstream@v0.1.2: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/openziti/foundation@v0.15.69: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/orcaman/concurrent-map@v0.0.0-20190826125027-8c72a8bb44f6: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/pkg/errors@v0.9.1: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/rcrowley/go-metrics@v0.0.0-20201227073835-cf1acfcdf475: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/shirou/gopsutil@v3.21.7+incompatible: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/sirupsen/logrus@v1.8.1: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/spf13/cobra@v1.2.1: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/stretchr/testify@v1.7.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/tklauser/go-sysconf@v0.3.7: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        go.mozilla.org/pkcs7@v0.0.0-20210730143726-725912489c62: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        golang.org/x/crypto@v0.0.0-20210711020723-a769d52b0f97: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        golang.org/x/sys@v0.0.0-20210630005230-0f9fa26af87c: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        golang.org/x/term@v0.0.0-20210615171337-6886f2dfbf5b: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        google.golang.org/protobuf@v1.27.1: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt

        To ignore the vendor directory, use -mod=readonly or -mod=mod.
        To sync the vendor directory, run:
                go mod vendor

Can you use

 go get -u github.com/openziti/sdk-golang/example/reflect

I think that would work?

that or you can build it from your own checkout if you really want with:

mkdir build
go build -o build ./example/reflect

Yes, after it’s published the go get form works, even for containers. Inconsequentially, it seems to take an annoyingly-long time. The reason I was looking for a local build is that it adds a steps to each iteration to commit and push.

I think this is equivalent to the go build command I showed in the pastes.

i’m still not entirely sure what you’re trying to accomplish. would it be helpful if i post a dockerfile here that shows you ‘how to’?

I’m able to build and install with go get, but I’m not able to do go build because of the errors shown above.

Here’s the Dockerfile I’m using to do go get. It works with the upstream main branch, but not with my branch, which may be an entirely different problem than the problem with modules for the local build.

thanks that helps. I am pretty sure the problem is the current working directory. after you clone the repo - you need to cd to that repo. I think you’re only cd’ing to the root folder.

for example these docker commands produce the same error for me (reduced for brevity)

FROM golang
...
RUN git clone https://github.com/openziti/sdk-golang.git ${APP_HOME}
...
RUN go build ./example/reflect

:point_up: this will cause: go: go.mod file not found in current directory or any parent directory; see 'go help modules

Assuming you’re doing what I outlined above - you either need to set the WORKDIR to ${APP_HOME}/sdk-golang or use the actual app folder when cloning the repo like: CMD git clone https://github.com/openziti/sdk-golang.git ${APP_HOME}

Then it should build just fine…

Here’s a working VERY minimal docker file:

FROM golang
#RUN git clone https://github.com/openziti/sdk-golang.git /app
RUN git clone https://github.com/qrkourier/sdk-golang.git /app
WORKDIR /app
RUN go build ./example/reflect
CMD /app/reflect

I was able to bypass the problem of versioned module in a repo fork with the replace directive in go.mod like this:

// use local repo instead of remote module
replace github.com/openziti/sdk-golang => ./

Then I needed to do one of the following shown at the end of the error output from the go build command.

        To ignore the vendor directory, use -mod=readonly or -mod=mod.
        To sync the vendor directory, run:
                go mod vendor