Skip to content

Commit f96a318

Browse files
authored
fix(pypi): prefer stock python3 if it satisfies version requirement (#1736)
1 parent 2af3b57 commit f96a318

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lua/mason-core/installer/managers/pypi.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ local function create_venv(pkg)
8585
-- 2. Resolve suitable versioned python3 installation (python3.12, python3.11, etc.).
8686
local versioned_candidates = {}
8787
if supported_python_versions ~= nil then
88-
log.fmt_debug("Finding versioned candidates for %s", supported_python_versions)
89-
versioned_candidates = get_versioned_candidates(supported_python_versions)
88+
if stock_target and not pep440_check_version(tostring(stock_target.version), supported_python_versions) then
89+
log.fmt_debug("Finding versioned candidates for %s", supported_python_versions)
90+
versioned_candidates = get_versioned_candidates(supported_python_versions)
91+
end
9092
end
9193
local target = resolve_python3(versioned_candidates) or stock_target
9294

tests/mason-core/installer/managers/pypi_spec.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,36 @@ describe("pypi manager", function()
179179
end
180180
)
181181

182+
it("should prioritize stock python", function()
183+
local ctx = create_dummy_context { force = true }
184+
spy.on(ctx.stdio_sink, "stderr")
185+
stub(ctx, "promote_cwd")
186+
stub(ctx.fs, "file_exists")
187+
stub(providers.pypi, "get_supported_python_versions", mockx.returns(Result.success ">=3.8"))
188+
stub(vim.fn, "executable")
189+
vim.fn.executable.on_call_with("python3.12").returns(1)
190+
stub(spawn, "python3", mockx.returns(Result.success()))
191+
spawn.python3.on_call_with({ "--version" }).returns(Result.success { stdout = "Python 3.8.0" })
192+
193+
installer.exec_in_context(ctx, function()
194+
pypi.init {
195+
package = { name = "cmake-language-server", version = "0.1.10" },
196+
upgrade_pip = true,
197+
install_extra_args = { "--proxy", "http://localhost" },
198+
}
199+
end)
200+
201+
assert.spy(ctx.promote_cwd).was_called(1)
202+
assert.spy(ctx.spawn.python3).was_called(1)
203+
assert.spy(ctx.spawn["python3.12"]).was_called(0)
204+
assert.spy(ctx.spawn.python3).was_called_with {
205+
"-m",
206+
"venv",
207+
"--system-site-packages",
208+
"venv",
209+
}
210+
end)
211+
182212
it("should install", function()
183213
local ctx = create_dummy_context()
184214
stub(ctx.fs, "file_exists")

0 commit comments

Comments
 (0)