Skip to content

Commit

Permalink
Add a check to ensure canonical import path
Browse files Browse the repository at this point in the history
Reject the candidate when it's imported via a path that differs from the
dependency's advertised canonical path.
  • Loading branch information
rtfb committed Aug 26, 2017
1 parent 42374f1 commit 1cc6c08
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
21 changes: 17 additions & 4 deletions internal/gps/satisfy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (s *solver) check(a atomWithPackages, pkgonly bool) error {
}
}

if err = s.checkRequiredPackagesExist(a); err != nil {
if err = s.checkImportRequirementsAreValid(a); err != nil {
return err
}

Expand Down Expand Up @@ -100,9 +100,9 @@ func (s *solver) checkAtomAllowable(pa atom) error {
return err
}

// checkRequiredPackagesExist ensures that all required packages enumerated by
// checkImportRequirementsAreValid ensures that all required packages enumerated by
// existing dependencies on this atom are actually present in the atom.
func (s *solver) checkRequiredPackagesExist(a atomWithPackages) error {
func (s *solver) checkImportRequirementsAreValid(a atomWithPackages) error {
ptree, err := s.b.ListPackages(a.a.id, a.a.v)
if err != nil {
// TODO(sdboyer) handle this more gracefully
Expand All @@ -121,11 +121,24 @@ func (s *solver) checkRequiredPackagesExist(a atomWithPackages) error {
fp[pkg] = errdep
} else {
perr, has := ptree.Packages[pkg]
if !has || perr.Err != nil {
switch {
case !has:
fallthrough
case perr.Err != nil:
fp[pkg] = errDeppers{
err: perr.Err,
deppers: []atom{dep.depender},
}
case perr.P.CommentPath != "" && pkg != perr.P.CommentPath:
// we have the package, but depender is trying to import it
// via non-canonical import path, so croak about it
fp[pkg] = errDeppers{
err: &canonicalImportPathFailure{
actual: pkg,
canonical: perr.P.CommentPath,
},
deppers: []atom{dep.depender},
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions internal/gps/solve_failures.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,19 @@ func (e *checkeeHasProblemPackagesFailure) traceString() string {
return buf.String()
}

// canonicalImportPathFailure indicates that the dependee tries to import a
// dependency via a path that is different from the one that the dependency
// declares as its canonical.
type canonicalImportPathFailure struct {
actual string
canonical string
}

func (e *canonicalImportPathFailure) Error() string {
return fmt.Sprintf("Importing via a path different from the canonical path (got %q, want %q)",
e.actual, e.canonical)
}

// depHasProblemPackagesFailure indicates that the goal dependency was rejected
// because there were problems with one or more of the packages the dependency
// requires in the atom currently selected for that dependency. (This failure
Expand Down

0 comments on commit 1cc6c08

Please sign in to comment.