From eb7eb920615a723ce6219c3e30d479bca4f3d3cf Mon Sep 17 00:00:00 2001 From: Adva Oren Date: Tue, 10 Nov 2026 13:56:34 +0200 Subject: [PATCH 1/4] fix(python): select venv python.exe on Windows for python3 sources On Windows the venv created from a `python3` source interpreter still installs Scripts\python.exe and Scripts\pip.exe, but prepareEnvironment() derived the venv executable names from the source interpreter name and selected the nonexistent Scripts\python3.exe. The pip upgrade then failed with an unrecognized-command error and Python analysis produced no report. Select the venv executable names by platform instead: Windows always uses python.exe/pip.exe, while POSIX keeps python3/pip3 for a python3 source and python/pip otherwise. The duplicated per-branch drive-letter handling is folded into a single path that still reuses #decideIfWindowsOrLinuxPath. Adds a failing-first esmock reproducer covering Windows with python3 and python sources plus a POSIX python3/pip3 regression case. Implements TC-7486 Signed-off-by: Adva Oren Assisted-by: Claude Code --- src/providers/python_controller.js | 30 ++++--- test/providers/python_controller.test.js | 102 +++++++++++++++++++++++ 2 files changed, 116 insertions(+), 16 deletions(-) diff --git a/src/providers/python_controller.js b/src/providers/python_controller.js index 953f63c8..cc66e98e 100644 --- a/src/providers/python_controller.js +++ b/src/providers/python_controller.js @@ -108,22 +108,20 @@ export default class Python_controller { } catch (error) { throw new Error('Failed creating virtual python environment', {cause: error}) } - if(this.pathToPythonBin.includes("python3")) { - this.pathToPipBin = path.join(path.sep,this.pythonEnvDir,os.platform() === 'win32' ? "Scripts" : "bin",this.#decideIfWindowsOrLinuxPath("pip3")) - this.pathToPythonBin = path.join(path.sep,this.pythonEnvDir,os.platform() === 'win32' ? "Scripts" : "bin",this.#decideIfWindowsOrLinuxPath("python3")) - if(os.platform() === 'win32') { - let driveLetter = path.parse(process.cwd()).root - this.pathToPythonBin = `${driveLetter}${this.pathToPythonBin.substring(1)}` - this.pathToPipBin = `${driveLetter}${this.pathToPipBin.substring(1)}` - } - } else { - this.pathToPipBin = path.join(path.sep,this.pythonEnvDir,os.platform() === 'win32' ? "Scripts" : "bin",this.#decideIfWindowsOrLinuxPath("pip")); - this.pathToPythonBin = path.join(path.sep,this.pythonEnvDir,os.platform() === 'win32' ? "Scripts" : "bin",this.#decideIfWindowsOrLinuxPath("python")) - if(os.platform() === 'win32') { - let driveLetter = path.parse(process.cwd()).root - this.pathToPythonBin = `${driveLetter}${this.pathToPythonBin.substring(1)}` - this.pathToPipBin = `${driveLetter}${this.pathToPipBin.substring(1)}` - } + const isWindows = os.platform() === 'win32' + // On Windows, venv always installs `Scripts\python.exe` and `Scripts\pip.exe` + // regardless of how the source interpreter is named, so a `python3` source + // must not be mapped to the nonexistent `Scripts\python3.exe` (TC-7485). + const usesPython3Source = this.pathToPythonBin.includes("python3") + const pipExecutable = isWindows ? "pip" : usesPython3Source ? "pip3" : "pip" + const pythonExecutable = isWindows ? "python" : usesPython3Source ? "python3" : "python" + const envBinDir = isWindows ? "Scripts" : "bin" + this.pathToPipBin = path.join(path.sep, this.pythonEnvDir, envBinDir, this.#decideIfWindowsOrLinuxPath(pipExecutable)) + this.pathToPythonBin = path.join(path.sep, this.pythonEnvDir, envBinDir, this.#decideIfWindowsOrLinuxPath(pythonExecutable)) + if(isWindows) { + let driveLetter = path.parse(process.cwd()).root + this.pathToPythonBin = `${driveLetter}${this.pathToPythonBin.substring(1)}` + this.pathToPipBin = `${driveLetter}${this.pathToPipBin.substring(1)}` } // upgrade pip version to latest try { diff --git a/test/providers/python_controller.test.js b/test/providers/python_controller.test.js index b6161532..cf28f8d1 100644 --- a/test/providers/python_controller.test.js +++ b/test/providers/python_controller.test.js @@ -1,7 +1,54 @@ +import path from 'node:path' + import { expect } from 'chai' +import esmock from 'esmock' import Python_controller from '../../src/providers/python_controller.js' +const REQUIREMENTS_MANIFEST = 'test/providers/tst_manifests/pip/pip_requirements_txt_no_ignore/requirements.txt' + +/** + * Splits a filesystem path into its components regardless of the separator used. + * @param {string} filePath - path to split + * @return {string[]} non-empty path components + */ +function pathComponents(filePath) { + return filePath.split(/[\\/]+/).filter(component => component !== '') +} + +/** + * Loads Python_controller with os.platform() and command invocation stubbed, then + * constructs a virtual-environment controller so the venv setup invocations can be + * inspected. Stubbing the parser keeps the test independent of the tree-sitter wasm + * artifacts. + * @param {string} platform - platform reported by os.platform() + * @param {string} sourcePython - configured source python binary + * @return {Promise<{controller: Python_controller, invocations: Array<{bin: string, args: string[]}>}>} + */ +async function constructVirtualEnvController(platform, sourcePython) { + const invocations = [] + /** Records a command invocation instead of executing it. */ + const recordInvocation = (bin, args) => { + invocations.push({ bin, args }) + return '' + } + const { default: MockedPythonController } = await esmock('../../src/providers/python_controller.js', { + os: { default: { platform: () => platform }, EOL: '\n' }, + '../../src/tools.js': { + environmentVariableIsPopulated: () => false, + getCustom: () => undefined, + invokeCommand: recordInvocation + }, + '../../src/providers/requirements_parser.js': { + getParser: async () => null, + getRequirementQuery: async () => null, + getPinnedVersionQuery: async () => null + } + }) + const controller = new MockedPythonController(false, 'pip3', sourcePython, REQUIREMENTS_MANIFEST) + return { controller, invocations } +} + suite('Python_controller BEST_EFFORTS validation', function() { let originalBestEfforts @@ -71,3 +118,58 @@ suite('Python_controller BEST_EFFORTS validation', function() { } }) }) + +suite('Python_controller virtual environment executables', function() { + // Windows venv executable names must not depend on how the source interpreter is named (TC-7485). + for (const sourcePython of ['python3', 'python']) { + /** Verifies that a Windows venv created from `sourcePython` upgrades pip via Scripts/python.exe. */ + test(`upgrades pip via Scripts/python.exe on Windows for a ${sourcePython} source`, async function() { + // Given a Windows host and a configured source interpreter named sourcePython + const { controller, invocations } = await constructVirtualEnvController('win32', sourcePython) + + // When the venv is prepared while the controller is constructed + const venvInvocation = invocations[0] + const pipUpgradeInvocation = invocations[1] + + // Then the venv is created from the configured source interpreter + expect(invocations).to.have.lengthOf(2) + expect(venvInvocation.bin).to.equal(sourcePython) + expect(venvInvocation.args).to.deep.equal(['-m', 'venv', path.join(path.sep, 'tmp', 'trustify_da_env_js')]) + + // Then the pip upgrade runs through the venv Scripts/python.exe + expect(pipUpgradeInvocation.args).to.deep.equal(['-m', 'pip', 'install', '--upgrade', 'pip']) + expect(pipUpgradeInvocation.bin).to.equal(controller.pathToPythonBin) + expect(pathComponents(pipUpgradeInvocation.bin)).to.include('Scripts') + expect(path.basename(pipUpgradeInvocation.bin)).to.equal('python.exe') + + // Then the venv pip path ends in Scripts/pip.exe + expect(pathComponents(controller.pathToPipBin)).to.include('Scripts') + expect(path.basename(controller.pathToPipBin)).to.equal('pip.exe') + + // Then no invocation targets the nonexistent Scripts/python3.exe + expect(path.basename(pipUpgradeInvocation.bin)).to.not.equal('python3.exe') + expect(invocations.map(invocation => path.basename(invocation.bin))).to.not.include('python3.exe') + expect(invocations.map(invocation => path.basename(invocation.bin))).to.not.include('pip3.exe') + }) + } + + /** Verifies that POSIX venv naming still mirrors a python3 source interpreter. */ + test('keeps bin/python3 and bin/pip3 on non-Windows hosts for a python3 source', async function() { + // Given a POSIX host and a configured source interpreter named python3 + const { controller, invocations } = await constructVirtualEnvController('linux', 'python3') + + // When the venv is prepared while the controller is constructed + const pipUpgradeInvocation = invocations[1] + + // Then the pip upgrade runs through the venv bin/python3 + expect(invocations).to.have.lengthOf(2) + expect(pipUpgradeInvocation.args).to.deep.equal(['-m', 'pip', 'install', '--upgrade', 'pip']) + expect(pipUpgradeInvocation.bin).to.equal(controller.pathToPythonBin) + expect(pathComponents(controller.pathToPythonBin)).to.include('bin') + expect(path.basename(controller.pathToPythonBin)).to.equal('python3') + + // Then the venv pip path stays bin/pip3 + expect(pathComponents(controller.pathToPipBin)).to.include('bin') + expect(path.basename(controller.pathToPipBin)).to.equal('pip3') + }) +}) From 1c5b0627b1b922f7f264d0dcec777bd5187db0f2 Mon Sep 17 00:00:00 2001 From: Adva Oren Date: Tue, 10 Nov 2026 16:03:32 +0200 Subject: [PATCH 2/4] test(python): assert exact Windows venv executable paths Anchor the Windows pip-upgrade interpreter, pathToPythonBin, and pathToPipBin expectations to the venv directory recorded on the -m venv invocation instead of accepting any Scripts component with the expected basename. Add a negative case proving a same-basename executable from a different venv is rejected, and tighten POSIX expectations the same way. Implements TC-7502 Signed-off-by: Adva Oren Assisted-by: Claude Code --- test/providers/python_controller.test.js | 49 +++++++++++++++++------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/test/providers/python_controller.test.js b/test/providers/python_controller.test.js index cf28f8d1..ba51cd34 100644 --- a/test/providers/python_controller.test.js +++ b/test/providers/python_controller.test.js @@ -136,15 +136,21 @@ suite('Python_controller virtual environment executables', function() { expect(venvInvocation.bin).to.equal(sourcePython) expect(venvInvocation.args).to.deep.equal(['-m', 'venv', path.join(path.sep, 'tmp', 'trustify_da_env_js')]) - // Then the pip upgrade runs through the venv Scripts/python.exe + // Then the executable paths are anchored to the venv directory that was created. + // Resolving the recorded target makes this match the controller's drive-prefixed + // Windows paths on a native host as well as under the mocked win32 platform. + const expectedVenvDir = path.resolve(venvInvocation.args[2]) + expect(path.resolve(controller.pythonEnvDir)).to.equal(expectedVenvDir) + const expectedPythonBin = path.join(expectedVenvDir, 'Scripts', 'python.exe') + const expectedPipBin = path.join(expectedVenvDir, 'Scripts', 'pip.exe') + + // Then the pip upgrade runs through that venv's Scripts/python.exe exactly expect(pipUpgradeInvocation.args).to.deep.equal(['-m', 'pip', 'install', '--upgrade', 'pip']) - expect(pipUpgradeInvocation.bin).to.equal(controller.pathToPythonBin) - expect(pathComponents(pipUpgradeInvocation.bin)).to.include('Scripts') - expect(path.basename(pipUpgradeInvocation.bin)).to.equal('python.exe') + expect(pipUpgradeInvocation.bin).to.equal(expectedPythonBin) + expect(controller.pathToPythonBin).to.equal(expectedPythonBin) - // Then the venv pip path ends in Scripts/pip.exe - expect(pathComponents(controller.pathToPipBin)).to.include('Scripts') - expect(path.basename(controller.pathToPipBin)).to.equal('pip.exe') + // Then the venv pip path is exactly that venv's Scripts/pip.exe + expect(controller.pathToPipBin).to.equal(expectedPipBin) // Then no invocation targets the nonexistent Scripts/python3.exe expect(path.basename(pipUpgradeInvocation.bin)).to.not.equal('python3.exe') @@ -153,23 +159,40 @@ suite('Python_controller virtual environment executables', function() { }) } + /** Verifies that a same-basename executable in another venv's Scripts directory cannot satisfy the exact-path checks. */ + test('rejects Scripts/python.exe from a different venv directory on Windows', async function() { + // Given a Windows host virtual environment controller + const { controller, invocations } = await constructVirtualEnvController('win32', 'python3') + + // When the candidate path has the same Scripts component and python.exe basename + // as the real venv, but belongs to a different venv root + const wrongVenvDir = path.resolve(path.join(path.sep, 'tmp', 'wrong_trustify_da_env_js')) + const wrongVenvPythonBin = path.join(wrongVenvDir, 'Scripts', 'python.exe') + + // Then the shape that previously satisfied the component/basename checks is rejected + expect(pathComponents(wrongVenvPythonBin)).to.include('Scripts') + expect(path.basename(wrongVenvPythonBin)).to.equal('python.exe') + expect(controller.pathToPythonBin).to.not.equal(wrongVenvPythonBin) + expect(invocations[1].bin).to.not.equal(wrongVenvPythonBin) + }) + /** Verifies that POSIX venv naming still mirrors a python3 source interpreter. */ test('keeps bin/python3 and bin/pip3 on non-Windows hosts for a python3 source', async function() { // Given a POSIX host and a configured source interpreter named python3 const { controller, invocations } = await constructVirtualEnvController('linux', 'python3') // When the venv is prepared while the controller is constructed + const venvInvocation = invocations[0] const pipUpgradeInvocation = invocations[1] - // Then the pip upgrade runs through the venv bin/python3 + // Then the pip upgrade runs through the venv bin/python3 exactly expect(invocations).to.have.lengthOf(2) + expect(venvInvocation.args[2]).to.equal(path.join(path.sep, 'tmp', 'trustify_da_env_js')) expect(pipUpgradeInvocation.args).to.deep.equal(['-m', 'pip', 'install', '--upgrade', 'pip']) - expect(pipUpgradeInvocation.bin).to.equal(controller.pathToPythonBin) - expect(pathComponents(controller.pathToPythonBin)).to.include('bin') - expect(path.basename(controller.pathToPythonBin)).to.equal('python3') + expect(pipUpgradeInvocation.bin).to.equal(path.join(venvInvocation.args[2], 'bin', 'python3')) + expect(controller.pathToPythonBin).to.equal(path.join(venvInvocation.args[2], 'bin', 'python3')) // Then the venv pip path stays bin/pip3 - expect(pathComponents(controller.pathToPipBin)).to.include('bin') - expect(path.basename(controller.pathToPipBin)).to.equal('pip3') + expect(controller.pathToPipBin).to.equal(path.join(venvInvocation.args[2], 'bin', 'pip3')) }) }) From 2254656570d90ba0e061de6258b064c11b48e8de Mon Sep 17 00:00:00 2001 From: Adva Oren Date: Thu, 12 Nov 2026 12:40:16 +0200 Subject: [PATCH 3/4] refactor(python): use standard venv executable names venv always installs the unversioned python and pip names, so the includes("python3") heuristic only added a branch without changing behavior. Use python/pip on both platforms and keep only the platform directory and .exe handling. Update the POSIX regression cases to cover both python3 and python sources. Implements TC-7486 Signed-off-by: Adva Oren Assisted-by: Claude Code --- src/providers/python_controller.js | 14 ++++---- test/providers/python_controller.test.js | 45 ++++++++++++++---------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/providers/python_controller.js b/src/providers/python_controller.js index cc66e98e..7312c6d2 100644 --- a/src/providers/python_controller.js +++ b/src/providers/python_controller.js @@ -109,15 +109,13 @@ export default class Python_controller { throw new Error('Failed creating virtual python environment', {cause: error}) } const isWindows = os.platform() === 'win32' - // On Windows, venv always installs `Scripts\python.exe` and `Scripts\pip.exe` - // regardless of how the source interpreter is named, so a `python3` source - // must not be mapped to the nonexistent `Scripts\python3.exe` (TC-7485). - const usesPython3Source = this.pathToPythonBin.includes("python3") - const pipExecutable = isWindows ? "pip" : usesPython3Source ? "pip3" : "pip" - const pythonExecutable = isWindows ? "python" : usesPython3Source ? "python3" : "python" + // venv always installs the unversioned `python` and `pip` names regardless of + // how the source interpreter is named: `Scripts\python.exe`/`Scripts\pip.exe` + // on Windows and `bin/python`/`bin/pip` on POSIX. A `python3` source must not + // be mapped to the nonexistent `Scripts\python3.exe` (TC-7485). const envBinDir = isWindows ? "Scripts" : "bin" - this.pathToPipBin = path.join(path.sep, this.pythonEnvDir, envBinDir, this.#decideIfWindowsOrLinuxPath(pipExecutable)) - this.pathToPythonBin = path.join(path.sep, this.pythonEnvDir, envBinDir, this.#decideIfWindowsOrLinuxPath(pythonExecutable)) + this.pathToPipBin = path.join(path.sep, this.pythonEnvDir, envBinDir, this.#decideIfWindowsOrLinuxPath("pip")) + this.pathToPythonBin = path.join(path.sep, this.pythonEnvDir, envBinDir, this.#decideIfWindowsOrLinuxPath("python")) if(isWindows) { let driveLetter = path.parse(process.cwd()).root this.pathToPythonBin = `${driveLetter}${this.pathToPythonBin.substring(1)}` diff --git a/test/providers/python_controller.test.js b/test/providers/python_controller.test.js index ba51cd34..2fd422b0 100644 --- a/test/providers/python_controller.test.js +++ b/test/providers/python_controller.test.js @@ -176,23 +176,30 @@ suite('Python_controller virtual environment executables', function() { expect(invocations[1].bin).to.not.equal(wrongVenvPythonBin) }) - /** Verifies that POSIX venv naming still mirrors a python3 source interpreter. */ - test('keeps bin/python3 and bin/pip3 on non-Windows hosts for a python3 source', async function() { - // Given a POSIX host and a configured source interpreter named python3 - const { controller, invocations } = await constructVirtualEnvController('linux', 'python3') - - // When the venv is prepared while the controller is constructed - const venvInvocation = invocations[0] - const pipUpgradeInvocation = invocations[1] - - // Then the pip upgrade runs through the venv bin/python3 exactly - expect(invocations).to.have.lengthOf(2) - expect(venvInvocation.args[2]).to.equal(path.join(path.sep, 'tmp', 'trustify_da_env_js')) - expect(pipUpgradeInvocation.args).to.deep.equal(['-m', 'pip', 'install', '--upgrade', 'pip']) - expect(pipUpgradeInvocation.bin).to.equal(path.join(venvInvocation.args[2], 'bin', 'python3')) - expect(controller.pathToPythonBin).to.equal(path.join(venvInvocation.args[2], 'bin', 'python3')) - - // Then the venv pip path stays bin/pip3 - expect(controller.pathToPipBin).to.equal(path.join(venvInvocation.args[2], 'bin', 'pip3')) - }) + // The venv layout is independent of the source interpreter name, so POSIX + // environments always expose bin/python and bin/pip. + for (const sourcePython of ['python3', 'python']) { + /** Verifies that POSIX venvs expose bin/python and bin/pip regardless of the source interpreter name. */ + test(`uses bin/python and bin/pip on non-Windows hosts for a ${sourcePython} source`, async function() { + // Given a POSIX host and a configured source interpreter named sourcePython + const { controller, invocations } = await constructVirtualEnvController('linux', sourcePython) + + // When the venv is prepared while the controller is constructed + const venvInvocation = invocations[0] + const pipUpgradeInvocation = invocations[1] + + // Then the venv is created from the configured source interpreter + expect(invocations).to.have.lengthOf(2) + expect(venvInvocation.bin).to.equal(sourcePython) + expect(venvInvocation.args[2]).to.equal(path.join(path.sep, 'tmp', 'trustify_da_env_js')) + + // Then the pip upgrade runs through the venv bin/python exactly + expect(pipUpgradeInvocation.args).to.deep.equal(['-m', 'pip', 'install', '--upgrade', 'pip']) + expect(pipUpgradeInvocation.bin).to.equal(path.join(venvInvocation.args[2], 'bin', 'python')) + expect(controller.pathToPythonBin).to.equal(path.join(venvInvocation.args[2], 'bin', 'python')) + + // Then the venv pip path is bin/pip + expect(controller.pathToPipBin).to.equal(path.join(venvInvocation.args[2], 'bin', 'pip')) + }) + } }) From 4c53d28c3985b850cd804b66ea093b992be2004b Mon Sep 17 00:00:00 2001 From: Adva Oren Date: Thu, 12 Nov 2026 12:57:36 +0200 Subject: [PATCH 4/4] test(python): drop vacuous wrong-venv assertion The removed test asserted inequality against a directory production can never produce, so it could not fail for any plausible regression. The exact-path equality assertions in the Windows cases already reject a same-basename executable from another venv. Drop the test and its now-unused pathComponents helper. Implements TC-7502 Signed-off-by: Adva Oren Assisted-by: Claude Code --- test/providers/python_controller.test.js | 26 ------------------------ 1 file changed, 26 deletions(-) diff --git a/test/providers/python_controller.test.js b/test/providers/python_controller.test.js index 2fd422b0..0f12ec43 100644 --- a/test/providers/python_controller.test.js +++ b/test/providers/python_controller.test.js @@ -7,15 +7,6 @@ import Python_controller from '../../src/providers/python_controller.js' const REQUIREMENTS_MANIFEST = 'test/providers/tst_manifests/pip/pip_requirements_txt_no_ignore/requirements.txt' -/** - * Splits a filesystem path into its components regardless of the separator used. - * @param {string} filePath - path to split - * @return {string[]} non-empty path components - */ -function pathComponents(filePath) { - return filePath.split(/[\\/]+/).filter(component => component !== '') -} - /** * Loads Python_controller with os.platform() and command invocation stubbed, then * constructs a virtual-environment controller so the venv setup invocations can be @@ -159,23 +150,6 @@ suite('Python_controller virtual environment executables', function() { }) } - /** Verifies that a same-basename executable in another venv's Scripts directory cannot satisfy the exact-path checks. */ - test('rejects Scripts/python.exe from a different venv directory on Windows', async function() { - // Given a Windows host virtual environment controller - const { controller, invocations } = await constructVirtualEnvController('win32', 'python3') - - // When the candidate path has the same Scripts component and python.exe basename - // as the real venv, but belongs to a different venv root - const wrongVenvDir = path.resolve(path.join(path.sep, 'tmp', 'wrong_trustify_da_env_js')) - const wrongVenvPythonBin = path.join(wrongVenvDir, 'Scripts', 'python.exe') - - // Then the shape that previously satisfied the component/basename checks is rejected - expect(pathComponents(wrongVenvPythonBin)).to.include('Scripts') - expect(path.basename(wrongVenvPythonBin)).to.equal('python.exe') - expect(controller.pathToPythonBin).to.not.equal(wrongVenvPythonBin) - expect(invocations[1].bin).to.not.equal(wrongVenvPythonBin) - }) - // The venv layout is independent of the source interpreter name, so POSIX // environments always expose bin/python and bin/pip. for (const sourcePython of ['python3', 'python']) {