<p dir="auto">Go version 1.5</p>
<h3 dir="auto">Summary</h3>
<p dir="auto">We've run into some behavior we didn't expect from go/types importing packages. If a package <code class="notranslate">p</code> has been imported (by some other package) and its source code is present but not installed (that is, there is no package archive such as <code class="notranslate">$GOROOT/pkg/darwin_amd64/p.a</code>) then Check returns an error saying it can't find the package.</p>
<p dir="auto">We're not sure if this is intended behavior, but we found it surprising.</p>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/tessr/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/tessr">@tessr</a></p>
<p dir="auto">We've tried to produce a minimal test case:<br>
<a href="https://github.com/kr/gotypesrepro">https://github.com/kr/gotypesrepro</a></p>
<h3 dir="auto">Steps to reproduce</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="1. git clone https://github.com/kr/gotypesrepro $GOPATH/src/p
2. cd $GOPATH/src/p
3. go run try.go # fails
4. go install p
5. go run try.go # now it works"><pre class="notranslate"><code class="notranslate">1. git clone https://github.com/kr/gotypesrepro $GOPATH/src/p
2. cd $GOPATH/src/p
3. go run try.go # fails
4. go install p
5. go run try.go # now it works
</code></pre></div>
<h3 dir="auto">Expected output</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="package testdata (&quot;testdata&quot;)
package testdata (&quot;testdata&quot;)"><pre class="notranslate"><code class="notranslate">package testdata ("testdata")
package testdata ("testdata")
</code></pre></div>
<h3 dir="auto">Actual output</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="testdata/import.go:3:8: could not import p (can't find import: p)
package testdata (&quot;testdata&quot;)"><pre class="notranslate"><code class="notranslate">testdata/import.go:3:8: could not import p (can't find import: p)
package testdata ("testdata")
</code></pre></div>
<p dir="auto">It is common to use go/types is as part of a go generate command<br>
or other command line tool to introspect a Go package.</p>
<p dir="auto">The go tool goes to some lengths to ensure that whenever<br>
you operate on a package, it builds the source for that<br>
package into object form before using it, so you don't<br>
end up using symbol information that's out of date with respect<br>
to the source.</p>
<p dir="auto">When using go/types from a command line tool, you<br>
don't have that luxury, and it's not necessarily appropriate<br>
to call "go install" automatically.</p>
<p dir="auto">I believe that a better way would be for the default<br>
importer to use the object file data only if it's newer<br>
than the source files, otherwise fall back to using the<br>
source directly. Or at least it would be nice to have<br>
that option, so that it's easy to make go generate<br>
utilities operate on the up to date code.</p>
<p dir="auto">I hope this could be considered before Go 1.5 as<br>
we won't be able to change this behaviour after then.</p>