Improve: use list command to resolve relative import path #40
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why this change?
With a very simple repository for demonstration:
git clone https://github.com/leventeliu/godepgraph-demo.git cd godepgraph-demo
Initially, when I was trying to get dep-graph inside a go module (given filter
-o <prefix>
) with a relative import path syntax,godepgraph
gave an empty result:After some code reading, I found the correct way to do it cloud be:
But it's weird, isn't it? The problem here is that the relative path
./cmd/nop
could (and should) be resolved as a full package pathgithub.com/leventeliu/godepgraph-demo/cmd/nop
, which will not be ignored by the filter-o github.com/leventeliu
.And after I tried the
list
command:I thought this is the proper way to solve the problem.
List command is implemented by an internal package, which will be tricky to call directly. Alternatively, I use an
os/exec.Command
to callgo list
and pass the parent environment to the command (any better idea?)Now it works well:
This improvement applies to the case when you are working with "GOPATH mode", instead of go module, too.
Also, this implies a special pattern
/...
will be supported: