Skip to content

Commit

Permalink
printEnvVariablesToFile.py overwrites itself if invoked without argum…
Browse files Browse the repository at this point in the history
…ents (microsoft#24487)

I saw the command in my terminal when launching the editor, was curious
to see what it did, so I ran it again without arguments, expecting to
get a `--help`-like message, instead it ended up overwriting itself.

This change changes the script's default behavior (default being the
invocation without arguments) from overwriting itself to throwing an
exception about a missing output file argument.
  • Loading branch information
joar authored Nov 26, 2024
1 parent dba0a4c commit 3f58831
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions python_files/printEnvVariablesToFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import os
import sys

# Last argument is the target file into which we'll write the env variables line by line.
output_file = sys.argv[-1]
# Prevent overwriting itself, since sys.argv[0] is the path to this file
if len(sys.argv) > 1:
# Last argument is the target file into which we'll write the env variables line by line.
output_file = sys.argv[-1]
else:
raise ValueError("Missing output file argument")

with open(output_file, "w") as outfile: # noqa: PTH123
for key, val in os.environ.items():
Expand Down

0 comments on commit 3f58831

Please sign in to comment.