Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Environment variables undefined during debug. #947

Closed
AlexShemeshWix opened this issue Apr 27, 2017 · 16 comments
Closed

Environment variables undefined during debug. #947

AlexShemeshWix opened this issue Apr 27, 2017 · 16 comments

Comments

@AlexShemeshWix
Copy link

Hi
Im on Mac 10.12.4.
VSCode 1.9.0 - Go plugin 0.6.59
GO 1.8.
My program is single file

package main

import (
	"fmt"
	"os"
	"os/exec"
)

func main() {
	var (
		cmdOut []byte
		err    error
	)
	fmt.Println( "PATH is "  + os.Getenv("PATH"))
	cmdName := "git"
	cmdArgs := []string{"rev-parse", "--verify", "HEAD"}
	if cmdOut, err = exec.Command(cmdName, cmdArgs...).Output(); err != nil {
		fmt.Fprintln(os.Stderr, "There was an error running git rev-parse command: ", err)
		os.Exit(1)
	}
	sha := string(cmdOut)
	firstSix := sha[:6]
	fmt.Println("The first six chars of the SHA at HEAD in this repo are", firstSix)
}

my launch config is

 {
            "name": "Launch GO prog",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${workspaceRoot}",
            "env": {
                "TEST_ENV": "development"
            }
        },

The probelm

exec.Command(cmdName, cmdArgs...).Output() fails under debugger since git cannot be found

There was an error running git rev-parse command: exec: "git": executable file not found in $PATH

Ive tried to figure out whats wrong so ive tried to print out os.Getenv("PATH") under debugger its empty.

When i run it from command line go run test.go it works ok.

I think i messed up configuration of VSCode but i dont know what it is.
Thanks.

@AlexShemeshWix AlexShemeshWix changed the title Cannot read env variables Environment variables undefined during debug. Apr 27, 2017
@ramya-rao-a
Copy link
Contributor

I tried your code, and I am able to debug through it and no errors.

Does go run test.go work in the integrated terminal as well?

Also, can you try using dlv from the command line and see if this works?

@AlexShemeshWix
Copy link
Author

Hi
dlv is not working as well.
Any env variable is not defined when im running under debugger.
Ive tried to reainstall golang and vscode.
No luck.
Its really frustrating that such simple thing does not work properly.
May be i can try some other editor? gogland didnt worked for me. It stack with Module "" is undefined when i define run configuration.
Thanks

@AlexShemeshWix
Copy link
Author

In the end i went for gogland IDE. Its not 100% and it has its own glitches but debugging of tests and project itself works ok.

@ramya-rao-a
Copy link
Contributor

@AlexShemeshWix Sorry that the Go extension for VS Code didn't work out for you.

But before you totally give up on it, would you mind helping us figure out the root cause of your issue?

Do you have your env variables set in .bashrc or ~/.profile?
See #708 (comment) for the difference.

@xoob
Copy link

xoob commented May 2, 2017

Okay I've been wrangling with the same issue. It looks like it has something to do with the lldb backend on macOS.

$ dlv version
Delve Debugger
Version: 0.12.2
Build: HEAD-dcf51a5
$ go version
go version go1.8.1 darwin/amd64
$ brew config
macOS: 10.11.6-x86_64
Xcode: 8.2.1
Clang: 8.0 build 800

Note that I'm still on 10.11, not 10.12.

main.go

package main

import (
	"fmt"
	"os"
)

func main() {
	fmt.Printf("args:       %v\n", os.Args)
	fmt.Printf("env.TMPDIR: %#v\n", os.Getenv("TMPDIR"))
}

Tests

Normal go-run:

$ go run main.go
args:       [/var/folders/v1/nd5ykpp11yjgymnc1zzt7n2h0000gn/T/go-build043785679/command-line-arguments/_obj/exe/main]
env.TMPDIR: "/var/folders/v1/nd5ykpp11yjgymnc1zzt7n2h0000gn/T/"

Default dlv-debug:

$ dlv debug main.go
Type 'help' for list of commands.
(dlv) c
args:       [.../dlvdebug/debug]
env.TMPDIR: ""

Native dlv-debug with --backend=native:

$ dlv debug --backend=native main.go
Type 'help' for list of commands.
(dlv) c
args:       [.../dlvdebug/debug]
env.TMPDIR: "/var/folders/v1/nd5ykpp11yjgymnc1zzt7n2h0000gn/T/"

As you can see, the run with dlv debug main.go does not receive any environment variables, while the run with dlv debug --backend=native main.go works as expected.

The backend flag is documented as follows:

$ dlv --help
...
Flags:
      --backend string       Backend selection:
	default		Uses lldb on macOS, native everywhere else.
	native		Native backend.
	lldb		Uses lldb-server or debugserver. (default "default")

Not sure what to do about it, but I hope this is helpful.

@xoob
Copy link

xoob commented May 2, 2017

Possibly related: microsoft/vscode-cpptools#191

@ramya-rao-a
Copy link
Contributor

@xoob Thanks for the detailed investigation!

@derekparker --backend is a recent addition to delve.
Is the behavior described by @xoob above expected/known and do you expect editors to pass the backend option?

@AlexShemeshWix
Copy link
Author

When using dlv --backend indeed helps.
Using dlv and remote debugging in VSCode works ok.
Still i when i run it from VSCode directly - its the same.
Ive checked bashrc and all other shell configuration stuff, i dont think its the issue.

  1. Go itself runs, its just it cant see any env variables at all, even when i define env in launch.json - its invisible under debugger.
  2. I use VSCode for variaty of languages, Python and Java Script mostly, and i never met such problem, so i guess its plugin problem.
    Thanks

@zcmack
Copy link

zcmack commented May 3, 2017

i'm having a similar issue. using fish shell so if the problem is related to shell config i figure i'd be worse off as it lacks full POSIX compliance.

could not launch process: exec: "lldb-server": executable file not found in $PATH Process exiting with code: 1

i'd prefer not to switch to gogland if possible so let me know what i can do to help. i'm on sierra 10.12.4 and go version 1.8.1, delve installed via go get github.com/derekparker/delve/cmd/dlv

@ramya-rao-a
Copy link
Contributor

Alright, looks like this is a Mac + latest delve issue.
The reason I couldn't repro must be because I am using the previous version of delve.

I'll update delve and try to get a repro and log an issue for delve
Meanwhile, I'll also add the backend option in the launch.config so that Mac users using the latest delve can be unblocked.

@AlexShemeshWix
Copy link
Author

Awesome 10x

@zcmack
Copy link

zcmack commented May 3, 2017

i reinstalled xcode tools and now it is working for me. might be worth a shot.

xcode-select --install

@AlexShemeshWix
Copy link
Author

AlexShemeshWix commented May 3, 2017 via email

@ramya-rao-a
Copy link
Contributor

@xoob @AlexShemeshWix Did you manually install delve or via brew?

I installed via brew in my Mac(10.11.6), and I can confirm I got the latest delve (0.12.2), but it doesn't have the --backend option.

I installed using go get on my Windows machine and I can see the --backend option there.

@ramya-rao-a
Copy link
Contributor

Installed using brew install --HEAD and I can repro now.
I have logged an issue in the dlv repo.

Ideal fix would be to fix the lldb case in dlv and https://github.com/derekparker/delve/issues/818 is tracking that.

With 4beecf1 you will be able to set the backend option in the launch.json file and I can confirm that setting this to native fixes the issue.

This will be out in the next update for the Go extension which is in a day or 2.

If you are blocked by this issue and cannot wait for the next update of the Go extension, current workaround is to install the stable release of delve via brew using brew install go-delve/delve/delve. Or if you had manually installed, go back to the commit derekparker/delve@f605716

@ramya-rao-a
Copy link
Contributor

The upstream issue has been fixed.
The option to set backend is also available in the Go extension in the latest update (0.6.60)

Users seeing this issue can either add the new backend property to their debug config in launch.json file and set it to native or get the latest delve

@vscodebot vscodebot bot locked and limited conversation to collaborators Jan 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants