Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/tools/src/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,38 @@ Done in 171ms using pnpm v10.16.1
expect(replaceUnstableOutput(output.trim(), cwd)).toMatchSnapshot();
});

describe.skipIf(process.platform !== 'win32')('Windows cwd replacement', () => {
test('mixed-separator cwd matches all-backslash output', () => {
// Simulates the CI failure: cwd has mixed separators (template literal),
// but Vite outputs all-backslash paths (path.resolve)
const cwd =
'C:\\Users\\RUNNER~1\\AppData\\Local\\Temp/vite-plus-test-abc/command-staged-broken-config';
const output =
'failed to load config from C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\vite-plus-test-abc\\command-staged-broken-config\\vite.config.ts';
expect(replaceUnstableOutput(output, cwd)).toBe(
'failed to load config from <cwd>/vite.config.ts',
);
});

test('all-backslash cwd matches all-backslash output', () => {
const cwd = 'C:\\Users\\runner\\project';
const output = 'error in C:\\Users\\runner\\project\\src\\main.ts';
expect(replaceUnstableOutput(output, cwd)).toBe('error in <cwd>/src/main.ts');
});

test('cwd at end of string without trailing separator', () => {
const cwd = 'C:\\Users\\runner\\project';
const output = 'path is C:\\Users\\runner\\project';
expect(replaceUnstableOutput(output, cwd)).toBe('path is <cwd>');
});

test('parent directory replacement with backslash paths', () => {
const cwd = 'C:\\Users\\RUNNER~1\\Temp/vite-plus-test/my-test';
const output = 'found C:\\Users\\RUNNER~1\\Temp\\vite-plus-test\\other\\file.ts';
expect(replaceUnstableOutput(output, cwd)).toBe('found <cwd>/../other/file.ts');
});
});

test('replace tsdown output', () => {
const output = `
ℹ tsdown v0.15.1 powered by rolldown v0.15.1
Expand Down
19 changes: 16 additions & 3 deletions packages/tools/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@ export function replaceUnstableOutput(output: string, cwd?: string) {
output = output.replaceAll(ANSI_ESCAPE_REGEX, '').replaceAll(/\r\n/g, '\n').replaceAll(/\r/g, '');

if (cwd) {
output = output.replaceAll(cwd, '<cwd>');
if (path.dirname(cwd) !== '/') {
output = output.replaceAll(path.dirname(cwd), '<cwd>/..');
// On Windows, cwd may have mixed separators (from template literals like `${tmp}/name`)
// while output uses all-backslash paths (from path.resolve()). Replace the all-backslash
// form of each path token, with trailing separator first so the separator after the
// placeholder is normalized to forward slash.
const replacePathToken = (rawPath: string, placeholder: string) => {
if (process.platform === 'win32') {
const backslash = rawPath.replaceAll('/', '\\');
output = output.replaceAll(backslash + '\\', placeholder + '/');
output = output.replaceAll(backslash, placeholder);
}
output = output.replaceAll(rawPath, placeholder);
};
replacePathToken(cwd, '<cwd>');
const parent = path.dirname(cwd);
if (parent !== '/') {
replacePathToken(parent, '<cwd>/..');
}
}
// On Windows, normalize path separators in file paths for consistent snapshots.
Expand Down
Loading