'Cannot import The current file is not included in the workspace module' in VS Code

王林
Release: 2024-02-10 18:42:17
forward
1200 people have browsed it

VS Code 中“无法导入 <package> The current file is not included in the workspace module

php editor Xigua may encounter an error message when using VS Code: "Cannot import the current file Not included in the workspace module". This error usually occurs when we try to import a package or module but the current file is not added to the workspace. The solution to this problem is actually very simple, just add the current file to the workspace. In this article, we will detail how to resolve this issue so that you can smoothly import the required packages and modules.

Question content

I cannot import the URL package. Even if I install the packages on my system using "go get github..." and make sure they are present in the go.mod file, I see:

could not import <package> current file is not included in a workspace module
Copy after login

I remember just a few weeks ago I didn't even need to run "go work init" in every Golang project directory on my system. But now I'm asked to add these to the workspace. At that point, when I install any package from the terminal, I just import the installed package in my code and the code runs!

I am currently trying to create a project using understand "ebitengine", to do this I ran the following command

go mod init PROJECT (in the directory same as the code)
go work init
go get -u github.com/hajimehoshi/ebiten/v2
go mod init PROJECT (Again because the error was still being shown)
go work init (Again because the error was still being shown)
Copy after login

Please help me understand what's wrong with my system and why I can't import packages correctly.

The output of running "go env" is:

GO111MODULE="auto"
GOARCH="amd64"
GOBIN="/home/user/go/bin"
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/user/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/user/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/user/Desktop/learnings/go/14SwitchCase/go.mod"
GOWORK="/home/user/Desktop/learnings/go/go.work"
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1152057857=/tmp/go-build -gno-record-gcc-switches"
Copy after login

Similarly, my bashrc contains:

export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=${PATH}:$GOBIN
Copy after login

Please help me understand where I am going wrong and why my VS Code no longer works properly.

Solution

The problem is the wrong GOPATH and GOROOT in my ~/.profile and ~/.bashrc files.

I replaced the existing GOPATH and GOROOT with the following lines and the problem was solved:

The above is the detailed content of 'Cannot import The current file is not included in the workspace module' in VS Code. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!