Code Monkey home page Code Monkey logo

depth's Introduction

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
  ├ internal/bytealg
  ├ io
  ├ sync
  ├ unicode
  ├ unicode/utf8
  └ unsafe
7 dependencies (7 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
  ├ internal/bytealg
  ├ io
  ├ sync
  ├ unicode
  ├ unicode/utf8
  └ unsafe
7 dependencies (7 internal, 0 external, 0 testing).
github.com/KyleBanks/depth
  ├ bytes
  ├ errors
  ├ go/build
  ├ os
  ├ 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
  │ └ internal/reflectlite
  │   ├ internal/unsafeheader
  │   │ └ unsafe
  │   ├ runtime
  │   │ ├ internal/abi
  │   │ │ └ unsafe
  │   │ ├ internal/bytealg
  │   │ │ ├ internal/cpu
  │   │ │ └ unsafe
  │   │ ├ internal/cpu
  │   │ ├ internal/goexperiment
  │   │ ├ runtime/internal/atomic
  │   │ │ └ unsafe
  │   │ ├ runtime/internal/math
  │   │ │ └ runtime/internal/sys
  │   │ ├ runtime/internal/sys
  │   │ └ unsafe
  │   └ unsafe
  ├ internal/bytealg
  ├ io
  │ ├ errors
  │ └ sync
  │   ├ internal/race
  │   │ └ unsafe
  │   ├ runtime
  │   ├ sync/atomic
  │   │ └ unsafe
  │   └ unsafe
  ├ sync
  ├ unicode
  ├ unicode/utf8
  └ unsafe
18 dependencies (18 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
  ├ internal/bytealg
  ├ internal/testenv
  ├ io
  ├ math/rand
  ├ reflect
  ├ strconv
  ├ sync
  ├ testing
  ├ unicode
  ├ unicode/utf8
  └ unsafe
14 dependencies (14 internal, 0 external, 7 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.

depth's People

Contributors

aaronbee avatar chavacava avatar dgarrick avatar kylebanks avatar ucirello 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  avatar

depth's Issues

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)

"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.

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 swaggo/swag#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

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.

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:

dgarrick@dgarrick-desktop:~/go/src/drive/drive$ go get github.com/KyleBanks/depth/cmd/depth
dgarrick@dgarrick-desktop:~/go/src/drive/drive$ go version
go version go1.8 linux/amd64
dgarrick@dgarrick-desktop:~/go/src/drive/drive$ depth github.com/KyleBanks/depth/cmd/depth
dgarrick@dgarrick-desktop:~/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.

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).

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.

If local Go is different version than Go used to compile `depth`, `tree.Resolve` fails with an unhelpful error message

Hi there!

We've been using depth as a library as a part of https://github.com/fossas/fossa-cli to power our Go dependency discovery. We recently ran into an interesting bug where using a copy of depth compiled with a different (newer?) version of Go than the local Go caused tree.Resolve to fail with cryptic errors like could not resolve package: fmt or could not resolve package: bytes. My suspicion is that this is related to depth's usage of go/build, and some weirdness is going on with importing standard library packages when the Go version changes. See fossas/fossa-cli#63 for our downstream tracking issue.

We're working around this issue by switching to go list parsing instead, but I wonder if this can be resolved within depth itself (maybe by special-casing the standard library packages?). If not, it may be possible to at least give a less cryptic error message by detecting when a resolution error occurs on a known standard library package.

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

Emit "internal" key in JSON output

It would be excellent if the JSON output included a boolean called internal that indicated whether a package is from the standard library.

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.