Let python-dotenv be the only reader of .env - #21
Merged
Conversation
The file was parsed twice with different rules: by systemd via EnvironmentFile and by python-dotenv via load_dotenv. They disagree about quoting, inline comments, and values containing # or spaces. The live host logged systemd rejecting a line the bot read fine, on every start. The failure mode this sets up is nasty: a token containing a # would be truncated by one parser and not the other, and the result looks like a bad credential rather than a parsing problem. Drop EnvironmentFile from the unit and leave python-dotenv in charge. Anchor the path to the project root instead of searching upward from the working directory, so the same file is used whether the bot is started by systemd, from a shell anywhere, or by the test runner. Document the syntax rules in .env.example, including that trailing comments after a value are not stripped. Closes #19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #19
The problem
.envwas parsed by two things with different rules:EnvironmentFile=in theloopify-botunitload_dotenv()inconfig.pyThey disagree about quoting, inline comments, and values containing
#, spaces or=.The live host logs the disagreement on every single start:
Harmless in that instance — the line is not a required variable — but it is the same file diverging between the two readers, in production, right now.
Why it matters
A token containing a
#gets truncated by one parser and not the other. The bot then fails to authenticate, and the symptom looks like a bad credential rather than a parsing problem — which is a genuinely unpleasant thing to chase, especially since you cannot paste the token anywhere to compare.The fix
python-dotenvowns the file.EnvironmentFile=is dropped from the unit, with a comment saying why so nobody adds it back as an obvious-looking improvement.The path is also anchored to the project root rather than left to search upward from the working directory:
Otherwise the bot picks up a different file depending on where it was started from — systemd sets
WorkingDirectory, but a shell invocation from anywhere else does not..env.examplenow states the rules, including the one that actually bites people: a trailing comment after a value is part of the value.Nothing is lost by removing it
load_dotenv()writes intoos.environ, so subprocesses (yt-dlp) inherit the same variables they did before. The unit's ownEnvironment=lines forXDG_CACHE_HOMEandDENO_DIRare unaffected, andload_dotenvdoes not override already-set variables, so they still win.Verification
232 passed. Two new tests, both non-vacuous:.envresolution is anchored to the project root.deploy/setup.shdeclares no activeEnvironmentFileline. The check filters out commented lines, so the explanatory comment does not make it pass trivially — re-adding the directive fails the suite.To confirm after deploy, the warning should be gone from a fresh start: