Skip to content

Instantly share code, notes, and snippets.

@renier
Last active September 23, 2024 20:30
Show Gist options
  • Save renier/0beddc56dff9cd27be434200efe7fe6a to your computer and use it in GitHub Desktop.
Save renier/0beddc56dff9cd27be434200efe7fe6a to your computer and use it in GitHub Desktop.
Like go mod why but for dependencies in go.sum and not in go.mod - will also work around direct dependencies listed as indirect which can happen with the use of replace directives downstream. Understands module argument with and without the version.
#!/bin/bash
# Like go mod why but for dependencies in go.sum and not in go.mod - will also
# work around direct dependencies in the graph listed as indirect in go.mod
# which can happen with the use of replace directives downstream. Understands
# module argument with and without the version.
#
# usage: ./goexplain.sh example.com/path/module
# usage: ./goexplain.sh example.com/path/[email protected]
bold=$(tput bold)
normal=$(tput sgr0)
blue=$(tput setaf 4)
function show_why() {
why_list="$(go mod graph | digraph allpaths $(go list -m) $1 | tr ' ' '\n' | awk '!x[$0]++' | grep -v $1)"
echo "${why_list}"
second_one="$(echo "${why_list}" | head -2 | tail -1)"
if grep "${second_one/@/ }" go.mod | grep indirect > /dev/null; then
echo "${bold}> explaining indirect dependency:${normal}"
go mod why -m ${second_one/@*/}
fi
}
if echo $1 | grep "@" > /dev/null; then
show_why $1
else
for d in $(go mod graph | grep ${1}@ | gsed -e "s|.*\($1@[^ ]\+\).*|\1|" | awk '!x[$0]++'); do
echo "${bold}${blue}>> chain for $d:${normal}"
show_why $d
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment