-
Notifications
You must be signed in to change notification settings - Fork 18k
go test
breaks when working directory is outside of go.mod
hierarchy even though argument path is inside module directory
#47363
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
Comments
Sorry, this is working as intended. All module-aware commands run in the context of the module in the current directory or a parent directory, defined in
|
Apparently I did not clearly communicate the problem here? I am using modules. The problem is that the |
go test
breaks when working directory is outside of go.mod
hierarchygo test
breaks when working directory is outside of go.mod
hierarchy even though argument path is inside module directory
How do I reopen the issue? |
The module context is only based on the current directory though, not the arguments. One thing that might be tripping you up is the argument on the command line |
Taking "the current module" from "the current working directory" is fundamentally broken. At a minimum, there needs to be a way to specify what I think the current module is, even when the current directory is not it. How do I make the file names printed by the compiler or test runner, be recognized by my editor, when the working directory is not "the current module" ? I may, in fact, be working in some monorepo with multiple parallel modules? |
That's rather extreme.
To be clear, you can build and test packages anywhere inside the current module or any module it requires (directly or indirectly). Modules may be replaced with local directories, and you can build and test packages in them using relative and absolute paths on the command line. It's not really meaningful for a package to be outside the current module or anything it depends on though.
Not sure what you're trying to do exactly. The compiler emits errors with absolute paths or paths relative to the current directory; those errors have very little do with modules. The go command emits errors about imports and modules. It may use file paths and line numbers when appropriate, or it may list import paths if that seems more useful. In your original comment, you asked specifically about If you're building editor tooling though, you may want to look at golang.org/x/tools/go/packages or gopls. But this is not the place for broader questions about those.
For now, if you're developing multiple modules at the same time, you'll probably need to add a |
My problem is one of "I'm a user of go, typescript, C++, protobuf, and a bunch of other languages, that all live in a single monorepo, and I want editors to be able to build and test code, and open the correct file when it fails." One of the subdirectories of this repo contains go code. The root of this monorepo is not a go module. I may not even be focused on a go module when I want to build integration tests. The working directory of the editor (be it vim, emacs, vscode, or something else) is the root of the monorepo, which is not inside the go module. Build and test commands invoked by the editor end up being run from the current working directory of the editor, which is the root of the monorepo ;they provide the relative path of the file in question from the editor working directory as argument. This works fine for all the other languages we use. Currently, I can't find a good way of gluing this presumably not uncommon setup into the go build system, such that the error parser of each kind of editor (vim, emacs, vscode, and more) will find and open the correct file for a test failure (or build failure.) What every other language and build system lets us do is:
With GO111MODULE turned off, that invocation now errors out, complaining that the CWD is not within a module, even though the interesting directory in question is within a module. For purposes of this comment, let's consider an overall project that looks like: /home/me/mything: cpp/ docs/ go/ src/ whatever/ go.mod vendor/ package/ some_test.go haskell/ typescript/ react/ If I were to change the build command for the go package to do something like Best would be "if a directory is the argument to the command, then determine the module based on that directory, not based on the CWD" which would make the current setup Just Work. Second best would be "allow the module-to-assume to be specified using a separate command line option" which means we could specify the go build/test command as Third best would be a command line option that forces the tooling to always output full path filenames. This will let the editor find the right file no matter what the working directory is. We'd invoke it like |
By the way -- given that the original issue was closed (even though the problem is unsolved and at this point hopefully clearly defined,) should I open a new issue? |
Honestly, the refined version of this ticket is essentially a new ticket, so I went ahead and opened #47399 with a mention of this ticket for historical record. |
Thanks for writing this up, both this comment and #47399. I have a clearer picture of what you're experiencing now. Agree that the new issue is the right call. I'll reply over there. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
It used to work fine to run tests inside a directory of a go module, when the working directory was outside of it.
Consider:
GOPATH
is/home/me/project/go
The package name is
myproject/somepack/otherpack
.Working directory for my editor is "/home/me/project".
I used to be able to run
The important part here is that
go test
would print log messages and failures with a path relative to the current working directory, which allows my editor to pick them up and jump to that file/line.This still works with
GO111MODULE=off
but that is going away in 1.17, so I'm testing without it in 1.16.6What did you expect to see?
I expect some mechanism whereby I can run tests within a go module, with the current working directory being outside that module, and have file paths printed relative to the current working directory.
What did you see instead?
I get errors about working directory not being part of the go module.
package src/myproject/somepack is not in GOROOT (/usr/local/src/src/myproject/somepack)
The text was updated successfully, but these errors were encountered: