Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Fixes #316 Replace full path for vendor packages with relative path #491

Merged
merged 1 commit into from
Oct 17, 2016

Conversation

ramya-rao-a
Copy link
Contributor

@ramya-rao-a ramya-rao-a commented Sep 20, 2016

PR to fix #316

This PR uses the prefix from the full path of vendor packages, appends that to $GOPATH to get the path to the vendor folder, tries to match it with current file path to see if both belong to the same project. If yes, then the relative path of the vendor replaces the full path.

Another way to find relative paths for vendor packages is by using the govendor tool:
govendor list -no-status +v gives the list of vendor packages (with relative path) for current go project.
But using this adds a 1 second delay between executing the command Go: Add Import and showing the results in Windows. In Mac, the difference is much less.
The implementation using govendor is here: https://github.com/ramya-rao-a/vscode-go/blob/vendor-imports-using-govendor/src/goImport.ts

Thoughts on both approaches are welcome.

return importedPkgs.indexOf(element) === -1;
});
}
return pkgs.sort().slice(1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is removing a "" entry - do you know why that exists? Should this slice be a safer check? Maybe roll it into the filter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the stdout from gopkgs is a \n separated pkg paths. So when you do a split, you end up with a trailing empty string. That's what this originally was, and I just moved the code around while refactoring. Will fix it nevertheless.

// If yes, then vendor pkg can be replaced with its relative path to the "vendor" folder
if (vendorIndex > 0) {
let rootProjectForVendorPkg = path.join(currentWorkspace, pkg.substr(0, vendorIndex));
let relativePathForVendorPkg = pkg.substring(vendorIndex + 8);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8... '/vendor/'.length?

Copy link
Contributor Author

@ramya-rao-a ramya-rao-a Oct 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, will pull out '/vendor/' into a var, so use .length so that 8 is not mysterious

// The current project github.com/someotherrepo/b may have a version of a as a vendor package
// github.com/someotherrepo/b/vendor/github.com/somerepo/a
// In this case, github.com/somerepo/a would already be returned by goPkgs
if (pkgs.indexOf(relativePathForVendorPkg) === -1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth it to get rid of this check for perf? It's only applied to vendored packages so maybe it's not a big deal but do people have 100s of vendored packages? You could make pkgs a Set, or do other things to de-dupe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set is good idea, though in the end I'll have to convert it to array to have sorting

@roblourens
Copy link
Member

This seems pretty straightforward, so it doesn't seem like the delay to start up govendor is worth it?

return;
}

let vendorIndex = pkg.indexOf('/vendor/');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can packages not include '/vendor/' somewhere else in the name? Could I 'go get github.com/foo/vendor' then this might be a false positive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below is from Understanding and using the vendor folder

However, if a package or a parent folder of a package contains folder named vendor it will be searched for dependencies using the vendor folder as an import path root.

And while trying different combinations, I could confirm that by "parent", they mean "ancestor"
Since that is the exact check I make which is making sure that the "vendor" folder's ancestors are current package's ancestors too, we should be good.

Go doesn't stop you from creating a package by the name "vendor", so you could totally do a "go get github.com/foo/vendor". Any packages under this folder will be available for use to any Go projects under "github.com/foo/*"

@@ -9,31 +9,91 @@ import vscode = require('vscode');
import cp = require('child_process');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ramya-rao-a ramya-rao-a merged commit 06eabf7 into microsoft:master Oct 17, 2016
@ramya-rao-a ramya-rao-a deleted the vendor-imports branch January 22, 2017 07:56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Go: Add Imports adds full path to vendored dependency, instead of the vendored path
3 participants