Skip to content

Commit

Permalink
fix: detect internal pkgs of the root, and give them same version
Browse files Browse the repository at this point in the history
this change also lays the ground for warning/throwing if an external pkg
is missing in the lockfile
  • Loading branch information
michael-go committed Aug 29, 2017
1 parent 372a8e8 commit 869524a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
34 changes: 19 additions & 15 deletions gosrc/deps-tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ import (
type Pkg struct {
Name string
FullImportPath string
SrcDir string
Dir string
Depth int

//TODO: these should better be rename to IsX
BuiltIn bool `json:"-"`
Resolved bool

Tree *Tree `json:"-"`
Parent *Pkg `json:"-"`
Deps []Pkg `json:",omitempty"`
Tree *Tree `json:"-"`
Parent *Pkg `json:"-"`
ParentDir string `json:"-"`
Deps []Pkg `json:",omitempty"`

Raw *build.Package `json:"-"`
}
Expand All @@ -50,14 +52,15 @@ func (p *Pkg) Resolve() {
importMode = build.FindOnly
}

pkg, err := build.Default.Import(name, p.SrcDir, importMode)
pkg, err := build.Default.Import(name, p.ParentDir, importMode)
if err != nil {
// TODO: Check the error type?
p.Resolved = false
p.Tree.rememverUnresolvedPkg(name)
return
}
p.Raw = pkg
p.Dir = pkg.Dir

// Clear some too verbose fields
p.Raw.ImportPos = nil
Expand All @@ -83,7 +86,7 @@ func (p *Pkg) Resolve() {
// setDeps takes a slice of import paths and the source directory they are relative to,
// and creates the Deps of the Pkg. Each dependency is also further resolved prior to being added
// to the Pkg.
func (p *Pkg) setDeps(imports []string, srcDir string) {
func (p *Pkg) setDeps(imports []string, parentDir string) {
unique := make(map[string]struct{})

for _, imp := range imports {
Expand All @@ -98,19 +101,20 @@ func (p *Pkg) setDeps(imports []string, srcDir string) {
}
unique[imp] = struct{}{}

p.addDep(imp, srcDir)
p.addDep(imp, parentDir)
}

sort.Sort(sortablePkgsList(p.Deps))
}

// addDep creates a Pkg and it's dependencies from an imported package name.
func (p *Pkg) addDep(name string, srcDir string) {
func (p *Pkg) addDep(name string, parentDir string) {
dep := Pkg{
Name: name,
SrcDir: srcDir,
Tree: p.Tree,
Parent: p,
Name: name,
Tree: p.Tree,
//TODO: maybe better pass ParentDir as a param to Resolve() instead
ParentDir: parentDir,
Parent: p,
}
dep.Resolve()

Expand Down Expand Up @@ -209,9 +213,9 @@ func (t *Tree) Resolve(name string) error {
}

t.Root = &Pkg{
Name: name,
Tree: t,
SrcDir: pwd,
Name: name,
Tree: t,
ParentDir: pwd,
}

// Reset the import cache each time to ensure a reused Tree doesn't
Expand Down
39 changes: 34 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function inspect(root, targetFile, options) {
}).then(function (tree) {
tree = JSON.parse(tree);

var pkgsTree = recursivelyBuildPkgTree(tree, depLocks, []);
var projectRootPath = path.dirname(path.resolve(targetFile));
var pkgsTree = recursivelyBuildPkgTree(tree, depLocks, projectRootPath, []);
pkgsTree.packageFormatVersion = 'golang:0.0.1';

return {
Expand All @@ -51,24 +52,52 @@ function inspect(root, targetFile, options) {
});
}

function recursivelyBuildPkgTree(goDepsTree, depLocks, fromPath) {
function isInternalPkg(pkgPath, projectRootPath) {
if (pkgPath == projectRootPath) {
return true;
}

var root = projectRootPath;
root =
(root[root.length - 1] == path.sep) ? root : (root + path.sep);

if (pkgPath.indexOf(root) != 0) {
return false;
}

var pkgRelativePath = pkgPath.slice(root.length);

if (
pkgRelativePath.indexOf('vendor/') == 0 ||
pkgRelativePath.indexOf('/vendor/') >= 0) {
return false;
}

return true;
}

function recursivelyBuildPkgTree(goDepsTree, depLocks, projectRootPath, fromPath) {
var isRoot = (fromPath.length == 0);

var pkg = {
name: (isRoot ? goDepsTree.FullImportPath : goDepsTree.Name),
dependencies: {},
}

if (isRoot) {
if (isRoot || isInternalPkg(goDepsTree.Dir, projectRootPath)) {
pkg.version = '0.0.0';
} else if (!depLocks[pkg.name]) {
pkg.version = '';
// TODO: throw or warn here
} else {
pkg.version = depLocks[pkg.name] ? depLocks[pkg.name].version : '';
pkg.version = depLocks[pkg.name].version;
}

pkg.from = fromPath.concat(pkg.name + '@' + pkg.version)

goDepsTree.Deps && goDepsTree.Deps.forEach(function (dep) {
var child = recursivelyBuildPkgTree(dep, depLocks, pkg.from)
var child = recursivelyBuildPkgTree(
dep, depLocks, projectRootPath, pkg.from)
pkg.dependencies[child.name] = child;
})

Expand Down
6 changes: 3 additions & 3 deletions test/inspect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ test('pkg with local import', function (t) {
var deps = pkg.dependencies;

t.match(deps['path/to/pkg-with-local-import/subpkg'], {
version: '',
version: '0.0.0',
dependencies: {
'gitpub.com/meal/dinner': {
version: 'v0.0.7',
Expand All @@ -100,7 +100,7 @@ test('pkg with local import', function (t) {
version: '#b6ffb7d62206806b573348160795ea16a00940a6',
from: [
'path/to/pkg-with-local-import@0.0.0',
'path/to/pkg-with-local-import/subpkg@',
'path/to/pkg-with-local-import/subpkg@0.0.0',
'gitpub.com/meal/dinner@v0.0.7',
'gitpub.com/food/salad@v1.3.7',
'gitpub.com/nature/vegetables/tomato@#b6ffb7d62206806b573348160795ea16a00940a6', // jscs:ignore maximumLineLength
Expand All @@ -111,7 +111,7 @@ test('pkg with local import', function (t) {
},
},
},
}, 'local subpkg is a dep with no version');
}, 'local subpkg has the same version of root');

t.end();
});
Expand Down

0 comments on commit 869524a

Please sign in to comment.