From abc61c36610c66747548fa7292e22ff50e3f4736 Mon Sep 17 00:00:00 2001 From: Vladimir Morozov Date: Mon, 2 Nov 2026 13:55:41 -0800 Subject: [PATCH] Fix CLI-init package publishing conflicts on 0.81 Backport of microsoft/react-native-windows#16559. (cherry picked from commit 79774fa2ecdf8260d460f705fdb051c5b4fb662b) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .ado/README.md | 3 + .ado/templates/verdaccio-start.yml | 2 +- .ado/tests/verdaccio-pruning.test.cjs | 117 ++++++++++++++++++++++++++ package.json | 2 +- 4 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 .ado/tests/verdaccio-pruning.test.cjs diff --git a/.ado/README.md b/.ado/README.md index 73e44959eff..c251263ec43 100644 --- a/.ado/README.md +++ b/.ado/README.md @@ -72,6 +72,9 @@ attempts that green builds would otherwise hide: - **Verdaccio (CLI init tests):** `verdaccio/generate-config.js` emits the config at runtime, so the uplink targets the feed (with the CI token when present) while local packages still publish offline. The former static `config.yaml` was removed. + Before publishing, CLI init checks for existing versions through the same local + Verdaccio registry, not the default proxy. This sees versions published directly + to the authenticated CI feed and prevents duplicate-publish conflicts. - **React Native DevTools:** `RNDT_DEV: 1` stops DevTools from fetching its standalone shell binary from a Facebook CDN when Metro starts. The debugger is never opened in CI, so this has no effect on tests. diff --git a/.ado/templates/verdaccio-start.yml b/.ado/templates/verdaccio-start.yml index 25f2ab58318..1a6cd1a455a 100644 --- a/.ado/templates/verdaccio-start.yml +++ b/.ado/templates/verdaccio-start.yml @@ -38,7 +38,7 @@ steps: - script: node .ado/scripts/npmPack.js --clean --no-color displayName: Pack all workspace packages - - script: node .ado/scripts/npmPack.js --no-pack --check-npm --no-color + - script: node .ado/scripts/npmPack.js --no-pack --check-npm --registry http://localhost:4873 --no-color displayName: Remove already published packages - script: | diff --git a/.ado/tests/verdaccio-pruning.test.cjs b/.ado/tests/verdaccio-pruning.test.cjs new file mode 100644 index 00000000000..d1ad5e7eb38 --- /dev/null +++ b/.ado/tests/verdaccio-pruning.test.cjs @@ -0,0 +1,117 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +const assert = require('node:assert/strict'); +const {execFile, execFileSync} = require('node:child_process'); +const {once} = require('node:events'); +const {promises: fs} = require('node:fs'); +const http = require('node:http'); +const os = require('node:os'); +const path = require('node:path'); +const {test} = require('node:test'); +const {promisify} = require('node:util'); + +const run = promisify(execFile); +const repoRoot = path.resolve(__dirname, '..', '..'); + +for (const name of ['@rnw-tests/registry-fixture', 'rnw-registry-fixture']) { + test(`Windows CLI-init prunes published ${name} versions using the publish registry, not the default proxy`, async t => { + if (process.platform !== 'win32') { + t.skip('CLI-init uses the Windows Node/npm installation layout'); + return; + } + + const template = await fs.readFile( + path.join(repoRoot, '.ado', 'templates', 'verdaccio-start.yml'), + 'utf8', + ); + const command = template.match( + /^\s*- script: node (\.ado\/scripts\/npmPack\.js[^\r\n]*--check-npm[^\r\n]*)$/m, + ); + assert.ok(command, 'CLI-init must check already published packages'); + const [script, ...args] = command[1].split(/\s+/); + const publishRegistry = template.match( + /npm publish "[^"]+" --registry (\S+)/, + )?.[1]; + assert.ok(publishRegistry, 'CLI-init must specify its publish registry'); + + const root = await fs.mkdtemp( + path.join(os.tmpdir(), 'rnw-verdaccio-pruning-'), + ); + t.after(() => fs.rm(root, {recursive: true, force: true})); + const packages = path.join(root, 'packages'); + const userConfig = path.join(root, '.npmrc'); + const globalConfig = path.join(root, '.global-npmrc'); + await Promise.all([ + fs.mkdir(packages), + fs.mkdir(path.join(root, 'package')), + fs.writeFile(userConfig, ''), + fs.writeFile(globalConfig, ''), + ]); + for (const version of ['1.0.0', '1.0.1']) { + await fs.writeFile( + path.join(root, 'package', 'package.json'), + JSON.stringify({name, version}), + ); + execFileSync('tar', [ + '-czf', + path.join(packages, `${version}.tgz`), + '-C', + root, + 'package', + ]); + } + + const requests = []; + const server = http.createServer((request, response) => { + const pathname = decodeURIComponent( + new URL(request.url, 'http://localhost').pathname, + ); + requests.push(pathname); + const isPublishRegistry = pathname === `/verdaccio/${name}`; + response.writeHead(isPublishRegistry ? 200 : 404, { + 'Content-Type': 'application/json', + }); + response.end( + JSON.stringify( + isPublishRegistry + ? { + name, + 'dist-tags': {latest: '1.0.0'}, + versions: {'1.0.0': {name, version: '1.0.0'}}, + } + : {error: 'Not found'}, + ), + ); + }); + server.listen(0, '127.0.0.1'); + await once(server, 'listening'); + t.after(() => promisify(server.close).call(server)); + const address = server.address(); + assert.ok(address && typeof address !== 'string'); + const baseUrl = `http://127.0.0.1:${address.port}`; + const registryIndex = args.indexOf('--registry'); + if (registryIndex !== -1) { + assert.equal(args[registryIndex + 1], publishRegistry); + args[registryIndex + 1] = `${baseUrl}/verdaccio/`; + } + + await run(process.execPath, [path.join(repoRoot, script), ...args, packages], { + cwd: root, + timeout: 60000, + env: { + ...process.env, + NPM_CONFIG_REGISTRY: `${baseUrl}/proxy/`, + NPM_CONFIG_USERCONFIG: userConfig, + NPM_CONFIG_GLOBALCONFIG: globalConfig, + NPM_CONFIG_CACHE: path.join(root, 'cache'), + NPM_CONFIG_FETCH_RETRIES: '0', + NPM_CONFIG_FETCH_TIMEOUT: '2000', + }, + }); + + assert.deepEqual(await fs.readdir(packages), ['1.0.1.tgz']); + assert.ok(requests.includes(`/verdaccio/${name}`)); + assert.ok(!requests.some(request => request.startsWith('/proxy/'))); + }); +} diff --git a/package.json b/package.json index 8c0d5121967..26d95d7ee40 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "postinstall": "yarn build", "release-notes": "yarn workspace @rnw-scripts/generate-release-notes release-notes", "spellcheck": "npx cspell", - "test": "node --test .ado/tests/detect-release-build.test.cjs && lage test --verbose --passWithNoTests", + "test": "node --test .ado/tests/detect-release-build.test.cjs .ado/tests/verdaccio-pruning.test.cjs && lage test --verbose --passWithNoTests", "validate-overrides": "react-native-platform-override validate", "fork-sync": "node packages/@rnw-scripts/fork-sync/lib-commonjs/sync.js", "fork-sync:status": "yarn fork-sync --dep fmt --status"