Skip to content

Commit d90b3b5

Browse files
authored
Merge pull request #327 from aminya/venv-mac
fix: check for existence of venv module before installing
2 parents f1ec26f + 0528a87 commit d90b3b5

File tree

8 files changed

+67
-28
lines changed

8 files changed

+67
-28
lines changed

.github/workflows/CI.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ jobs:
109109
~/.pnpm-store
110110
D:\.pnpm-store
111111
./node_modules
112-
key: "setupcpp-node_modules-cache-OS:${{ matrix.os }}-${{ hashFiles('./.npmrc', './package.json', '.nvmrc', './packages/*/package.json') }}"
112+
key: "setupcpp-node_modules-cache-OS:${{ matrix.os }}-${{ hashFiles('./.npmrc', './package.json', './.nvmrc', './pnpm-*.yaml') }}"
113113
restore-keys: |
114114
"setupcpp-node_modules-cache-OS:${{ matrix.os }}-"
115115
@@ -174,7 +174,7 @@ jobs:
174174
~/.pnpm-store
175175
D:\.pnpm-store
176176
./node_modules
177-
key: "setupcpp-node_modules-cache-OS:${{ matrix.os }}-${{ hashFiles('./.npmrc', './package.json', '.nvmrc', './packages/*/package.json') }}"
177+
key: "setupcpp-node_modules-cache-OS:${{ matrix.os }}-${{ hashFiles('./.npmrc', './package.json', './.nvmrc', './pnpm-*.yaml') }}"
178178
restore-keys: |
179179
"setupcpp-node_modules-cache-OS:${{ matrix.os }}-"
180180

dist/legacy/setup-cpp.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.mjs

+1-1
Large diffs are not rendered by default.

dist/modern/setup-cpp.mjs.map

+1-1
Large diffs are not rendered by default.

src/llvm/__tests__/llvm.test.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,22 @@ describe("setup-llvm", () => {
6464
const directory = await setupTmpDir("llvm")
6565

6666
const osVersion = await ubuntuVersion()
67-
const { binDir } = await setupLLVM(getVersion("llvm", "true", osVersion), directory, process.arch)
68-
await testBin("clang++", ["--version"], binDir)
67+
{
68+
const { binDir } = await setupLLVM(getVersion("llvm", "true", osVersion), directory, process.arch)
69+
await testBin("clang++", ["--version"], binDir)
6970

70-
expect(process.env.CC?.includes("clang")).toBeTruthy()
71-
expect(process.env.CXX?.includes("clang++")).toBeTruthy()
71+
expect(process.env.CC?.includes("clang")).toBeTruthy()
72+
expect(process.env.CXX?.includes("clang++")).toBeTruthy()
7273

73-
// test compilation
74-
const file = join(dirname, "main.cpp")
75-
const main_exe = join(dirname, addExeExt("main"))
76-
await execa("clang++", [file, "-o", main_exe], { cwd: dirname })
77-
if (process.platform !== "win32") {
78-
await chmod(main_exe, "755")
74+
// test compilation
75+
const file = join(dirname, "main.cpp")
76+
const main_exe = join(dirname, addExeExt("main"))
77+
await execa("clang++", [file, "-o", main_exe], { cwd: dirname })
78+
if (process.platform !== "win32") {
79+
await chmod(main_exe, "755")
80+
}
81+
await execa(main_exe, { cwd: dirname, stdio: "inherit" })
7982
}
80-
await execa(main_exe, { cwd: dirname, stdio: "inherit" })
8183

8284
{
8385
const { binDir } = await setupClangFormat(getVersion("llvm", "true", osVersion), directory, process.arch)

src/python/python.ts

+41-9
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ import type { InstallationInfo } from "../utils/setup/setupBin.js"
2222
import { setupChocoPack } from "../utils/setup/setupChocoPack.js"
2323
import { setupDnfPack } from "../utils/setup/setupDnfPack.js"
2424
import { setupPacmanPack } from "../utils/setup/setupPacmanPack.js"
25-
import { hasPipx, setupPipPackSystem, setupPipPackWithPython } from "../utils/setup/setupPipPack.js"
25+
import {
26+
hasPipxBinary,
27+
hasPipxModule,
28+
setupPipPackSystem,
29+
setupPipPackWithPython,
30+
} from "../utils/setup/setupPipPack.js"
2631
import { isBinUptoDate } from "../utils/setup/version.js"
2732
import { unique } from "../utils/std/index.js"
2833
import { getVersionDefault, isMinVersion } from "../versions/versions.js"
@@ -36,45 +41,72 @@ export async function setupPython(
3641
assert(installInfo.bin !== undefined)
3742
const foundPython = installInfo.bin
3843

44+
// setup venv
45+
await setupVenv(foundPython)
46+
3947
// setup pip
4048
const foundPip = await findOrSetupPip(foundPython)
4149
if (foundPip === undefined) {
4250
throw new Error("pip was not installed correctly")
4351
}
4452

4553
await setupPipx(foundPython)
46-
4754
await setupWheel(foundPython)
4855

4956
return installInfo as InstallationInfo & { bin: string }
5057
}
5158

5259
async function setupPipx(foundPython: string) {
5360
try {
54-
if (!(await hasPipx(foundPython))) {
61+
if (!(await hasPipxModule(foundPython))) {
5562
try {
63+
// first try with the system-wide pipx
64+
await setupPipPackSystem("pipx", isArch())
65+
// then install with the system-wide pipx
5666
await setupPipPackWithPython(foundPython, "pipx", undefined, { upgrade: true, usePipx: false })
5767
} catch (err) {
58-
if (setupPipPackSystem("pipx", false) === null) {
59-
throw new Error(`pipx was not installed correctly ${err}`)
60-
}
68+
throw new Error(`pipx was not installed completely: ${err}`)
6169
}
6270
}
63-
await execa(foundPython, ["-m", "pipx", "ensurepath"], { stdio: "inherit" })
64-
await setupVenv(foundPython)
71+
if (await hasPipxModule(foundPython)) {
72+
await execa(foundPython, ["-m", "pipx", "ensurepath"], { stdio: "inherit" })
73+
return
74+
} else if (await hasPipxBinary()) {
75+
notice("pipx module not found. Trying to install with pipx binary...")
76+
await execa("pipx", ["ensurepath"], { stdio: "inherit" })
77+
return
78+
} else {
79+
throw new Error("pipx module or pipx binary not found. Corrput pipx installation.")
80+
}
6581
} catch (err) {
6682
notice(`Failed to install pipx: ${(err as Error).toString()}. Ignoring...`)
6783
}
6884
}
6985

7086
async function setupVenv(foundPython: string) {
87+
if (await hasVenv(foundPython)) {
88+
info("venv module already installed.")
89+
return
90+
}
91+
7192
try {
72-
await setupPipPackWithPython(foundPython, "venv", undefined, { upgrade: false, usePipx: false })
93+
await setupPipPackSystem("venv")
7394
} catch (err) {
7495
info(`Failed to install venv: ${(err as Error).toString()}. Ignoring...`)
7596
}
7697
}
7798

99+
async function hasVenv(foundPython: string): Promise<boolean> {
100+
try {
101+
// check if venv module exits
102+
await execa(foundPython, ["-m", "venv", "-h"], { stdio: "ignore" })
103+
return true
104+
} catch {
105+
// if module not found, continue
106+
}
107+
return false
108+
}
109+
78110
/** Setup wheel and setuptools */
79111
async function setupWheel(foundPython: string) {
80112
try {

src/utils/setup/setupPipPack.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export async function setupPipPackWithPython(
5151
): Promise<InstallationInfo> {
5252
const { usePipx = true, user = true, upgrade = false, isLibrary = false } = options
5353

54-
const isPipx = usePipx && !isLibrary && (await hasPipx(givenPython))
54+
const isPipx = usePipx && !isLibrary && (await hasPipxModule(givenPython))
5555

5656
const pip = isPipx ? "pipx" : "pip"
5757

@@ -120,7 +120,11 @@ async function finishPipPackageInstall(givenPython: string, name: string) {
120120
return binDir
121121
}
122122

123-
export async function hasPipx(givenPython: string) {
123+
export async function hasPipxBinary() {
124+
return (await which("pipx", { nothrow: true })) !== null
125+
}
126+
127+
export async function hasPipxModule(givenPython: string) {
124128
const res = await execa(givenPython, ["-m", "pipx", "--help"], { stdio: "ignore", reject: false })
125129
return res.exitCode === 0
126130
}
@@ -282,6 +286,7 @@ export function setupPipPackSystem(name: string, addPythonPrefix = true) {
282286
return installAptPack([{ name: addPythonPrefix ? `python3-${name}` : name }])
283287
}
284288
} else if (process.platform === "darwin") {
289+
// brew doesn't have venv
285290
if (["venv"].includes(name)) {
286291
return null
287292
}

0 commit comments

Comments
 (0)