fix: use which if the pip package executable was not found

This commit is contained in:
Amin Yahyaabadi 2022-11-04 19:45:54 -07:00
parent e376f26cd2
commit 04867b97c5
7 changed files with 31 additions and 14 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -18,6 +18,7 @@ import { isBinUptoDate } from "../utils/setup/version"
import { getVersion } from "../versions/versions"
import assert from "assert"
import execa from "execa"
import { unique } from "../utils/std"
export async function setupPython(version: string, setupDir: string, arch: string) {
if (ciDetect() !== "github-actions") {
@ -129,7 +130,7 @@ export async function setupPythonAndPip(): Promise<string> {
}
export async function addPythonBaseExecPrefix(python: string) {
let dirs: string[] = []
const dirs: string[] = []
// detection based on the platform
if (process.platform === "linux") {
@ -143,11 +144,6 @@ export async function addPythonBaseExecPrefix(python: string) {
// any of these are possible depending on the operating system!
dirs.push(join(base_exec_prefix, "Scripts"), join(base_exec_prefix, "Scripts", "bin"), join(base_exec_prefix, "bin"))
// exclude the non existing ones
dirs = dirs.filter((dir) => existsSync(dir))
// add the directories to the path
await Promise.all(dirs.map((dir) => addPath(dir)))
return dirs
// remove duplicates
return unique(dirs)
}

View File

@ -4,7 +4,9 @@ import { info } from "@actions/core"
import { addPythonBaseExecPrefix, setupPythonAndPip } from "../../python/python"
import { InstallationInfo } from "./setupBin"
import { existsSync } from "fs"
import { addExeExt, join } from "patha"
import { addExeExt, dirname, join } from "patha"
import { addPath } from "../env/addEnv"
import which from "which"
let python: string | undefined
let binDirs: string[] | undefined
@ -25,7 +27,23 @@ export async function setupPipPack(name: string, version?: string): Promise<Inst
binDirs = await addPythonBaseExecPrefix(python)
}
const binDir = binDirs.find((dir) => existsSync(join(dir, addExeExt(name)))) ?? binDirs.pop()!
const binDir = findBinDir(binDirs, name)
await addPath(binDir)
return { binDir }
}
function findBinDir(dirs: string[], name: string) {
const foundDir = dirs.find((dir) => existsSync(join(dir, addExeExt(name))))
if (foundDir !== undefined) {
return foundDir
}
const whichDir = which.sync(addExeExt(name), { nothrow: true })
if (whichDir !== null) {
return dirname(whichDir)
}
return dirs[dirs.length - 1]
}

3
src/utils/std/index.ts Normal file
View File

@ -0,0 +1,3 @@
export function unique(dirs: string[]) {
return [...new Set(dirs)]
}