Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: main_test must depend on main.a, not main binary #3417

Closed
rogpeppe opened this issue Mar 28, 2012 · 23 comments
Closed

cmd/go: main_test must depend on main.a, not main binary #3417

rogpeppe opened this issue Mar 28, 2012 · 23 comments
Milestone

Comments

@rogpeppe
Copy link
Contributor

I don't know how to reproduce this from scratch, but here are the symptoms I'm seeing
currently. I've seen the issue a couple of times now.

The problem goes away, as you can see, when I touch all the go files. It doesn't make a
difference if I import as the full path name or ".".

% go clean -r -i
% go test -i
% go test
# launchpad.net/juju/go/cmd/juju_test
./cmd_test.go:7: can't find import: "launchpad.net/juju/go/cmd/juju"
FAIL    launchpad.net/juju/go/cmd/juju [build failed]
% echo $status
2
% go list .../cmd/juju
launchpad.net/juju/go/cmd/juju
% # change source to use package that's genuinely not there.
% go test
# launchpad.net/juju/go/cmd/juju
cmd_test.go:7:2: import "launchpad.net/juju/go/cmd/jujux": cannot find package
% echo $status
1
% touch *.go
% go test
# launchpad.net/juju/go/cmd/juju_test
./cmd_test.go:76: undefined: allowsExtraArgs
FAIL    launchpad.net/juju/go/cmd/juju [build failed]
% echo $status
2
%
@rsc
Copy link
Contributor

rsc commented Apr 2, 2012

Comment 1:

If you can still make this happen, please try -v -x to see if you get any useful extra
output.
If all the code is inside one $GOPATH tree then I don't understand what's going on.
Also interesting might be plain 'go list -e -json' (no import path args).

Status changed to WaitingForReply.

@dsymonds
Copy link
Contributor

dsymonds commented Apr 4, 2012

Comment 2:

Labels changed: added priority-later, removed priority-triage.

@rogpeppe
Copy link
Contributor Author

Comment 3:

I just encountered this problem again. Here's the output of go test -v -x:
% go test -v -x
WORK=/tmp/go-build386097667
mkdir -p $WORK/launchpad.net/juju/go/cmd/juju/_test/launchpad.net/juju/go/cmd/
mkdir -p $WORK/launchpad.net/juju/go/cmd/juju/_test/
cd /home/rog/src/go/src/launchpad.net/juju/go/cmd/juju
/home/rog/go/pkg/tool/linux_amd64/6g -o
$WORK/launchpad.net/juju/go/cmd/juju/_test/_go_.6 -p launchpad.net/juju/go/cmd/juju_test
-D _/home/rog/src/go/src/launchpad.net/juju/go/cmd/juju -I $WORK -I
/home/rog/src/go/pkg/linux_amd64 ./cmd_test.go ./main_test.go
# launchpad.net/juju/go/cmd/juju_test
./cmd_test.go:7: can't find import: "launchpad.net/juju/go/cmd/juju"
$WORK/launchpad.net/juju/go/cmd/juju/_test/juju.test -test.v=true
FAIL    launchpad.net/juju/go/cmd/juju [build failed]
% 
i've archived my $GOPATH directory in the hope that i might be able
to reproduce the problem later.

@rsc
Copy link
Contributor

rsc commented May 2, 2012

Comment 4:

The presence of the -D_... option suggests that the go command
believes you are outside the go root and any GOPATH directories.
Instead of go test -v -x what does 'go list -json' say?  Specifically,
what does the Root field say?  If it is "", that confirms my theory.
And yet the output line beginning with # makes clear that at least
part of the go command believes that you are in a directory with a
root.
Is it possible that there is a stale juju directory tree earlier in
your GOPATH or in the GOROOT itself?  In that situation the go command
does try to avoid believing that the current directory might be the
canonical juju package (since it is shadowed by that other one).

@rogpeppe
Copy link
Contributor Author

rogpeppe commented May 2, 2012

Comment 5:

unfortunately, and possibly significantly, unarchiving $GOPATH (inc. restoring mtimes)
did not allow me to reproduce the issue. i should probably have archived $GOROOT too.
i'll have to wait until i see the issue again. let's hope it's magically gone away
forever.
it's highly unlikely that the was a stale juju directory tree earlier in GOPATH or
GOROOT. (i use a single directory in GOPATH, and there's nothing in GOROOT now and i
haven't removed anything since then). if there *was*, then it seems to me that the issue
would probably not be resolved by touching the files in the package directory, which it
was; another indication that that's probably not the case.

@rogpeppe
Copy link
Contributor Author

Comment 6:

the issue just came up again. same directory, exactly the same symptoms as before.
theory not validated, i'm afraid - Root is not empty.
here's the output of go list -json.
{
    "Dir": "/home/rog/src/go/src/launchpad.net/juju/go/cmd/juju",
    "ImportPath": "launchpad.net/juju/go/cmd/juju",
    "Name": "main",
    "Target": "/home/rog/src/go/bin/juju",
    "Root": "/home/rog/src/go",
    "GoFiles": [
        "bootstrap.go",
        "destroy.go",
        "main.go"
    ],
    "Imports": [
        "launchpad.net/gnuflag",
        "launchpad.net/juju/go/cmd",
        "launchpad.net/juju/go/environs/ec2",
        "launchpad.net/juju/go/juju",
        "os"
    ],
    "Deps": [
        "archive/tar",
        "archive/zip",
        "bufio",
        "bytes",
        "compress/flate",
        "compress/gzip",
        "crypto",
        "crypto/aes",
        "crypto/cipher",
        "crypto/des",
        "crypto/dsa",
        "crypto/elliptic",
        "crypto/hmac",
        "crypto/md5",
        "crypto/rand",
        "crypto/rc4",
        "crypto/rsa",
        "crypto/sha1",
        "crypto/sha256",
        "crypto/subtle",
        "crypto/tls",
        "crypto/x509",
        "crypto/x509/pkix",
        "encoding/asn1",
        "encoding/base64",
        "encoding/binary",
        "encoding/hex",
        "encoding/pem",
        "encoding/xml",
        "errors",
        "fmt",
        "hash",
        "hash/crc32",
        "io",
        "io/ioutil",
        "launchpad.net/gnuflag",
        "launchpad.net/goamz/aws",
        "launchpad.net/goamz/ec2",
        "launchpad.net/goamz/s3",
        "launchpad.net/goyaml",
        "launchpad.net/gozk/zookeeper",
        "launchpad.net/juju/go/charm",
        "launchpad.net/juju/go/cloudinit",
        "launchpad.net/juju/go/cmd",
        "launchpad.net/juju/go/environs",
        "launchpad.net/juju/go/environs/ec2",
        "launchpad.net/juju/go/juju",
        "launchpad.net/juju/go/log",
        "launchpad.net/juju/go/schema",
        "launchpad.net/juju/go/state",
        "launchpad.net/juju/go/state/presence",
        "launchpad.net/juju/go/state/watcher",
        "launchpad.net/juju/go/version",
        "launchpad.net/mgo/bson",
        "launchpad.net/tomb",
        "log",
        "math",
        "math/big",
        "math/rand",
        "mime",
        "mime/multipart",
        "net",
        "net/http",
        "net/http/httputil",
        "net/textproto",
        "net/url",
        "os",
        "os/exec",
        "path",
        "path/filepath",
        "reflect",
        "regexp",
        "regexp/syntax",
        "runtime",
        "runtime/cgo",
        "runtime/debug",
        "sort",
        "strconv",
        "strings",
        "sync",
        "sync/atomic",
        "syscall",
        "time",
        "unicode",
        "unicode/utf8",
        "unsafe"
    ],
    "XTestGoFiles": [
        "cmd_test.go",
        "main_test.go"
    ],
    "XTestImports": [
        "flag",
        "fmt",
        "io/ioutil",
        "launchpad.net/gnuflag",
        "launchpad.net/gocheck",
        "launchpad.net/juju/go/cmd",
        "launchpad.net/juju/go/cmd/juju",
        "launchpad.net/juju/go/environs",
        "launchpad.net/juju/go/environs/dummy",
        "launchpad.net/juju/go/version",
        "os",
        "os/exec",
        "path/filepath",
        "reflect",
        "sort",
        "strings",
        "testing"
    ]
}
other info:
% whatis GOPATH
GOPATH=/home/rog/src/go
% whatis GOROOT
GOROOT=/home/rog/go
% whatis GOBIN
GOBIN: not found
%

@rogpeppe
Copy link
Contributor Author

Comment 7:

for the record, i did rm -r $GOPATH/pkg/* and it started working again.
next time, i'll ls -l the contents first.

@davecheney
Copy link
Contributor

Comment 8:

Roger, this happens to me if I do a hg update and ./all.bash. I think the underlying
problem is a version mismatch between the $GOROOT/pkg pkgs and $GOPATH/pkg packages
which are not being rebuilt because the source has not changed.

@davecheney
Copy link
Contributor

Comment 9:

I should say, this happens only when testing cmds, not packages. So it might be a hole
in the logic in gotest for main packages.

@davecheney
Copy link
Contributor

Comment 10:

I think this may be the way we're testing our main packages. Our package main tests
declare themselves package main_test. Looking at the standard library, this practice
appears to be unique. 
Adding a shim like this
lucky(~/src/launchpad.net/juju/go/cmd/jujud) % more export_test.go 
package main
// this file exists only to trick go test into creating a .a for this package.
// works around https://golang.org/issue/3417
works around the issue for me.

@davecheney
Copy link
Contributor

Comment 11:

I think this issue should be closed. I think that we (juju) were using go test
incorrectly. Thoughts ?

@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 12:

Labels changed: added go1.1.

@rsc
Copy link
Contributor

rsc commented Dec 10, 2012

Comment 13:

Labels changed: added size-m.

@rsc
Copy link
Contributor

rsc commented Jan 31, 2013

Comment 14:

Can anyone explain how to reproduce this? If not, let's close it.

@davecheney
Copy link
Contributor

Comment 15:

I believe this was caused by using package main_test to test commands. Since juju moved
away from this practice, we have not seen the issue reoccur. 
I think this issue can be closed. For extra credit stopping go test from accepting
main_test as a package name would probably prevent it reoccurring.

@rsc
Copy link
Contributor

rsc commented Jan 31, 2013

Comment 16:

Closing the issue. I wrote a trivial main_test and it worked.

Status changed to Retracted.

@rogpeppe
Copy link
Contributor Author

Comment 17:

yes, main_test seems to work... most of the time.
we avoided the issue by avoiding use of main_test, but i
think it's quite likely there's still a bug there.
*should* main_test work correctly?

@rsc
Copy link
Contributor

rsc commented Jan 31, 2013

Comment 18:

I'll try again. I do want to fix the bug if there's a bug.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Mar 12, 2013

Comment 19:

Labels changed: added go1.1maybe, removed go1.1.

@masiulaniec
Copy link

Comment 20:

See also issue #5198.

@robpike
Copy link
Contributor

robpike commented May 18, 2013

Comment 21:

Labels changed: added go1.2maybe, removed go1.1maybe.

@rsc
Copy link
Contributor

rsc commented Sep 10, 2013

Comment 22:

The problem is that cmd/go does not distinguish the main.a from the generated binary.
If the binary is up to date, it does not rebuild main.a, so main_test cannot import it.
Here is a simple test case.
g% cat foo.go
package main
func F() {}
func main() {}
g% cat foo_test.go
package main_test
import "testing"
import . "foo"
func Test1(t *testing.T) {
    F()
}
g% touch foo.go
g% go test
PASS
ok      foo 0.016s
g% go install
g% go test
# foo_test
./foo_test.go:4: can't find import: "foo"
FAIL    foo [build failed]
g%

@rsc
Copy link
Contributor

rsc commented Sep 10, 2013

Comment 23:

This issue was closed by revision 627d17c.

Status changed to Fixed.

@rsc rsc added this to the Go1.2 milestone Apr 14, 2015
@rsc rsc removed the go1.2maybe label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants