Skip to content

Commit 1e3faba

Browse files
committed
fix: avoid needing rcFile for nala lang settings
1 parent efbc01e commit 1e3faba

File tree

7 files changed

+76
-54
lines changed

7 files changed

+76
-54
lines changed

dist/actions/setup-cpp.js

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

dist/actions/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/legacy/setup-cpp.js

+5-5
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.js

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

dist/modern/setup-cpp.js.map

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

src/utils/setup/setupAptPack.ts

+58-36
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { execRoot, execRootSync } from "admina"
1+
import { defaultExecOptions, execRoot, execRootSync } from "admina"
22
import { GITHUB_ACTIONS } from "ci-info"
33
import { info, warning } from "ci-log"
44
import escapeRegex from "escape-string-regexp"
55
import { type ExecaError, execa } from "execa"
66
import { appendFile } from "fs/promises"
7-
import { addEnv, sourceRC } from "os-env"
7+
import memoize from "micro-memoize"
8+
import { sourceRC } from "os-env"
89
import { pathExists } from "path-exists"
910
import which from "which"
1011
import { rcOptions } from "../../cli-options.js"
@@ -37,8 +38,6 @@ export async function setupAptPack(packages: AptPackage[], update = false): Prom
3738
info(`Installing ${name} ${version ?? ""} via ${apt}`)
3839
}
3940

40-
process.env.DEBIAN_FRONTEND = "noninteractive"
41-
4241
// Update the repos if needed
4342
if (update) {
4443
updateRepos(apt)
@@ -63,13 +62,13 @@ export async function setupAptPack(packages: AptPackage[], update = false): Prom
6362

6463
// Install
6564
try {
66-
execRootSync(apt, ["install", "--fix-broken", "-y", ...needToInstall])
65+
execRootSync(apt, ["install", "--fix-broken", "-y", ...needToInstall], getAptExecOptions(apt))
6766
} catch (err) {
6867
if ("stderr" in (err as ExecaError)) {
6968
const stderr = (err as ExecaError).stderr
7069
if (retryErrors.some((error) => stderr.includes(error))) {
7170
warning(`Failed to install packages ${needToInstall}. Retrying...`)
72-
execRootSync(apt, ["install", "--fix-broken", "-y", "-o", aptTimeout, ...needToInstall])
71+
execRootSync(apt, ["install", "--fix-broken", "-y", "-o", aptTimeout, ...needToInstall], getAptExecOptions(apt))
7372
}
7473
} else {
7574
throw err
@@ -79,6 +78,42 @@ export async function setupAptPack(packages: AptPackage[], update = false): Prom
7978
return { binDir: "/usr/bin/" }
8079
}
8180

81+
export function hasNala() {
82+
return which.sync("nala", { nothrow: true }) !== null
83+
}
84+
85+
export function getApt() {
86+
let apt: string
87+
if (hasNala()) {
88+
apt = "nala"
89+
} else {
90+
apt = "apt-get"
91+
}
92+
return apt
93+
}
94+
95+
function getEnv(apt: string) {
96+
const env: NodeJS.ProcessEnv = { ...process.env, DEBIAN_FRONTEND: "noninteractive" }
97+
98+
if (apt === "nala") {
99+
// if LANG/LC_ALL is not set, enable utf8 otherwise nala fails because of ASCII encoding
100+
if (env.LANG === undefined) {
101+
env.LANG = "C.UTF-8"
102+
}
103+
if (env.LC_ALL === undefined) {
104+
env.LC_ALL = "C.UTF-8"
105+
}
106+
}
107+
108+
return env
109+
}
110+
111+
function getAptExecOptionsRaw(apt: string = "apt-get") {
112+
return { env: getEnv(apt), ...defaultExecOptions }
113+
}
114+
115+
const getAptExecOptions = memoize(getAptExecOptionsRaw)
116+
82117
export enum AptPackageType {
83118
NameDashVersion = 0,
84119
NameEqualsVersion = 1,
@@ -111,7 +146,7 @@ async function addRepositories(apt: string, packages: AptPackage[]) {
111146
await installAddAptRepo(apt)
112147
for (const repo of allRepositories) {
113148
// eslint-disable-next-line no-await-in-loop
114-
execRootSync("add-apt-repository", ["-y", "--no-update", repo])
149+
execRootSync("add-apt-repository", ["-y", "--no-update", repo], getAptExecOptions())
115150
}
116151
updateRepos(apt)
117152
didUpdate = true
@@ -124,15 +159,15 @@ export async function aptPackageType(name: string, version: string | undefined):
124159
"search",
125160
"--names-only",
126161
`^${escapeRegex(name)}-${escapeRegex(version)}$`,
127-
])
162+
], getAptExecOptions())
128163
if (stdout.trim() !== "") {
129164
return AptPackageType.NameDashVersion
130165
}
131166

132167
try {
133168
// check if apt-get show can find the version
134169
// eslint-disable-next-line @typescript-eslint/no-shadow
135-
const { stdout } = await execa("apt-cache", ["show", `${name}=${version}`])
170+
const { stdout } = await execa("apt-cache", ["show", `${name}=${version}`], getAptExecOptions())
136171
if (stdout.trim() === "") {
137172
return AptPackageType.NameEqualsVersion
138173
}
@@ -142,7 +177,7 @@ export async function aptPackageType(name: string, version: string | undefined):
142177
}
143178

144179
try {
145-
const { stdout: showStdout } = await execa("apt-cache", ["show", name])
180+
const { stdout: showStdout } = await execa("apt-cache", ["show", name], getAptExecOptions())
146181
if (showStdout.trim() !== "") {
147182
return AptPackageType.Name
148183
}
@@ -174,29 +209,23 @@ async function getAptArg(name: string, version: string | undefined) {
174209
}
175210
}
176211

177-
export function hasNala() {
178-
return which.sync("nala", { nothrow: true }) !== null
179-
}
180-
181-
export function getApt() {
182-
let apt: string
183-
if (hasNala()) {
184-
apt = "nala"
185-
} else {
186-
apt = "apt-get"
187-
}
188-
return apt
189-
}
190-
191212
function updateRepos(apt: string) {
192-
execRootSync(apt, apt !== "nala" ? ["update", "-y", "-o", aptTimeout] : ["update", "-o", aptTimeout])
213+
execRootSync(
214+
apt,
215+
apt !== "nala" ? ["update", "-y", "-o", aptTimeout] : ["update", "-o", aptTimeout],
216+
getAptExecOptions(apt),
217+
)
193218
}
194219

195220
async function installAddAptRepo(apt: string) {
196221
if (await isPackageInstalled("software-properties-common")) {
197222
return
198223
}
199-
execRootSync(apt, ["install", "-y", "--fix-broken", "-o", aptTimeout, "software-properties-common"])
224+
execRootSync(
225+
apt,
226+
["install", "-y", "--fix-broken", "-o", aptTimeout, "software-properties-common"],
227+
getAptExecOptions(apt),
228+
)
200229
}
201230

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

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

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

@@ -290,7 +312,7 @@ export async function updateAptAlternatives(name: string, path: string, rcPath:
290312
export async function isPackageInstalled(pack: string) {
291313
try {
292314
// check if a package is installed
293-
const { stdout } = await execa("dpkg", ["-s", pack])
315+
const { stdout } = await execa("dpkg", ["-s", pack], getAptExecOptions())
294316
if (typeof stdout !== "string") {
295317
return false
296318
}
@@ -305,7 +327,7 @@ export async function isPackageInstalled(pack: string) {
305327
export async function isPackageRegexInstalled(regexp: string) {
306328
try {
307329
// check if a package matching the regexp is installed
308-
const { stdout } = await execa("dpkg", ["-l", regexp])
330+
const { stdout } = await execa("dpkg", ["-l", regexp], getAptExecOptions())
309331
if (typeof stdout !== "string") {
310332
return false
311333
}

0 commit comments

Comments
 (0)