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

ginkgo generate fails in module #524

Closed
mokiat opened this issue Sep 26, 2018 · 4 comments · Fixed by #578
Closed

ginkgo generate fails in module #524

mokiat opened this issue Sep 26, 2018 · 4 comments · Fixed by #578

Comments

@mokiat
Copy link

mokiat commented Sep 26, 2018

It seems that running ginkgo generate inside a go module, as per go 1.11, somewhere outside GOPATH results in a broken test skeleton.

Example:

package something_test

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	. "UNKNOWN_PACKAGE_PATH"
)

var _ = Describe("Something", func() {

})

The problem likely stems from the usage of ImportDir which does not work with go modules:

pkg, err := build.ImportDir(path, 0)

This has been described here:
golang/go#26504

A workaround is to use the golang.org/x/tools/go/packages package, however it is not stable yet.

@blgm
Copy link
Collaborator

blgm commented Sep 26, 2018

I’ve had that happen too. As you point out, we are in a tricky situation because people expect Gomega to work with Go modules, but Go modules (and their support packages) are not completely stable yet. I’ve heard several reports of issues with Go 1.11. I expect the stability will improve rapidly. If you (or anyone else) fancy submitting a PR that maintains backwards compatibility, then I’d gladly review it

@BrandonZacharie
Copy link

BrandonZacharie commented Sep 26, 2018

In the meantime, you can symlink your project and run ginkgo generate from there.

  • mkdir -p $GOPATH/src/github.com/_my_account_/_my_project_/
  • cd $GOPATH/src/github.com/_my_account_/
  • ln -s _path_to_my_project_
  • cd _path_to_my_project_/_path_to_package_
  • ginkgo generate _some_file_

EDIT: This suggestion is targeted for macOS users; adapt as necessary.

@mokiat
Copy link
Author

mokiat commented Sep 27, 2018

@blgm , as you mentioned, their new modules support packages (namely golang.org/x/tools/go/packages) are still not stable. According to the linked issue, the go/build package should shell out to the go executable as a quick workaround, however, that did not work for a tool of mine (whereas go/packages did).

On the plus side, the ginkgo tool does not crash and handles this in a somewhat graceful way. There is always the workaround proposed by @BrandonZacharie .

That said, I would advise against half-baked workaround solutions for now, and instead wait for go/packages to become a stable package or for alternative (stable) solutions. Still, considering the ever-increasing number of packages that are migrated to go modules, I would keep this issue open as a reminder.

@leninhasda
Copy link
Contributor

leninhasda commented May 21, 2019

Can we use this as a solution?

// ModulePath returns the module path from the gomod file text.
// If it cannot find a module path, it returns an empty string.
// It is tolerant of unrelated problems in the go.mod file.
func ModulePath(mod []byte) string {
	for len(mod) > 0 {
		line := mod
...

https://github.com/golang/go/blob/master/src/cmd/go/internal/modfile/read.go#L837

I mean, this is how go internally read the module path from go.mod file. And maybe it's a good idea to copy a function instead of pulling a package!
Or is there something else I might be missing and need to look into?

leninhasda added a commit to leninhasda/ginkgo that referenced this issue May 22, 2019
Typo fix

This changes how `generate` command looks for import path. Now it will
try to find `go.mod` file and determine import path from there first,
and will fallback to old way otherwise.

[Fixes onsi#524]
@blgm blgm closed this as completed in #578 Nov 19, 2019
blgm pushed a commit that referenced this issue Nov 19, 2019
Typo fix

This changes how `generate` command looks for import path. Now it will
try to find `go.mod` file and determine import path from there first,
and will fallback to old way otherwise.

[Fixes #524]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants