Only subset of shell functionality is supported
graywolf-at-work opened this issue · comments
Steps to reproduce
I've run into an issue today, when under impression that dotenv (since it
states that the file can be sources into shell) supports basically POSIX shell.
However, that was actually not so. Main issue is not necessarily the lack of the
functionality (it is and I had to stop using dotenv in our CI), but that the
unsupported syntax is just silently ignored.
+$ ls
env Gemfile Gemfile.lock test.rb test.sh
+$ cat env
X=${Y-Z}
export X
+$ env | grep ^Y=
+$ cat Gemfile
source 'https://rubygems.org' do
gem 'dotenv'
end
+$ cat Gemfile.lock
GEM
specs:
GEM
remote: https://rubygems.org/
specs:
dotenv (2.7.6)
PLATFORMS
x86_64-linux
DEPENDENCIES
dotenv!
BUNDLED WITH
2.2.16
+$ cat test.rb
#!/usr/bin/env ruby
# frozen_string_literal: true
$-v = true
require('dotenv')
Dotenv.load('env')
pp(ENV['X'])
+$ cat test.sh
#!/bin/sh
. ./env
echo $X
+$ bundle exec ./test.rb
"-Z}"
+$ ./test.sh
Z
Assuming we would be willing to invoke actual shell, I believe it is possible to
support full POSIX range of features. Assuming that would be accepted, I'm
willing to work on such addition. Would it be merged?
Expected behavior
+$ bundle exec ./test.rb
"Z"
Actual behavior
+$ bundle exec ./test.rb
"-Z}"
System configuration
dotenv version:
2.7.6
Rails version:
N/A
Ruby version:
2.7.2
@graywolf-at-work thanks for reporting the issue. You're right that dotenv only supports a subset of POSIX shell functionality. Most of it has been added over time as people needed different features. It is definitely a bug that unsupported syntax is silently ignored.
I would gladly consider a PR that invokes shell, as long as the current tests continue to pass, and it can be implemented in a way that is supported across all platforms (or at least falls back to current implementation on unsupported platforms).
I've got ack from my management so I'll will try to look into this next week.