@@ -68,6 +68,8 @@ def __init__(self, message):
6868collected_tests_so_far = []
6969TEST_RUN_PIPE = os .getenv ("TEST_RUN_PIPE" )
7070SYMLINK_PATH = None
71+ # Get variable set in the `run_pytest_script.py` for the parent process to check if its forked
72+ ROOT_PROCESS_PID : int = os .getenv ("PROCESS_ID" )
7173
7274
7375def pytest_load_initial_conftests (early_config , parser , args ): # noqa: ARG001
@@ -92,6 +94,24 @@ def pytest_load_initial_conftests(early_config, parser, args): # noqa: ARG001
9294 global IS_DISCOVERY
9395 IS_DISCOVERY = True
9496
97+ global ROOT_PROCESS_PID
98+ print (early_config .pluginmanager .list_name_plugin ())
99+ if early_config .pluginmanager .has_plugin ("pytest_forked" ):
100+ if ROOT_PROCESS_PID and int (os .getpid ()) == int (ROOT_PROCESS_PID ):
101+ print (
102+ "Plugin info[vscode-pytest]: Forked plugin detected but NOT running in forked process."
103+ )
104+ elif ROOT_PROCESS_PID :
105+ print (
106+ f"Plugin info[vscode-pytest]: Forked plugin detected and running in forked process. Root PID: { ROOT_PROCESS_PID } , Current PID: { os .getpid ()} "
107+ )
108+ else :
109+ print ("Plugin info[vscode-pytest]: No root PID defined, setting to 0." )
110+ ROOT_PROCESS_PID = 0
111+ else :
112+ # If the plugin is not detected, then the root process is the current process.
113+ ROOT_PROCESS_PID = 0
114+
95115 # check if --rootdir is in the args
96116 for arg in args :
97117 if "--rootdir=" in arg :
@@ -858,12 +878,19 @@ def send_execution_message(
858878 status (Literal["success", "error"]): Execution status indicating success or error.
859879 tests (Union[testRunResultDict, None]): Test run results, if available.
860880 """
861- payload : ExecutionPayloadDict = ExecutionPayloadDict (
862- cwd = cwd , status = status , result = tests , not_found = None , error = None
863- )
864- if ERRORS :
865- payload ["error" ] = ERRORS
866- send_message (payload )
881+ current_pid = os .getpid ()
882+ # if ROOT_PROCESS_PID is 0 then forked plugin is not enabled
883+ if ROOT_PROCESS_PID != 0 and int (current_pid ) != int (ROOT_PROCESS_PID ):
884+ print ("PID NOT equal to the root proccess, no return" , current_pid , ROOT_PROCESS_PID )
885+ return
886+ else :
887+ print ("PID equal to the root of forked proccess" , current_pid , ROOT_PROCESS_PID )
888+ payload : ExecutionPayloadDict = ExecutionPayloadDict (
889+ cwd = cwd , status = status , result = tests , not_found = None , error = None
890+ )
891+ if ERRORS :
892+ payload ["error" ] = ERRORS
893+ send_message (payload )
867894
868895
869896def send_discovery_message (cwd : str , session_node : TestNode ) -> None :
0 commit comments