dotenv icon indicating copy to clipboard operation
dotenv copied to clipboard

Handle parentheses in variables in commands

Open i7an opened this issue 1 year ago • 4 comments

Variables with parentheses are not handled correctly in commands:

FOO='passwo(rd'
BAR=$(echo '$FOO')

Result:

expected: {"BAR"=>"passwo(rd",           "FOO"=>"passwo(rd"}
     got: {"BAR"=>"$(echo 'passwo(rd')", "FOO"=>"passwo(rd"}

The solution I suggest is to parse commands first and expand variables before executing commands.

Dependencies

This change will break kamal which is included in Rails by default. This is how I want to handle it:

  • Get an approval for the dotenv patch but not merge it.
  • Deliver backward compatible fix to kamal https://github.com/basecamp/kamal/pull/1346
  • Merge this PR once the patched version of kamal is released.

i7an avatar Jan 08 '25 16:01 i7an

Will this also fix #530 (improperly expanding double-quotes in command variables)? This makes Kamal only able to use JSON secrets when its double-quotes are backslashed. I see you escaped quotes and braces in your secrets example. Shouldn't neither be necessary, so the secrets can be normal JSON?

mrj avatar Apr 01 '25 18:04 mrj

@mrj Unfortunately this PR does not fix the issue you reported.

i7an avatar Apr 08 '25 09:04 i7an

@i7an thanks for reporting this and coming up with a fix. I'm good with going forward with this, let me know when you're ready.

Can you also add a test case that verifies that command => variable => command works?

FOO=$(echo bar)
BAR-$(echo $FOO)

Should equal

FOO=bar
BAR=bar

:turtle: :turtle: :turtle:

bkeepers avatar Apr 08 '25 22:04 bkeepers

@bkeepers this PR is ready to be merged. Context. Kamal released a new version that includes the backward compatibility fix which this PR was waiting for.

i7an avatar May 19 '25 11:05 i7an

@bkeepers ping

i7an avatar Jul 29 '25 06:07 i7an

@bkeepers ping ping

i7an avatar Sep 21 '25 19:09 i7an