Files
renovate/lib/modules/manager/devbox/artifacts.ts
Sebastian Poxhofer 4220c0fe36 feat(manager): share the .npmrc host-rule wrapper between bun, deno and npm (#46131)
* refactor(manager): share the .npmrc host-rule wrapper between bun, deno and npm

Adds `withNpmrcHostRules()` to the npm manager utils: it injects the host-rule lines into a directory's `.npmrc`, runs the callback, and restores the original `.npmrc` from a `finally` block. bun, deno, the corepack `updateArtifacts()` and the three lock file loops in `post-update/index.ts` now share it instead of repeating getNpmrcContent / updateNpmrcContent / resetNpmrcContent.

Adds `readUpdatedBinaryLockFile()` to the shared manager utils for the `Buffer.compare` plus file-addition block that bun and deno both carried.

Behaviour changes:

- bun and deno now restore `.npmrc` when `exec()` throws. The reset previously ran only after a successful install, so a failed lock file update left the injected registry credentials in the working tree. Covered by "restores .npmrc when the install fails" in `bun/artifacts.spec.ts` and `deno/artifacts.spec.ts`.
- The npm, yarn and pnpm loops in `post-update/index.ts` now restore `.npmrc` when the loop body throws an `ExternalHostError`, for the same reason.
- deno reads `config.isLockFileMaintenance` instead of deriving the flag from `config.updateType === 'lockFileMaintenance'`. Grouped branches overwrite `updateType` while `isLockFileMaintenance` is set explicitly, so the flag is the reliable source. The deno specs were updated to set it.

* refactor(manager): let updateLockFile() compare binary lock files

A Buffer as the existing content makes the skeleton read, compare and
return the new lock file as bytes, so bun and deno use it instead of a
separate readUpdatedBinaryLockFile() helper.

* refactor(manager/devbox): update the lock file through updateLockFile()

devbox kept the same Buffer compare as bun and deno; the trace log before
returning the lock file goes away with it.

* fix(manager/devbox): type the command list explicitly

The list is now only read inside the run() closure, where TypeScript
cannot resolve an evolving array type, so tsc reported an implicit any.
2026-09-18 09:30:27 +00:00

96 lines
2.9 KiB
TypeScript

import { isNonEmptyArray } from '@sindresorhus/is';
import semver from 'semver';
import { quote } from 'shlex';
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 } from '../types.ts';
import {
artifactErrorResult,
resolveToolConstraint,
updateLockFile,
} from '../util.ts';
export async function updateArtifacts({
config,
packageFileName,
updatedDeps,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
const lockFileName = getSiblingFileName(packageFileName, 'devbox.lock');
const existingLockFileContent = await readLocalFile(lockFileName, 'utf8');
if (!existingLockFileContent) {
logger.debug('No devbox.lock found');
return null;
}
const nixConstraint = await resolveToolConstraint(config, 'nix');
const devboxConstraint = await resolveToolConstraint(config, 'devbox');
const supportsNoInstall = devboxConstraint
? semver.intersects(devboxConstraint, '>=0.14.0')
: true;
const execOptions: ExecOptions = {
cwdFile: packageFileName,
toolConstraints: [
// we are required to install nix because devbox spawns nix commands internally
// https://github.com/renovatebot/renovate/discussions/35382
// https://github.com/jetify-com/devbox/issues/2585
{
toolName: 'nix',
constraint: nixConstraint,
},
{
toolName: 'devbox',
constraint: devboxConstraint,
},
],
docker: {},
};
const cmd: string[] = [];
if (config.isLockFileMaintenance) {
cmd.push(
supportsNoInstall ? 'devbox update --no-install' : 'devbox update',
);
} else if (isNonEmptyArray(updatedDeps)) {
if (supportsNoInstall) {
const updateCommands: string[] = updatedDeps
.map(
(dep) =>
dep.depName && `devbox update ${quote(dep.depName)} --no-install`,
)
.filter((dep): dep is string => Boolean(dep));
if (updateCommands.length) {
cmd.push(...updateCommands);
} else {
logger.trace('No updated devbox packages - returning null');
return null;
}
} else {
cmd.push('devbox install');
}
} else {
logger.trace('No updated devbox packages - returning null');
return null;
}
const oldLockFileContent = await readLocalFile(lockFileName);
if (!oldLockFileContent) {
logger.trace(`No ${lockFileName} found`);
return null;
}
try {
return await updateLockFile({
lockFileName,
existingLockFileContent: oldLockFileContent,
run: () => exec(cmd, execOptions),
});
} catch (err) {
logger.warn({ err }, 'Error updating devbox.lock');
return artifactErrorResult(lockFileName, err);
}
}