improve redirection to canonical import path


Our GitHub organisation is named Teamwork, but for various hard to
change reasons we import packages as teamwork (lower case t). To enforce
this we add an import enforcement for our packages:

	package duck // import ""

This works well, but enforces users to use the canonical
GitHub path (Teamwork), but with the import statement the Go compiler
enforces a different path, leading to an error.


Instead of immediately redirecting in the gosrc package, it will now
only store the canonical import path there. This is before the Go source
code is actually scanned, so we don't *actually* know what the real
canonical path should be.

In the doc.newPackage() function it will check for the canonical import
path, taking both the Go import path as well as the reported path from
the gosrc package in to account, and redirect as needed.

This seems to work for all the cases I can think of:    ->           -> ->              -> not found (expected)       -> works       -> works (should probably redirect too) -> works (#534)     -> redirects (on master this actually seems to error out?)

It should also be easy to add a similar check for for some other repo
providers, such as BitBucket, GitLab, etc.

Fixes #507

Change-Id: I49c5ccb328694f95dd7a0e5d577297d56d88893f
GitHub-Last-Rev: 28efc3756488fdd07649986f26afcbe85385bd5d
GitHub-Pull-Request: golang/gddo#560
Reviewed-by: Dmitri Shuralyov <>
3 files changed