Skip to content

Commit 9ab878f

Browse files
committed
fix: skip apt installation calls if package already installed
1 parent 3774e9b commit 9ab878f

File tree

8 files changed

+124
-101
lines changed

8 files changed

+124
-101
lines changed

dist/actions/setup-cpp.js

Lines changed: 27 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/actions/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js

Lines changed: 27 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.js

Lines changed: 27 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/llvm/llvm_installer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { execa } from "execa"
44
import { chmod, readFile, writeFile } from "fs/promises"
55
import { DEFAULT_TIMEOUT } from "../installTool"
66
import { addPath } from "../utils/env/addEnv"
7-
import { hasNala, isPackageInstalled, setupAptPack } from "../utils/setup/setupAptPack"
7+
import { hasNala, isPackageRegexInstalled, setupAptPack } from "../utils/setup/setupAptPack"
88
import type { InstallationInfo } from "../utils/setup/setupBin"
99

1010
export enum LLVMPackages {
@@ -87,7 +87,7 @@ async function removeConflictingPackages(givenScript: string) {
8787
await Promise.all(
8888
conflictingPackages.map(async (pack) => {
8989
const installingPack = pack.replace("$LLVM_VERSION", "*")
90-
if (await isPackageInstalled(installingPack)) {
90+
if (await isPackageRegexInstalled(installingPack)) {
9191
info(`Removing conflicting package ${installingPack}`)
9292
script = script.replace(pack, "")
9393
}

src/utils/setup/setupAptPack.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@ export async function setupAptPack(packages: AptPackage[], update = false): Prom
3535

3636
process.env.DEBIAN_FRONTEND = "noninteractive"
3737

38-
if (!didUpdate || update) {
39-
updateRepos(apt)
40-
didUpdate = true
41-
}
42-
43-
if (!didInit) {
44-
await initApt(apt)
45-
didInit = true
46-
}
47-
4838
const allRepositories = [...new Set(packages.flatMap((pack) => pack.repositories ?? []))]
4939

5040
if (allRepositories.length !== 0) {
@@ -56,15 +46,33 @@ export async function setupAptPack(packages: AptPackage[], update = false): Prom
5646
updateRepos(apt)
5747
}
5848

59-
const aptArgs = await Promise.all(packages.map((pack) => getAptArg(pack.name, pack.version)))
49+
let qualifiedPacks = await Promise.all(packages.map((pack) => getAptArg(pack.name, pack.version)))
50+
51+
// find the packages that are not installed
52+
qualifiedPacks = await Promise.all(qualifiedPacks.filter(async (pack) => !(await isPackageInstalled(pack))))
53+
54+
if (qualifiedPacks.length === 0) {
55+
return { binDir: "/usr/bin/" }
56+
}
57+
58+
if (!didUpdate || update) {
59+
updateRepos(apt)
60+
didUpdate = true
61+
}
62+
63+
if (!didInit) {
64+
await initApt(apt)
65+
didInit = true
66+
}
67+
6068
try {
61-
execRootSync(apt, ["install", "--fix-broken", "-y", ...aptArgs])
69+
execRootSync(apt, ["install", "--fix-broken", "-y", ...qualifiedPacks])
6270
} catch (err) {
6371
if ("stderr" in (err as ExecaError)) {
6472
const stderr = (err as ExecaError).stderr
6573
if (retryErrors.some((error) => stderr.includes(error))) {
66-
warning(`Failed to install packages ${aptArgs}. Retrying...`)
67-
execRootSync(apt, ["install", "--fix-broken", "-y", ...aptArgs])
74+
warning(`Failed to install packages ${qualifiedPacks}. Retrying...`)
75+
execRootSync(apt, ["install", "--fix-broken", "-y", ...qualifiedPacks])
6876
}
6977
} else {
7078
throw err
@@ -229,7 +237,22 @@ export async function updateAptAlternatives(name: string, path: string, priority
229237
}
230238
}
231239

232-
export async function isPackageInstalled(regexp: string) {
240+
export async function isPackageInstalled(pack: string) {
241+
try {
242+
// check if a package is installed
243+
const { stdout } = await execa("dpkg", ["-s", pack])
244+
if (typeof stdout !== "string") {
245+
return false
246+
}
247+
const lines = stdout.split("\n")
248+
// check if the output contains a line that starts with "Status: install ok installed"
249+
return lines.some((line) => line.startsWith("Status: install ok installed"))
250+
} catch {
251+
return false
252+
}
253+
}
254+
255+
export async function isPackageRegexInstalled(regexp: string) {
233256
try {
234257
// check if a package matching the regexp is installed
235258
const { stdout } = await execa("dpkg", ["-l", regexp])

0 commit comments

Comments
 (0)