6

The piece of code is as follows:

// Log in to Discord using a bot token from the environment                  
let discord = Discord::from_bot_token(                                       
    &env::var("DISCORD_TOKEN").unwrap()                                      
).expect("login failed");

I get an error saying it is unable to find the environment variable DISCORD_TOKEN.

My environment does show the variable:

myawesomename$env | grep DISCORD
DISCORD_TOKEN=you'llneverknow

If I print all the keys and values that Rust knows:

for (key, value) in env::vars() {                                            
    println!("{}: {}", key, value);                                      
}

It doesn't show the environment variable.

On a similar note, when I do env | grep CARGO, none of the CARGO variables exist, but they are printed in the Rust code.

There is something I fundamentally doesn't understand about the profile/system env variables Rust is looking at (which, I assumed, are the variables in the environment in which the process is launched).

UPDATE: I don't know what I change, but it works now. I apologize for intruding on everyone's time. Thank you for helping me look into this though.

chr0nikler
  • 468
  • 4
  • 13
  • 1
    Just to be sure: `you'llneverknow` is just a replacement for this question? Are there any non-ASCII characters in the real bot token? – Lukas Kalbertodt Nov 15 '16 at 07:17
  • 1
    @LukasKalbertodt Are you saying there non-ASCII characters in you'llneverknow? – Bill Woodger Nov 15 '16 at 07:20
  • 1
    @BillWoodger No, that's fine. But if it's an replacement, the real token might contain non-ASCII chars. – Lukas Kalbertodt Nov 15 '16 at 07:23
  • 1
    @abcde13 Can you show the code which takes the snapshot? – Bill Woodger Nov 15 '16 at 07:24
  • 1
    @LukasKalbertodt Thanks, understand now. – Bill Woodger Nov 15 '16 at 07:25
  • 1
    Does it work if you run directly (target/debug/appname) instead of cargo run? – Chris Emerson Nov 15 '16 at 07:35
  • 3
    Do you run you project in the terminal where this is set? Did you try to run it like `DISCORD_TOKEN=yourtoken cargo run`? – VP. Nov 15 '16 at 10:40
  • 2
    *none of the `CARGO` variables exist, but they are printed in the Rust code* — That's because `cargo run` sets them. – Shepmaster Nov 15 '16 at 14:06
  • @LukasKalbertodt That's just a replacement string. But the real string is all ASCII characters. – chr0nikler Nov 15 '16 at 14:57
  • 1
    @ChrisEmerson You are right. It does work if I run it from target/debug/appname – chr0nikler Nov 15 '16 at 14:59
  • @VictorPolevoy I've run it in the terminal where, just before, I source my bashrc and grep to make sure that environment variable exists. – chr0nikler Nov 15 '16 at 14:59
  • 1
    `echo 'fn main() { println!("{:?}", ::std::env::var("FOO")); }' > src/main.rs && FOO=bar cargo run` results in `Ok("bar")`. Cargo shouldn't be removing any environment variables. – Shepmaster Nov 15 '16 at 15:03
  • @Shepmaster your code returned `Ok("bar")`. So how does that reflect what I should do in my code? And why does running the actual executable in the target dir work? – chr0nikler Nov 16 '16 at 03:09
  • @abcde13 are you saying that if you use your environment variable with my code, suitably modified, you get the value of your key? You really need to produce a [mcve] to narrow down your problem. – Shepmaster Nov 16 '16 at 03:11
  • @Shepmaster yes. If I run your code as main.rs with `"DISCORD_TOKEN"` as the key and remove the part FOO=bar, it gets the token. And I concur. I will get a testcase up in a sec. – chr0nikler Nov 16 '16 at 03:17
  • And I don't know what happened. I reran it just now, and it worked. Opened and closed the terminal, and it still worked. Sorry for wasting everyone's time. Gonna upvote you all for helping regardless. – chr0nikler Nov 16 '16 at 03:22
  • I think it is better to delete this question because there was not a real problem, so it will not be helpful for others who read it. – VP. Nov 16 '16 at 07:49

1 Answers1

1

Undeleted this question because I got the answer. But it's because I didn't examine all the factors before asking the question.

It worked when I ran cargo run, but didn't work when I ran sudo cargo run. I was running it in sudo because I was trying to read memory of another process. The sudo profile has it's own set of env vars, and it resets the environment before going sudo.

To fix this, I ran sudo visudo and inserted this line

Defaults env_keep += "DISCORD_TOKEN'

From there, it worked.

This link got me the answer.

Community
  • 1
  • 1
chr0nikler
  • 468
  • 4
  • 13