Add extra ENV vars

This commit is contained in:
Reinier Criel 2025-11-11 10:37:39 -08:00
parent 76acf43128
commit 9b102412af
2 changed files with 48 additions and 3 deletions

View file

@ -2,6 +2,10 @@ import { ui } from "../../environment/userInteraction.js";
import { safeSpawn } from "../../utils/safeSpawn.js";
import { mergeSafeChainProxyEnvironmentVariables } from "../../registryProxy/registryProxy.js";
import { getCombinedCaBundlePath } from "../../registryProxy/certBundle.js";
import { knownPipRegistries } from "../../registryProxy/parsePackageFromUrl.js";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
/**
* @param {string} command
@ -20,6 +24,33 @@ export async function runPip(command, args) {
env.REQUESTS_CA_BUNDLE = combinedCaPath;
env.SSL_CERT_FILE = combinedCaPath;
// To counter behavior that is sometimes seen where pip ignores REQUESTS_CA_BUNDLE/SSL_CERT_FILE,
// 1. Set additional env vars for pip
// 2. Create a pip config file that specifies the cert and trusted hosts
env.PIP_CERT = combinedCaPath;
// Create a temporary pip config file
const tmpDir = os.tmpdir();
const pipConfigPath = path.join(tmpDir, `safe-chain-pip-${Date.now()}.ini`);
// Trusted hosts: use knownPipRegistries from parsePackageFromUrl
const trustedHosts = Array.from(new Set(knownPipRegistries));
// Proxy settings
const httpProxy = env.HTTP_PROXY || '';
const httpsProxy = env.HTTPS_PROXY || '';
// Build pip config INI
let pipConfig = '[global]\n';
pipConfig += `cert = ${combinedCaPath}\n`;
if (httpProxy) pipConfig += `proxy = ${httpProxy}\n`;
if (httpsProxy) pipConfig += `proxy = ${httpsProxy}\n`;
if (trustedHosts.length) pipConfig += `trusted-host = ${trustedHosts.join(' ')}\n`;
await fs.writeFile(pipConfigPath, pipConfig);
env.PIP_CONFIG_FILE = pipConfigPath;
const result = await safeSpawn(command, args, {
stdio: "inherit",
env,