Code Monkey home page Code Monkey logo

godepgraph's Introduction

godepgraph

godepgraph is a program for generating a dependency graph of Go packages.

Build Status

Install

go install github.com/kisielk/godepgraph@latest

Use

For basic usage, just give the package path of interest as the first argument:

godepgraph github.com/kisielk/godepgraph

If you intent to graph a go mod project, your package should be passed as a relative path:

godepgraph ./pkg/api

The output is a graph in Graphviz dot format. If you have the graphviz tools installed you can render it by piping the output to dot:

godepgraph github.com/kisielk/godepgraph | dot -Tpng -o godepgraph.png

By default godepgraph will display packages in the standard library in the graph, though it will not delve in to their dependencies.

Colors

godepgraph uses a simple color scheme to denote different types of packages:

  • green: a package that is part of the Go standard library, installed in $GOROOT.
  • blue: a regular Go package found in $GOPATH.
  • yellow: a vendored Go package found in $GOPATH.
  • orange: a package found in $GOPATH that uses cgo by importing the special package "C".

Ignoring Imports

The Go Standard Library

If you want to ignore standard library packages entirely, use the -s flag:

godepgraph -s github.com/kisielk/godepgraph

Vendored Libraries

If you want to ignore vendored packages entirely, use the -novendor flag:

godepgraph -novendor github.com/something/else

By Name

Import paths can be ignored in a comma-separated list passed to the -i flag:

godepgraph -i github.com/foo/bar,github.com/baz/blah github.com/something/else

The packages and their imports will be excluded from the graph, unless the imports are also imported by another package which is not excluded.

By Prefix

Import paths can also be ignored by prefix. The -p flag takes a comma-separated list of prefixes:

godepgraph -p github.com,launchpad.net bitbucket.org/foo/bar

Example

Here's some example output for godepgraph itself:

Example output

godepgraph's People

Contributors

13rac1 avatar aviau avatar barrettj12 avatar calmh avatar cespare avatar danehammer avatar davcamer avatar dominikh avatar igrmk avatar justin-w avatar kisielk avatar kveselkov avatar moul avatar paulbuis avatar porridge avatar radeksimko avatar regismelgaco avatar starius avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

godepgraph's Issues

How to show only the dependencies of one module?

Hi Kamil, thank you for that tool!

I want to see the package dependency inside one module.

I want to ignore other dependencies (like stdlib or external packages).

Is there a way to get this?

Fail to import module. Exit status 128

Hi!
I encountered an error:

~/work/my_project$ godepgraph -novendor -s -p github.com,google.golang.org,gopkg.in {gitlab_repo/project/subproject/package_name}

2021/03/25 00:13:56 failed to import {gitlab_repo/project/subproject/package_name} (imported at level 1 by ):
module {gitlab_repo/project/subproject}: git ls-remote -q origin in /home/user/go/pkg/mod/cache/vcs/{COMMIT_HASH_HERE}: exit status 128:
remote: 
remote: ========================================================================
remote: 
remote: The project you were looking for could not be found.
remote: 
remote: ========================================================================
remote: 
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists

Environment:
go version go1.15.7 linux/amd64
Linux 5.4.0-70-generic #78-Ubuntu SMP Fri Mar 19 13:29:52 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Package go.mod contains header
module gitlab_repo/project/subproject/package_name

I assume that this is due to gitlab has subprojects and thus incompatible path to module.
How to resolve such behavior?
Thanks

include standard library dependencies

By default godepgraph will display packages in the standard library in the graph, though it will not delve in to their dependencies.

How do I turn this off?

The example graph does include the dependencies. How do you do that?

Does not support modules yet

Although the command will start parsing dependencies, it fails at finding the imports in the modules cache, instead relying only on GOPATH/GOROOT.

Option to change orientation

Could you add an option to have the dependencies to the right, rather than below? Most import paths are quite long, and the depth of the imports quite shallow, so think at least in my case it would fit the screen better and be more readable.

-novendor doesn't working properly

Hello!

go env
$ go env                                                                              
GO111MODULE="on"
GOARCH="arm64"
GOBIN="/Users/anthony/golang_workspace/bin"
GOCACHE="/Users/anthony/Library/Caches/go-build"
GOENV="/Users/anthony/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/anthony/golang_workspace/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/anthony/golang_workspace"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.18.2"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/anthony/.../go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/6t/v80c8sfs5zqf38b2yhzq592h0000gn/T/go-build3004086577=/tmp/go-build -gno-record-gcc-switches -fno-common"
 godepgraph -horizontal  -s -novendor ./internal/config | dot -Tpng -o godepgraph.png

godepgraph

$ ls -l vendor/github.com/            
total 0
drwxr-xr-x   3 anthony  staff   96 Aug 15 12:17 BurntSushi
...
drwxr-xr-x   5 anthony  staff  160 Aug 15 12:17 go-playground
...

Expected Behaviour

No vendored packages in the graph.

Same packaged referenced from two paths isn't recognized

If you have a package in a directory named somepackage and import it from your main package using

import (
    "./somepackage"
)

and then you have another package named otherpackage and import somepackage in it using

import(
    "../somepackage"
)

It will be picked up as two separate packages, one being ./somepackage and the other being ../somepackage.

Potential Fix

For local packages like this, would it work better to resolve the absolute paths of relative packages. If two are the same, ommit one.
Afterwards, you can update their names again by generating the relative path to the package from the main package. Does this make sense?

Option to group packages by Go module?

Can we have an option to group packages by their Go module?
Meaning all packages inside a Go module be combined into one rectangle/node.
Including sub-packages of current module (root of the tree).
So that the nodes are modules, not packages.

Because that's what we generally mean by "dependency".
Once your module depends on a package from another module, your module depends on that module.

Optionally include test imports

I'd like to see the chain of things that's leading me to pull in a whole bunch of questionable testing libraries into my project :)

Allow delving into stdlib dependencies

Could we add a flag to enable delving into the dependencies of packages in the standard library, so that godepgraph can be used on those packages, too?

Run for main packages

It would be very useful if it would be possible to run for executable sources.

Commonly I am trying to move all functionality in separate modules and I clue logic at executable sources. But godepgraph can't parse not imported source files.

As I have commonly 2 executable sources then I just rename main to somename and it works.

cannot find package with 3 levels such as "github.com/golang/protobuf/proto"

godepgraph github.com/coreos/etcd | dot -Tpng -o godepgraph.png && imgcat godepgraph.png
2018/03/29 20:31:16 failed to import github.com/golang/protobuf/proto: cannot find package "github.com/golang/protobuf/proto" in any of:
	/usr/local/go/src/github.com/golang/protobuf/proto (from $GOROOT)
	/Users/weaming/go/src/github.com/golang/protobuf/proto (from $GOPATH)

cannot find package "C"

Latest HEAD is having issues when I use import "C". Here's what I get.

2013/10/21 11:02:32 failed to import C: cannot find package "C" in any of:
/opt/local/go/src/pkg/C (from $GOROOT)
/Users/quest/Go/src/C (from $GOPATH)

If I go back to commit 369296b everything works as expected. Not sure how this regressed but I figured I would post an issue. =)

trying to run godepgraph

Hello -

I just cloned your project into my GitHub, & then tried to run it using your README guidance, & it doesn't work.

What am I doing incorrectly?

Here's the pertinent information:

$ pwd
/Users/ajpowel/GitHub/godepgraph

s986572:godepgraph ajpowel$ ls -lah
total 4312
drwxr-xr-x 8 ajpowel SANDIA\Domain Users 272B May 18 12:20 ./
drwxr-xr-x 9 ajpowel SANDIA\Domain Users 306B May 18 12:20 ../
drwxr-xr-x 13 ajpowel SANDIA\Domain Users 442B May 18 12:20 .git/
-rw-r--r-- 1 ajpowel SANDIA\Domain Users 1.0K May 18 12:20 LICENSE
-rw-r--r-- 1 ajpowel SANDIA\Domain Users 1.8K May 18 12:20 README.md
-rw-r--r-- 1 ajpowel SANDIA\Domain Users 2.1M May 18 12:20 example.png
-rw-r--r-- 1 ajpowel SANDIA\Domain Users 6.9K May 18 12:20 example.svg
-rw-r--r-- 1 ajpowel SANDIA\Domain Users 4.0K May 18 12:20 main.go

I don't even see an executable, or anything to build.

What am I missing here?

Many thanks for any guidance you can offer!

Best,

AJP

one about vendors problems

first thanks for your project.

my project relations:

project A a.go import "abc/consul"

project abc/consul imports "github.com/hashicorp/consul/api"

when run "godepgraph projectA"

2018/03/13 17:27:55 failed to import github.com/hashicorp/go-cleanhttp: cannot find package "github.com/hashicorp/go-cleanhttp" in any of:
/var/www/gopath/src/projectA/vendor/github.com/hashicorp/go-cleanhttp (vendor tree)
/usr/local/go/src/github.com/hashicorp/go-cleanhttp (from $GOROOT)
/var/www/go/src/github.com/hashicorp/go-cleanhttp (from $GOPATH)
/var/www/gopath/src/github.com/hashicorp/go-cleanhttp

"github.com/hashicorp/go-cleanhttp" in "/var/www/go/src/github.com/hashicorp/consul/vendor"

but go build is ok.

go env:

GOOS="linux"
GOPATH="/var/www/go/:/var/www/gopath/"
GORACE=""
GOROOT="/usr/local/go"

thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.