Files
renovate/lib/modules/manager/pub/artifacts.ts
Sebastian Poxhofer 9fc451c44d refactor(manager): share the lock file updateArtifacts skeleton (#46093)
Migrate pub, gleam, helmfile, pixi, mise, poetry, pep621/pdm and
pep621/uv onto `updateLockFile()`, so each manager keeps only its
command and `ExecOptions` construction.

Behaviour changes:

- Artifact error text now comes from `artifactErrorMessageFromExecError`,
  i.e. a non-blank `err.stderr`, else a non-blank `err.stdout`, else
  `err.message`. Previously pixi used `${err}` ("Error: " prefix), mise
  joined stdout, stderr and message, and poetry rendered
  "<stdout>\n<stderr>" (literally "undefined\nundefined" when the error
  carried neither).
- helmfile, pixi, poetry, pdm and uv now return null instead of an
  addition when the lock file is missing or empty after the run, which
  is what gleam and mise already did.
- Unchanged/missing lock file debug messages are now uniformly
  "<lockFileName> is unchanged" / "No <lockFileName> found". pub gains
  such a message, mise loses "Returning updated mise lock file" and
  poetry loses "Returning updated <lockFileName>".
2026-09-16 13:13:42 +00:00

114 lines
3.7 KiB
TypeScript

import { isEmptyArray, isString } from '@sindresorhus/is';
import { quote } from 'shlex';
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 { getSiblingFileName, readLocalFile } from '../../../util/fs/index.ts';
import type {
UpdateArtifact,
UpdateArtifactsResult,
Upgrade,
} from '../types.ts';
import {
artifactErrorResult,
resolveToolConstraint,
updateLockFile,
} from '../util.ts';
import { parsePubspec, parsePubspecLock } from './utils.ts';
const SDK_NAMES = ['dart', 'flutter'];
const PUB_GET_COMMAND = 'pub get --no-precompile';
export async function updateArtifacts({
packageFileName,
updatedDeps,
newPackageFileContent,
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.debug(`pub.updateArtifacts(${packageFileName})`);
const { isLockFileMaintenance } = config;
if (isEmptyArray(updatedDeps) && !isLockFileMaintenance) {
logger.debug('No updated pub deps - returning null');
return null;
}
const lockFileName = getSiblingFileName(packageFileName, 'pubspec.lock');
const oldLockFileContent = await readLocalFile(lockFileName, 'utf8');
if (!oldLockFileContent) {
logger.debug('No pubspec.lock found');
return null;
}
try {
const isFlutter = newPackageFileContent.includes('sdk: flutter');
const toolName = isFlutter ? 'flutter' : 'dart';
const cmd = getExecCommand(toolName, updatedDeps, isLockFileMaintenance);
const constraint = await resolveToolConstraint(config, toolName, () => {
const pubspec = parsePubspec(packageFileName, newPackageFileContent);
const pubspecToolName = isFlutter ? 'flutter' : 'sdk';
const pubspecConstraint = pubspec?.environment[pubspecToolName];
if (pubspecConstraint) {
return pubspecConstraint;
}
const pubspecLock = parsePubspecLock(lockFileName, oldLockFileContent);
return pubspecLock?.sdks[toolName];
});
const execOptions: ExecOptions = {
cwdFile: packageFileName,
docker: {},
toolConstraints: [
{
toolName,
constraint,
},
],
};
return await updateLockFile({
lockFileName,
existingLockFileContent: oldLockFileContent,
packageFile: { path: packageFileName, contents: newPackageFileContent },
run: () => exec(cmd, execOptions),
});
} catch (err) {
/* v8 ignore if -- defensive rethrow, not reproduced in the pub specs */
if (err.message === TEMPORARY_ERROR) {
throw err;
}
logger.warn({ lockfile: lockFileName, err }, `Failed to update lock file`);
return artifactErrorResult(lockFileName, err);
}
}
function getExecCommand(
toolName: string,
updatedDeps: Upgrade<Record<string, unknown>>[],
isLockFileMaintenance: boolean | undefined,
): string {
if (isLockFileMaintenance) {
return `${toolName} pub upgrade`;
}
const depNames = updatedDeps.map((dep) => dep.depName).filter(isString);
if (depNames.length === 1 && SDK_NAMES.includes(depNames[0])) {
return `${toolName} ${PUB_GET_COMMAND}`;
}
// If there are two updated dependencies and both of them are SDK updates (Dart and Flutter),
// we use Flutter over Dart to run `pub get` as it is a Flutter project.
if (
depNames.length === 2 &&
depNames.filter((depName) => SDK_NAMES.includes(depName)).length === 2
) {
return `flutter ${PUB_GET_COMMAND}`;
}
const depNamesCmd = depNames
.filter((depName) => !SDK_NAMES.includes(depName))
.map(quote)
.join(' ');
return `${toolName} pub upgrade ${depNamesCmd}`;
}