pdm icon indicating copy to clipboard operation
pdm copied to clipboard

fix: properly handle interpreter resolution while importing

Open intelfx opened this issue 1 year ago • 4 comments

Pull Request Checklist

  • [ ] A news fragment is added in news/ describing what is new.
  • [ ] Test cases added for changed code.

Describe what you have changed in this PR.

Add and use Project.resolve_interpreter(in_import=True) which does not memoize the result of resolution that uses a potentially incorrect requires-python information and does not create a virtualenv from this (potentially wrong) python version.

Additionally, the quality of informational output from Project.resolve_interpreter() is improved, hiding some misleading messages during the first resolution.

Fixes #2608.

intelfx avatar Feb 03 '24 13:02 intelfx

This is too complex IMO. What you need is to revert all side-effect caused by Project.resolve_interpreter(), this is enough:

project._saved_python = None
project._python = None

frostming avatar Feb 03 '24 14:02 frostming

@frostming Unfortunately, one of the side-effects of Project.resolve_interpreter() is creating a virtualenv.

Consider a project that has an upper-bound requires-python dependency, something like requires-python < 3.10 (together with a virtualenv provider that can satisfy such a dependency). In this case, the virtualenv will be created with a wrong python version, because it is created on the first resolution pass, where the bound is not yet known.

intelfx avatar Feb 03 '24 14:02 intelfx

I agree that resolve_interpreter has too many side-effects and it's bad.

We need a side-effect-free version of it, no writing files, no saving states, no virtualenv creation. but in_import isn't a good name for it.

And I wonder if the side-effect version can call the side-effect-free version inside.

frostming avatar Feb 03 '24 14:02 frostming

Fair enough, I'll think about it!

intelfx avatar Feb 03 '24 14:02 intelfx

I am closing this PR, if you have further fixes feel free to open a new one.

frostming avatar Mar 27 '24 04:03 frostming