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

Activation issue: activation incorrectly using cmd not bat #23037

Closed
MatejVitek opened this issue Mar 7, 2024 · 39 comments
Closed

Activation issue: activation incorrectly using cmd not bat #23037

MatejVitek opened this issue Mar 7, 2024 · 39 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster needs PR Ready to be worked on

Comments

@MatejVitek
Copy link

MatejVitek commented Mar 7, 2024

Uploading Python.log…
Type: Bug

Behaviour

Expected vs. Actual

Expected: I can code without interruptions.
Actual: Command prompt windows keep popping up, stealing focus from VSCode and making it difficult to write code.

Steps to reproduce:

I'm not certain how to reproduce the problem in general, but I've narrowed it down to the use of Pytest. After a clean reinstall, the issue wasn't present until I enabled Pytest in VSCode's Testing tab.

Studying the Python log in the Output tab, I noticed that whenever the window pops up, the following appears in the log:

2024-03-07 14:39:05.892 [info] Discover tests for workspace name: EyeZ - uri: c:\Users\matej\Documents\Programs\Python\EyeZ\eyez\data\sets\split.py
2024-03-07 14:39:05.898 [info] > conda run -n torch --no-capture-output python ~\.vscode\extensions\ms-python.python-2024.2.1\pythonFiles\get_output_via_markers.py ~\.vscode\extensions\ms-python.python-2024.2.1\pythonFiles\testing_tools\run_adapter.py discover pytest -- --rootdir ~\Documents\Programs\Python\EyeZ -s --cache-clear tests
2024-03-07 14:39:05.898 [info] cwd: ~\Documents\Programs\Python\EyeZ

Trying to exectue the above conda run command manually gives me an error, since ~ is not a thing in powershell or command prompt.

This issue only started appearing in the last month or two, prior to that I could use Pytest on the same machine with no issues.

Diagnostic data

  • Python version (& distribution if applicable, e.g. Anaconda): 3.11.5
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Conda
  • Value of the python.languageServer setting: Pylance
Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

Python log attached, since it was too long to copy here.

User Settings

Multiroot scenario, following user settings may not apply:

languageServer: "Pylance"

testing
• pytestArgs: "<placeholder>"
• pytestEnabled: true

Extension version: 2024.2.1
VS Code version: Code 1.87.0 (019f4d1419fbc8219a181fab7892ebccf7ee29a2, 2024-02-27T23:41:44.469Z)
OS version: Windows_NT x64 10.0.19045
Modes:

System Info
Item Value
CPUs AMD Ryzen 7 2700X Eight-Core Processor (16 x 3700)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
Load (avg) undefined
Memory (System) 31.95GB (19.90GB free)
Process Argv
Screen Reader no
VM 0%
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Mar 7, 2024
@mlamarre
Copy link

I have the same issue with test discovery triggering a pop-up. Corresponds to the log below in the Output->Python.

2024-03-11 14:49:00.483 [info] Discover tests for workspace name: [***removed path***]
2024-03-11 14:49:00.483 [info] Running discovery for unittest using the new test adapter.
2024-03-11 14:49:00.488 [info] Discovering unittest tests for workspace [***removed path*** with arguments: [***removed path***\.vscode\extensions\ms-python.python-2024.2.1\pythonFiles\unittestadapter\discovery.py,--udiscovery,-v,-s,./tests,-p,test*.py

2024-03-11 14:49:00.489 [info] > D:\miniforge\Scripts\conda.exe run -n [removed name] --no-capture-output python ~\.vscode\extensions\ms-python.python-2024.2.1\pythonFiles\get_output_via_markers.py ~\.vscode\extensions\ms-python.python-2024.2.1\pythonFiles\unittestadapter\discovery.py --udiscovery -v -s ./tests -p test*.py
2024-03-11 14:49:00.489 [info] cwd: .
2024-03-11 14:49:01.644 [info] >>>PYTHON-EXEC-OUTPUT

2024-03-11 14:49:05.390 [info] Test server connected to a client.
2024-03-11 14:49:05.395 [info] Test server connected to a client.
2024-03-11 14:49:05.395 [info] <<<PYTHON-EXEC-OUTPUT

2024-03-11 14:49:05.396 [info] ResultResolver EOT received for discovery.

@eleanorjboyd
Copy link
Member

Hi! Could you provide a screenshot of this popup you are referencing? I am not sure exactly what you are talking about. Thanks!

@eleanorjboyd eleanorjboyd added the info-needed Issue requires more information from poster label Mar 27, 2024
@MatejVitek
Copy link
Author

Kinda hard to get a screenshot since the popup is so short. Here's a video showing the issue.

Untitled.video.-.Made.with.Clipchamp.mp4

In the video I change the code and save the file (CTRL + S) to trigger a test rediscovery.

The popup doesn't appear in other workspaces, only Python workspaces with testing set up.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Mar 28, 2024
@eleanorjboyd
Copy link
Member

thanks for the video, that is very helpful! I have never seen this issue before- let me talk with some colleagues and circle back.

@eleanorjboyd
Copy link
Member

one quick question: do you have any workspace or user settings having to do with saving like "python.workspaceSymbols.rebuildOnFileSave" or "python.testing.autoTestDiscoverOnSaveEnabled" ? Curious if turning either of these to false changes the behavior

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Mar 28, 2024
@MatejVitek
Copy link
Author

I didn't have either of those two in my settings files (attaching here both my user settings.json
and the settings.json in the .vscode folder in the workspace). My .code-workspace files only contain folder paths and launch configurations, no extra settings.

I tried adding the two settings and changing their values, but it didn't seem to have any effect. The test discovery still runs and triggers a popup, regardless of the two settings.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Mar 28, 2024
@eleanorjboyd
Copy link
Member

@karthiknadig any ideas here? I am not seeing any settings that I think could trigger this

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Mar 29, 2024
@karthiknadig
Copy link
Member

karthiknadig commented Mar 29, 2024

Do we use shell execute when running tests? If that is yes, then command prompt initialization script that sets up conda or any other thing might be a cause of this. Each time we run something using shell execute, it will attempt to run initialization. I would check the init script that conda init adds first.

@eleanorjboyd
Copy link
Member

we spawn with child_process so it might apply, @MatejVitek could you check on your conda init script?

You could also try creating a venv without conda and running tests to see if conda is a common factor in the popup occurring which would give us more direction. Thanks

@karthiknadig
Copy link
Member

@MatejVitek You might need to check the file that conda init updates when you run in from cmd. This is what allows conda to add base env into the cmd path.

@MatejVitek
Copy link
Author

MatejVitek commented Apr 2, 2024

I have checked some more and it turns out any conda run in command prompt causes a popup on my system, so it's likely some issue with my conda/mamba installation. The reason I didn't notice this before is that I usually use Powershell, where things work normally. In VS Code I also have Powershell set as the default shell and seemingly only the testing runs in command prompt. As a temporary workaround, is there a way to set Python testing to use the same shell that's used elsewhere (in my case Powershell)?

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Apr 2, 2024
@eleanorjboyd
Copy link
Member

@karthiknadig how do we set what shell is used when we run a subprocess?

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Apr 16, 2024
@MatejVitek
Copy link
Author

MatejVitek commented Apr 16, 2024

As a note, other users seem to be experiencing similar issues with cmd popups being triggered by conda installations on Windows. For example, see this issue in another repo. Also, I found this older issue that seems to describe a similar problem with cmd triggering a popup, and the use of Powershell resolving the issue. It may be worth giving the user the option to choose the shell that's used (if not already the case) or simply use the same shell that's used elsewhere.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Apr 16, 2024
@karthiknadig
Copy link
Member

@eleanorjboyd One thing we can try is to pass process creation flags to the runner. Try setting CREATE_NO_WINDOW on windows. Typically when we run something to be background it should not be creating any windows.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Apr 18, 2024
@tonibofarull
Copy link

tonibofarull commented Apr 22, 2024

I see the popup when "python.testing.pytestEnabled": true in Ubuntu.

@eleanorjboyd
Copy link
Member

Hi @tonibofarull- did you check your conda init script? Or do you have any settings which could be related? Please send your settings over if you are still experiencing issues and also your logs from the python output channel.

@karthiknadig
Copy link
Member

I just want to clarify, this has nothing to do with python extension itself. This is an issue with conda and its init script. Note that even if you set to use a different shell with VS Code, if you have services that rely on the faulty shell init script it can cause issues on your system. I recommend looking at the init scrips for various cases and see if something is causing this.

The cause of this seems to be ContinuumIO/anaconda-issues#10593

@eleanorjboyd The CREATE_NO_WINDOW recommendation is only for windows, and I don't know if there is a similar option for linux/mac

@tonibofarull
Copy link

In my case this is happening with virtualenv. Not conda! I'll share the logs tomorrow since now I'm not on my PC.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Apr 22, 2024
@eleanorjboyd
Copy link
Member

Great thanks!

@tonibofarull
Copy link

Now I understand my issue.

To reproduce,

pip install kivymd kivy

and create test_a.py under ./tests,

from kivymd.app import MDApp

def test_a():
    pass

VSCode logs,

2024-04-23 09:07:10.767 [info] Discover tests for workspace name: garbage - uri: /home/tonibc/repos/b/garbage/tests/test_a.py
2024-04-23 09:07:10.767 [info] Running discovery for pytest using the new test adapter.
2024-04-23 09:07:10.768 [info] All environment variables set for pytest discovery for workspace /home/tonibc/repos/b/garbage: ...

2024-04-23 09:07:10.777 [info] > ./.venv/bin/python -m pytest -p vscode_pytest --collect-only tests
2024-04-23 09:07:10.777 [info] cwd: .
2024-04-23 09:07:10.852 [info] ============================= test session starts ==============================
platform linux -- Python 3.10.12, pytest-8.1.1, pluggy-1.5.0
rootdir: /home/tonibc/repos/b/garbage

2024-04-23 09:07:11.052 [info] collected 1 item

<Dir garbage>
  <Dir tests>
    <Module test_a.py>
      <Function test_a>

2024-04-23 09:07:11.052 [info] Test server connected to a client.
2024-04-23 09:07:11.052 [info] 

2024-04-23 09:07:11.053 [info] ResultResolver EOT received for discovery.
2024-04-23 09:07:11.053 [info] ========================== 1 test collected in 0.20s ===========================

2024-04-23 09:07:11.076 [info] Disposing data receiver for /home/tonibc/repos/b/garbage and deleting UUID; pytest discovery.

It seems to me that the option "python.testing.pytestEnabled": true makes the tests to be imported at each CTRL+S and kivy is the one popping up the terminal.

@eleanorjboyd
Copy link
Member

eleanorjboyd commented Apr 23, 2024

you can also turn off test discovery on save which might fix the pop happening on save and instead you can trigger it yourself when you want to execute test discovery. The setting to do so is: "python.testing.autoTestDiscoverOnSaveEnabled": false

@tonibofarull
Copy link

Oh thanks!

@FrederickGeek8
Copy link

FrederickGeek8 commented Apr 24, 2024

I have isolated the issue and I think I have a solution (maybe) -- but I don't know how to patch it in VS Code without getting my hands dirty. TLDR: VS Code is calling a bash script instead of the .bat script for activation on Windows.

I personally noticed that the cmd.exe prompt appears in VS Code when in the Python "Output" tab the following is run:

~/miniforge3/Scripts/activate ; conda activate science ; echo '<some string>' . python ~/.vscode/extensions/ms-python.python-2024.4.1/python_files/printEnvVariables.py

When I run this command in my Powershell prompt, the cmd.exe window appears. On the other hand if I run the same with activate replaced with activate.bat as that comment suggested in my Powershell prompt I do not get such a popup.

Prior Art

This is pointed out in #20836 comment which is linked above in this thread:

Can confirm that the new CMD window is caused by D:/DevEnv/miniconda3/Scripts/activate, but i also noticed that there's a activate.bat under ${CONDA_ROOT}/Scripts/, and there's no extra window when executing it. Would it be better to use .bat to activate?

There was the response:

.bat scripts are used on command prompt, whereas .Ps1 scripts are for powershell, so unfortunately we cannot use that. All this code will be gone with #11039 anyways, hence closing this for now.

which I cannot validate the claims of, since the .bat command works fine in my Powershell prompt.

What is activate vs activate.bat

Looking at my install of Miniforge3, activate is a bash script whereas activate.bat is a .bat script.

More of My Background

This issue is no localized to Pytest for me. This occurs whenever I launch VS Code and I have a Python file open. In this case I just had a Jupyter Notebook window open.

I haven't used Windows VS Code in a while (normally I'm in Linux), but I've seen this happen with coworkers when I help them set up miniforge. Now that I had to use VS Code on Windows today, it is quite frustrating to have these popups happen, especially because it kicks me out of VS Code and into my Windows Terminal (which has Powershell open) despite immediately closing the cmd.exe window.

I've seen this attributed to conda run, but using that in my Powershell prompt does not popup cmd.exe. On the other hand, running activate instead of activate.bat does.

If it helps, on my laptop I appear to have used the 23.11.0.0 version of Miniforge3.

EDIT: The Issue I tried to link (#20836) got autocompleted to a separate ticket. Oops..

@eleanorjboyd
Copy link
Member

@karthiknadig, could you review the comment above and let me know your thoughts? Thanks

@github-actions github-actions bot added the info-needed Issue requires more information from poster label May 6, 2024
@eleanorjboyd
Copy link
Member

Hi @MatejVitek, thank you so much for this in-depth investigation! You are totally correct and this is a bug that will need to be resolved. Updating the title to reflect the specific bug and adding the correct labels now. Thanks

@eleanorjboyd eleanorjboyd changed the title VSCode with Pytest keeps popping up command prompt windows Activation issue: activation incorrectly using cmd not bat May 7, 2024
@eleanorjboyd eleanorjboyd removed their assignment May 7, 2024
@eleanorjboyd eleanorjboyd added bug Issue identified by VS Code Team member as probable bug needs PR Ready to be worked on and removed triage-needed Needs assignment to the proper sub-team labels May 7, 2024
@machsix
Copy link

machsix commented Jun 3, 2024

Any update on this? I'm also running Miniforge3 on windows and I didn't create second conda environment. The following command will trigger a command prompt

conda run -n base --no-capture-output python ~\.vscode\extensions\ms-python.python-2024.6.0\python_files\get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2024.6.0/python_files/printEnvVariables.py

It's very annoying.

@karthiknadig
Copy link
Member

@machsix conda run does not directly activate (i.e, conda does the activation we don't pick the script). Does this happen when you run it outside of VS Code?

This issue is particularly about using the activate script for running project code. Here we are in some cases picking the wrong activate script. But conda run is a feature of conda where conda handles activation and running, so we are not involved in the activation. So this might be related to the ContinuumIO/anaconda-issues#10593 issue on conda.

@machsix
Copy link

machsix commented Jun 3, 2024

Yes,

@machsix conda run does not directly activate (i.e, conda does the activation we don't pick the script). Does this happen when you run it outside of VS Code?

This issue is particularly about using the activate script for running project code. Here we are in some cases picking the wrong activate script. But conda run is a feature of conda where conda handles activation and running, so we are not involved in the activation. So this might be related to the ContinuumIO/anaconda-issues#10593 issue on conda.

Yes, it occurs whether I run the command in powershell or command prompt. I think the issue you mentioned is a bug of Anaconda navigator but not conda?

@karthiknadig
Copy link
Member

If this occurs outside of VS Code with conda run then it should be raised on conda.

I think the issue you mentioned is a bug of Anaconda navigator but not conda?

I based it on the conda.py mentioned as the one causing this behavior mentioned in those issue comments.

@machsix
Copy link

machsix commented Jun 3, 2024

Ok, I went through some debug and found my issue is not related to either vscode-python or conda. Here is what happened in my case, I'm telling in the sequence of launching

  1. VS code: run conda run -n base --no-capture-output python ~\.vscode\extensions\ms-python.python-2024.6.0\python_files\get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2024.6.0/python_files/printEnvVariables.py
  2. The script from conda https://github.com/conda/conda/blob/main/conda/cli/main_run.py generate a temporary batch script like the following to activate the environment and run the command
@ECHO OFF
@SET PYTHONIOENCODING=utf-8
@SET PYTHONUTF8=1
@FOR /F "tokens=2 delims=:." %%A in ('chcp') do for %%B in (%%A) do set "_CONDA_OLD_CHCP=%%B"
@chcp 65001 > NUL
@CALL "C:\Python\miniforge3\condabin\conda.bat" activate "C:\Python\miniforge3"
@IF %ERRORLEVEL% NEQ 0 EXIT /b %ERRORLEVEL%
@python D:\Temp\test.py
@IF %ERRORLEVEL% NEQ 0 EXIT /b %ERRORLEVEL%
@chcp %_CONDA_OLD_CHCP%>NUL
  1. The activation script @CALL "C:\Python\miniforge3\condabin\conda.bat" activate "C:\Python\miniforge3", source code at https://github.com/conda/conda/blob/main/conda/shell/condabin/_conda_activate.bat create another temporary script named %_TEMP_SCRIPT_PATH%
  2. In my case, the script %_TEMP_SCRIPT_PATH% has the following content
@SET "PATH=xxxxx"
@SET "CONDA_PREFIX=C:\Python\miniforge3"
@SET "CONDA_SHLVL=1"
@SET "CONDA_DEFAULT_ENV=base"
@SET "CONDA_PROMPT_MODIFIER=(base) "
@SET "CONDA_EXE=C:\Python\miniforge3\Scripts\conda.exe"
@SET "_CE_M="
@SET "_CE_CONDA="
@SET "CONDA_PYTHON_EXE=C:\Python\miniforge3\python.exe"
@CALL "C:\Python\miniforge3\etc\conda\activate.d\khronos-opencl-icd-loader_activate.bat"
@CALL "C:\Python\miniforge3\etc\conda\activate.d\libxml2_activate.bat"
@CALL "C:\Python\miniforge3\etc\conda\activate.d\openssl_activate-win.bat"
@CALL "C:\Python\miniforge3\etc\conda\activate.d\proj4-activate.bat"

The line @CALL "C:\Python\miniforge3\etc\conda\activate.d\khronos-opencl-icd-loader_activate.bat" leads to the annoying prompt. I have no idea where this activation script is from ...

Update: I can confirm the commit conda-forge/khronos-opencl-icd-loader-feedstock@eb8e0d6 fixes my issue.

@inikishev
Copy link

inikishev commented Aug 27, 2024

Update: I can confirm the commit conda-forge/khronos-opencl-icd-loader-feedstock@eb8e0d6 fixes my issue.

hi! How do I install the update? Do I just change that bat file to the new version? I appear to have a lot of "activate.bat" files and "recipe" folders in my miniconda installation and not sure which one to change

@inikishev
Copy link

inikishev commented Aug 27, 2024

you can also turn off test discovery on save which might fix the pop happening on save and instead you can trigger it yourself when you want to execute test discovery. The setting to do so is: "python.testing.autoTestDiscoverOnSaveEnabled": false

I have disabled this setting but still getting the popups unfortunately.

It started happening when I selected >Python: Configure Tests. Unfortunately I do not know how to undo that. I am okay with running tests from cmd, but how do I disabled tests?

@karthiknadig
Copy link
Member

@inikishev You can set the unittest or pytest setting in your settings.json to false.

@inikishev
Copy link

@inikishev You can set the unittest or pytest setting in your settings.json to false.

hi, thanks for the reply! I didn't see anything related to unittest or pytest in my settings.json, however I found that the setting is called python.testing.unittestEnabled and python.testing.pytestEnabled there https://donjayamanne.github.io/pythonVSCodeDocs/docs/unittests_unittest-framework/, and was able to turn it off in the settings UI.

Copy link

github-actions bot commented Oct 6, 2024

Hey @karthiknadig, this issue might need further attention.

@MatejVitek, you can help us out by closing this issue if the problem no longer exists, or adding more information.

@MatejVitek
Copy link
Author

MatejVitek commented Oct 6, 2024

I have tried the fixes outlined in this thread, but the problem was not resolved for me. However, the comment I previously made no longer holds, as now I don't get any popup windows when running conda run in either command prompt or powershell, however I do get it if I run a conda run command in VS Code's terminal. As a temporary workaround, I have disabled test discovery on save (python.testing.autoTestDiscoverOnSaveEnabled), which, after a VS Code restart, stopped the popups and let me work.

Since I'm not having these issues on my (very similar) home installation, I intend to reinstall conda/mamba on my work PC in the future to see if that resolves the issue. However, I can't do that at the moment, as I have a very fragile conda environment set up for my work that will not be reproducible if I reinstall conda.

@eleanorjboyd
Copy link
Member

@MatejVitek sorry for the delay- any updates on your end or still seeing this problem?

@MatejVitek
Copy link
Author

@eleanorjboyd I have now fully reinstalled VSCode and Mamba, and I'm currently not seeing any popups, even with test auto-discovery turned on. I will close this thread for the time being and will reopen it if the issue reoccurs.

@eleanorjboyd
Copy link
Member

Sounds good! Let me know if anything changes- happy coding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster needs PR Ready to be worked on
Projects
None yet
Development

No branches or pull requests

8 participants