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
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@mvdan
Copy link
Member

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
Copy link

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
Copy link

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
@griesemer griesemer added this to the Go1.11 milestone Jan 11, 2018
@griesemer griesemer added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 11, 2018
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
Copy link
Contributor

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
Copy link
Contributor

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 The path to resolution is known, but the work has not been done. label Nov 15, 2018
@gopherbot gopherbot removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. 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
Copy link
Member Author

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
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

6 participants