Skip to content

Commit 1cc6c08

Browse files
committed
Add a check to ensure canonical import path
Reject the candidate when it's imported via a path that differs from the dependency's advertised canonical path.
1 parent 42374f1 commit 1cc6c08

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

internal/gps/satisfy.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (s *solver) check(a atomWithPackages, pkgonly bool) error {
3535
}
3636
}
3737

38-
if err = s.checkRequiredPackagesExist(a); err != nil {
38+
if err = s.checkImportRequirementsAreValid(a); err != nil {
3939
return err
4040
}
4141

@@ -100,9 +100,9 @@ func (s *solver) checkAtomAllowable(pa atom) error {
100100
return err
101101
}
102102

103-
// checkRequiredPackagesExist ensures that all required packages enumerated by
103+
// checkImportRequirementsAreValid ensures that all required packages enumerated by
104104
// existing dependencies on this atom are actually present in the atom.
105-
func (s *solver) checkRequiredPackagesExist(a atomWithPackages) error {
105+
func (s *solver) checkImportRequirementsAreValid(a atomWithPackages) error {
106106
ptree, err := s.b.ListPackages(a.a.id, a.a.v)
107107
if err != nil {
108108
// TODO(sdboyer) handle this more gracefully
@@ -121,11 +121,24 @@ func (s *solver) checkRequiredPackagesExist(a atomWithPackages) error {
121121
fp[pkg] = errdep
122122
} else {
123123
perr, has := ptree.Packages[pkg]
124-
if !has || perr.Err != nil {
124+
switch {
125+
case !has:
126+
fallthrough
127+
case perr.Err != nil:
125128
fp[pkg] = errDeppers{
126129
err: perr.Err,
127130
deppers: []atom{dep.depender},
128131
}
132+
case perr.P.CommentPath != "" && pkg != perr.P.CommentPath:
133+
// we have the package, but depender is trying to import it
134+
// via non-canonical import path, so croak about it
135+
fp[pkg] = errDeppers{
136+
err: &canonicalImportPathFailure{
137+
actual: pkg,
138+
canonical: perr.P.CommentPath,
139+
},
140+
deppers: []atom{dep.depender},
141+
}
129142
}
130143
}
131144
}

internal/gps/solve_failures.go

+13
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,19 @@ func (e *checkeeHasProblemPackagesFailure) traceString() string {
369369
return buf.String()
370370
}
371371

372+
// canonicalImportPathFailure indicates that the dependee tries to import a
373+
// dependency via a path that is different from the one that the dependency
374+
// declares as its canonical.
375+
type canonicalImportPathFailure struct {
376+
actual string
377+
canonical string
378+
}
379+
380+
func (e *canonicalImportPathFailure) Error() string {
381+
return fmt.Sprintf("Importing via a path different from the canonical path (got %q, want %q)",
382+
e.actual, e.canonical)
383+
}
384+
372385
// depHasProblemPackagesFailure indicates that the goal dependency was rejected
373386
// because there were problems with one or more of the packages the dependency
374387
// requires in the atom currently selected for that dependency. (This failure

0 commit comments

Comments
 (0)