go/analysis/unitchecker: use importer.ForCompiler under go1.12

importer.For does not populate the caller's token.FileSet
leading to spurious position information in diagnostics.

This change is the upstream fix for
https://go-review.googlesource.com/c/go/+/152258.

Fixes golang/go#28995

Change-Id: I9307d4f1f25c2b0877558426d4d71b3f1df99505
Reviewed-on: https://go-review.googlesource.com/c/152578
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/go/analysis/unitchecker/unitchecker.go b/go/analysis/unitchecker/unitchecker.go
index ec3103b..018191a 100644
--- a/go/analysis/unitchecker/unitchecker.go
+++ b/go/analysis/unitchecker/unitchecker.go
@@ -181,6 +181,11 @@
 	return cfg, nil
 }
 
+var importerForCompiler = func(_ *token.FileSet, compiler string, lookup importer.Lookup) types.Importer {
+	// broken legacy implementation (github.com/golang/go/issues/28995)
+	return importer.For(compiler, lookup)
+}
+
 func run(fset *token.FileSet, cfg *Config, analyzers []*analysis.Analyzer) ([]result, error) {
 	// Load, parse, typecheck.
 	var files []*ast.File
@@ -196,7 +201,7 @@
 		}
 		files = append(files, f)
 	}
-	compilerImporter := importer.For(cfg.Compiler, func(path string) (io.ReadCloser, error) {
+	compilerImporter := importerForCompiler(fset, cfg.Compiler, func(path string) (io.ReadCloser, error) {
 		// path is a resolved package path, not an import path.
 		file, ok := cfg.PackageFile[path]
 		if !ok {
diff --git a/go/analysis/unitchecker/unitchecker112.go b/go/analysis/unitchecker/unitchecker112.go
new file mode 100644
index 0000000..683b7e9
--- /dev/null
+++ b/go/analysis/unitchecker/unitchecker112.go
@@ -0,0 +1,9 @@
+// +build go1.12
+
+package unitchecker
+
+import "go/importer"
+
+func init() {
+	importerForCompiler = importer.ForCompiler
+}