use of internal package xxx not allowed after rename the project - go

I am trying to start my first Go project based on someone else's code, so I download his GitHub repo as zip, upload it to my own repo. Then use go get -u github.com/#username/#repo to install my repo, and add a replace statement to go.mod:
replace someone/repo v4.19.1+incompatible => ./
As a result, when I try go test ./... and go build ./... in the top directory, I get an error saying use of internal package xxx/yyy/internal not allowed. I am sure the only file include import xxx/yyy/internal is at the dir xxx/yyy, which should match the restriction of internal package of Go.
I don't know why go test ./... runs well in the original repo, but fails in my repo. Is there anything else I need to do to modify other people's repo?
Any help is appreciated, thanks!

Related

Modify underlying Go sub dependency on the package I am using

I am getting this error from go mod tidy when I am trying to update my dependancies. I am primarily developing a webhook service to use with cert-manager and I cannot figure out how to resolve this dependency isssue since the packages I am dependent on are created by other developers and I cannot control those "sub dependency".
This is my output:
go.opentelemetry.io/otel/semconv: module go.opentelemetry.io/otel#latest found (v1.9.0), but does not contain package go.opentelemetry.io/otel/semconv
I looked the package up here: https://pkg.go.dev/go.opentelemetry.io/otel/semconv
and the problem for me seems like that the package have been restructured like this:
go.opentelemetry.io/otel/semconv/v1.9.0
as a subdirectory instead of package version.
Is there a way I can manipulate the underlying dependancy of the packages that my service is depending on?
Please leave a comment if you need addictional information.
Take a look at the accepted solution's comments
You may want to use a local copy of the module where you can fix the issue and use it. Steps for that
Clone the module repository git clone https://github.com/open-telemetry/opentelemetry-go.git
If needed, checkout to a branch/tag git checkout branch_name
In the go.mod file of your module, add the following lines
replace (
go.opentelemetry.io => /path/where/cloned/opentelemetry-go
)
Now you should be able to modify code in the cloned opentelemetry-go repo and use it in your module

go get fails for hashicorp/levant

I inherited a build script that builds a docker image and uses hashicorp/levant library to deploy. For a year or so we have been running go get github.com/jrasell/levant to grab the levant library. In past few days, the repo URL was merged under Hashicorp's organization and we've changed our script to pull with go get github.com/hashicorp/levant. But either way, we get this multi assign error. What does this mean, doesn't 'go' just basically pull the git repo?
../go/src/github.com/hashicorp/levant/template/render.go:28:11: cannot assign
*"github.com/hashicorp/nomad/vendor/github.com/hashicorp/nomad/api".Job to job
(type *"github.com/hashicorp/nomad/api".Job) in multiple assignment
Firstly, go get works with packages, not repositories.
In addition to pulling them go get also compiles and installs them, and here's when your error pops up.
More info here:
https://nanxiao.gitbooks.io/golang-101-hacks/content/posts/go-get-command.html
I recommend you to use Go modules.
hashicorp/levant does have go.{mod,sum} files and hence you should forget using the go get way.
It's better to do a clone and follow the go module way i.e.,
git clone git#github.com:hashicorp/levant.git
go test ./...
go build ./...
The steps with not only just clone your repo but would also bring your dependent packages required for building/ testing the package.
Note: You should have Go v1.11+

Go modules when running get vs build

I have a project where I use go modules. There I need to specify that I depend on a particular fork of a library (call it ), because it has an important patch. When I run go get -u <my_project>, I get a compilation error that clearly means that go took the main repo of , instead of the fork.
After that, I switch to the directory where go downloaded and run go build. Then, go takes the proper version of and compilation is successful.
Could you tell me what could be the reason for that and how to fix it?
Here is specific command to get :
go get -u github.com/planetA/konk
The dependency is "github.com/opencontainers/runc". For this dependency, go.mod contains following:
replace (
github.com/opencontainers/runc => github.com/planeta/runc v1.0.0-rc9.0.20191206160324-51eabe724369
)
require (
github.com/opencontainers/runc v1.0.0-rc9.0.20191206223258-201b06374548
)
Interestingly, in following sequence of commands, second go get does not produce error
go get -u github.com/planetA/konk
cd ~/go/src/github.com/planetA/konk
go build -tags seccomp
go get -u github.com/planetA/konk
I had a similar problem and I resolved this by detaching the fork itself and making a separate repo. Depends on your use case, this is also a much cleaner solution as you don't need to add too many edits to make it work. You can do so by:
Clone the fork repository on your local.
Delete the fork repository from github/gitlab etc. if you want the package name to be the same as the fork if not you can rename the new repo you create.
Create a new repo and push your content onto it
Now go get github.com/myusername/mynewrepo
But if you still want to make it work using the fork here is a great reference.

Modifying an imported library in Go

My Problem
Elastic Beats is an open source project for log shippers written in Go. It features several log outputs, including console, Elasticsearch and Redis. I would like to add an output of my own - to AWS Kinesis.
I have cloned the repo to ~/github/beats, and tried building it:
$ cd filebeat; go build main.go
However, it failed due to a missing library which is a part of the project:
main.go:6:2: cannot find package "github.com/elastic/beats/filebeat/cmd" in any of:
/usr/local/go/src/github.com/elastic/beats/filebeat/cmd (from $GOROOT)
/Users/adam/go/src/github.com/elastic/beats/filebeat/cmd (from $GOPATH)
A directory of the project is dependent on a package from the same repo, but instead of looking one directory up the hierarchy it looks in the GOPATH.
So, go get github.com/elastic/beats/filebeat/cmd fetched the code, and now go build main.go works. Changing the code in my GOPATH is reflected in these builds.
This leaves me with an structural inconvenience. Some of my code is at a working directory, and some of it is at my GOPATH and included by the code in my working directory.
I would like to have all my code in a single directory for various reasons, not the least being keeping everything under version control.
What Have I Tried
Mostly searching for the problem. I am quite new to Go, so I might have missed the correct terminology.
My Question
What is the right way to edit the code of an imported library in Go?
One of the recommended ways to work with other's packages is:
Get the sources of the original package:
go get github.com/elastic/beats
As a result you will clone project's git repository to the folder
$GOPATH/src/github.com/elastic/beats
Make some fixes, compile code, fix, compile... When you make go install package will be compiled and installed to your system. When you need merge updates from original repository you can git pull them.
Everything is OK. What's next? How to share your work with others?
Fork project on github, suppose it will be github.com/username/beats
Add this fork as another remote mycopy (or any other name you like) to your local repository
git remote add mycopy git://github.com/username/beats.git
When all is done you can push updated sources to your repo on github
git push mycopy
and then open a pull-request to original sources. This way you can share your work with others. And keep your changes in sync with mainstream.
Previous answers to this question are obsolete when developing projects that using Go Modules.
For projects that using Go Modules, one may use the following command to replace an imported library(eg. example.com/imported/module) with a local module(eg. ./local/module):
go mod edit -replace=example.com/imported/module=./local/module
Or by adding the following line into the go.mod file:
replace example.com/imported/module => ./local/module
Reference Docs: https://golang.org/doc/modules/managing-dependencies#unpublished
A project working copy should be checked out into $GOPATH/src/package/import/path - for example, this project should be checked out into /Users/adam/go/src/github.com/elastic/beats. With the project in the correct location, the go tooling will be able to operate on it normally; otherwise, it will not be able to resolve imports correctly. See go help gopath for more info.

Controlling what godoc documents in a repo that is not just Go packages

I have a Go sub-project in a multi-language project repo. I want to be able to use 'go get' and 'go doc'.
My layout looks like:
proton-c/bindings/go/<my packages>
examples/go/<some go examples>
I set up go-import tags on the website and created a "go" symlink in the repo root so I can go get qpid.apache.org/proton/go/<package>. It works!! It clones the entire project repo into my GOPATH but that's ok.
The problem is if I run godoc -http it does the following bad things:
ignores the root "go" symlink entirely
documents my packages as "qpid.apache.org/proton/proton-c/bindings/go/"
shows the package path to the directory with nothing in it.
The command line "godoc qpid.apache.org/proton/go/package" DOES do the right thing so godoc can extract the doc correctly but the "directory browsing" feature of godoc -http is picking up too much and not following the symlink.
So can I restrict/control what godoc picks as documentation?
I read https://github.com/golang/gddo/wiki/Source-Code-Links, but I don't think it helps my problem, could be wrong.

Resources