GoLand setting terminal cells doesn't work - go

I'm using the tcell library to display terminal cell graphics.
While writing this project in GoLand, I've noticed that using a normal run configuration and running the program in the integrated terminal, I'm not seeing the cells getting set as intended, despite tcell not giving any errors.
Program:
package main
import (
"time"
"github.com/gdamore/tcell"
)
func main() {
screen, err := tcell.NewScreen()
if err != nil {
panic(err)
}
err = screen.Init()
if err != nil {
panic(err)
}
screen.SetCell(0, 0, tcell.StyleDefault, 'X')
screen.SetCell(1, 0, tcell.StyleDefault, 'X')
screen.SetCell(1, 1, tcell.StyleDefault, 'X')
screen.SetCell(10, 10, tcell.StyleDefault, 'X')
screen.Show()
time.Sleep(time.Second*5)
}
GoLand output:
The program works as expected when running through cmd:
How can I set a run configuration in GoLand to run my program in cmd, or some other form of terminal that will allow me to set cells like this?

Open Help | Find Action...
Type Registry and hit Enter.
Find go.run.processes.with.pty there and turn it on.
Please, keep in mind that it can cause problems with run configurations like failing green tests or vice versa, never finishing debug sessions, and so on. If you notice weird IDE behavior related to console output, please disable the registry option back.
I'm not sure if points on Y-axis do display properly inside the Run window.

I guess GoLands terminal is a fake terminal without real cursor addressability. There may not be a good solution if that is the case.
I’m the author of tcell and I use goland but I confess I always run my test programs in a real terminal rather than in the toy terminal that the IDE provides. This is true whether I use goland, visual studio code, or even the venerable emacs.

By using a new Batch run configuration, you can run a batch file to build the program, then run the program in a new cmd window.
In the run configuration in GoLand, set "Working directory" to the main package directory. Then set the script to a new batch file.
Here is the code in my batch file for my package client
go build
start cmd /C client.exe
Running this configuration will build the package, then run the program in a new external cmd window where the cells display properly.
This solution isn't great, because most of the advantages of the GoLand run configuration system are lost, including debugging, process management (stop / restart), and other build options.
Does anyone have a better solution?

Related

Debugging go in vscode doesn't stop at breakpoints, says "Could not find file ..." when debugger starts

Ubuntu. vscode 1.62.1. go1.17.3. vscode go extension v0.29.0. delve v1.7.1.
I'm new to vscode and Go. I have many years of experience debugging Java apps in Eclipse.
I've constructed a small multi-module Go app. I can set a breakpoint in main and other functions in other modules. Inside main.go, I select "Start Debugging".
It starts the application, and I can tell it's working from the console, and that the REST endpoint responds with my dummy response.
However, it will NOT stop at breakpoints. As soon as I start the session, the red breakpoint markers suddenly become hollow, and hovering on one of them shows a message "Could not find file ...", which prints the full path to the source file in question.
When I start it, it shows the following in the console:
Starting: /home/.../go/bin/dlv-dap dap --check-go-version=false --listen=127.0.0.1:43347 --log-dest=3 from /home/.../...
DAP server listening at: 127.0.0.1:43347
I haven't modified the launch.json (I hope someday a friendlier interface to editing launch configurations is provided).
What else could I be doing wrong?
Update:
This is a screenshot showing main.go just before I press F5 (Start Debugging):
Notice that I have a breakpoint on the print statement, on the first line of main.
This is what I see after I press F5:
Notice that it printed "At start of main" in the console. It didn't stop at the breakpoint. Also notice message in tooltip when hovering over the breakpoint.
Update:
This is a view of my directory structure:
First, just make sure you have initiated your project with go mod init voltagems: that would explain the import "voltagems/xxx", but also helps delve to find your main.go file at debug time.
You should have go.mod and go.sum files beside main.go.
Second, check your go env output, making sure GOPATH and GOROOT are set to default paths.
The OP David M. Karr adds in the comments:
I did run "go mod init" when I first created the project, but I realized that I didn't like the root module name, so I changed it to "voltagems"
I believe you can edit directly go.mod first line, and make sure it says:
module voltagems
Then go mod verify + go mod tidy
Finally, go build .. Restart your VSCode (or the command Reload Window), and see if the issue persists.
The OP David M. Karr points out to a root cause:
There are symbolic links in my project path.
There is a "substitutePath" configuration in VSCode-Go that is used to map to absolute paths.
You can see this parameter mentioned in Debugging with Legacy Debug Adapter
substitutePath
Path mappings to apply to get from a path in the editor to a path in the compiled program (default: []).
That comes from issue 622 "debug: breakpoints don't work when working with symlink".
And commit 93f32bb
src/debugAdapter: add substitutePath config for debugging
This change adds a new configuration option to both launch and
attach requests.
substituePath takes an array that maps from string to string that is used to translate paths passed to the debugger and then
back to the client.
This allows users to translate their symlinked directories to the
files that were actually used to build the binary.
In addition this can also be used for remote debugging, and when the location of the files has moved since the program was built.
Example: you need a from and to key:
"substitutePath": [
{
"from": "/symlink/path/dir/on/local/machine",
"to": "/absolute/path/dir/on/local/machine",
},

Unable to use Delve to debug Go - Access is denied

Attempting to use Delve to debug Go, I get the following error:
could not launch process: fork/exec C:\code\go_stuff\debugtest\__debug_bin: Access is denied.
could not remove C:\code\go_stuff\debugtest\__debug_bin: remove C:\code\go_stuff\debugtest\__debug_bin: Access is denied.
This is on a very simple Go project I created using go mod init and I wrote the main.go using Vim, so no VSCode or anything else is involved.
I tried to run dlv debug from the terminal and I get the above output.
I also get the above output when I try and debug using Delve in VSCode as well.
I have tried this on another PC and it works perfectly so it may well be something environmental but I cannot fathom what is causing this.
None of the projects I am trying to debug are in git or indeed any other source control.
Before I incur any downvotes due to lack of code, here is my entire project:
package main
import "fmt"
func main() {
fmt.Println("So we begin")
fmt.Println("Here we end")
}
It seems to be have been an issue caused by anti-virus deleting the executable when I tried to run it or even debug it.
I moved to developing Go in WSL2 using VSCode and I can debug the code without issue, so this would appear to be environmental and not an issue with either Delve or Go.
Actually I've met similar problem. I modify the "settings.json" file of VScode as following:
{
    "workbench.colorTheme": "Default Dark+",
    "workbench.editorAssociations": {
        "*.ipynb": "jupyter.notebook.ipynb"
    },
    "gopls": {
        "experimentalWorkspaceModule": true
    },
    "go.alternateTools": {
    
    },
    "go.delveConfig": {    
        "dlvLoadConfig": {
            "followPointers": true,
            "maxVariableRecurse": 1,
            "maxStringLen": 64,
            "maxArrayValues": 64,
            "maxStructFields": -1
        },
        "apiVersion": 2,
        "showGlobalVariables": false,
        "debugAdapter": "legacy",
        "substitutePath": []
    }
}
Then try to debug or either run without debug. It works and the "__debug_bin.exe" will not generate anymore. However I don't know the reason, it seems some problem of dlv configuration. Does anyone know the root cause? Hope this help you!

File paths for running golang code for debug vs run

I have a golang code on Linux VM which I am remotely debugging using VS Code. Below is my folder structure
MainFolder
|__Config
|__Other folders
Other Files
When I run the code using VS debugger, it runs properly and my code is able to find the path to files. But when I use the terminal to the code (I have workspace configured and need other project to run, and one project to debug) using go run ./folder, it gives some path like /tmp/go-build2342342342/b001/ when absolute path is populated. Any idea why it works this way and how to make the behavior consistent for properly getting the path?
Below is the code converting relative path to absolute
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
var path = filepath.Join(dir, relPath)
absPath, err := filepath.Abs(path)
Go binaries are compiled, even when run via go run. When you go run a package, os.Args[0] is set to the path of the compiled binary within the build cache, not the directory containing the source code for that binary.
To determine the path to source files, you must either pass the source location explicitly (as a flag or explicit argument), or invoke go run in a well-known directory and use os.Getwd to located it.
However, for run-time debugging information specifically, see the runtime and runtime/debug packages.

How to display command history with jline3?

I want the most recent command entered to be displayed when the user presses the up arrow key.
The Terminal is defined like this (Scala code):
val terminal: Terminal =
TerminalBuilder.builder
.system(true)
.build
The LineReader is defined like this:
def reader(parser: Parser, terminal: Terminal): LineReader = {
val lineReader: LineReader = LineReaderBuilder.builder
.terminal(terminal)
.completer(shellManager.topShell.completer)
.parser(parser)
.variable(LineReader.HISTORY_FILE, historyFile)
.history(new DefaultHistory())
.build
lineReader.unsetOpt(LineReader.Option.INSERT_TAB)
lineReader
}
Update: I found that the above actually works on some consoles, not others. I am still discovering what works and what does not. Any insight would be appreciated.
This is supposed to work out-of-the box. If you have an issue with a specific terminal, please report which exact terminal you use. For what it's worth, this can't work from inside build tools (gradle, maven) or IDE (Eclipse, Intellij IDEA).

Run Golang project in Sublime Text 3

I cannot find a suitable solution for my needs with running a Go project inside a Sublime Text 3 and seeing it's output in real-time.
If I try a build system from:
GoSublime - I can use run, it even runs and stops, but there is no output, which I need. It appears only when I cancel build - it's too late.
Official Golang Build from Go developers - I can build the project, and that's it. It allows to run 1 file (current) only, but I need the whole project.
I've tried to use flags for run command and to add *.go, but then I get *.go: no such file or directory
How do I see the output in real-time in one of these solutions? I've tried to create my own build system with shell_cmd = go run *.go, but stopping the process with Cancel build is not working then. Maybe you can explain how do I stop a running Go program? My mybuild.sublime-build is similar to this:
{
"env": {
"GOROOT": "/path/gosrc/go",
"GOPATH": "/path/godev"
},
"path": "$PATH:/path/gosrc/go/bin",
"working_dir": "/path/godev/src/github.com/user/program",
"cmd": "go run *.go",
"shell": true
}
..I can run Build and see the needed output, but how do I then stop a running process?
P.S. Program is not just executing and exiting - it's a service, so I should see the output when the needed actions happens.
I personally use GoSublime and go build . followed by running the app by name as a second command. Obviously this sucks in a lot of ways, but it kinda sorta works most of the time. It provides a nice fast way to check for compile errors, which is most of what I need.
Honestly, just running in a dedicated shell is nicer in every way.
AFAIK, there is no better Go build system available for Sublime Text (that isn't an endorsement, it sucks, just less than most).
I found a tutorial where I was able to run Go files on build in Sublime Text 3 here: https://www.alexedwards.net/blog/streamline-your-sublime-text-and-go-workflow
If you use Build With: Go - Run you get outputs, but if you are running for example a net/http local host in Go you won't be able to run multiple programs and cancel build also does not work.
Here is an example of simple fmt.Println output in Sublime Text 3:
> Environment:
> GOPATH=C:/Users/Christiaan/go
> Directory: C:\Users\Christiaan\Documents\02_Personal\04_Learning\09_Go\01_test
> Command: C:\Users\Christiaan\go\go1.15rc1\bin\go.exe run -v C:\Users\Christiaan\Documents\02_Personal\04_Learning\09_Go\01_test\test2.go
> Output:
command-line-arguments
gas_pedal: 22314 brake_pedal: 0 steering_wheel: 12562
> Elapsed: 3.856s
> Result: Success

Resources