Files
renovate/lib/modules/manager/gradle/artifacts.ts
Sebastian Poxhofer 4b4d14ec00 feat(manager): pass extracted constraints to updateArtifacts (#45826)
* feat(manager): pass extracted constraints to updateArtifacts

`extractedConstraints` were collected during extraction but only ever read
by `fetch.ts` for the lookup. At artifact time every manager had to derive
the same tool constraints a second time, and when the updated package file
no longer carried the constraint there was nothing to fall back to.

`generateBranchConfig()` now merges the `extractedConstraints` of all
upgrades of a branch onto the branch config, which reaches `updateArtifacts()`
through `patchConfigForArtifactsUpdate()`. `UpdateArtifactsConfig` declares
the field and documents what it contains.

The new `resolveToolConstraint()` helper implements the ladder once:
user `constraints` > an optional artifact-time derivation > the extracted
value. The derivation is ranked above the extracted value on purpose: it
reads the package files as the branch changed them, so poetry (python is an
updatable dependency there), composer, pipenv (`pipenv` can be a dev
package) and pep621 (`requires-python` is an updatable dependency) would
otherwise regress to a stale constraint. pipenv, pep621 uv/pdm, poetry and
composer are migrated to the helper and keep their derivation.

Behaviour change: these managers now use the extract-time constraint as a
last resort instead of running unconstrained, for example on lock file
maintenance where pipenv deletes `Pipfile.lock` before deriving.

`execute-post-upgrade-commands.ts` is unaffected: it reads
`upgrade.constraints` per upgrade, and the extracted values are kept in a
separate field rather than folded into `constraints`, so post-upgrade task
tool constraints still come from user config only.

Not migrated, because their constraints are never extracted, so the
fallback would be dead code: bundler (`ruby`/`bundler`), pixi (`pixi`),
pep621 uv (`required-version`) and npm's node ladder, whose last step
already reads `package.json` engines.

* refactor(manager): resolve tool constraints through resolveToolConstraint()

Every manager now obtains the constraint of a tool it runs through
`resolveToolConstraint()` instead of reading `config.constraints` itself, so
the precedence "user config, then a value derived from the updated files, then
the constraints collected during extraction" applies everywhere. The helper
treats an empty string as not set at every step and accepts the
`PostUpdateConfig` shape, whose `constraints` may be `null`.

Behaviour changes that go beyond the new fallback to extracted constraints:

- `mix` applies its hardcoded erlang default only after the extracted
  constraint, so an erlang version collected at extraction can win over it.
- `kustomize` decides `HELM_EXPERIMENTAL_OCI` from the fully resolved helm
  constraint; `generateHelmEnvs()` takes that value instead of the config.
- `npm` (yarn): an upgrade of yarn without a new value now consults the user
  constraint before deriving one from package.json or the lock file.
- The "Using <tool> constraint from config" debug lines in bundler, composer,
  copier and pip-compile are gone, as the helper is the only reader of the
  config now.

Helpers that read the config synchronously became async: bundler
`getBundlerConstraint()`, copier `getPythonVersionConstraint()` and
`getCopierVersionConstraint()`, pip-compile `get*VersionConstraint()` and
`getToolVersionConstraint()`, gomod `deriveGoToolchainConstraints()` and
`getUpdateImportPathCmds()`, mise `getMiseLockToolConstraints()`. composer
`getPhpConstraint()` is removed.

Each converted manager has a test showing the extracted constraint reaching
the executed tool, and, where a derive callback exists, that the derived value
still wins over it.

* refactor(linters): add prefer-resolve-tool-constraint rule

Reports member reads and destructuring of `constraints` and
`extractedConstraints` from a manager config in `lib/modules/manager/**`, so
that tool constraints are resolved through `resolveToolConstraint()` in
`lib/modules/manager/util.ts` only. Writes and the same properties on package
files, which extraction has to populate, are not reported. The helper itself
and spec files are exempt in `.oxlintrc.json`.

* refactor(manager): inline thin resolveToolConstraint wrappers

Drop copier/utils.ts and the pip-compile getPipToolsVersionConstraint /
getUvVersionConstraint helpers, which only forwarded to
resolveToolConstraint(). Callers now call it directly.

* docs(development): note lock file maintenance in tool constraints section

* refactor(manager): address review on tool constraint resolution

composer only runs extractConstraints() when a user constraint is missing,
via Lazy. UpdateArtifactsConfig no longer re-declares constraints and
extractedConstraints, which ToolConstraintsConfig already provides.
2026-09-14 11:09:07 +00:00

295 lines
8.6 KiB
TypeScript

import { isNonEmptyStringAndNotWhitespace } from '@sindresorhus/is';
import { quote } from 'shlex';
import upath from 'upath';
import { GlobalConfig } from '../../../config/global.ts';
import { TEMPORARY_ERROR } from '../../../constants/error-messages.ts';
import { logger } from '../../../logger/index.ts';
import {
exec,
getToolSettingsOptions,
gradleJvmArg,
} from '../../../util/exec/index.ts';
import type { ExecOptions } from '../../../util/exec/types.ts';
import {
findUpLocal,
readLocalFile,
writeLocalFile,
} from '../../../util/fs/index.ts';
import { getFiles, getRepoStatus } from '../../../util/git/index.ts';
import { regEx } from '../../../util/regex.ts';
import { scm } from '../../platform/scm.ts';
import {
extraEnv,
extractGradleVersion,
getJavaConstraint,
gradleWrapperFileName,
prepareGradleCommand,
} from '../gradle-wrapper/utils.ts';
import type { UpdateArtifact, UpdateArtifactsResult } from '../types.ts';
import { resolveToolConstraint } from '../util.ts';
import {
isGcvLockFile,
isGcvPropsFile,
} from './extract/consistent-versions-plugin.ts';
import { isGradleBuildFile } from './utils.ts';
export function isGradleExecutionAllowed(command: string): boolean {
const allowlist = GlobalConfig.get('allowedUnsafeExecutions');
if (!allowlist.includes('gradleWrapper')) {
logger.once.warn(
{ command },
'Gradle wrapper command was requested to run, but `gradleWrapper` is not permitted in the allowedUnsafeExecutions',
);
return false;
}
return true;
}
// .lockfile is gradle default lockfile, /versions.lock is gradle-consistent-versions plugin lockfile
function isLockFile(fileName: string): boolean {
return fileName.endsWith('.lockfile') || isGcvLockFile(fileName);
}
async function getUpdatedLockfiles(
oldLockFileContentMap: Record<string, string | null>,
): Promise<UpdateArtifactsResult[]> {
const res: UpdateArtifactsResult[] = [];
const status = await getRepoStatus();
for (const modifiedFile of status.modified) {
if (
isLockFile(modifiedFile) ||
modifiedFile.endsWith('gradle/verification-metadata.xml')
) {
const newContent = await readLocalFile(modifiedFile, 'utf8');
if (oldLockFileContentMap[modifiedFile] !== newContent) {
res.push({
file: {
type: 'addition',
path: modifiedFile,
contents: newContent,
},
});
}
}
}
return res;
}
async function getSubProjectList(
cmd: string,
execOptions: ExecOptions,
): Promise<string[]> {
const subprojects = ['']; // = root project
const subprojectsRegex = regEx(/^[ \t]*subprojects: \[(?<subprojects>.+)\]/m);
const gradleProperties = await exec(`${cmd} properties`, execOptions);
const subprojectsMatch = gradleProperties.stdout.match(subprojectsRegex);
if (subprojectsMatch?.groups?.subprojects) {
const projectRegex = regEx(/project '(?<name>.+?)'/g);
const matches = subprojectsMatch.groups.subprojects.matchAll(projectRegex);
for (const match of matches) {
if (match?.groups?.name) {
subprojects.push(match.groups.name);
}
}
}
return subprojects;
}
async function getGradleVersion(gradlewFile: string): Promise<string | null> {
const propertiesFile = upath.join(
upath.dirname(gradlewFile),
'gradle/wrapper/gradle-wrapper.properties',
);
const properties = await readLocalFile(propertiesFile, 'utf8');
const extractResult = extractGradleVersion(properties ?? '');
return extractResult ? extractResult.version : null;
}
async function buildUpdateVerificationMetadataCmd(
verificationMetadataFile: string | undefined,
baseCmd: string,
): Promise<string | null> {
if (!verificationMetadataFile) {
return null;
}
const verificationMetadata = await readLocalFile(verificationMetadataFile);
const verifiesChecksums = verificationMetadata?.includes(
'<verify-metadata>true</verify-metadata>',
);
const verifiesSignatures = verificationMetadata?.includes(
'<verify-signatures>true</verify-signatures>',
);
const hashTypes = ['sha256', 'sha512'].filter((type) =>
verificationMetadata?.includes(`<${type} `),
);
if (verifiesChecksums || hashTypes.length) {
logger.debug(
'Dependency metadata verification enabled or checksums present - generating checksums',
);
}
if ((verifiesChecksums || verifiesSignatures) && !hashTypes.length) {
// fallback algorithm for pgp and in case a weak algorithm (md5, sha1) is used for checksums
hashTypes.push('sha256');
}
if (verifiesSignatures) {
logger.debug(
'Dependency signature verification enabled - generating PGP signatures',
);
hashTypes.push('pgp');
}
if (!hashTypes.length) {
return null;
}
return `${baseCmd} --write-verification-metadata ${hashTypes.join(',')} dependencies`;
}
export async function updateArtifacts({
packageFileName,
updatedDeps,
newPackageFileContent,
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.debug(`gradle.updateArtifacts(${packageFileName})`);
const fileList = await scm.getFileList();
const lockFiles = fileList.filter((file) => isLockFile(file));
const verificationMetadataFile = fileList.find((fileName) =>
fileName.endsWith('gradle/verification-metadata.xml'),
);
if (!lockFiles.length && !verificationMetadataFile) {
logger.debug(
'No Gradle dependency lockfiles or verification metadata found - skipping update',
);
return null;
}
const gradlewName = gradleWrapperFileName();
const gradlewFile = await findUpLocal(
gradlewName,
upath.dirname(packageFileName),
);
if (!gradlewFile) {
logger.debug(
'Found Gradle dependency lockfiles but no gradlew - aborting update',
);
return null;
}
if (
config.isLockFileMaintenance &&
(!isGradleBuildFile(packageFileName) ||
upath.dirname(packageFileName) !== upath.dirname(gradlewFile))
) {
logger.trace(
'No build.gradle(.kts) file or not in root project - skipping lock file maintenance',
);
return null;
}
if (!isGradleExecutionAllowed(gradlewFile)) {
logger.trace(
'Not allowed to execute gradle due to allowedUnsafeExecutions - aborting update',
);
return null;
}
logger.debug('Updating found Gradle dependency lockfiles');
try {
const oldLockFileContentMap = await getFiles(lockFiles);
await prepareGradleCommand(gradlewFile);
const baseCmd = `${gradlewName}${gradleJvmArg(getToolSettingsOptions(config.toolSettings))} --console=plain --dependency-verification lenient -q`;
const execOptions: ExecOptions = {
cwdFile: gradlewFile,
docker: {},
extraEnv,
toolConstraints: [
{
toolName: 'java',
constraint: await resolveToolConstraint(config, 'java', async () =>
getJavaConstraint(await getGradleVersion(gradlewFile), gradlewFile),
),
},
],
};
const cmds = [];
if (lockFiles.length) {
const subprojects = await getSubProjectList(baseCmd, execOptions);
let lockfileCmd = `${baseCmd} ${subprojects
.map((project) => `${project}:dependencies`)
.map(quote)
.join(' ')}`;
if (
config.isLockFileMaintenance === true ||
!updatedDeps.length ||
isGcvPropsFile(packageFileName)
) {
lockfileCmd += ' --write-locks';
} else {
const updatedDepNames = updatedDeps
.map(({ depName, packageName }) => packageName ?? depName)
.filter(isNonEmptyStringAndNotWhitespace);
lockfileCmd += ` --update-locks ${updatedDepNames
.map(quote)
.join(',')}`;
}
cmds.push(lockfileCmd);
}
const updateVerificationMetadataCmd =
await buildUpdateVerificationMetadataCmd(
verificationMetadataFile,
baseCmd,
);
if (updateVerificationMetadataCmd) {
cmds.push(updateVerificationMetadataCmd);
}
if (!cmds.length) {
logger.debug('No lockfile or verification metadata update necessary');
return null;
}
await writeLocalFile(packageFileName, newPackageFileContent);
await exec(cmds, { ...execOptions, ignoreStdout: true });
const res = await getUpdatedLockfiles(oldLockFileContentMap);
logger.debug('Returning updated Gradle dependency lockfiles');
return res.length > 0 ? res : null;
} catch (err) {
if (err.message === TEMPORARY_ERROR) {
throw err;
}
logger.debug({ err }, 'Error while updating Gradle dependency lockfiles');
return [
{
artifactError: {
fileName: packageFileName,
stderr: err.message,
},
},
];
}
}