0

I added a new environment variable into ~/.zshrc in macOS 13.2 few minites ago, this is the content about the variable in .zshrc:

export ALT_DATABASE_URL=postgres://postgres:123456@reddwarf-postgresql.reddwarf-storage.svc.cluster.local:5432/alt

then run the source command to make it works:

source ~/.zshrc

check the variable:

> env|grep "DATABASE_URL"
DATABASE_URL=postgres://postgres:123456@127.0.0.1:5432/dolphin
CV_DATABASE_URL=postgres://postgres:123456@reddwarf-postgresql.reddwarf-storage.svc.cluster.local:5432/cv
ALT_DATABASE_URL=postgres://postgres:123456@reddwarf-postgresql.reddwarf-storage.svc.cluster.local:5432/alt

when I using this rust code to read the environment variable in visual studio code with lldb plugin:

fn main() {
    let cv = std::env::var("CV_DATABASE_URL").expect("get cv url error");
    print!("cv:{}", cv);
    let alt = std::env::var("ALT_DATABASE_URL").expect("get alt url error");
    print!("alt:{}", alt);
}

the newly add variable read failed:

thread 'main' panicked at 'get alt url error: NotPresent', src/main.rs:4:49
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
cv:postgres://postgres:123456@reddwarf-postgresql.reddwarf-storage.svc.cluster.local:5432/cv%                                                                                             

am I missing something? what should I do to fixed this issue? I have do some investgate seems the visual studio code lldb plugin could not read the os environment variable.

Dolphin
  • 29,069
  • 61
  • 260
  • 539
  • Weird that it finds `CV_DATABASE_URL` but not `ALT_DATABASE_URL`. Were the `env|grep` and the `cargo run` command executed in the same terminal session? – PitaJ Jul 05 '23 at 17:02
  • How and where are `DATABASE_URL` and `CV_DATABASE_URL` defined? – PitaJ Jul 05 '23 at 17:09
  • defined in the .zshrc using the export command – Dolphin Jul 05 '23 at 17:18
  • Are you sure you are using the same shell session for both `env` and your Rust code? – kmdreko Jul 05 '23 at 18:54
  • yes, I am sure the rust and shell env command are in the same shell session. – Dolphin Jul 05 '23 at 20:05
  • I was not able to reproduce: you have to use `export` for `source ~/.zshrc` to set them in your current shell, but either way Rust's `std::env::vars()` was always consistent with `env`. – kmdreko Jul 05 '23 at 21:00
  • I have do some investgate, in the terminal run the rust app, it works fine, but when I debbuging in visual studio code using lldb plugin, it could not read the environment variable. @kmdreko – Dolphin Jul 05 '23 at 21:08
  • It's not an **environment** variable unless you export it. Just `var=value` creates a _shell_ variable, not an _environment_ variable. – Charles Duffy Jul 05 '23 at 21:10
  • I have already exported it in the .zshrc using export command. – Dolphin Jul 05 '23 at 21:12
  • You might add that to the `.zshrc` fragment in the question, then. – Charles Duffy Jul 05 '23 at 21:12
  • 1
    That said, how are you starting VS Code? If you don't start it as a subprocess of a shell, it won't inherit variables the shell's startup creates. – Charles Duffy Jul 05 '23 at 21:13
  • (Also, which operating system are you on? How environment variables are set in interactive sessions is different between them; for that matter, it's different between traditional UNIX systems -- which typically had the GUI started by a login shell -- and more modern ones, which often don't). – Charles Duffy Jul 05 '23 at 21:15
  • 1
    I am using the macOS 13.2, seem I have to start the visual studio code from terminal to make it inherit the shell environment – Dolphin Jul 05 '23 at 21:16
  • There are other approaches as well; you can set environment variables direct in VS Code, or you can set them through launchd configuration. – Charles Duffy Jul 05 '23 at 21:17
  • I see you've edited the question to include the `export` in `.zshrc`. If it's really there then you should not need a further export, but in the original question the variable was only defined in `.zshrc`, so it's a shell variable and only visible to the shell itself. It's only visible to child processes when exported, as I explained. – ekalin Jul 06 '23 at 12:18

2 Answers2

1

This is more a shell question than a Rust question, but the variable must be exported to be seen in child processes.

So run

export ALT_DATABASE_URL

before running your program.

You can also set and export at the same time:

export ALT_DATABASE_URL=whatever

Please see this question for more details on non-exported (shell) and exported (environment) variables, personally I find this answer the best one.

ekalin
  • 872
  • 7
  • 19
  • If this is the cause, then why does it show up in `env|grep "DATABASE_URL"`? – PitaJ Jul 05 '23 at 17:03
  • can you explain the reason more detail? why should I do like that. why need to reexport the environment variable before read it in rust? – Dolphin Jul 05 '23 at 17:19
  • 1
    If this is the issue, the question should arguably be marked as a duplicate of [Difference between shell and environment variables](https://stackoverflow.com/questions/3341372/difference-between-shell-and-environment-variables) – Charles Duffy Jul 05 '23 at 21:11
0

Finally I found a way to manage the rust environment varialbe, on the visual studio code launch.json, add the envFile configuration:

"envFile": "${workspaceFolder}/.env"

then in the project folder add an .env file and add the environment key value into .env like this:

KEY1=VALUE1

also you can choose the dotenv package but I found this repo stop maintain for 3 years, not recommend.

Dolphin
  • 29,069
  • 61
  • 260
  • 539