Skip to content
This repository was 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
Ramya Rao (ramya-rao-a) merged 1 commit into
microsoft:masterfrom
ramya-rao-a:vendor-imports
Oct 17, 2016
Merged

Fixes #316 Replace full path for vendor packages with relative path#491
Ramya Rao (ramya-rao-a) merged 1 commit into
microsoft:masterfrom
ramya-rao-a:vendor-imports

Conversation

@ramya-rao-a

@ramya-rao-a Ramya Rao (ramya-rao-a) commented Sep 20, 2016

Copy link
Copy Markdown
Contributor

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.

Comment thread src/goImport.ts Outdated
return importedPkgs.indexOf(element) === -1;
});
}
return pkgs.sort().slice(1);

Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread src/goImport.ts Outdated
// 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
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

@ramya-rao-a Ramya Rao (ramya-rao-a) Oct 16, 2016

Copy link
Copy Markdown
Contributor Author

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

Comment thread src/goImport.ts Outdated
// 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

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

Comment thread src/goImport.ts Outdated
return;
}

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

Copy link
Copy Markdown
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
Copy Markdown
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/*"

Comment thread src/goImport.ts
@@ -9,31 +9,91 @@ import vscode = require('vscode');
import cp = require('child_process');

Copy link
Copy Markdown
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 (ramya-rao-a) merged commit 06eabf7 into microsoft:master Oct 17, 2016
@ramya-rao-a
Ramya Rao (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