Skip to content

Commit 0180c04

Browse files
vmorozCopilot
andauthored
Fix CLI-init package pruning registry (#16559)
Check published versions through the local Verdaccio registry so pruning and publishing see the same packages. Add regression tests for scoped and unscoped packages. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent bdce17d commit 0180c04

4 files changed

Lines changed: 122 additions & 2 deletions

File tree

‎.ado/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ attempts that green builds would otherwise hide:
120120
- **Verdaccio (CLI init tests):** `verdaccio/generate-config.js` emits the config at
121121
runtime, so the uplink uses the proxy for PRs and the ADO feed with the CI token while
122122
local packages still publish offline. The former static `config.yaml` was removed.
123+
Before publishing, CLI init checks for existing versions through the same local
124+
Verdaccio registry, not the default proxy. This sees versions published directly
125+
to the authenticated CI feed and prevents duplicate-publish conflicts.
123126
- **React Native DevTools:** `RNDT_DEV: 1` stops DevTools from fetching its
124127
standalone shell binary from a Facebook CDN when Metro starts. The debugger is
125128
never opened in CI, so this has no effect on tests.

‎.ado/templates/verdaccio-start.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ steps:
3838
- script: node .ado/scripts/npmPack.js --clean --no-color
3939
displayName: Pack all workspace packages
4040

41-
- script: node .ado/scripts/npmPack.js --no-pack --check-npm --no-color
41+
- script: node .ado/scripts/npmPack.js --no-pack --check-npm --registry http://localhost:4873 --no-color
4242
displayName: Remove already published packages
4343

4444
- script: |
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
const assert = require('node:assert/strict');
5+
const {execFile, execFileSync} = require('node:child_process');
6+
const {once} = require('node:events');
7+
const {promises: fs} = require('node:fs');
8+
const http = require('node:http');
9+
const os = require('node:os');
10+
const path = require('node:path');
11+
const {test} = require('node:test');
12+
const {promisify} = require('node:util');
13+
14+
const run = promisify(execFile);
15+
const repoRoot = path.resolve(__dirname, '..', '..');
16+
17+
for (const name of ['@rnw-tests/registry-fixture', 'rnw-registry-fixture']) {
18+
test(`Windows CLI-init prunes published ${name} versions using the publish registry, not the default proxy`, async t => {
19+
if (process.platform !== 'win32') {
20+
t.skip('CLI-init uses the Windows Node/npm installation layout');
21+
return;
22+
}
23+
24+
const template = await fs.readFile(
25+
path.join(repoRoot, '.ado', 'templates', 'verdaccio-start.yml'),
26+
'utf8',
27+
);
28+
const command = template.match(
29+
/^\s*- script: node (\.ado\/scripts\/npmPack\.js[^\r\n]*--check-npm[^\r\n]*)$/m,
30+
);
31+
assert.ok(command, 'CLI-init must check already published packages');
32+
const [script, ...args] = command[1].split(/\s+/);
33+
const publishRegistry = template.match(
34+
/npm publish "[^"]+" --registry (\S+)/,
35+
)?.[1];
36+
assert.ok(publishRegistry, 'CLI-init must specify its publish registry');
37+
38+
const root = await fs.mkdtemp(
39+
path.join(os.tmpdir(), 'rnw-verdaccio-pruning-'),
40+
);
41+
t.after(() => fs.rm(root, {recursive: true, force: true}));
42+
const packages = path.join(root, 'packages');
43+
const userConfig = path.join(root, '.npmrc');
44+
const globalConfig = path.join(root, '.global-npmrc');
45+
await Promise.all([
46+
fs.mkdir(packages),
47+
fs.mkdir(path.join(root, 'package')),
48+
fs.writeFile(userConfig, ''),
49+
fs.writeFile(globalConfig, ''),
50+
]);
51+
for (const version of ['1.0.0', '1.0.1']) {
52+
await fs.writeFile(
53+
path.join(root, 'package', 'package.json'),
54+
JSON.stringify({name, version}),
55+
);
56+
execFileSync('tar', [
57+
'-czf',
58+
path.join(packages, `${version}.tgz`),
59+
'-C',
60+
root,
61+
'package',
62+
]);
63+
}
64+
65+
const requests = [];
66+
const server = http.createServer((request, response) => {
67+
const pathname = decodeURIComponent(
68+
new URL(request.url, 'http://localhost').pathname,
69+
);
70+
requests.push(pathname);
71+
const isPublishRegistry = pathname === `/verdaccio/${name}`;
72+
response.writeHead(isPublishRegistry ? 200 : 404, {
73+
'Content-Type': 'application/json',
74+
});
75+
response.end(
76+
JSON.stringify(
77+
isPublishRegistry
78+
? {
79+
name,
80+
'dist-tags': {latest: '1.0.0'},
81+
versions: {'1.0.0': {name, version: '1.0.0'}},
82+
}
83+
: {error: 'Not found'},
84+
),
85+
);
86+
});
87+
server.listen(0, '127.0.0.1');
88+
await once(server, 'listening');
89+
t.after(() => promisify(server.close).call(server));
90+
const address = server.address();
91+
assert.ok(address && typeof address !== 'string');
92+
const baseUrl = `http://127.0.0.1:${address.port}`;
93+
const registryIndex = args.indexOf('--registry');
94+
if (registryIndex !== -1) {
95+
assert.equal(args[registryIndex + 1], publishRegistry);
96+
args[registryIndex + 1] = `${baseUrl}/verdaccio/`;
97+
}
98+
99+
await run(process.execPath, [path.join(repoRoot, script), ...args, packages], {
100+
cwd: root,
101+
timeout: 60000,
102+
env: {
103+
...process.env,
104+
NPM_CONFIG_REGISTRY: `${baseUrl}/proxy/`,
105+
NPM_CONFIG_USERCONFIG: userConfig,
106+
NPM_CONFIG_GLOBALCONFIG: globalConfig,
107+
NPM_CONFIG_CACHE: path.join(root, 'cache'),
108+
NPM_CONFIG_FETCH_RETRIES: '0',
109+
NPM_CONFIG_FETCH_TIMEOUT: '2000',
110+
},
111+
});
112+
113+
assert.deepEqual(await fs.readdir(packages), ['1.0.1.tgz']);
114+
assert.ok(requests.includes(`/verdaccio/${name}`));
115+
assert.ok(!requests.some(request => request.startsWith('/proxy/')));
116+
});
117+
}

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"release-notes": "yarn workspace @rnw-scripts/generate-release-notes release-notes",
1616
"warm-feed": "yarn workspace @rnw-scripts/warm-feed warm-feed",
1717
"spellcheck": "npx cspell",
18-
"test": "node --test .ado/tests/extract-zip.test.cjs .ado/tests/detect-release-build.test.cjs && lage test --verbose --passWithNoTests",
18+
"test": "node --test .ado/tests/extract-zip.test.cjs .ado/tests/detect-release-build.test.cjs .ado/tests/verdaccio-pruning.test.cjs && lage test --verbose --passWithNoTests",
1919
"validate-overrides": "react-native-platform-override validate",
2020
"fork-sync": "node packages/@rnw-scripts/fork-sync/lib-commonjs/sync.js",
2121
"fork-sync:status": "yarn fork-sync --dep fmt --status"

0 commit comments

Comments
 (0)