Chris G.
Chris G.

Reputation: 25974

go modules - replace does not work - replacement module without version must be directory path (rooted or starting with

I just want to use a local package using go modules.

I have these files in a folder goweb:

enter image description here

and go.mod

module goweb

go 1.12

require mypack v0.0.0

replace mypack => ./src/mypack

But go.mod complains:

replacement module without version must be directory path (rooted or starting with .

go get -u ./...

go: parsing src/mypack/go.mod: open <local path>/goweb/src/mypack/go.mod: no such file or directory
go: error loading module requirements

So I am missing some path structure here

Upvotes: 20

Views: 41033

Answers (3)

Stephen Thomas
Stephen Thomas

Reputation: 41

This can be resolve by knowing the directory of where the local module will be used.

./ => project folder dir.
../ => nested folder, and so on.

Upvotes: 0

Alex Punnen
Alex Punnen

Reputation: 6244

I had this scenario while updating from Go 1.12 to Go 1.19; Quite a lot has changed.

I had the Protobuffer files in a separte folder called interfaces out as shown below.

Inside each microservice_x I was creating a directory called generated to hold the protoc generated artefacts.

Now I need to do a go mod init in the generated folder ; along with the replace keywords in respective go mod files

Illustrative code here https://github.com/alexcpn/go_grpc_2022 for better understanding. Please check the Make file where the build is happening.

go_grpc_2022$ tree
.
├── interfaces
│   └── microservice_1
│       └── test.proto
├── LICENSE
├── microservice_1
│   ├── generated
│   │   ├── go.mod
│   │   ├── microservice_1
│   │   │   ├── test_grpc.pb.go
│   │       └── test.pb.go
│   ├── go.mod
│   ├── go.sum
│   ├── integration_test
│   │   └── validation_test.go
│   ├── Makefile
│   ├── server
│   │   ├── go.mod
│   │   ├── go.sum
│   │   └── server.go
│   ├── test_client
│   │   └── client.go
│   └── test_server
│       ├── go.mod
│       ├── go.sum
│       └── main.go
└── README.md

Upvotes: 2

icza
icza

Reputation: 418435

If your app and the package it uses are part of the same go module, you don't have to add it to go.mod, you can just refer to it.

If they are not part of the same go module, then you can follow these steps:

The path you specify for the replace directive must be either an absolute path or a relative path, relative to the module's root.

So if mypack is a sibling of your module's root, you could use this:

replace mypack => ../mypack

Also, for this to work, you also have to "convert" mypack into a go module (mypack must contain a go.mod file). Run go mod init mypack in its folder.

Also check out related question: How to use a module that is outside of "GOPATH" in another module?

Upvotes: 21

Related Questions