From 4091ef0a678d7ff8a3309cf95fe8ca5a23ea9593 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Mon, 21 Sep 2020 12:46:37 -0700 Subject: [PATCH 1/3] Remove cwd while running using isolate script --- news/2 Fixes/13942.md | 1 + pythonFiles/pyvsc-run-isolated.py | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 news/2 Fixes/13942.md diff --git a/news/2 Fixes/13942.md b/news/2 Fixes/13942.md new file mode 100644 index 000000000000..cfed3d1937a1 --- /dev/null +++ b/news/2 Fixes/13942.md @@ -0,0 +1 @@ +Fix isolate script to only remove current working directory. diff --git a/pythonFiles/pyvsc-run-isolated.py b/pythonFiles/pyvsc-run-isolated.py index 1f6770787490..dfc17c9af31f 100644 --- a/pythonFiles/pyvsc-run-isolated.py +++ b/pythonFiles/pyvsc-run-isolated.py @@ -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) del sys.argv[0] module = sys.argv[0] if module == "-c": From 1df12164b1c069de9fff8c3d39de8c76d9eb102d Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Mon, 21 Sep 2020 15:38:16 -0700 Subject: [PATCH 2/3] Run inplace. --- pythonFiles/pyvsc-run-isolated.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonFiles/pyvsc-run-isolated.py b/pythonFiles/pyvsc-run-isolated.py index dfc17c9af31f..f7064b954386 100644 --- a/pythonFiles/pyvsc-run-isolated.py +++ b/pythonFiles/pyvsc-run-isolated.py @@ -16,7 +16,7 @@ def normalize(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) +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": From d1e6e2ed938411998e5abf0f142c714f638d5ccf Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Mon, 21 Sep 2020 17:46:28 -0700 Subject: [PATCH 3/3] Fix formatting --- pythonFiles/pyvsc-run-isolated.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pythonFiles/pyvsc-run-isolated.py b/pythonFiles/pyvsc-run-isolated.py index f7064b954386..92447e1fc5ff 100644 --- a/pythonFiles/pyvsc-run-isolated.py +++ b/pythonFiles/pyvsc-run-isolated.py @@ -13,10 +13,11 @@ 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[:] = (p for p in sys.path if p != '' and normalize(p) != cwd) +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":