I am testing this very simple Go code on MacOS using VS Code. The project consists of these sample packages / files:
On the terminal command line, everything builds and all tests pass: go build . // (works in every folder) go test . // (tests work and pass)
However, from VS code I have 2 problems: 1. Imports from package to package do not work at all. For example:
package test
import (
service "azure.com/myproj/cmd/service" // VS complains on this line when running the test.
)
Go Tests tab:
unknown import path "azure.com/myproj/cmd/service":
cannot find module providing package
azure.com/myproj/cmd/service
Go tab:
/Users/computername/go/src/azure.com/projname/cmd/service/tests
>Finished running tool: /usr/local/bin/go test -c -o
/var/folders/q5/hm9v_6x53lj0gj02yxqtkmd40000gn/T/vscode-goKGOMES/go- code-check azure.com/myproj/cmd/service/tests
can't load package: package
azure.com/myproj/cmd/service: unknown import path
"azure.com/myproj/cmd/service": cannot find module
providing package azure.com/myproj/cmd/service
I do not understand what VS code is doing above with hm9v_6x53lj0gj02yxqtkmd40000gn and how I can change it. It looks like a cache.
So to summarize: When testing through VS Code, i do not understand why it's using the command that it is to run the tests (above), and why it can not find the imports, which the regular "go build . " and "go test . " commands have no problem with through the terminal.
Once again: From the terminal command line everything builds and all tests pass.
Seems to clearly be a VS Code related issue.
I found the answer to my problem. VS did not like the file path structure for the project. Here are the changes I made:
Moved main.go from azure.com/myproj/cmd/service/main/main.go up a level to azure.com/myproj/cmd/service/main.go
Moved the service implementation (service.go) into the internal folder: azure.com/myproj/internal/service/service.go
Moved the tests into the internal folder: azure.com/myproj/internal/service/test/service_test.go
How VS code is able to run the tests without complaining of the imports not being found.
So now I only have the main.go in my /cmd/service folder. Everything lives in Internal, including tests. I suspect that burying main.go into a subfolder is what was confusing VS Code.