depth is tool to retrieve and visualize Go source code dependency trees.

Overview

depth

GoDoc  Build Status  Go Report Card  Coverage Status

depth is tool to retrieve and visualize Go source code dependency trees.

Install

Download the appropriate binary for your platform from the Releases page, or:

go get github.com/KyleBanks/depth/cmd/depth

Usage

depth can be used as a standalone command-line application, or as a package within your own project.

Command-Line

Simply execute depth with one or more package names to visualize. You can use the fully qualified import path of the package, like so:

$ depth github.com/KyleBanks/depth/cmd/depth
github.com/KyleBanks/depth/cmd/depth
  ├ encoding/json
  ├ flag
  ├ fmt
  ├ io
  ├ log
  ├ os
  ├ strings
  └ github.com/KyleBanks/depth
    ├ fmt
    ├ go/build
    ├ path
    ├ sort
    └ strings
12 dependencies (11 internal, 1 external, 0 testing).

Or you can use a relative path, for example:

$ depth .
$ depth ./cmd/depth
$ depth ../

You can also use depth on the Go standard library:

$ depth strings
strings
  ├ errors
  ├ io
  ├ unicode
  └ unicode/utf8
5 dependencies (5 internal, 0 external, 0 testing).

Visualizing multiple packages at a time is supported by simply naming the packages you'd like to visualize:

$ depth strings github.com/KyleBanks/depth 
strings
  ├ errors
  ├ io
  ├ unicode
  └ unicode/utf8
5 dependencies (5 internal, 0 external, 0 testing).
github.com/KyleBanks/depth
  ├ fmt
  ├ go/build
  ├ path
  ├ sort
  └ strings
7 dependencies (7 internal, 0 external, 0 testing).

-internal

By default, depth only resolves the top level of dependencies for standard library packages, however you can use the -internal flag to visualize all internal dependencies:

$ depth -internal strings
strings
  ├ errors
  ├ io
    ├ errors
    └ sync
      ├ internal/race
        └ unsafe
      ├ runtime
        ├ runtime/internal/atomic
          └ unsafe
        ├ runtime/internal/sys
        └ unsafe
      ├ sync/atomic
        └ unsafe
      └ unsafe
  ├ unicode
  └ unicode/utf8
12 dependencies (12 internal, 0 external, 0 testing).

-max

The -max flag limits the dependency tree to the maximum depth provided. For example, if you supply -max 1 on the depth package, your output would look like so:

$ depth -max 1 github.com/KyleBanks/depth/cmd/depth
github.com/KyleBanks/depth/cmd/depth
  ├ encoding/json
  ├ flag
  ├ fmt
  ├ io
  ├ log
  ├ os
  ├ strings
  └ github.com/KyleBanks/depth
7 dependencies (6 internal, 1 external, 0 testing).

The -max flag is particularly useful in conjunction with the -internal flag which can lead to very deep dependency trees.

-test

By default, depth ignores dependencies that are only required for testing. However, you can view test dependencies using the -test flag:

$ depth -test strings
strings
  ├ bytes
  ├ errors
  ├ fmt
  ├ io
  ├ io/ioutil
  ├ math/rand
  ├ reflect
  ├ sync
  ├ testing
  ├ unicode
  ├ unicode/utf8
  └ unsafe
13 dependencies (13 internal, 0 external, 8 testing).

-explain target-package

The -explain flag instructs depth to print import chains in which the target-package is found:

$ depth -explain strings github.com/KyleBanks/depth/cmd/depth
github.com/KyleBanks/depth/cmd/depth -> strings
github.com/KyleBanks/depth/cmd/depth -> github.com/KyleBanks/depth -> strings

-json

The -json flag instructs depth to output dependencies in JSON format:

$ depth -json github.com/KyleBanks/depth/cmd/depth
{
  "name": "github.com/KyleBanks/depth/cmd/depth",
  "deps": [
    {
      "name": "encoding/json",
      "internal": true,
      "deps": null
    },
    ...
    {
      "name": "github.com/KyleBanks/depth",
      "internal": false,
      "deps": [
        {
          "name": "go/build",
          "internal": true,
          "deps": null
        },
        ...
      ]
    }
  ]
}

Integrating With Your Project

The depth package can easily be used to retrieve the dependency tree for a particular package in your own project. For example, here's how you would retrieve the dependency tree for the strings package:

import "github.com/KyleBanks/depth"

var t depth.Tree
err := t.Resolve("strings")
if err != nil {
    log.Fatal(err)
}

// Output: "'strings' has 4 dependencies."
log.Printf("'%v' has %v dependencies.", t.Root.Name, len(t.Root.Deps)) 

For additional customization, simply set the appropriate flags on the Tree before resolving:

import "github.com/KyleBanks/depth"

t := depth.Tree {
  ResolveInternal: true,
  ResolveTest: true,
  MaxDepth: 10,
}


err := t.Resolve("strings")

Author

depth was developed by Kyle Banks.

License

depth is available under the MIT license.

Comments
  • Output a summary after tree

    Output a summary after tree

    It would be nice to have a summary printed below the tree, for example:

    12 dependencies (8 internal, 4 external).
    

    If the -test flag is set it would also show test-only dependencies:

    15 dependencies (8 internal, 4 external, 3 testing).
    
    enhancement easy-first-pr 
    opened by KyleBanks 5
  • nicer dependencies tree

    nicer dependencies tree

    Little modification of deps tree printing.

    Before:

    strings
      ├ errors
      ├ internal/bytealg
        ├ internal/cpu
        └ unsafe
      ├ io
        ├ errors
        ├ sync
          ├ internal/race
            └ unsafe
          ├ runtime
            ├ internal/bytealg
            ├ internal/cpu
            ├ runtime/internal/atomic
              └ unsafe
            ├ runtime/internal/sys
            └ unsafe
          ├ sync/atomic
            └ unsafe
          └ unsafe
        └ sync/atomic
      ├ unicode
      ├ unicode/utf8
      └ unsafe
    

    After:

    strings
      ├ errors
      ├ internal/bytealg
      │ ├ internal/cpu
      │ └ unsafe
      ├ io
      │ ├ errors
      │ ├ sync
      │ │ ├ internal/race
      │ │ │ └ unsafe
      │ │ ├ runtime
      │ │ │ ├ internal/bytealg
      │ │ │ ├ internal/cpu
      │ │ │ ├ runtime/internal/atomic
      │ │ │ │ └ unsafe
      │ │ │ ├ runtime/internal/sys
      │ │ │ └ unsafe
      │ │ ├ sync/atomic
      │ │ │ └ unsafe
      │ │ └ unsafe
      │ └ sync/atomic
      ├ unicode
      ├ unicode/utf8
      └ unsafe
    
    opened by chavacava 3
  • depth fails with no output when run from source

    depth fails with no output when run from source

    I tried to run from source and depth simply exited with no output. Here's what I saw, compared to the output from the last release for my system:

    [email protected]:~/go/src/drive/drive$ go get github.com/KyleBanks/depth/cmd/depth
    [email protected]:~/go/src/drive/drive$ go version
    go version go1.8 linux/amd64
    [email protected]:~/go/src/drive/drive$ depth github.com/KyleBanks/depth/cmd/depth
    [email protected]:~/go/src/drive/drive$ ~/Downloads/depth_1.1.1_linux_amd64 github.com/KyleBanks/depth/cmd/depth
    github.com/KyleBanks/depth/cmd/depth
      ├ encoding/json
      ├ flag
      ├ fmt
      ├ io
      ├ os
      ├ strings
      └ github.com/KyleBanks/depth
        ├ bytes
        ├ errors
        ├ go/build
        ├ os
        ├ path
        ├ sort
        └ strings
    

    For reference, I'm running Ubuntu 16.04 LTS.

    opened by dgarrick 3
  • Improve dependency resolution speed with concurrency

    Improve dependency resolution speed with concurrency

    Currently depth resolves all dependencies synchronously, which can be slightly time consuming on larger projects or when using -internal and/or -test flags, taking several seconds for large enough dependency trees. This would likely be improved by resolving dependencies concurrently and merging the results at the end.

    Will need to add some benchmarks prior to implementing this.

    enhancement wontfix 
    opened by KyleBanks 2
  • Update tests and documentation

    Update tests and documentation

    This change updates output provided by depth to align with reality.

    Throughout this tool, string package from Go's standard library is used as test subject and for documentation purposes. This change aligns tests the output that the tool gives corresponding with current dependencies. It does not carry any change in behavior.

    opened by magandrez 1
  • I had the same problem, but it worked when it called it from the `cmd` folder (where `main.go` is)

    I had the same problem, but it worked when it called it from the `cmd` folder (where `main.go` is)

    I had the same problem, but it worked when it called it from the cmd folder (where main.go is)

    Originally posted by @ghohmann-asapp in https://github.com/KyleBanks/depth/issues/18#issuecomment-540127086

    opened by virak-panha 1
  • depth crashes when it is unable to resolve an imported package

    depth crashes when it is unable to resolve an imported package

    This may be fine for typical use, but for testing package imports it's particularly troubling. Consider the following:

    mypkg 
        - vendoredpkg
             - testdep
    

    When resolving dependencies most package managers do not resolve secondary dependencies that are only required for testing. If mypkg depends on vendoredpkg that's fine, but running depth with -test will try to resolve it's testdep, which likely won't exist.

    I think in the case of test packages only, depth could output something like so:

    mypkg 
        - vendoredpkg
             - testdep (UNRESOLVED)
    
    bug 
    opened by KyleBanks 0
  • Handle relative paths from the command line

    Handle relative paths from the command line

    Currently you have to type the full import path of a package, for example github.com/KyleBanks/depth.

    It would be nice to use relative paths like so:

    $ pwd 
    $GOPATH/src/github.com/KyleBanks/depth
    $ depth .
    github.com/KyleBanks/dept
    ...
    $ depth ./cmd/depth
    github.com/KyleBanks/depth/CMD/depth
    
    enhancement easy-first-pr 
    opened by KyleBanks 0
  • Log error message

    Log error message

    Error logging helps to track down issues. For example, I was investigating why swaggo failed in my project and found the package name is not in GOROOT error after I modified depth/pkg.go. It wasn't an obvious thing in my project.

    This PR adds error logging in the pkg.go file when it fails to import.

    opened by nodivbyzero 0
  • Possible performance issue detected in downstream library using `depth`

    Possible performance issue detected in downstream library using `depth`

    Hi @KyleBanks,

    An issue has been reported recently in a downstream tool, swaggo/swag, which uses depth to calculate required dependencies in order to build valid OpenAPI 2.0 output as a result of parsing code comments in a given folder structure.

    Under https://github.com/swaggo/swag/issues/1032 the user @matrixik has exquisitely pointed out, there seems to be a performance issue in swag, which I have narrowed down, with the help of @sdghchj to be a problem with depth.

    1. Could you give a look at the flamegraphs provided under the issue linked and verify the assumption the problem lies in this tool is correct?
    2. Would you be able and willing to help fixing it?

    We use swaggo throughout our platform at Compensate in order to build the documentation for all our services.

    Thank you,

    -Manuel

    opened by magandrez 3
  • "depth" does not seem to work on any packages other than "depth"

    [~] % depth github.com/KyleBanks/conways-gol
    'github.com/KyleBanks/conways-gol': FATAL: unable to resolve root package
    [~]!% depth github.com/KyleBanks/goggles
    'github.com/KyleBanks/goggles': FATAL: unable to resolve root package
    

    It doesn't work on any KyleBanks packages other than "depth" itself, as far as I have tried.

    opened by iphydf 6
  • Does it work for package 'main'?

    Does it work for package 'main'?

    I tried this on a server program where I want to see the dependency tree starting from the main package. I cd'ed into the same directory where main.go is found and ran depth main. This returned 'main': FATAL: unable to resolve root package.

    Of note is that I have many 'local' packages under main, and that is the tree that I would like to see.

    Thanks.

    opened by webern 2
Releases(v1.2.1)
The most opinionated Go source code linter for code audit.

go-critic Highly extensible Go source code linter providing checks currently missing from other linters. There is never too much static code analysis.

null 1.4k Dec 4, 2022
A Golang tool that does static analysis, unit testing, code review and generate code quality report.

goreporter A Golang tool that does static analysis, unit testing, code review and generate code quality report. This is a tool that concurrently runs

360 Enterprise Security Group, Endpoint Security, inc. 3k Dec 7, 2022
🐶 Automated code review tool integrated with any code analysis tools regardless of programming language

reviewdog - A code review dog who keeps your codebase healthy. reviewdog provides a way to post review comments to code hosting service, such as GitHu

reviewdog 5.8k Dec 2, 2022
🐶 Automated code review tool integrated with any code analysis tools regardless of programming language

reviewdog - A code review dog who keeps your codebase healthy. reviewdog provides a way to post review comments to code hosting service, such as GitHu

reviewdog 5.8k Dec 4, 2022
Clean architecture validator for go, like a The Dependency Rule and interaction between packages in your Go projects.

Clean Architecture checker for Golang go-cleanarch was created to keep Clean Architecture rules, like a The Dependency Rule and interaction between mo

Robert Laszczak 637 Dec 1, 2022
[mirror] This is a linter for Go source code.

Golint is a linter for Go source code. Installation Golint requires a supported release of Go. go get -u golang.org/x/lint/golint To find out where g

Go 4k Dec 6, 2022
Print all source code for a given go package or module.

gosrcs gosrcs is a tool to print all the source code a given go package depends on. The original motivation of this tool is to integrate go builds int

null 3 Oct 25, 2021
Detect non-inclusive language in your source code.

Detect non-inclusive language in your source code. I stay woke - Erykah Badu Creating an inclusive work environment is imperative to a healthy, suppor

woke 359 Nov 23, 2022
Sloc, Cloc and Code: scc is a very fast accurate code counter with complexity calculations and COCOMO estimates written in pure Go

Sloc Cloc and Code (scc) A tool similar to cloc, sloccount and tokei. For counting physical the lines of code, blank lines, comment lines, and physica

Ben Boyter 4k Dec 4, 2022
Tool to populate your code with traceable and secure error codes

Essential part of any project, especially customer facing is proper and secure error handling. When error happens and customer reports it, it would be nice to know the context of the error and where it exactly occured.

vs 51 Sep 28, 2022
Refactoring and code transformation tool for Go.

gopatch is a tool to match and transform Go code. It is meant to aid in refactoring and restyling.

Uber Go 522 Dec 2, 2022
a tool for code clone detection

dupl dupl is a tool written in Go for finding code clones. So far it can find clones only in the Go source files. The method uses suffix tree for seri

Michal Bohuslávek 301 Nov 12, 2022
a simple golang SSA viewer tool use for code analysis or make a linter

ssaviewer A simple golang SSA viewer tool use for code analysis or make a linter ssa.html generate code modify from src/cmd/compile/internal/ssa/html.

null 7 May 17, 2022
Remove unnecessary type conversions from Go source

About The unconvert program analyzes Go packages to identify unnecessary type conversions; i.e., expressions T(x) where x already has type T. Install

Matthew Dempsky 342 Nov 27, 2022
A reference for the Go community that covers the fundamentals of writing clean code and discusses concrete refactoring examples specific to Go.

A reference for the Go community that covers the fundamentals of writing clean code and discusses concrete refactoring examples specific to Go.

Lasse Martin Jakobsen 2.5k Dec 8, 2022
Act as part of the business code and will report aqua scan report after application installed

starboard-report This repo aim to enrich the functionality of starboard. Starboard integrates security tools into the Kubernetes environment, so that

null 17 Nov 25, 2021
Run linters from Go code -

Lint - run linters from Go Lint makes it easy to run linters from Go code. This allows lint checks to be part of a regular go build + go test workflow

Surul Software Labs GmbH 67 Sep 27, 2022
A static code analyzer for annotated TODO comments

todocheck todocheck is a static code analyzer for annotated TODO comments. It let's you create actionable TODOs by annotating them with issues from an

Preslav Mihaylov 395 Nov 27, 2022
A little fast cloc(Count Lines Of Code)

gocloc A little fast cloc(Count Lines Of Code), written in Go. Inspired by tokei. Installation $ go get -u github.com/hhatto/gocloc/cmd/gocloc Usage

Hideo Hattori 623 Nov 23, 2022