Files
renovate/lib/modules/manager/util.spec.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

303 lines
8.4 KiB
TypeScript

import * as hostRules from '../../util/host-rules.ts';
import { GitRefsDatasource } from '../datasource/git-refs/index.ts';
import { GitTagsDatasource } from '../datasource/git-tags/index.ts';
import { GithubTagsDatasource } from '../datasource/github-tags/index.ts';
import { GitlabTagsDatasource } from '../datasource/gitlab-tags/index.ts';
import { type PackageDependency } from './types.ts';
import {
applyGitSource,
artifactErrorMessageFromExecError,
resolveToolConstraint,
} from './util.ts';
describe('modules/manager/util', () => {
beforeEach(() => {
hostRules.clear();
});
it('applies GitHub source for tag', () => {
const dependency: PackageDependency = {};
const git = 'https://github.com/foo/bar';
const tag = 'v1.2.3';
applyGitSource(dependency, git, undefined, tag, undefined);
expect(dependency).toStrictEqual({
datasource: GithubTagsDatasource.id,
registryUrls: ['https://github.com'],
packageName: 'foo/bar',
currentValue: tag,
skipReason: undefined,
});
});
it('applies GitLab source for tag', () => {
const dependency: PackageDependency = {};
const git = 'https://gitlab.com/foo/bar';
const tag = 'v1.2.3';
applyGitSource(dependency, git, undefined, tag, undefined);
expect(dependency).toStrictEqual({
datasource: GitlabTagsDatasource.id,
registryUrls: ['https://gitlab.com'],
packageName: 'foo/bar',
currentValue: tag,
skipReason: undefined,
});
});
it('applies other git source for tag', () => {
const dependency: PackageDependency = {};
const git = 'https://a-git-source.com/foo/bar';
const tag = 'v1.2.3';
applyGitSource(dependency, git, undefined, tag, undefined);
expect(dependency).toStrictEqual({
datasource: GitTagsDatasource.id,
packageName: git,
currentValue: tag,
skipReason: undefined,
});
});
it('applies git source with subdomain', () => {
const dependency: PackageDependency = {};
const git = 'https://git.example.com/foo/bar';
const tag = 'v1.2.3';
hostRules.add({
hostType: 'github',
matchHost: 'git.example.com',
});
applyGitSource(dependency, git, undefined, tag, undefined);
expect(dependency).toStrictEqual({
datasource: GithubTagsDatasource.id,
packageName: 'foo/bar',
currentValue: tag,
registryUrls: ['https://git.example.com'],
skipReason: undefined,
});
});
it('applies GitHub source for tag with SSH URL', () => {
const dependency: PackageDependency = {};
const git = 'ssh://git@github.com/foo/bar';
const tag = 'v1.2.3';
applyGitSource(dependency, git, undefined, tag, undefined);
expect(dependency).toStrictEqual({
datasource: GithubTagsDatasource.id,
registryUrls: ['https://github.com'],
packageName: 'foo/bar',
currentValue: tag,
skipReason: undefined,
});
});
it('applies GitLab source for tag with SSH URL', () => {
const dependency: PackageDependency = {};
const git = 'ssh://git@gitlab.com/foo/bar';
const tag = 'v1.2.3';
applyGitSource(dependency, git, undefined, tag, undefined);
expect(dependency).toStrictEqual({
datasource: GitlabTagsDatasource.id,
registryUrls: ['https://gitlab.com'],
packageName: 'foo/bar',
currentValue: tag,
skipReason: undefined,
});
});
it('applies GitHub source for tag with HTTPS URL', () => {
const dependency: PackageDependency = {};
const git = 'https://github.com/foo/bar';
const tag = 'v1.2.3';
applyGitSource(dependency, git, undefined, tag, undefined);
expect(dependency).toStrictEqual({
datasource: GithubTagsDatasource.id,
registryUrls: ['https://github.com'],
packageName: 'foo/bar',
currentValue: tag,
skipReason: undefined,
});
});
it('applies git source for rev', () => {
const dependency: PackageDependency = {};
const git = 'https://github.com/foo/bar';
const rev = 'abc1234';
applyGitSource(dependency, git, rev, undefined, undefined);
expect(dependency).toStrictEqual({
datasource: GitRefsDatasource.id,
packageName: git,
currentDigest: rev,
replaceString: rev,
skipReason: undefined,
});
});
it('skips git source for branch', () => {
const dependency: PackageDependency = {};
const git = 'https://github.com/foo/bar';
const branch = 'main';
applyGitSource(dependency, git, undefined, undefined, branch);
expect(dependency).toStrictEqual({
datasource: GitRefsDatasource.id,
packageName: git,
currentValue: branch,
skipReason: 'git-dependency',
});
});
it('skips git source for git only', () => {
const dependency: PackageDependency = {};
const git = 'https://github.com/foo/bar';
applyGitSource(dependency, git, undefined, undefined, undefined);
expect(dependency).toStrictEqual({
datasource: GitRefsDatasource.id,
packageName: git,
currentValue: undefined,
skipReason: 'unspecified-version',
});
});
});
describe('modules/manager/util', () => {
it('returns stderr when present', () => {
const message = artifactErrorMessageFromExecError(
{ stderr: 'some error', stdout: 'some output' },
'fallback message',
);
expect(message).toBe('some error');
});
it('returns stdout when stderr is empty', () => {
const message = artifactErrorMessageFromExecError(
{ stderr: '', stdout: 'some output' },
'fallback message',
);
expect(message).toBe('some output');
});
it('returns stdout when stderr is only whitespace', () => {
const message = artifactErrorMessageFromExecError(
{ stderr: ' ', stdout: 'some output' },
'fallback message',
);
expect(message).toBe('some output');
});
it('returns stdout when stderr is undefined', () => {
const message = artifactErrorMessageFromExecError(
{ stdout: 'some output' },
'fallback message',
);
expect(message).toBe('some output');
});
it('returns fallback message when neither stderr nor stdout are present', () => {
const message = artifactErrorMessageFromExecError({}, 'fallback message');
expect(message).toBe('fallback message');
});
it('returns fallback message when stderr and stdout are only whitespace', () => {
const message = artifactErrorMessageFromExecError(
{ stderr: ' ', stdout: ' ' },
'fallback message',
);
expect(message).toBe('fallback message');
});
describe('resolveToolConstraint()', () => {
it('prefers the user configured constraint', async () => {
const constraint = await resolveToolConstraint(
{
constraints: { python: '==3.12' },
extractedConstraints: { python: '==3.10' },
},
'python',
() => '==3.11',
);
expect(constraint).toBe('==3.12');
});
it('prefers the derived constraint over the extracted one', async () => {
const constraint = await resolveToolConstraint(
{ extractedConstraints: { python: '==3.10' } },
'python',
() => Promise.resolve('==3.11'),
);
expect(constraint).toBe('==3.11');
});
it('falls back to the extracted constraint', async () => {
const constraint = await resolveToolConstraint(
{ extractedConstraints: { python: '==3.10' } },
'python',
() => null,
);
expect(constraint).toBe('==3.10');
});
it('returns the extracted constraint when nothing can be derived', async () => {
const constraint = await resolveToolConstraint(
{ extractedConstraints: { python: '==3.10' } },
'python',
);
expect(constraint).toBe('==3.10');
});
it('returns undefined when no constraint is known', async () => {
const constraint = await resolveToolConstraint({}, 'python', () => null);
expect(constraint).toBeUndefined();
});
it('treats an empty string as not set', async () => {
const constraint = await resolveToolConstraint(
{
constraints: { python: '' },
extractedConstraints: { python: '==3.10' },
},
'python',
() => '',
);
expect(constraint).toBe('==3.10');
});
it('accepts the null constraints of a post-update config', async () => {
const constraint = await resolveToolConstraint(
{ constraints: null, extractedConstraints: null },
'node',
);
expect(constraint).toBeUndefined();
});
});
});