Files
Sebastian Poxhofer bf5ff94d28 refactor(manager): build artifact results with the shared helpers (#46092)
Migrate apm, bun, cargo, cocoapods, composer, conan, copier, deno,
devbox, gomod, helmv3, jsonnet-bundler, kustomize, mix, nuget and vendir
onto `fileAddition()`, `artifactError()` and `artifactErrorResult()`.
copier's private `artifactError()` is replaced by the shared one.

Behaviour change: artifact error text now comes from
`artifactErrorMessageFromExecError` everywhere, i.e. a non-blank
`err.stderr`, else a non-blank `err.stdout`, else `err.message`.
Previously apm used `${err}` ("Error: " prefix), cocoapods used a
nullish coalesce that kept an empty stderr, nuget preferred stdout over
stderr, and jsonnet-bundler emitted an undefined stderr with no
fallback. The rest used `err.message` only and are unchanged for errors
without output.
2026-09-16 13:13:41 +00:00

225 lines
6.4 KiB
TypeScript

import { isEmptyArray, isString } from '@sindresorhus/is';
import { quote } from 'shlex';
import { GlobalConfig } from '../../../config/global.ts';
import { TEMPORARY_ERROR } from '../../../constants/error-messages.ts';
import { logger } from '../../../logger/index.ts';
import { exec } from '../../../util/exec/index.ts';
import type { ExecOptions } from '../../../util/exec/types.ts';
import {
deleteLocalFile,
ensureCacheDir,
findLocalSiblingOrParent,
getSiblingFileName,
localPathExists,
readLocalFile,
writeLocalFile,
} from '../../../util/fs/index.ts';
import * as hostRules from '../../../util/host-rules.ts';
import { regEx } from '../../../util/regex.ts';
import type { UpdateArtifact, UpdateArtifactsResult } from '../types.ts';
import {
artifactError,
artifactErrorResult,
fileAddition,
resolveToolConstraint,
} from '../util.ts';
const hexRepoUrl = 'https://hex.pm/';
const hexRepoOrgUrlRegex = regEx(
`^https://hex\\.pm/api/repos/(?<organization>[a-z0-9_]+)/$`,
);
export async function updateArtifacts({
packageFileName,
updatedDeps,
newPackageFileContent,
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.debug(`mix.getArtifacts(${packageFileName})`);
const { isLockFileMaintenance } = config;
if (isEmptyArray(updatedDeps) && !isLockFileMaintenance) {
logger.debug('No updated mix deps');
return null;
}
let lockFileName = getSiblingFileName(packageFileName, 'mix.lock');
let isUmbrella = false;
let existingLockFileContent = await readLocalFile(lockFileName, 'utf8');
if (!existingLockFileContent) {
const lockFileError = await checkLockFileReadError(lockFileName);
if (lockFileError) {
return lockFileError;
}
const parentLockFileName = await findLocalSiblingOrParent(
packageFileName,
'mix.lock',
);
existingLockFileContent =
parentLockFileName && (await readLocalFile(parentLockFileName, 'utf8'));
if (parentLockFileName && existingLockFileContent) {
lockFileName = parentLockFileName;
isUmbrella = true;
} else if (parentLockFileName) {
const lockFileError = await checkLockFileReadError(parentLockFileName);
// v8 ignore else -- needs an umbrella parent lock file that reads cleanly
if (lockFileError) {
return lockFileError;
}
}
}
if (isLockFileMaintenance && isUmbrella) {
logger.debug(
`Cannot use lockFileMaintenance in an umbrella project, see ${GlobalConfig.get('productLinks').documentation}modules/manager/mix/#lockFileMaintenance`,
);
return null;
}
if (isLockFileMaintenance && !existingLockFileContent) {
logger.debug(
'Cannot use lockFileMaintenance when no mix.lock file is present',
);
return null;
}
try {
await writeLocalFile(packageFileName, newPackageFileContent);
if (isLockFileMaintenance) {
await deleteLocalFile(lockFileName);
}
} catch (err) {
logger.warn({ err }, 'mix.exs could not be written');
return artifactErrorResult(lockFileName, err);
}
if (!existingLockFileContent) {
logger.debug('No mix.lock found');
return null;
}
const organizations = new Set<string>();
const hexHostRulesWithMatchHost = hostRules
.getAll()
.filter(
(hostRule) =>
!!hostRule.matchHost && hexRepoOrgUrlRegex.test(hostRule.matchHost),
);
for (const { matchHost } of hexHostRulesWithMatchHost) {
// v8 ignore else -- the filter above already required a match host
if (matchHost) {
const result = hexRepoOrgUrlRegex.exec(matchHost);
// v8 ignore else -- the same regex already matched in that filter
if (result?.groups) {
const { organization } = result.groups;
organizations.add(organization);
}
}
}
for (const { packageName } of updatedDeps) {
if (packageName) {
const [, organization] = packageName.split(':');
// v8 ignore else -- needs an updated dep whose name carries no organization
if (organization) {
organizations.add(organization);
}
}
}
const preCommands = Array.from(organizations).reduce((acc, organization) => {
const url = `${hexRepoUrl}api/repos/${organization}/`;
const { token } = hostRules.find({ url });
if (token) {
logger.debug(`Authenticating to hex organization ${organization}`);
const authCommand = `mix hex.organization auth ${quote(organization)} --key ${quote(token)}`;
return [...acc, authCommand];
}
return acc;
}, [] as string[]);
// renovate: will update this
const erlangVersion = '26';
const execOptions: ExecOptions = {
extraEnv: {
// https://hexdocs.pm/mix/1.15.0/Mix.Tasks.Archive.html
// TODO: should include a version constraint
MIX_ARCHIVES: await ensureCacheDir('mix_archives'),
},
cwdFile: lockFileName,
docker: {},
toolConstraints: [
{
toolName: 'erlang',
// https://hexdocs.pm/elixir/1.14.5/compatibility-and-deprecations.html#compatibility-between-elixir-and-erlang-otp
constraint:
(await resolveToolConstraint(config, 'erlang')) ??
`^${erlangVersion}`,
},
{
toolName: 'elixir',
constraint: await resolveToolConstraint(config, 'elixir'),
},
],
preCommands,
};
let command: string;
if (isLockFileMaintenance) {
command = 'mix deps.get';
} else {
command = [
'mix',
'deps.update',
...updatedDeps
.map((dep) => dep.depName)
.filter(isString)
.map((dep) => quote(dep)),
].join(' ');
}
try {
await exec(command, execOptions);
} catch (err) {
/* v8 ignore if -- defensive rethrow of TEMPORARY_ERROR from exec, not reproduced in mix specs */
if (err.message === TEMPORARY_ERROR) {
throw err;
}
logger.debug(
{ err, message: err.message, command },
'Failed to update Mix lock file',
);
return artifactErrorResult(lockFileName, err);
}
const newMixLockContent = await readLocalFile(lockFileName, 'utf8');
if (existingLockFileContent === newMixLockContent) {
logger.debug('mix.lock is unchanged');
return null;
}
logger.debug('Returning updated mix.lock');
return [fileAddition(lockFileName, newMixLockContent)];
}
async function checkLockFileReadError(
lockFileName: string,
): Promise<UpdateArtifactsResult[] | null> {
if (await localPathExists(lockFileName)) {
return [artifactError(lockFileName, `Error reading ${lockFileName}`)];
}
return null;
}