Files
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

410 lines
13 KiB
TypeScript

import { codeBlock } from 'common-tags';
import upath from 'upath';
import { mockDeep } from 'vitest-mock-extended';
import { envMock, mockExecAll } from '~test/exec-util.ts';
import { env, fs } from '~test/util.ts';
import { GlobalConfig } from '../../../config/global.ts';
import type {
InternalGlobalConfigOptions,
RepoGlobalConfig,
} from '../../../config/types.ts';
import * as docker from '../../../util/exec/docker/index.ts';
import * as _datasource from '../../datasource/index.ts';
import type { UpdateArtifact, UpdateArtifactsConfig } from '../types.ts';
import * as pub from './index.ts';
vi.mock('../../../util/exec/env.ts');
vi.mock('../../../util/fs/index.ts');
vi.mock('../../../util/http/index.ts');
vi.mock('../../datasource/index.ts', () => mockDeep());
process.env.CONTAINERBASE = 'true';
const lockFile = 'pubspec.lock';
const oldLockFileContent = 'Old pubspec.lock';
const newLockFileContent = 'New pubspec.lock';
const depNames = ['dep1', 'dep2', 'dep3'];
const depNamesWithSdks = [...depNames, 'dart', 'flutter'];
const depNamesWithSpace = depNames.join(' ');
const datasource = vi.mocked(_datasource);
const adminConfig: RepoGlobalConfig & InternalGlobalConfigOptions = {
localDir: upath.join('/tmp/github/some/repo'),
cacheDir: upath.join('/tmp/cache'),
containerbaseDir: upath.join('/tmp/cache/containerbase'),
binarySource: 'global',
};
const config: UpdateArtifactsConfig = {};
const updateArtifact: UpdateArtifact = {
packageFileName: 'pubspec.yaml',
updatedDeps: depNamesWithSdks.map((depName) => {
return { depName };
}),
newPackageFileContent: '',
config,
};
describe('modules/manager/pub/artifacts', () => {
beforeEach(() => {
env.getChildProcessEnv.mockReturnValue(envMock.basic);
GlobalConfig.set(adminConfig);
docker.resetPrefetchedImages();
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [{ version: '2.10.5' }, { version: '3.3.9' }],
});
});
afterEach(() => {
GlobalConfig.set(adminConfig);
});
it('returns null if no pubspec.lock found', async () => {
await expect(pub.updateArtifacts(updateArtifact)).resolves.toBeNull();
});
it('returns null if updatedDeps is empty', async () => {
await expect(
pub.updateArtifacts({ ...updateArtifact, updatedDeps: [] }),
).resolves.toBeNull();
});
it(`runs flutter pub get if only dart and flutter sdks are updated`, async () => {
const execSnapshots = mockExecAll();
fs.getSiblingFileName.mockReturnValueOnce(lockFile);
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent);
await expect(
pub.updateArtifacts({
...updateArtifact,
newPackageFileContent: codeBlock`
environment:
sdk: ^3.0.0
flutter: 2.0.0
`,
updatedDeps: [{ depName: 'dart' }, { depName: 'flutter' }],
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: lockFile,
contents: newLockFileContent,
},
},
]);
expect(execSnapshots).toMatchObject([
{
cmd: 'flutter pub get --no-precompile',
},
]);
});
describe.each([
{ sdk: 'dart', packageFileContent: '' },
{ sdk: 'flutter', packageFileContent: 'sdk: flutter' },
])('validates pub tools', (params) => {
it(`returns null for ${params.sdk} if unchanged`, async () => {
const execSnapshots = mockExecAll();
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent);
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent);
await expect(
pub.updateArtifacts({
...updateArtifact,
newPackageFileContent: params.packageFileContent,
}),
).resolves.toBeNull();
expect(execSnapshots).toMatchObject([
{
cmd: `${params.sdk} pub upgrade ${depNamesWithSpace}`,
},
]);
});
it(`returns updated ${params.sdk} pubspec.lock`, async () => {
const execSnapshots = mockExecAll();
fs.getSiblingFileName.mockReturnValueOnce(lockFile);
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent);
await expect(
pub.updateArtifacts({
...updateArtifact,
newPackageFileContent: params.packageFileContent,
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: lockFile,
contents: newLockFileContent,
},
},
]);
expect(execSnapshots).toMatchObject([
{
cmd: `${params.sdk} pub upgrade ${depNamesWithSpace}`,
},
]);
});
it(`runs ${params.sdk} pub get if only the sdk is updated`, async () => {
const execSnapshots = mockExecAll();
fs.getSiblingFileName.mockReturnValueOnce(lockFile);
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent);
await expect(
pub.updateArtifacts({
...updateArtifact,
newPackageFileContent: params.packageFileContent,
updatedDeps: [{ depName: params.sdk }],
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: lockFile,
contents: newLockFileContent,
},
},
]);
expect(execSnapshots).toMatchObject([
{
cmd: `${params.sdk} pub get --no-precompile`,
},
]);
});
it(`returns updated ${params.sdk} pubspec.lock for lockfile maintenance`, async () => {
const execSnapshots = mockExecAll();
fs.getSiblingFileName.mockReturnValueOnce(lockFile);
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent);
await expect(
pub.updateArtifacts({
...updateArtifact,
newPackageFileContent: params.packageFileContent,
config: { ...config, isLockFileMaintenance: true },
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: lockFile,
contents: newLockFileContent,
},
},
]);
expect(execSnapshots).toMatchObject([
{
cmd: `${params.sdk} pub upgrade`,
},
]);
});
it(`supports ${params.sdk} docker mode`, async () => {
GlobalConfig.set({
...adminConfig,
binarySource: 'docker',
dockerSidecarImage: 'ghcr.io/renovatebot/base-image',
});
const execSnapshots = mockExecAll();
fs.getSiblingFileName.mockReturnValueOnce(lockFile);
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent);
await expect(
pub.updateArtifacts({
...updateArtifact,
newPackageFileContent: params.packageFileContent,
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: lockFile,
contents: newLockFileContent,
},
},
]);
expect(execSnapshots).toMatchObject([
{
cmd: 'docker pull ghcr.io/renovatebot/base-image',
},
{
cmd: 'docker ps --filter name=renovate_sidecar -aq',
},
{
cmd:
'docker run --rm --name=renovate_sidecar --label=renovate_child ' +
'-v "/tmp/github/some/repo":"/tmp/github/some/repo" ' +
'-v "/tmp/cache":"/tmp/cache" ' +
'-e CI ' +
'-e CONTAINERBASE_CACHE_DIR ' +
'-w "/tmp/github/some/repo" ' +
'ghcr.io/renovatebot/base-image ' +
"bash -l -c '" +
`install-tool ${params.sdk} 3.3.9` +
' && ' +
`${params.sdk} pub upgrade ${depNamesWithSpace}` +
"'",
},
]);
});
it(`supports ${params.sdk} install mode`, async () => {
GlobalConfig.set({ ...adminConfig, binarySource: 'install' });
const execSnapshots = mockExecAll();
fs.getSiblingFileName.mockReturnValueOnce(lockFile);
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent);
await expect(
pub.updateArtifacts({
...updateArtifact,
newPackageFileContent: params.packageFileContent,
config: { ...config, constraints: { dart: '3.3.9' } },
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: lockFile,
contents: newLockFileContent,
},
},
]);
expect(execSnapshots).toMatchObject([
{ cmd: `install-tool ${params.sdk} 3.3.9` },
{ cmd: `${params.sdk} pub upgrade ${depNamesWithSpace}` },
]);
});
it(`catches errors for ${params.sdk}`, async () => {
const stderr = 'not found';
fs.getSiblingFileName.mockReturnValueOnce(lockFile);
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent);
fs.writeLocalFile.mockImplementationOnce(() => {
throw new Error(stderr);
});
await expect(
pub.updateArtifacts({
...updateArtifact,
newPackageFileContent: params.packageFileContent,
}),
).resolves.toEqual([{ artifactError: { fileName: lockFile, stderr } }]);
});
});
it('uses flutter constraint from pubspec.yaml', async () => {
GlobalConfig.set({ ...adminConfig, binarySource: 'install' });
const execSnapshots = mockExecAll();
const flutterVersion = '3.19.0';
const newPackageFileContent = codeBlock`
environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ${flutterVersion}
dependencies:
flutter:
sdk: flutter
`;
fs.getSiblingFileName.mockReturnValueOnce(lockFile);
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent);
await expect(
pub.updateArtifacts({
...updateArtifact,
newPackageFileContent,
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: lockFile,
contents: newLockFileContent,
},
},
]);
expect(execSnapshots).toMatchObject([
{ cmd: `install-tool flutter ${flutterVersion}` },
{ cmd: `flutter pub upgrade ${depNamesWithSpace}` },
]);
});
it('uses dart constraint from pubspec.yaml', async () => {
GlobalConfig.set({ ...adminConfig, binarySource: 'install' });
const execSnapshots = mockExecAll();
const dartVersion = '3.3.0';
const newPackageFileContent = codeBlock`
environment:
sdk: ${dartVersion}
`;
fs.getSiblingFileName.mockReturnValueOnce(lockFile);
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent);
await expect(
pub.updateArtifacts({
...updateArtifact,
newPackageFileContent,
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: lockFile,
contents: newLockFileContent,
},
},
]);
expect(execSnapshots).toMatchObject([
{ cmd: `install-tool dart ${dartVersion}` },
{ cmd: `dart pub upgrade ${depNamesWithSpace}` },
]);
});
it('falls back to the extracted dart constraint', async () => {
GlobalConfig.set({ ...adminConfig, binarySource: 'install' });
const execSnapshots = mockExecAll();
fs.getSiblingFileName.mockReturnValueOnce(lockFile);
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent);
await pub.updateArtifacts({
...updateArtifact,
newPackageFileContent: codeBlock`
dependencies:
dep1: ^1.0.0
`,
config: { ...config, extractedConstraints: { dart: '3.3.0' } },
});
expect(execSnapshots).toMatchObject([
{ cmd: 'install-tool dart 3.3.0' },
{ cmd: `dart pub upgrade ${depNamesWithSpace}` },
]);
});
it('prefers the pubspec constraint over the extracted one', async () => {
GlobalConfig.set({ ...adminConfig, binarySource: 'install' });
const execSnapshots = mockExecAll();
fs.getSiblingFileName.mockReturnValueOnce(lockFile);
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent);
await pub.updateArtifacts({
...updateArtifact,
newPackageFileContent: codeBlock`
environment:
sdk: 3.3.0
`,
config: { ...config, extractedConstraints: { dart: '2.0.0' } },
});
expect(execSnapshots).toMatchObject([
{ cmd: 'install-tool dart 3.3.0' },
{ cmd: `dart pub upgrade ${depNamesWithSpace}` },
]);
});
});