doujingjiao0015 2017-06-13 12:07 采纳率: 0%
浏览 776

如何查看嵌套Go依赖的完整依赖树

I'm trying to debug the following build error in our CI where "A depends on B which can't build because it depends on C." I'm building my data service which doesn't directly depend on kafkaAvailMonitor.go which makes this error hard to trace. In other words:

data (what I'm building) depends on (?) which depends on kafkaAvailMonitor.go

It may seem trivial to fix for a developer they just do "go get whatever" but I can't do that as part of the release process - I have to find the person that added the dependency and ask them to fix it.

I'm aware that there are tools to visualize the dependency tree and other more sophisticated build systems, but this seems like a pretty basic issue: is there any way I can view the full trace to see what's causing the build issue? In response to one of the comments - every programming languages I've used to date e.g. Java/Python all provide this information out of the box and it is very helpful for debugging code that I didn't write especially since I can't just look in a single place like a POM or requirements.txt to see who added the dependency.

Also in response to a comment - if the following isn't a stack trace what is it? An list of errors logged to stdout by a binary? Aren't those errors first put on the stack by the go build binary that's being executed? Seems like a silly distinction to me to say "that's not a stack trace" especially when it has nothing to do with my question, but I removed the word "stack" from the question if that makes people happy.

go build -a -v

../../../msgq/kafkaAvailMonitor.go:8:2: cannot find package 
  "github.com/Shopify/sarama/tz/breaker" in any of:
  /usr/lib/go-1.6/src/github.com/Shopify/sarama/tz/breaker (from $GOROOT)
  /home/jenkins/go/src/github.com/Shopify/sarama/tz/breaker (from $GOPATH)
  /home/jenkins/vendor-library/src/github.com/Shopify/sarama/tz/breaker
  /home/jenkins/go/src/github.com/Shopify/sarama/tz/breaker
  /home/jenkins/vendor-library/src/github.com/Shopify/sarama/tz/breaker
  • 写回答

2条回答 默认 最新

  • dongzhuo1498 2017-06-26 20:45
    关注

    if the following isn't a stack trace what is it?

    It is the list of path where Go is looking for your missing package.

    I have no idea who is importing kafkaAvailMonitor.go

    It is not "imported", just part of your sources and compiled.
    Except it cannot compile, because it needs github.com/Shopify/sarama/tz/breaker, which is not in GOROOT or GOPATH.

    Still, check what go list would return on your direct package, to see if kafkaAvailMonitor is mentioned.

    go list can show both the packages that your package directly depends, or its complete set of transitive dependencies.

    % go list -f '{{ .Imports }}' github.com/davecheney/profile
    [io/ioutil log os os/signal path/filepath runtime runtime/pprof]
    % go list -f '{{ .Deps }}' github.com/davecheney/profile
    [bufio bytes errors fmt io io/ioutil log math os os/signal path/filepath reflect run
    

    You can then script go list in order to list all dependencies.
    See this bash script for instance, by Noel Cower (nilium)

    #!/usr/bin/env bash
    # Usage: lsdep [PACKAGE...]
    #
    # Example (list github.com/foo/bar and package dir deps [the . argument])
    # $ lsdep github.com/foo/bar .
    #
    # By default, this will list dependencies (imports), test imports, and test
    # dependencies (imports made by test imports).  You can recurse further by
    # setting TESTIMPORTS to an integer greater than one, or to skip test
    # dependencies, set TESTIMPORTS to 0 or a negative integer.
    
    : "${TESTIMPORTS:=1}"
    
    lsdep_impl__ () {
        local txtestimps='{{range $v := .TestImports}}{{print . "
    "}}{{end}}'
        local txdeps='{{range $v := .Deps}}{{print . "
    "}}{{end}}'
    
        {
            go list -f "${txtestimps}${txdeps}" "$@"
            if [[ -n "${TESTIMPORTS}" ]] && [[ "${TESTIMPORTS:-1}" -gt 0 ]]
            then
                go list -f "${txtestimps}" "$@" |
                sort | uniq |
                comm -23 - <(go list std | sort) |
                    TESTIMPORTS=$((TESTIMPORTS - 1)) xargs bash -c 'lsdep_impl__ "$@"' "$0"
            fi
        } |
        sort | uniq |
        comm -23 - <(go list std | sort)
    }
    export -f lsdep_impl__
    
    lsdep_impl__ "$@"
    
    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条