From 6b5449fe4bf1d94d8679075a7d83e518b2ab4c47 Mon Sep 17 00:00:00 2001 From: Ramya Rao Date: Wed, 9 Nov 2016 11:44:54 -0800 Subject: [PATCH] Dont allow import on vendored pkg from another project (#605) --- src/goImport.ts | 5 +++-- test/go.test.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/goImport.ts b/src/goImport.ts index b30bd7521..72c838f9b 100644 --- a/src/goImport.ts +++ b/src/goImport.ts @@ -79,17 +79,18 @@ export function listPackages(excludeImportedPkgs: boolean = false): Thenable 0) { let rootProjectForVendorPkg = path.join(currentWorkspace, pkg.substr(0, vendorIndex)); let relativePathForVendorPkg = pkg.substring(vendorIndex + magicVendorString.length); if (relativePathForVendorPkg && currentFileDirPath.startsWith(rootProjectForVendorPkg)) { pkgSet.add(relativePathForVendorPkg); - return; } + return; } - // pkg is not a vendor project or is a vendor project not belonging to current project + // pkg is not a vendor project pkgSet.add(pkg); }); diff --git a/test/go.test.ts b/test/go.test.ts index bb761cb9c..7be725962 100644 --- a/test/go.test.ts +++ b/test/go.test.ts @@ -491,4 +491,49 @@ encountered. }); }).then(() => done(), done); }); + + test('Vendor pkgs from other projects should not be allowed to import', (done) => { + // This test needs a go project that has vendor folder and vendor packages + // Since the Go extension takes a dependency on the godef tool at github.com/rogpeppe/godef + // which has vendor packages, we are using it here to test the "replace vendor packages with relative path" feature. + // If the extension ever stops depending on godef tool or if godef ever stops having vendor packages, then this test + // will fail and will have to be replaced with any other go project with vendor packages + + let vendorSupportPromise = isVendorSupported(); + let filePath = path.join(process.env['GOPATH'], 'src', 'github.com', 'lukehoban', 'go-outline', 'main.go'); + let vendorPkgs = [ + 'github.com/rogpeppe/godef/vendor/9fans.net/go/acme', + 'github.com/rogpeppe/godef/vendor/9fans.net/go/plan9', + 'github.com/rogpeppe/godef/vendor/9fans.net/go/plan9/client' + ]; + + vendorSupportPromise.then((vendorSupport: boolean) => { + let gopkgsPromise = new Promise((resolve, reject) => { + cp.execFile(getBinPath('gopkgs'), [], (err, stdout, stderr) => { + let pkgs = stdout.split('\n').sort().slice(1); + if (vendorSupport) { + vendorPkgs.forEach(pkg => { + assert.equal(pkgs.indexOf(pkg) > -1, true, `Package not found by goPkgs: ${pkg}`); + }); + } + return resolve(); + }); + }); + + let listPkgPromise: Thenable = vscode.workspace.openTextDocument(vscode.Uri.file(filePath)).then(document => { + return vscode.window.showTextDocument(document).then(editor => { + return listPackages().then(pkgs => { + if (vendorSupport) { + vendorPkgs.forEach(pkg => { + assert.equal(pkgs.indexOf(pkg), -1, `Vendor package ${pkg} should not be shown by listPackages method`); + }); + } + return Promise.resolve(); + }); + }); + }); + + return Promise.all([gopkgsPromise, listPkgPromise]); + }).then(() => done(), done); + }); });