How to run a make file on save in VS Code - using go extension - go

How can I run a make build on save in VS Code on save event?
I can see that I can do something like the following.
{
"go.buildOnSave": "workspace"
}
I guess the lack doc. is be cause it is just basic VS Code - on setting a command.

How can I run a make build on save in VS Code on save event?
The Build, Lint and Vet of the VSCode Go extension from Microsoft (v1.26) currently based on go build. You can choose the preset option for package (default), workspace, or off. See vscode-go/goBuild.ts
If you would like to have a custom build i.e. make build on your workspace, you can set VSCode Custom Tasks instead.

Related

How can vscode stop send error notifications?

I am doing leetcode exercises on vscode, using this extension:
However vscode keeps sending error notifications about the golang codes written locally lack of main module
How can I disable vscode error notifications? As those golang codes are used to run remotely, so there are no needs to compile locally
Check first if you can disable the Go extension, just for your current (leetcode) workspace.
If you don't want to permanently remove an extension, you can instead temporarily disable the extension by clicking the gear button at the right of an extension entry.
You can disable an extension globally or just for your current Workspace.
You will be prompted to reload VS Code after you disable an extension.
By disabling it just for the workspace, you can still use Go for Go projects in a separate workspace in the same VSCode.

Is there a way in Visual Studio Code to set a test file target other than the current file

I am trying to switch to VS Code from LiteIDE for developing GO code. One of the features that I used frequently in LiteIDE was the ability to pick a file, other than the current file, as the target of run or test commands.
For example, I could set the file my_go_file_test.go as the target and then be able to run the tests with a keyboard shortcut while editing any other file.
In LiteIDE this is accomplished by pinning the file in a control at the top of the IDE. Is there a way to do something similar in VS Code.
I've poked around in setting and googled it. The best I can come up with at this point is a three-step process to toggle to the test file, run the test, and then toggle back to the file I was in. This requires 3 separate shortcuts instead of just one.
The goal is to be able to use a single keyboard shortcut to run tests while doing TDD.

How do i log all logging output to a file with wildcards if i run the build from visual studio

There are plenty of posts on how to capture logging output while manually invoking msbuild from the console but i am wondering if there is any way to log all logging output to a file of my desire using wildcards (timestamps, Configuration etc) if the build is fired from Visual Studio.
No - running it from console is not an option. I've spent 2 days trying to get it to work. Seems like the Xamarin targets were not built with dev friendly build automation in mind.
Why is it not an option?
- Wrong toolset,
- wrong MSBuild,
- mtouch warnings,
- targets which expect env variables which seem to be added by VS at runtime
so i gave up and am looking for a way to capture my logs so i can dump the output into a file together with other artifacts.
How do i log all logging output to a file with wildcards if i run the build from visual studio
According to the How to: View, Save, and Configure Build Log Files:
After you build a project in the Visual Studio IDE, you can view
information about that build in the Output window. By using this
information, you can, for example, troubleshoot a build failure. For
C++ projects, you can also view the same information in a .txt file
that's created and saved automatically. For managed-code projects, you
can copy and paste the information from the Output window into a .txt
file and save it yourself. You can also use the IDE to specify what
kinds of information you want to view about each build.
If you build
any kind of project by using MSBuild, you can create a .txt file to
save information about the build. For more information, see Obtaining
Build Logs.
We could to know log file from Visual Studio is only supported for C++ projects. we have to work with the output window for others.
If you do not want to copy and pasting the content to a text file. Here is a quiets way to avoid copy past and save the content directly:
Follow these steps:
1. Set the focus to the Output Window
2. From the main menu navigate to File –> Save Selected Items (Ctrl + S) or Save Output As.
To save the content directly in to a text file.
Default filename will have the name with “Output-Build”, you can change it to what you want, but it could not use wildcards in the name.
Hope this helps.

Gogland: debugging is grayed out

I can't seem to get the debugger to run. The debugging run icon is grayed out and the menu options are missing.
This is only the case for main, I can debug unit tests just fine.
Similar questions mention the project structure, but I can't see anything wrong:
$GOPATH/src/foo.bar.com/some/more/dirs/
project/ <-- this directory is opened in Gogland
app1/
main.go
app2/
main.go
mylib/
abc.go
abc_test.go
I can right click on abc_test.go and run it with or without debugging, but if I right click on one of the main.go files, I can only run it.
Run configuration:
Gogland build 162.12024.32
Go Single File does not currently support debugging. If such a Run Configuration is active then a new one must be created. The type for the new one must be Go Application.
To create a Go Application Run Configuration you can also choose: Build and run ... from the menu when presented with the option.
Also changing the current active Run Configuration from a Go Single File to a Go Application one will fix the problem.

How to set up debugging in GoLand?

I am trying out GoLand and hear that the debugger is supposedly awesome, but I can’t find any documentation explaining how to set it up (GoLand is in preview stage, so that’s not really surprising). My use case: I am writing a REST API to which I send requests using curl. Could someone please tell me, step by step, how to get started with the debugger ?
[edit 1] (moved from comment) When I click Run/Debug (or Run/Run), a window is displayed, asking me to edit configurations. I am given a choice of several kinds of configurations, I try to choose "Go application" but the Debug button never is grayed and I can't get it to become clickable. I am very new to GoLand, I may be missing something obvious, but I can’t find whatever settings are missing.
[edit 2] It turns out that my workspace did not conform to the Go specifications : my code was not in a directory named src. Now that it is the case, I can click Run/Debug and GoLand seems to be doing the right thing, except it doesn’t stop at breakpoints or otherwise behave like a debugger, my code just runs.
[edit 3] Tried again, and it works. Sort of. I have no idea why it works now, even though it didn’t previously and I didn’t change anything. Now breakpoints work at some lines, and not at others. Or the debugger stops at the breakpoint, but I wait forever for the list of variables to be loaded. Well, it is a preview, after all...
[edit 4] I was notified just today that a new version of GoLand was available. I upgraded, and debugging is working well for me now.
GoLand will not enable the debug action (although run is available) if your code does not reside inside src. There is no warning about this, so it might be really a pain to solve this.
Additionally, depending on your project’s structure it might be possible, that debugging is still not working. In that case, it might help to remove all run configurations and create a new one. There’s is a bug report about this behavior.
PS: Although the OP mentions the first part in an edit of the question, but it think it’s worth mentioning in an answer.
Things changed in goland since #Jamillo Santos added his reply.
To start with, there's no more "Go Application".
The easiest way is to locate the file containing the main() function.
There should be a small triangle pointing to it, like in the attached screenshot:
When you click on the small green triangle, you get a menu with Run/Debug options.
Each will create a go build configuration.
You can edit that go build configuration and add the program arguments for your application.
For example, if your program have a command line argument of --arg=value you can add it like this:
I was facing this same issue and I solved by using the "Go Application" configuration (at the Run/Debug Configurations window), instead of the "Go Single File" configuration.
So, go to the upper right corner of the Gogland and "Edit configurations".
Then, add a new configuration using the "Go Application" profile.
After saving, you should be able to debug your code. o/
An easy way to enable this configuration is to use the #user1793301 method and right click on the file you want to run and select "Debug 'go run .go'".
POSSIBLE EXPLANATION
NOTICE: I did not look any further details about it.
Fact: "Go Application" configuration does build the binary executable before running it.
Fact: I could see looking at the console output (inside of the Goglang) is that the IDE uses the DLV as debugging tool.
Fact: I found a dlv debug at the DLV documentation and it seems to compile and debug the code.
Hypothesis: The guys from Jetbrains did not implement it. Or at least not YET.
On version 2019.1.1 there's no more Go Application. Use Go Build instead. Here I am debugging a single script file, but if you have a server app or smth, just point to the main app file:
Notice that in the Files field you have to specify the actual file path. You can put more files (if you want to have more starting points) by separating them with a pipe |. For each file you want to be able to debug, you need to specify the full path.
Again, if you are debugging a server app or something else, that has a single starting point, you only need the startup file specified in the config. :)
I had this exact problem.
Deleted and recreated the Debug run configuration. Debugging then worked perfectly.
I followed the following procedure to start my golang server in debug mode. (You might want to use some other method than creating a makefile for starting your server maybe)
Create a makefile
Define your server/worker startup command in the file
Run it by creating a new configuration for Makefile
Go to Run -> Attach to process -> choose your running server
This will attach the debugger to your process.
Note:
You might need to restart your server for this to work.
The IDE will ask you to install gops, do install it. Without installing gops you won't see any running process.
I right click on project (main.go inside) and choose "Debug ..." and it work

Resources