Skip to content

Commit

Permalink
fix: inline the exec options into all apt exec calls + improve error …
Browse files Browse the repository at this point in the history
…handling
  • Loading branch information
aminya committed Aug 16, 2024
1 parent 080dafd commit 91ff4c5
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 121 deletions.
60 changes: 30 additions & 30 deletions dist/actions/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/actions/setup-cpp.js.map

Large diffs are not rendered by default.

60 changes: 30 additions & 30 deletions dist/legacy/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/legacy/setup-cpp.js.map

Large diffs are not rendered by default.

60 changes: 30 additions & 30 deletions dist/modern/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/modern/setup-cpp.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/installTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export async function installTool(
} catch (e) {
// push error message to the logger
error(e as string | Error)
if (e instanceof Error && e.stack !== undefined) {
error(e.stack)
}
errorMessages.push(`${tool} failed to install`)
}
endGroup()
Expand Down
50 changes: 22 additions & 28 deletions src/utils/setup/setupAptPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,15 @@ export async function setupAptPack(packages: AptPackage[], update = false): Prom

// Install
try {
execRootSync(apt, ["install", "--fix-broken", "-y", ...needToInstall], getAptExecOptions({ apt }))
execRootSync(apt, ["install", "--fix-broken", "-y", ...needToInstall], { ...defaultExecOptions, env: getEnv(apt) })
} catch (err) {
if ("stderr" in (err as ExecaError)) {
const stderr = (err as ExecaError).stderr
if (retryErrors.some((error) => stderr.includes(error))) {
if (isExecaError(err)) {
if (retryErrors.some((error) => err.stderr.includes(error))) {
warning(`Failed to install packages ${needToInstall}. Retrying...`)
execRootSync(
apt,
["install", "--fix-broken", "-y", "-o", aptTimeout, ...needToInstall],
getAptExecOptions({ apt }),
{ ...defaultExecOptions, env: getEnv(apt) },
)
}
} else {
Expand All @@ -82,6 +81,10 @@ export async function setupAptPack(packages: AptPackage[], update = false): Prom
return { binDir: "/usr/bin/" }
}

function isExecaError(err: unknown): err is ExecaError {
return typeof (err as ExecaError).stderr === "string"
}

export function hasNala() {
return which.sync("nala", { nothrow: true }) !== null
}
Expand Down Expand Up @@ -112,18 +115,6 @@ function getEnv(apt: string) {
return env
}

function getAptExecOptionsRaw(givenOpts: { apt?: string; pipe?: boolean } = {}): SyncOptions {
const opts = {
apt: "apt-get",
pipe: false,
...givenOpts,
}

return { env: getEnv(opts.apt), ...defaultExecOptions, stdio: opts.pipe ? "pipe" : "inherit" }
}

const getAptExecOptions = memoize(getAptExecOptionsRaw)

export enum AptPackageType {
NameDashVersion = 0,
NameEqualsVersion = 1,
Expand Down Expand Up @@ -156,7 +147,7 @@ async function addRepositories(apt: string, packages: AptPackage[]) {
await installAddAptRepo(apt)
for (const repo of allRepositories) {
// eslint-disable-next-line no-await-in-loop
execRootSync("add-apt-repository", ["-y", "--no-update", repo], getAptExecOptions())
execRootSync("add-apt-repository", ["-y", "--no-update", repo], { ...defaultExecOptions, env: getEnv(apt) })
}
updateRepos(apt)
didUpdate = true
Expand All @@ -169,15 +160,15 @@ async function aptPackageType(apt: string, name: string, version: string | undef
"search",
"--names-only",
`^${escapeRegex(name)}-${escapeRegex(version)}$`,
], getAptExecOptions({ apt, pipe: true }))
if (stdout.trim() !== "") {
], { env: getEnv(apt), stdio: "pipe" })
if (typeof stdout === "string" && stdout.trim().split("\n").some((line) => line.startsWith(`${name}-${version}`))) {
return AptPackageType.NameDashVersion
}

try {
// check if apt-get show can find the version
// eslint-disable-next-line @typescript-eslint/no-shadow
const { stdout } = await execa("apt-cache", ["show", `${name}=${version}`], getAptExecOptions())
const { stdout } = await execa("apt-cache", ["show", `${name}=${version}`], { env: getEnv(apt) })
if (stdout.trim() === "") {
return AptPackageType.NameEqualsVersion
}
Expand All @@ -187,7 +178,7 @@ async function aptPackageType(apt: string, name: string, version: string | undef
}

try {
const { stdout: showStdout } = await execa("apt-cache", ["show", name], getAptExecOptions({ pipe: true }))
const { stdout: showStdout } = await execa("apt-cache", ["show", name], { env: getEnv(apt), stdio: "pipe" })
if (showStdout.trim() !== "") {
return AptPackageType.Name
}
Expand All @@ -214,7 +205,7 @@ async function getAptArg(apt: string, name: string, version: string | undefined)
return `${name}=${version}`
case AptPackageType.Name:
if (version !== undefined) {
warning(`Could not find package ${name} ${version}. Installing the latest version.`)
warning(`Could not find package ${name} with version ${version}. Installing the latest version.`)
}
return name
default:
Expand All @@ -226,7 +217,7 @@ function updateRepos(apt: string) {
execRootSync(
apt,
apt !== "nala" ? ["update", "-y", "-o", aptTimeout] : ["update", "-o", aptTimeout],
getAptExecOptions({ apt }),
{ ...defaultExecOptions, env: getEnv(apt) },
)
}

Expand All @@ -237,7 +228,7 @@ async function installAddAptRepo(apt: string) {
execRootSync(
apt,
["install", "-y", "--fix-broken", "-o", aptTimeout, "software-properties-common"],
getAptExecOptions({ apt }),
{ ...defaultExecOptions, env: getEnv(apt) },
)
}

Expand All @@ -256,7 +247,10 @@ async function initApt(apt: string) {
])

if (toInstall.length !== 0) {
execRootSync(apt, ["install", "-y", "--fix-broken", "-o", aptTimeout, ...toInstall], getAptExecOptions({ apt }))
execRootSync(apt, ["install", "-y", "--fix-broken", "-o", aptTimeout, ...toInstall], {
...defaultExecOptions,
env: getEnv(apt),
})
}

const promises: Promise<string | void>[] = [
Expand Down Expand Up @@ -325,7 +319,7 @@ export async function updateAptAlternatives(name: string, path: string, rcPath:
export async function isPackageInstalled(pack: string) {
try {
// check if a package is installed
const { stdout } = await execa("dpkg", ["-s", pack], getAptExecOptions({ pipe: true }))
const { stdout } = await execa("dpkg", ["-s", pack], { env: getEnv("apt-get"), stdio: "pipe" })
if (typeof stdout !== "string") {
return false
}
Expand All @@ -340,7 +334,7 @@ export async function isPackageInstalled(pack: string) {
export async function isPackageRegexInstalled(regexp: string) {
try {
// check if a package matching the regexp is installed
const { stdout } = await execa("dpkg", ["-l", regexp], getAptExecOptions({ pipe: true }))
const { stdout } = await execa("dpkg", ["-l", regexp], { env: getEnv("apt-get"), stdio: "pipe" })
if (typeof stdout !== "string") {
return false
}
Expand Down

0 comments on commit 91ff4c5

Please sign in to comment.