Skip to content

Commit

Permalink
fix: avoid needing rcFile for nala lang settings
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Aug 16, 2024
1 parent efbc01e commit e74799b
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 66 deletions.
2 changes: 0 additions & 2 deletions dist/actions/hdi.23cd1c99.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/actions/hdi.23cd1c99.js.map

This file was deleted.

2 changes: 1 addition & 1 deletion dist/actions/hdi.647acde1.js.map

Large diffs are not rendered by default.

10 changes: 5 additions & 5 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.

2 changes: 1 addition & 1 deletion dist/legacy/hdi.619de66c.js.map

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions dist/legacy/hdi.bbd33e35.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/legacy/hdi.bbd33e35.js.map

This file was deleted.

10 changes: 5 additions & 5 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.

2 changes: 0 additions & 2 deletions dist/modern/hdi.23cd1c99.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/modern/hdi.23cd1c99.js.map

This file was deleted.

2 changes: 1 addition & 1 deletion dist/modern/hdi.647acde1.js.map

Large diffs are not rendered by default.

10 changes: 5 additions & 5 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.

94 changes: 58 additions & 36 deletions src/utils/setup/setupAptPack.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { execRoot, execRootSync } from "admina"
import { defaultExecOptions, execRoot, execRootSync } from "admina"
import { GITHUB_ACTIONS } from "ci-info"
import { info, warning } from "ci-log"
import escapeRegex from "escape-string-regexp"
import { type ExecaError, execa } from "execa"
import { appendFile } from "fs/promises"
import { addEnv, sourceRC } from "os-env"
import memoize from "micro-memoize"
import { sourceRC } from "os-env"
import { pathExists } from "path-exists"
import which from "which"
import { rcOptions } from "../../cli-options.js"
Expand Down Expand Up @@ -37,8 +38,6 @@ export async function setupAptPack(packages: AptPackage[], update = false): Prom
info(`Installing ${name} ${version ?? ""} via ${apt}`)
}

process.env.DEBIAN_FRONTEND = "noninteractive"

// Update the repos if needed
if (update) {
updateRepos(apt)
Expand All @@ -63,13 +62,13 @@ export async function setupAptPack(packages: AptPackage[], update = false): Prom

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

export function hasNala() {
return which.sync("nala", { nothrow: true }) !== null
}

export function getApt() {
let apt: string
if (hasNala()) {
apt = "nala"
} else {
apt = "apt-get"
}
return apt
}

function getEnv(apt: string) {
const env: NodeJS.ProcessEnv = { ...process.env, DEBIAN_FRONTEND: "noninteractive" }

if (apt === "nala") {
// if LANG/LC_ALL is not set, enable utf8 otherwise nala fails because of ASCII encoding
if (env.LANG === undefined) {
env.LANG = "C.UTF-8"
}
if (env.LC_ALL === undefined) {
env.LC_ALL = "C.UTF-8"
}
}

return env
}

function getAptExecOptionsRaw(apt: string = "apt-get") {
return { env: getEnv(apt), ...defaultExecOptions }
}

const getAptExecOptions = memoize(getAptExecOptionsRaw)

export enum AptPackageType {
NameDashVersion = 0,
NameEqualsVersion = 1,
Expand Down Expand Up @@ -111,7 +146,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])
execRootSync("add-apt-repository", ["-y", "--no-update", repo], getAptExecOptions())
}
updateRepos(apt)
didUpdate = true
Expand All @@ -124,15 +159,15 @@ export async function aptPackageType(name: string, version: string | undefined):
"search",
"--names-only",
`^${escapeRegex(name)}-${escapeRegex(version)}$`,
])
], getAptExecOptions())
if (stdout.trim() !== "") {
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}`])
const { stdout } = await execa("apt-cache", ["show", `${name}=${version}`], getAptExecOptions())
if (stdout.trim() === "") {
return AptPackageType.NameEqualsVersion
}
Expand All @@ -142,7 +177,7 @@ export async function aptPackageType(name: string, version: string | undefined):
}

try {
const { stdout: showStdout } = await execa("apt-cache", ["show", name])
const { stdout: showStdout } = await execa("apt-cache", ["show", name], getAptExecOptions())
if (showStdout.trim() !== "") {
return AptPackageType.Name
}
Expand Down Expand Up @@ -174,29 +209,23 @@ async function getAptArg(name: string, version: string | undefined) {
}
}

export function hasNala() {
return which.sync("nala", { nothrow: true }) !== null
}

export function getApt() {
let apt: string
if (hasNala()) {
apt = "nala"
} else {
apt = "apt-get"
}
return apt
}

function updateRepos(apt: string) {
execRootSync(apt, apt !== "nala" ? ["update", "-y", "-o", aptTimeout] : ["update", "-o", aptTimeout])
execRootSync(
apt,
apt !== "nala" ? ["update", "-y", "-o", aptTimeout] : ["update", "-o", aptTimeout],
getAptExecOptions(apt),
)
}

async function installAddAptRepo(apt: string) {
if (await isPackageInstalled("software-properties-common")) {
return
}
execRootSync(apt, ["install", "-y", "--fix-broken", "-o", aptTimeout, "software-properties-common"])
execRootSync(
apt,
["install", "-y", "--fix-broken", "-o", aptTimeout, "software-properties-common"],
getAptExecOptions(apt),
)
}

/** Install gnupg and certificates (usually missing from docker containers) */
Expand All @@ -214,20 +243,13 @@ async function initApt(apt: string) {
])

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

const promises: Promise<string | void>[] = [
addAptKeyViaServer(["3B4FE6ACC0B21F32", "40976EAF437D05B5"], "setup-cpp-ubuntu-archive.gpg"),
addAptKeyViaServer(["1E9377A2BA9EF27F"], "launchpad-toolchain.gpg"),
]
if (apt === "nala") {
// If LANGE/LC_ALL is not set, enable utf8 otherwise nala fails because of ASCII encoding
promises.push(
addEnv("LANG", "C.UTF-8", { overwrite: false, ...rcOptions }),
addEnv("LC_ALL", "C.UTF-8", { overwrite: false, ...rcOptions }),
)
}
await Promise.all(promises)
}

Expand Down Expand Up @@ -290,7 +312,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])
const { stdout } = await execa("dpkg", ["-s", pack], getAptExecOptions())
if (typeof stdout !== "string") {
return false
}
Expand All @@ -305,7 +327,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])
const { stdout } = await execa("dpkg", ["-l", regexp], getAptExecOptions())
if (typeof stdout !== "string") {
return false
}
Expand Down

0 comments on commit e74799b

Please sign in to comment.