I have my main.go in a subfolder cmd/admin/main.go but when I'm debugging and there are errors in a file, it gives me the relative path from the main.go folder instead of the workspace folder. So for example I will have the error ..\..\path\to\file.go:238:3: undefined: test which won't work if I try to Ctrl+click it.
If I launch the command from the root go run cmd/admin/main.go that works as intended returning path\to\file.go:238:3: undefined: test.
My launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/cmd/admin",
"env": {},
"args": []
}
]
}
Go version 1.16.6
VSC version 1.58.2
OS Windows10
Go File > Add Folder to Workspace
Then select the folders containing main.go
You can also do it in the command line:
code cmd/admin -a
Now make sure your current launch.json has been deleted to start fresh, your workspace should look like this:
Notice that there is a 'package main' and 'func main()' this is required for Go to know the entry point.
Now press Run and Debug with a breakpoint:
That's it, it should now work on any folders you add to your workspace. If you want more specific debug options, add them to your workspace and they'll apply in the context of the file you run from. Click the 'create a launch.json file.:
Select workspace:
Select Go: Launch package
You now have a launch config that will apply to the directory you run it from:
Make sure to save your workspace to keep it:
Notes
Be sure to delete your current launch.json files if they already exist anywhere.
Make sure all of your source code is located in GOPATH/src, you can find out where GOPATH is by putting this into a command line:
go env GOPATH
The file doesn't need to be named main.go, but you MUST have a package named main, and a func named main for Go to know the entry point, for each executable, for example influxdb's two cli's: https://github.com/influxdata/influxdb/tree/master/cmd
More info
VS Code workspace debugging:
https://code.visualstudio.com/docs/editor/multi-root-workspaces#_debugging
GOPATH: https://golang.org/doc/gopath_code
Try and make sure:
you have no GOxxx environment variable set (no GO_BASE_PATH, no GOROOT), except for GOPATH set to %USERPROFILE%\go, and %GOPATH%\bin in your %PATH%
you are using Go installed in the default C:\Program Files\Go\ folder
you have set up your project using Go modules, with a go mod init myproject
you have defined a multi-root workspace for your project root folder, compose of only one root: your project. Save that workspace (it will create a <name>.code-workspace JSON file).
See then if the issue persists (and no cwd should be needed in your launch.json)
Related
I was working in one VS Code window which had the mapping package in OtherProject. Given that I didn't understand what was going on there, I decided to create another directory in another window and create some toy/test code to experiment with. The relative file structure is.
- GoPractice
- osTest.go
- assignments.go
- OtherProject
- src
- main
- mapping.go
To continue with probing and trying to understand in OtherProject, I tried to use dlv debug. I couldn't build because I kept getting an error so I decided to start small with files in my GoPractice directory and searched for tutorials and landed on Digital Ocean's. I created a launch.json file but when I launched the debugger, I got this error that refers to mapping that isn't related to the directory I have.
found packages main (assignments.go) and osTest (osTest.go) in C:\Users\User\Documents\GoPractice
strconv.go:6:2: cannot find package "mapping" in any of:
C:\Program Files\Go\src\mapping (from $GOROOT)
C:\Users\User\go\src\mapping (from $GOPATH) (exit status 1)
Running go env in the window of:
OtherProject I get:
set GOPATH=C:/Users/User/OtherProject
...
set GOROOT=C:\Program Files\Go
I don't understand why the file paths for GOPATH and GOROOT alternate between UNIX and Windows paths from the same command, go env.
GoPractice I get:
set GOPATH=C:\Users\User\go
...
set GOROOT=C:\Program Files\Go
So mapping isn't coming from GOPATH even though I modified it in the original window. And here lies my confusion, it seems that there is a link to the original window's bash session but I can't tell what it is as my initial hunch of the environment variable is wrong based on go env output.
Here's my launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "DebugServer",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}",
"dlvFlags": ["--check-go-version=false"]
}
]
}
I am running on Go 1.19.1 and I needed to suppress the warning for dlv
I want to use VSCode with WSL for development.
I have problems debugging my Go applications and I don't know why. I dont see the things in the call stack, no variables and also the buttons to jumping from breakpoint to breakpoint are greyed out. My DEBUG Console log is clean, no erros (see details below) I try to follow these articles to set up my environment:
https://github.com/Microsoft/vscode-go/wiki/GOPATH-in-the-VS-Code-Go-extension
https://github.com/Microsoft/vscode-go/wiki/Debugging-Go-code-using-VS-Code
So what I do in detail:
First I tell VSCode to set my GOPATH dynamacly from the current Workspace and also seperate the GooTools Installation from my GOPATH via go.toolsGopath. My settings.json looks like this:
{
"go.inferGopath": true,
"go.toolsGopath": "/mnt/c/Users/cloudnaut/gospace/gotools",
}
So my GoTools are installed to /mnt/c/Users/cloudnaut/gospace/gotools. I have installed also the dlv debugger. Also My working directory for go is instead /mnt/c/Users/cloudnaut/gospace/go
It haves the common go project structure:
.
├── bin
├── pkg
└── src
└── github.com
└── cloudnaut
└── project1
└── main.go
Everything seems fine. Go:Install/Update Tools are installed in my separate path. And my GOPATH structure also works. I use GO:Current GOPATH it shows my the correct GOPATH i want and go install also creates the go binary from my main.go in /bin. Perfect ...
Now I want to start to debbuging. I just use a simple launch.json file directly point to my main.go
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"showLog": true,
"type": "go",
"request": "launch",
"mode": "auto",
"program": "/mnt/c/Users/cloudnaut/gospace/go/src/github.com/cloudnaut/project1/main.go",
"env": {},
"args": []
}
]
}
So and when I start now the debugging (breakpoints are set and shown under BREAKPOINTS) he creates in the folder a __debug_bin. But in my vscode debuger interface, I see no variables and stacktrace. The Problem is, I see also now errors or something. My Debug Console with the showLog: true option is nearly clean. It only contains the following lines:
API server listening at: 127.0.0.1:34477
2019-12-19T13:55:52+01:00 info layer=debugger launching process with args: [/mnt/c/Users/cloudnaut/gospace/go/src/github.com/cloudnaut/project1/__debug_bin]
Nothing else. The "step over/step info/Step out" buttons from the debugger are greyed out. I only can press pause, restart and stop.
See:
Picture VS Code Debugger Problem
I bring the solution for my own question:
The problem is that the Go dlv debugger dont works with WSL 1 because of some unsupported system calls. It only works with WSL 2, wich is included only in the Microsoft insider build.
See:
https://github.com/microsoft/vscode-go/issues/2505
I'm using the protractor-cucumber-framework to create a testbed environment for our QA team. I've searched around and been able to find zero help in implementing VS Code's debugging capability for use in this code. Has anyone does this? I'd really like to step away from console.log() statements.
1) Upgrade your Nodejs to 8 and later
2) Create a folder .vscode under your project base directory
3) Create a file launch.json under .vscode
4) Copy below content into launch.json
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [{
"type": "node",
"request": "launch",
"name": "gie",
"program": "${workspaceFolder}/node_modules/protractor/built/cli.js",
"cwd": "${workspaceFolder}",
"args": [
"config/gie.config.js",
"--browser=chrome"
]
}]
}
The ${workspaceFolder} represent your project base directory
The first value in args is your protractor config file, you can use relative path to ${workspaceFolder}
The second and next value in args is command options as you type in command line to run test.
My Environment: VSCode 1.8.1, Nodejs v8.9.0, Protractor 5.2.0,
I am new to web development in general, and I am trying to setup sass following the documentation here:
https://code.visualstudio.com/docs/languages/css
However I am receiving this error:
"An output directory must be specified when compiling a directory". On
the internet people are suggesting solutions, but they are related to
configuration of other editors.
My project looks like this, so you can have an idea how my project is set up:
SASS project config screenshot
I tried adding all design files in the same folder as the tasks.json file, but it didn't work and I got the same error.
I have one more question: do I need to create the .css file, or does the task create it if it's not found?
I appreciate any help possible.
I just figured this out, while looking for the same solution.
Your "args" have to be configured like this:
"args": ["./src/app/styles.scss", "./src/app/styles.css"]
or
"args": ["./(static or assets folder)/(sass folder)/styles.scss", "./(static or assets folder)/(css folder)/styles.css"]
The "./" points to the root of the project, then simply include the appropriate folder path.
Cheers!
The default setup for the Sass Task Runner on https://code.visualstudio.com/docs/languages/css now looks like this:
// Sass configuration
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Sass Compile",
"type": "shell",
"command": "node-sass styles.scss styles.css",
"group": "build"
}
]
}
If you get the error
"An output directory must be specified when compiling a directory"
then change the configuration like:
"command": "node-sass ./<your path>/styles.scss ./<your path>/styles.css",
I downloaded MinGW following the first link here https://isocpp.org/get-started and now I need to configure it in SubimeText 3. I know I should go to Tools > Build System > New Build System... But what should I specify there?
I use Win7x64. And MinGW is in C:\MinGW
The complete reference for build systems is here. The first thing you need to do is make sure that the C:\MinGW\bin directory is in your PATH, then restart Sublime so the change gets picked up.
Once you've done that, create a new build system with the following contents:
{
"cmd": ["gcc", "${file}", "-o", "${file_base_name}.exe"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"shell": true,
"variants":
[
{
"name": "Run",
"cmd": ["start", "cmd", "/k", "${file_path}/${file_base_name}.exe"],
"shell": true
}
]
}
and save it as Packages/User/C.sublime-build where the Packages folder is the one opened by selecting Preferences -> Browse Packages....
You can now choose this build system by selecting Tools -> Build System -> C. Once you are ready to compile, save your source file, then hit CtrlB to build. To run the program, hit CtrlShiftB and a cmd window will open up to run the resulting .exe file, then stay open until you close it (so you can see any output produced by the program).
You can try to use the C++ build system that comes with Sublime, but some users have run into issues with it in the past, especially on Windows, so this custom one may suit your needs better.
Good luck!