Skip to content

Commit 20b89b1

Browse files
committed
fix(ci): preserve Vitest alignment during branding
1 parent fc46e08 commit 20b89b1

3 files changed

Lines changed: 137 additions & 46 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { execFileSync } from 'node:child_process';
2+
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
3+
import { tmpdir } from 'node:os';
4+
import { dirname, join } from 'node:path';
5+
6+
import { expect, test } from 'vitest';
7+
8+
import { brandVite } from '../brand-vite.ts';
9+
import { alignVendoredVitestDependencies } from '../vendored-vitest.mjs';
10+
11+
const upstreamSources = {
12+
'constants.ts': 'export const VERSION = version as string\n',
13+
'cli.ts': [
14+
"import { VERSION } from './constants'",
15+
"cac('vite')",
16+
'cli.version(VERSION)',
17+
'colors.green(',
18+
" `${colors.bold('VITE')} v${VERSION}`,",
19+
' )',
20+
'',
21+
].join('\n'),
22+
'build.ts': [
23+
' logger.info(',
24+
' colors.cyan(',
25+
' `vite v${VERSION} ${colors.green(',
26+
' `building ${environment.name} environment for ${environment.config.mode}...`,',
27+
' )}`,',
28+
' ),',
29+
' )',
30+
'`[vite]: Rolldown failed`',
31+
'',
32+
].join('\n'),
33+
'logger.ts': "prefix = '[vite]'\n",
34+
'plugins/reporter.ts': [
35+
"import path from 'node:path'",
36+
' logInfo: shouldLogInfo ? (msg) => env.logger.info(msg) : undefined,',
37+
'',
38+
].join('\n'),
39+
'config.ts': [
40+
' !process.env.VITE_CONFIG_NATIVE_IGNORE_WARNING &&',
41+
' createNativeConfigCompatPlugin(nativeIncompatibilities),',
42+
'',
43+
].join('\n'),
44+
};
45+
46+
function write(file: string, source: string) {
47+
mkdirSync(dirname(file), { recursive: true });
48+
writeFileSync(file, source);
49+
}
50+
51+
test('branding preserves aligned dependencies and unrelated changes across repeated builds', () => {
52+
const root = mkdtempSync(join(tmpdir(), 'vp-brand-vite-'));
53+
const viteDir = join(root, 'vite');
54+
const nodeDir = join(viteDir, 'packages/vite/src/node');
55+
const git = (...args: string[]) => execFileSync('git', args, { cwd: viteDir, stdio: 'pipe' });
56+
try {
57+
for (const [file, source] of Object.entries(upstreamSources)) {
58+
write(join(nodeDir, file), source);
59+
}
60+
const manifest = join(viteDir, 'packages/vite/package.json');
61+
const lockfile = join(viteDir, 'pnpm-lock.yaml');
62+
const unrelatedSource = join(nodeDir, 'server.ts');
63+
write(manifest, '{"devDependencies":{"@vitest/utils":"4.1.10"}}\n');
64+
write(lockfile, '# upstream lockfile\n');
65+
write(unrelatedSource, '// upstream source\n');
66+
git('init');
67+
git('config', 'core.autocrlf', 'false');
68+
git('config', 'core.hooksPath', join(root, 'no-hooks'));
69+
git('add', '.');
70+
git(
71+
'-c',
72+
'user.name=Test',
73+
'-c',
74+
'user.email=test@example.com',
75+
'-c',
76+
'commit.gpgsign=false',
77+
'commit',
78+
'-m',
79+
'upstream fixture',
80+
);
81+
82+
mkdirSync(join(root, 'rolldown/packages'), { recursive: true });
83+
alignVendoredVitestDependencies(root, '5.0.0');
84+
const alignedManifest = readFileSync(manifest, 'utf8');
85+
expect(JSON.parse(alignedManifest).devDependencies['@vitest/utils']).toBe('5.0.0');
86+
write(lockfile, '# local lockfile change\n');
87+
write(unrelatedSource, '// unrelated local source change\n');
88+
89+
let brandedSources: string[] | undefined;
90+
for (let run = 0; run < 2; run++) {
91+
brandVite(root);
92+
93+
expect(readFileSync(manifest, 'utf8')).toBe(alignedManifest);
94+
expect(readFileSync(lockfile, 'utf8')).toBe('# local lockfile change\n');
95+
expect(readFileSync(unrelatedSource, 'utf8')).toBe('// unrelated local source change\n');
96+
expect(readFileSync(join(nodeDir, 'cli.ts'), 'utf8')).toContain("cac('vp')");
97+
expect(readFileSync(join(nodeDir, 'build.ts'), 'utf8')).not.toContain('logger.info(');
98+
const sources = Object.entries(upstreamSources).map(([file, upstream]) => {
99+
const source = readFileSync(join(nodeDir, file), 'utf8');
100+
expect(source).not.toBe(upstream);
101+
return source;
102+
});
103+
if (brandedSources) {
104+
expect(sources).toEqual(brandedSources);
105+
}
106+
brandedSources = sources;
107+
}
108+
} finally {
109+
rmSync(root, { recursive: true, force: true });
110+
}
111+
});

packages/tools/src/brand-vite.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,24 @@ function logPatch(file: string, desc: string, result: 'patched' | 'already') {
8989
export function brandVite(rootDir: string = process.cwd()) {
9090
log('Applying Vite+ branding patches...');
9191

92-
// Always patch raw upstream sources, including when sync-remote already applied branding.
93-
execFileSync('git', ['restore', '--source=HEAD', '--', '.'], {
94-
cwd: join(rootDir, VITE_DIR),
95-
});
96-
9792
const nodeDir = join(rootDir, VITE_NODE_DIR);
93+
// Start from upstream sources on every run, but preserve dependency alignment
94+
// from sync-remote and any other files that branding does not modify.
95+
execFileSync(
96+
'git',
97+
[
98+
'restore',
99+
'--source=HEAD',
100+
'--',
101+
'constants.ts',
102+
'cli.ts',
103+
'build.ts',
104+
'logger.ts',
105+
'plugins/reporter.ts',
106+
'config.ts',
107+
],
108+
{ cwd: nodeDir },
109+
);
98110

99111
// 1. constants.ts: Add VITE_PLUS_VERSION constant after VERSION
100112
const constantsFile = join(nodeDir, 'constants.ts');

pnpm-lock.yaml

Lines changed: 9 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)