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: slightly confusing import cycle error when a package imports itself #23295

Open
mvdan opened this issue Jan 1, 2018 · 5 comments
Open

cmd/go: slightly confusing import cycle error when a package imports itself #23295

mvdan opened this issue Jan 1, 2018 · 5 comments
Labels
Milestone

Comments

@mvdan
Copy link
Member

@mvdan mvdan commented Jan 1, 2018

What version of Go are you using (go version)?

go version devel +f3f507b2d8 Fri Dec 22 21:13:38 2017 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

Create the package p in my GOPATH, with a single file, then run go install:

package p

import "p"

What did you expect to see?

Some sort of error, like "a package cannot import itself".

What did you see instead?

$ go install
can't load package: import cycle not allowed
package p
        imports p
import cycle not allowed
package p
        imports p

This is a minimal example - I found this while merging package B into A, when B was importing A. This resulted in a couple of files in package A still importing package A.

I understand that the error is technically correct, but it's a bit confusing and it threw me off for a couple of seconds. The error message suggested above would have pointed me in the right direction faster.

It's also worth noting how the "import cycle not allowed" message is printed twice for some reason.

HaraldNordgren added a commit to HaraldNordgren/go that referenced this issue Jan 7, 2018
This attempt to sort out confusion about cyclical imports. In addition to the
error message 'import cycle not allowed', we will now also give a specialized
error for direct self-import, namely:

    self import not allowed

Fixes golang#23295

Change-Id: I14e2418aa4eceb8caf2e6520c686fac7e85cb0dd
@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Jan 7, 2018

Change https://golang.org/cl/86536 mentions this issue: load: special error message for direct self-import

HaraldNordgren added a commit to HaraldNordgren/go that referenced this issue Jan 7, 2018
This attempt to sort out confusion about cyclical imports. In addition
to the error message 'import cycle not allowed', we will now also give
a specialized error for direct self-import, namely:

    self import not allowed

Fixes golang#23295

Change-Id: I14e2418aa4eceb8caf2e6520c686fac7e85cb0dd
HaraldNordgren added a commit to HaraldNordgren/go that referenced this issue Jan 7, 2018
This change prevents import errors from being printed multiple times.
Creating a bare-bones package `p` with only one file importing itself
and running `go install p`, the current implementation gives this error
message:

```
can't load package: import cycle not allowed
package p
        imports p
import cycle not allowed
package p
        imports p
```

With this change we will show the message only once.

Partially fixes golang#23295

Change-Id: I653b34c1c06c279f3df514f12ec0b89745a7e64a
@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Jan 7, 2018

Change https://golang.org/cl/86535 mentions this issue: cmd/go: print each import error only once

HaraldNordgren added a commit to HaraldNordgren/go that referenced this issue Jan 7, 2018
This attempts to sort out confusion about cyclical imports. In addition
to the error message "import cycle not allowed", for direct self
imports we will now instead give a specialized error message, namely:

    self import not allowed

Fixes golang#23295

Change-Id: I14e2418aa4eceb8caf2e6520c686fac7e85cb0dd
HaraldNordgren added a commit to HaraldNordgren/go that referenced this issue Jan 7, 2018
This attempts to sort out confusion about cyclical imports. In addition
to the error message "import cycle not allowed", for direct self
imports we will now instead give a specialized error message, namely:

    self import not allowed

Fixes golang#23295

Change-Id: I14e2418aa4eceb8caf2e6520c686fac7e85cb0dd
HaraldNordgren added a commit to HaraldNordgren/go that referenced this issue Jan 7, 2018
This change prevents import errors from being printed multiple times.
Creating a bare-bones package `p` with only one file importing itself
and running `go install p`, the current implementation gives this error
message:

	can't load package: import cycle not allowed
	package p
		imports p
	import cycle not allowed
	package p
		imports p

With this change we will show the message only once.

Partially fixes golang#23295

Change-Id: I653b34c1c06c279f3df514f12ec0b89745a7e64a
HaraldNordgren added a commit to HaraldNordgren/go that referenced this issue Jan 7, 2018
This change prevents import errors from being printed multiple times.
Creating a bare-bones package `p` with only one file importing itself
and running `go install p`, the current implementation gives this error
message:

	can't load package: import cycle not allowed
	package p
		imports p
	import cycle not allowed
	package p
		imports p

With this change we will show the message only once.

Updates golang#23295

Change-Id: I653b34c1c06c279f3df514f12ec0b89745a7e64a
HaraldNordgren added a commit to HaraldNordgren/go that referenced this issue Jan 7, 2018
This attempts to sort out confusion about cyclical imports. In addition
to the error message "import cycle not allowed", for direct self
imports we will now instead give a specialized error message, namely:

    self import not allowed

Fixes golang#23295

Change-Id: I14e2418aa4eceb8caf2e6520c686fac7e85cb0dd
gopherbot pushed a commit that referenced this issue Mar 27, 2018
This change prevents import errors from being printed multiple times.
Creating a bare-bones package 'p' with only one file importing itself
and running 'go build p', the current implementation gives this error
message:

	can't load package: import cycle not allowed
	package p
		imports p
	import cycle not allowed
	package p
		imports p

With this change we will show the message only once.

Updates #23295

Change-Id: I653b34c1c06c279f3df514f12ec0b89745a7e64a
Reviewed-on: https://go-review.googlesource.com/86535
Reviewed-by: Harald Nordgren <haraldnordgren@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@griesemer

This comment has been minimized.

Copy link
Contributor

@griesemer griesemer commented Jun 5, 2018

This error is reported by go build/install, not the compiler. Reassigning.

@griesemer griesemer changed the title cmd/compile: slightly confusing import cycle error when a package imports itself cmd/go: slightly confusing import cycle error when a package imports itself Jun 5, 2018
@griesemer griesemer assigned rsc and unassigned mdempsky and griesemer Jun 5, 2018
@griesemer griesemer modified the milestones: Go1.11, Go1.12 Jun 5, 2018
@bcmills

This comment has been minimized.

Copy link
Member

@bcmills bcmills commented Nov 15, 2018

The doubled error message is fixed, but the remaining error is still awkward.

gopath$ mkdir p
gopath$ cd p
gopath/p$ cat >> p.go
package p
import "p"

gopath/p$ GO111MODULE=off go install
p.go:2:8: cannot find package "p" in any of:
        /home/bcmills/go/src/p (from $GOROOT)
        /tmp/tmp.aMjUpuxIN7/gopath/src/p (from $GOPATH)
@bcmills bcmills added the NeedsFix label Nov 15, 2018
@bcmills bcmills modified the milestones: Go1.12, Go1.13 Nov 15, 2018
@bcmills bcmills unassigned rsc May 7, 2019
@bcmills bcmills modified the milestones: Go1.13, Unplanned May 7, 2019
@mvdan

This comment has been minimized.

Copy link
Member Author

@mvdan mvdan commented Sep 11, 2019

Do we care about GOPATH mode that much at this point? Module mode behaves better:

#!/bin/bash

docker run -i golang:1.13 <<-SCRIPT

        mkdir foo
        cd foo
        go mod init test.tld/foo
        go env GOMOD

        cat >foo.go <<-EOF
                package foo

                import "test.tld/foo"
        EOF

        go build

SCRIPT
$ ./repro.sh
go: creating new go.mod: module test.tld/foo
/go/foo/go.mod
can't load package: import cycle not allowed
package test.tld/foo
        imports test.tld/foo

The double error is gone, but I still think the message could be more helpful to humans. For example, pointing at how a package can never import itself. But if we decide that that's unnecessary, I think the issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.