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

Remove cwd while running using isolate script #14014

Merged
merged 3 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2 Fixes/13942.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix isolate script to only remove current working directory.
13 changes: 9 additions & 4 deletions pythonFiles/pyvsc-run-isolated.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
if __name__ != "__main__":
raise Exception("{} cannot be imported".format(__name__))

import os
import os.path
import runpy
import sys

# We "isolate" the script/module (sys.argv[1]) by
# replacing sys.path[0] with a dummy path and then sending the target
# on to runpy.
sys.path[0] = os.path.join(os.path.dirname(__file__), ".does-not-exist")

def normalize(path):
return os.path.normcase(os.path.normpath(path))

# We "isolate" the script/module (sys.argv[1]) by removing current working
# directory or '' in sys.path and then sending the target on to runpy.
cwd = normalize(os.getcwd())
sys.path = list(p for p in sys.path if p != '' and normalize(p) != cwd)
ericsnowcurrently marked this conversation as resolved.
Show resolved Hide resolved
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a bad idea to replace the entire list object, in case anything stashes away a reference. Better to replace the elements in-place:

sys.path[:] = (p for p in sys.path if p != '' and normalize(p) != cwd)

del sys.argv[0]
module = sys.argv[0]
if module == "-c":
Expand Down