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

Fix asynchronous environment discovery and selection #7489

Merged
merged 7 commits into from
Apr 25, 2023

Conversation

AdamYoblick
Copy link
Member

@AdamYoblick AdamYoblick commented Mar 30, 2023

Fixes #7444

There are some bugs around environment loading and selection when the environments are discovered asynchronously, like with conda environments. There are lots of edge cases that need to be tested. See comments below for each scenario.

Specifically, conda environments weren't being shown in solution explorer because the currently selected environment was the global default, and the existing conditional was not behaving properly in that case. We were also showing an infobar for "missing conda environment" before they had a chance to be discovered.

@AdamYoblick
Copy link
Member Author

AdamYoblick commented Mar 30, 2023

Case 1 - Conda environment found

To repro:

  1. Install miniconda
  2. Create a new python application
  3. Add a new conda environment to the project (If you see an error about openssl missing, that's a conda bug. See CondaSSLError: OpenSSL appears to be unavailable on this machine. conda/conda#11795 (comment) for a workaround)
  4. Save the project and close the solution
  5. Re-open the solution
  6. The previously selected conda environment should be present under Python Environments in the solution explorer, and it should be activated and bolded.

Scenario 1 - Single conda environment (selected), no other environments

Expected behavior - On project load, no environments are found. So the global default is added and activated. Then, when the conda environment is detected, it's added to the solution explorer and the global default is removed. Since the conda environment is the one specified as active in the pyproj, it gets activated.

Actual behavior - The conda environment is never added to the solution explorer. The InterpreterId specified in the pyproj isn't found.

✅ Open existing solution is now working correctly
❌ Reload project is activating the correct environment, but it's not being bolded. I spoke with Steve and he said this is a nasty race condition with the project system and it's not worth the work to fix. Since the correct environment is activated, this bug is purely graphical, so we're going to skip it for now. I've reached out to VS Extensibility mailing list to see if anyone can help, but this one isn't important.

Scenario 2 - Single conda environment (selected), single virtual environment

Expected behavior - On project load, a single environment is found (the virtual environment). This environment should NOT be activated since it's not the one specified as active in the pyproj. When the conda environment is detected, it's added to the solution explorer. Since the conda environment is the one specified as active in the pyproj, it gets activated.

Actual behavior - On project load, a single environment is found (the virtual environment). Since there are no other environments, this virtual env is activated, even though it's NOT the one specified as active in the pyproj. This is a problem since the <InterpreterId> element is updated in the pyproj, which means the conda environment will not be activated correctly after being added.

The fix is not to activate environments arbitrarily, which is what the code was doing.

✅ This is now working correctly. We no longer arbitrarily select an interpreter as active. The "missing" conda interpreter shows up at missing with NOTHING selected at first. Then, once the conda environment is detected, the list is updated and the conda environment is selected and bolded.

Scenario 3 - Single conda environment, single virtual environment (selected)

Expected behavior - Both environments load correctly, virtualEnv is selected and bolded

Actual behavior - Both environments do load correctly, virtualEnv is selected and bolded. BUT we get an info bar saying there's a reference to a missing conda environment.

The fix is either to delay the info bar displaying for conda environments, or remove it, or hide the info bar once the conda environment is finished being detected.

✅ This is now working correctly. The normal infobar checks still run, but the conda infobar check is delayed until conda environment discovery is finished.

@AdamYoblick
Copy link
Member Author

AdamYoblick commented Apr 21, 2023

Case 2 - Conda environment not found (project view)

To repro:

  1. Complete all repro steps from previous case above
  2. Remove conda environment from machine using conda command prompt (conda remove -n ENV_NAME --all)
  3. Open python project with reference to this now-deleted environment.

Scenario 1 - Only conda environment in list

Expected behavior - Conda environment shows as missing in the python environments folder, global environment loads correctly and is activated, info bar is displayed that says conda env is missing.
Actual behavior - same as above

✅ Works as intended

Scenario 2 - Virtual environment also in list

Expected behavior - Conda environment shows as missing in the python environments folder, virtual environment loads correctly and is activated, info bar is displayed that says conda env is missing.
Actual behavior - same as above

✅ Works as intended

@AdamYoblick AdamYoblick marked this pull request as ready for review April 21, 2023 20:58
@AdamYoblick AdamYoblick requested a review from a team as a code owner April 21, 2023 20:58
@AdamYoblick AdamYoblick changed the title [WIP] - Fix asynchronous environment discovery and selection (DO NOT REVIEW) Fix asynchronous environment discovery and selection Apr 21, 2023
@AdamYoblick AdamYoblick reopened this Apr 24, 2023
@int19h
Copy link
Contributor

int19h commented Apr 25, 2023

Thank you very much for adding copious comments; this will help a lot in the future!

@sonarcloud
Copy link

sonarcloud bot commented Apr 25, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@AdamYoblick AdamYoblick merged commit 0ff2570 into main Apr 25, 2023
4 checks passed
@AdamYoblick AdamYoblick deleted the conda_detection_race_condition branch April 25, 2023 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Anaconda/Conda environments are broken in VS2022, works in VS2019
4 participants