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

importPathToDir used by repos using this doesn't work in module mode #84

Open
segevfiner opened this issue Jul 22, 2020 · 2 comments
Open

Comments

@segevfiner
Copy link

Trying the following example project that uses Go modules vfsgen-test2.zip, I get the following error:

2020/07/23 00:40:54 cannot find module providing package github.com/segevfiner/vfsgen-test2/assets/assets: module github.com/segevfiner/vfsgen-test2/assets/assets: git ls-remote -q origin in /Users/segev/go/pkg/mod/cache/vcs/b1dce0b5aa30a69878ddaa81a87781bd321f95d17cec9ee5bf25190b026b94ea: exit status 128:
        remote: Repository not found.
        fatal: repository 'https://github.com/segevfiner/vfsgen-test2/' not found
exit status 1

Whether I try go run or go generate, caused by the importPathToDir trick that tries to avoid working directory problems. I resorted to just placing the go:generate comment at the main package and using Filename to get vfsgen to write its output under the assets package instead of at the main package.

From #83

@segevfiner
Copy link
Author

I wonder if debug.ReadBuildInfo can handle this reliably in module mode.

@dmitshur
Copy link
Member

@segevfiner In module mode, build.Import can only be used to find Go packages, meaning the directory needs to contain at least one .go file. In your snippet:

var Assets http.FileSystem = http.Dir(importPathToDir("github.com/segevfiner/vfsgen-test2/assets/assets"))

That corresponds to a directory with hello.txt and nothing else. You'll want to change it to something like:

// Assets contains project assets.
var Assets http.FileSystem = http.Dir(filepath.Join(importPathToDir("github.com/segevfiner/vfsgen-test2/assets"), "assets"))

That seems to fix things.

You may also want to provide an absolute directory rather than empty string as the srcDir parameter to build.Import:

+wd, err := os.Getwd()
+if err != nil {
+	log.Fatalln(err)
+}
-p, err := build.Import(importPath, "", build.FindOnly)
+p, err := build.Import(importPath, wd, build.FindOnly)

Overall, I suspect it's not worth using build.Import in module mode anymore. It may be better to find the directory corresponding to the root of the module, and go from there. See golang/dl@1b9ab27 for an example of that.

I don't see mentions of importPathToDir in this repository, so I don't think there's anything more to do here.

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

No branches or pull requests

2 participants