Skip to content

Commit 4ebcbc1

Browse files
committed
fix: fix checking of a pip package existence
1 parent d316735 commit 4ebcbc1

File tree

6 files changed

+87
-74
lines changed

6 files changed

+87
-74
lines changed

dist/legacy/setup-cpp.js

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.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.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/modern/setup-cpp.mjs.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.

packages/setup-apt/src/install.ts

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -68,55 +68,60 @@ const retryErrors = [
6868
* ```
6969
*/
7070
export async function installAptPack(packages: AptPackage[], update = false): Promise<InstallationInfo> {
71-
const apt: string = getApt()
72-
73-
for (const { name, version } of packages) {
74-
info(`Installing ${name} ${version ?? ""} via ${apt}`)
75-
}
71+
try {
72+
const apt: string = getApt()
7673

77-
// Update the repos if needed
78-
if (update) {
79-
updateAptReposMemoized(apt)
80-
}
74+
for (const { name, version } of packages) {
75+
info(`Installing ${name} ${version ?? ""} via ${apt}`)
76+
}
8177

82-
// Add the repos if needed
83-
await addRepositories(apt, packages)
78+
// Update the repos if needed
79+
if (update) {
80+
updateAptReposMemoized(apt)
81+
}
8482

85-
const needToInstall = await filterAndQualifyAptPackages(packages, apt)
83+
// Add the repos if needed
84+
await addRepositories(apt, packages)
8685

87-
if (needToInstall.length === 0) {
88-
info("All packages are already installed")
89-
return { binDir: "/usr/bin/" }
90-
}
86+
const needToInstall = await filterAndQualifyAptPackages(packages, apt)
9187

92-
// Initialize apt if needed
93-
await initAptMemoized(apt)
88+
if (needToInstall.length === 0) {
89+
info("All packages are already installed")
90+
return { binDir: "/usr/bin/" }
91+
}
9492

95-
try {
96-
// Add the keys if needed
97-
await addAptKeys(packages)
98-
99-
// Install
100-
execRootSync(apt, ["install", "--fix-broken", "-y", ...needToInstall], {
101-
...defaultExecOptions,
102-
env: getAptEnv(apt),
103-
})
104-
} catch (err) {
105-
if (isExecaError(err)) {
106-
if (retryErrors.some((error) => err.stderr.includes(error))) {
107-
warning(`Failed to install packages ${needToInstall}. Retrying...`)
108-
execRootSync(
109-
apt,
110-
["install", "--fix-broken", "-y", "-o", aptTimeout, ...needToInstall],
111-
{ ...defaultExecOptions, env: getAptEnv(apt) },
112-
)
93+
// Initialize apt if needed
94+
await initAptMemoized(apt)
95+
96+
try {
97+
// Add the keys if needed
98+
await addAptKeys(packages)
99+
100+
// Install
101+
execRootSync(apt, ["install", "--fix-broken", "-y", ...needToInstall], {
102+
...defaultExecOptions,
103+
env: getAptEnv(apt),
104+
})
105+
} catch (err) {
106+
if (isExecaError(err)) {
107+
if (retryErrors.some((error) => err.stderr.includes(error))) {
108+
warning(`Failed to install packages ${needToInstall}. Retrying...`)
109+
execRootSync(
110+
apt,
111+
["install", "--fix-broken", "-y", "-o", aptTimeout, ...needToInstall],
112+
{ ...defaultExecOptions, env: getAptEnv(apt) },
113+
)
114+
}
115+
} else {
116+
throw err
113117
}
114-
} else {
115-
throw err
116118
}
117-
}
118119

119-
return { binDir: "/usr/bin/" }
120+
return { binDir: "/usr/bin/" }
121+
} catch (err) {
122+
const msg = err instanceof Error ? `${err.message}\n${err.stack}` : String(err)
123+
throw new Error(`Failed to install apt packages: ${msg}`)
124+
}
120125
}
121126

122127
async function addRepositories(apt: string, packages: AptPackage[]) {

src/utils/setup/setupPipPack.ts

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,16 @@ export async function setupPipPackWithPython(
5555

5656
// if upgrade is not requested, check if the package is already installed, and return if it is
5757
if (!upgrade) {
58-
const installed = await pipPackageIsInstalled(givenPython, pip, name)
58+
const installed = isPipx
59+
? await pipxPackageInstalled(givenPython, name)
60+
: await pipPackageIsInstalled(givenPython, name)
5961
if (installed) {
6062
const binDir = await finishPipPackageInstall(givenPython, name)
6163
return { binDir }
6264
}
6365
}
6466

65-
const hasPackage = await pipHasPackage(givenPython, pip, name)
67+
const hasPackage = await pipHasPackage(givenPython, name)
6668
if (hasPackage) {
6769
try {
6870
info(`Installing ${name} ${version ?? ""} via ${pip}`)
@@ -84,7 +86,8 @@ export async function setupPipPackWithPython(
8486
env,
8587
})
8688
} catch (err) {
87-
info(`Failed to install ${name} via ${pip}: ${err}.`)
89+
const msg = err instanceof Error ? `${err.message}\n${err.stack}` : String(err)
90+
info(`Failed to install ${name} via ${pip}: ${msg}`)
8891
if ((await setupPipPackSystem(name)) === null) {
8992
throw new Error(`Failed to install ${name} via ${pip}: ${err}.`)
9093
}
@@ -168,6 +171,18 @@ async function getPython(): Promise<string> {
168171
return pythonBin
169172
}
170173

174+
async function pipPackageIsInstalled(python: string, name: string) {
175+
try {
176+
const result = await execa(python, ["-m", "pip", "-qq", "show", name], {
177+
stdio: "ignore",
178+
reject: false,
179+
})
180+
return result.exitCode === 0
181+
} catch {
182+
return false
183+
}
184+
}
185+
171186
type PipxShowType = {
172187
venvs: Record<string, {
173188
metadata: {
@@ -180,42 +195,35 @@ type PipxShowType = {
180195
}>
181196
}
182197

183-
async function pipPackageIsInstalled(python: string, pip: string, name: string) {
198+
async function pipxPackageInstalled(python: string, name: string) {
184199
try {
185-
if (pip === "pipx") {
186-
const result = await execa(python, ["-m", pip, "list", "--json"], {
187-
stdio: "ignore",
188-
reject: false,
189-
})
190-
if (result.exitCode !== 0 || typeof result.stdout !== "string") {
191-
return false
192-
}
200+
const result = await execa(python, ["-m", "pipx", "list", "--json"], {
201+
stdio: "ignore",
202+
reject: false,
203+
})
204+
if (result.exitCode !== 0 || typeof result.stdout !== "string") {
205+
return false
206+
}
193207

194-
const pipxOut = JSON.parse(result.stdout as unknown as string) as PipxShowType
195-
// search among the venvs
196-
if (name in pipxOut.venvs) {
208+
const pipxOut = JSON.parse(result.stdout) as PipxShowType
209+
// search among the venvs
210+
if (name in pipxOut.venvs) {
211+
return true
212+
}
213+
// search among the urls
214+
for (const venv of Object.values(pipxOut.venvs)) {
215+
if (venv.metadata.main_package.package_or_url === name || venv.metadata.main_package.package === name) {
197216
return true
198217
}
199-
// search among the urls
200-
for (const venv of Object.values(pipxOut.venvs)) {
201-
if (venv.metadata.main_package.package_or_url === name || venv.metadata.main_package.package === name) {
202-
return true
203-
}
204-
}
205218
}
206-
207-
const result = await execa(python, ["-m", pip, "-qq", "show", name], {
208-
stdio: "ignore",
209-
reject: false,
210-
})
211-
return result.exitCode === 0
212219
} catch {
213-
return false
220+
// ignore
214221
}
222+
return false
215223
}
216224

217-
async function pipHasPackage(python: string, pip: string, name: string) {
218-
const result = await execa(python, ["-m", pip, "-qq", "index", "versions", name], {
225+
async function pipHasPackage(python: string, name: string) {
226+
const result = await execa(python, ["-m", "pip", "-qq", "index", "versions", name], {
219227
stdio: "ignore",
220228
reject: false,
221229
})

0 commit comments

Comments
 (0)