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

Environment variables undefined during debug. #947

Closed
@AlexShemeshWix

Description

@AlexShemeshWix

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.

Activity

changed the title [-]Cannot read env variables[/-] [+]Environment variables undefined during debug.[/+] on Apr 27, 2017
ramya-rao-a

ramya-rao-a commented on Apr 29, 2017

@ramya-rao-a
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

AlexShemeshWix commented on Apr 30, 2017

@AlexShemeshWix
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

AlexShemeshWix commented on Apr 30, 2017

@AlexShemeshWix
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

ramya-rao-a commented on May 1, 2017

@ramya-rao-a
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

xoob commented on May 2, 2017

@xoob

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

xoob commented on May 2, 2017

@xoob
ramya-rao-a

ramya-rao-a commented on May 2, 2017

@ramya-rao-a
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

AlexShemeshWix commented on May 3, 2017

@AlexShemeshWix
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

zcmack commented on May 3, 2017

@zcmack

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

ramya-rao-a commented on May 3, 2017

@ramya-rao-a
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

AlexShemeshWix commented on May 3, 2017

@AlexShemeshWix
Author

Awesome 10x

zcmack

zcmack commented on May 3, 2017

@zcmack

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

xcode-select --install

11 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @xoob@zcmack@ramya-rao-a@AlexShemeshWix

        Issue actions

          Environment variables undefined during debug. · Issue #947 · microsoft/vscode-go