Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unquoted .env vars with spaces not parsed correctly #2610

Closed
2 tasks done
devanoneth opened this issue Aug 4, 2022 · 6 comments
Closed
2 tasks done

unquoted .env vars with spaces not parsed correctly #2610

devanoneth opened this issue Aug 4, 2022 · 6 comments
Assignees
Labels
C-forge Command: forge Cmd-forge-script Command: forge script T-bug Type: bug
Milestone

Comments

@devanoneth
Copy link
Contributor

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

9b0575e 2022-08-04T00:10:43.515642Z

What command(s) is the bug in?

forge script

Operating System

macOS (Intel)

Describe the bug

I have a .env file in the root of my project, and it contains RPC_URL=XXX.

When running vm.createSelectFork(vm.envString("RPC_URL")); in a script, I get the error:

VM::envString("RPC_URL")
    │   └─ ← "environment variable not found"

I expected that this would work. Is this not the expected use?

Of course before Foundry shipped dotenv support, I was using my solenv library and it worked fine.

Thanks!

@devanoneth devanoneth added the T-bug Type: bug label Aug 4, 2022
@onbjerg
Copy link
Member

onbjerg commented Aug 4, 2022

Where are you running forge script? I can't reproduce it: this works fine for me.

FOO=bar

in .env in root of project

forge script script/Contract.s.sol

executes fine and it contains this line:

vm.envString("FOO")

Edit: Also added a contract with a function that takes the string and returns it back, and it works as expected:

image

@onbjerg onbjerg self-assigned this Aug 4, 2022
@onbjerg onbjerg added C-forge Command: forge Cmd-forge-script Command: forge script labels Aug 4, 2022
@onbjerg onbjerg added this to the v1.0.0 milestone Aug 4, 2022
@devanoneth
Copy link
Contributor Author

devanoneth commented Aug 4, 2022

Okay, I debugged it a bit more and found out it's because I had a variable set above RPC_URL which has spaces in it. I realized variables with spaces are not parsed correctly, they need to be wrapped in quotes.

FYI, in the very commonly used dotenv package for NodeJS, it parses anything after the = until a line break. I guess the dotenv rust crate parses it differently. I just wouldn't be surprised if this stumps other developers who are so used to the JS ecosystem around Ethereum. Maybe worth adding an example .env to the book or template?

@onbjerg
Copy link
Member

onbjerg commented Aug 4, 2022

Wow, that's very surprising to me. I think I should just look for another .env crate because that is part of the actual "spec" and every other .env package supports that

Thanks for debugging :)

@onbjerg onbjerg changed the title dotenv variables not accessible in forge script unquoted .env vars with spaces not parsed correctly Aug 4, 2022
@Genysys
Copy link
Contributor

Genysys commented Aug 4, 2022

I think we solved something similar in the telegram chat by adding an export to env vars .i.e export FOO=BAR

https://t.me/foundry_support/19661

@onbjerg
Copy link
Member

onbjerg commented Aug 4, 2022

@Genysys This was before .env was supported - now .env autoloads and works w/o having to source it manually 😄

@onbjerg
Copy link
Member

onbjerg commented Aug 11, 2022

Closing as a wontfix since there are no .env crates that support this currently, will revisit if one pops up, otherwise we are just going to document this behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-forge Command: forge Cmd-forge-script Command: forge script T-bug Type: bug
Projects
Archived in project
Development

No branches or pull requests

3 participants