Unable to Call Function in Go debugger
Asked Answered
I

3

9

I am following the "Little Go Book" by Karl Seguin, in order to learn Go.

My working environment is Visual Studio Code.

Upon debugging, when I try to call a function from the debug console, i get the following error: "function calls not allowed without using 'call'", if I try using "call fib(10)", i get "Unable to eval expression: "1:6: expected 'EOF', found fib". This is the function I am trying to evaluate:

//Fibonnaci
func fib(n int) int64 {
    if n == 0 {
        return 0
    } else if n == 1 {
        return 1
    } else {
        return fib(n-1) + fib(n-2)
    }
}

If i try to call the function from the code itself ( from the main() for instance, it works perfectly).

However, if I set a breakpoint and try to call the same function from the debugger console, I get the below error:

Eval error: function calls not allowed without using 'call'
call fib(10)
Unable to eval expression: "1:6: expected 'EOF', found fib"
Failed to eval expression:  {
 "Expr": "call fib(10)",
 "Scope": {
  "goroutineID": 1,
  "frame": 0
 },
 "Cfg": {
  "followPointers": true,
  "maxVariableRecurse": 1,
  "maxStringLen": 64,
  "maxArrayValues": 64,
  "maxStructFields": -1
 }
} 
Intersperse answered 3/10, 2019 at 10:58 Comment(0)
C
3

Looks like "Function calls via delve 'call' are not supported" yet github issue in microsoft/vscode-go repo :(

Coinsure answered 17/2, 2020 at 17:0 Comment(4)
How is this answer helpful to anyone? Are you even using the same environment as the asker?Rooster
@GrantBirchmeier it doesn't matter, because all go UI debuggers are using delve under the hood. I demonstrated that it works well in console debugger mode.Coinsure
Well, it does matter, because I have the same problem as the asker and your answer in no way helps me solve it.Rooster
try to run dlv version, i'll try to help you.Coinsure
S
1

The issue vscode-go issue 100 "debug: support function calls via delve 'call'" just got closed with PR 101 and commit 5a7752c / CL 249377

Delve supports function calls. Even though it is still experimental and can be applied only to a limited set of functions, this is a useful feature, many vscode-go users long for.

Unlike other javascript/typescript debuggers, delve treats function calls specially and requires different call paths than usual expression evaluation.
That is because Go is a compiled, runtime-managed GC language, calling a function safely from debugger is complex.
DAP and VS Code UI does not distinguish function calls and other expression evaluation either, so we have to implement this in the same evaluateRequest context.

We use a heuristic to guess which route (call or expression evaluation) we need to take based on evaluateRequest's request.

This is part of the 0.17.0 milestone, yet to be released, and available for now in the nightly build.

Swept answered 7/9, 2020 at 6:51 Comment(3)
I tried with github.com/golang/vscode-go/releases/tag/v0.17.0-rc.3 and still seeing the issueWaterbuck
@VidyasagarMachupalli I suppose that would be a good reason to update github.com/golang/vscode-go/issues/100. Did you make an explicit call command to indicate the intention?Swept
Fix is still not implemented in Jetbrains Goland as of 2022.2.5Elvera
S
1

With the latest version of dlv 1.22.1 (dont remember since which exact version this is available!), you can simply use "call" infront of the function and it will return the value: Eg: call err.Error()

enter image description here

Sorrento answered 18/7, 2024 at 6:11 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.