Skip to content

Commit 820fab0

Browse files
committed
fix(task): must flush on replay logs to stdout/stderr
1 parent 3154ccf commit 820fab0

7 files changed

Lines changed: 147 additions & 2 deletions

File tree

crates/vite_task/fixtures/cache-sharing/pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { spawn } from 'node:child_process';
2+
3+
async function main() {
4+
const commands = [
5+
['node', ['echo.js', '1'.repeat(100)], { stdio: 'inherit' }],
6+
['node', ['echo.js', '2'.repeat(100)], { stdio: 'inherit' }],
7+
['node', ['echo.js', '3'.repeat(100)], { stdio: 'inherit' }],
8+
['node', ['echo.js', '4'.repeat(100)], { stdio: 'inherit' }],
9+
['node', ['echo.js', '5'.repeat(100)], { stdio: 'inherit' }],
10+
['node', ['echo.js', '6'.repeat(100)], { stdio: 'inherit' }],
11+
['node', ['echo.js', '7'.repeat(100)], { stdio: 'inherit' }],
12+
['node', ['echo.js', '8'.repeat(100)], { stdio: 'inherit' }],
13+
['node', ['echo.js', '9'.repeat(100)], { stdio: 'inherit' }],
14+
['node', ['echo.js', '10'.repeat(100)], { stdio: 'inherit' }],
15+
['node', ['echo.js', '11'.repeat(100)], { stdio: 'inherit' }],
16+
['node', ['echo.js', '12'.repeat(100)], { stdio: 'inherit' }],
17+
['node', ['echo.js', '13'.repeat(100)], { stdio: 'inherit' }],
18+
['node', ['echo.js', '14'.repeat(100)], { stdio: 'inherit' }],
19+
['node', ['echo.js', '15'.repeat(100)], { stdio: 'inherit' }],
20+
['node', ['echo.js', '16'.repeat(100)], { stdio: 'inherit' }],
21+
['node', ['echo.js', '17'.repeat(100)], { stdio: 'inherit' }],
22+
['node', ['echo.js', '18'.repeat(100)], { stdio: 'inherit' }],
23+
['node', ['echo.js', '19'.repeat(100)], { stdio: 'inherit' }],
24+
['node', ['echo.js', '20'.repeat(100)], { stdio: 'inherit' }],
25+
['node', ['echo.js', '21'.repeat(100)], { stdio: 'inherit' }],
26+
['node', ['echo.js', '22'.repeat(100)], { stdio: 'inherit' }],
27+
['node', ['echo.js', '23'.repeat(100)], { stdio: 'inherit' }],
28+
['node', ['echo.js', '24'.repeat(100)], { stdio: 'inherit' }],
29+
['node', ['echo.js', '25'.repeat(100)], { stdio: 'inherit' }],
30+
['node', ['echo.js', '26'.repeat(100)], { stdio: 'inherit' }],
31+
['node', ['echo.js', '27'.repeat(100)], { stdio: 'inherit' }],
32+
['node', ['echo.js', '28'.repeat(100)], { stdio: 'inherit' }],
33+
['node', ['echo.js', '29'.repeat(100)], { stdio: 'inherit' }],
34+
['node', ['echo.js', '30'.repeat(100)], { stdio: 'inherit' }],
35+
];
36+
37+
console.log('[build.js] --------------------------------');
38+
console.log('[build.js] start');
39+
40+
await Promise.all(commands.map(cmd => exec(...cmd)));
41+
// const r = await exec('node', ['echo.js', '4'.repeat(100)], { stdio: 'inherit' });
42+
// console.log(r);
43+
44+
console.log('[build.js] main process end');
45+
}
46+
47+
main().catch(console.error);
48+
49+
/**
50+
* @param {string} command
51+
* @param {ReadonlyArray<string>} args
52+
* @param {object} [options]
53+
*/
54+
export async function exec(command, args, options) {
55+
return new Promise((resolve, reject) => {
56+
const _process = spawn(command, args, {
57+
stdio: [
58+
'ignore', // stdin
59+
'pipe', // stdout
60+
'pipe', // stderr
61+
],
62+
...options,
63+
shell: process.platform === 'win32',
64+
});
65+
66+
/**
67+
* @type {Buffer[]}
68+
*/
69+
const stderrChunks = [];
70+
/**
71+
* @type {Buffer[]}
72+
*/
73+
const stdoutChunks = [];
74+
75+
_process.stderr?.on('data', chunk => {
76+
stderrChunks.push(chunk);
77+
});
78+
79+
_process.stdout?.on('data', chunk => {
80+
stdoutChunks.push(chunk);
81+
});
82+
83+
_process.on('error', error => {
84+
reject(error);
85+
});
86+
87+
_process.on('exit', code => {
88+
const ok = code === 0;
89+
const stderr = Buffer.concat(stderrChunks).toString().trim();
90+
const stdout = Buffer.concat(stdoutChunks).toString().trim();
91+
92+
if (ok) {
93+
const result = { ok, code, stderr, stdout };
94+
resolve(result);
95+
} else {
96+
reject(
97+
new Error(
98+
`Failed to execute command: ${command} ${args.join(' ')}: ${stderr}`,
99+
),
100+
);
101+
}
102+
});
103+
});
104+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const args = process.argv.slice(2);
2+
console.warn('[echo.js] --------------------------------');
3+
console.warn('[echo.js] ' + args.join(' '));
4+
console.warn('[echo.js] --------------------------------');
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@test/replay-logs-chronological-order",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"scripts": {
6+
"build": "node build.js"
7+
},
8+
"packageManager": "pnpm@10.15.1"
9+
}

crates/vite_task/fixtures/replay-logs-chronological-order/pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
packages:
2+
- "."

crates/vite_task/src/schedule.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,16 @@ async fn get_cached_or_execute<'a>(
159159
let mut stderr = tokio::io::stderr();
160160
for output_section in std_outputs.as_ref() {
161161
match output_section.kind {
162-
OutputKind::StdOut => stdout.write_all(&output_section.content).await?,
163-
OutputKind::StdErr => stderr.write_all(&output_section.content).await?,
162+
OutputKind::StdOut => {
163+
stdout.write_all(&output_section.content).await?;
164+
// flush stdout to ensure the output is displayed in the correct order
165+
stdout.flush().await?;
166+
}
167+
OutputKind::StdErr => {
168+
stderr.write_all(&output_section.content).await?;
169+
// flush stderr too
170+
stderr.flush().await?;
171+
}
164172
}
165173
}
166174
Ok(())

0 commit comments

Comments
 (0)