Skip to content

Commit 5f2248b

Browse files
authored
Add "Execution: Add stderr block" again (#2503)
Reverts #2479, which is a revert of #2397. I think the problem was not related to @Alx-Lai's change, the [Playground was slow](https://rust-lang.zulipchat.com/#narrow/channel/242791-t-infra/topic/playground.20incident.202024-12-03) for everyone.
1 parent be47639 commit 5f2248b

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

theme/book.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,17 @@ function playground_text(playground, hidden = true) {
110110
}
111111

112112
function run_rust_code(code_block) {
113-
var result_block = code_block.querySelector(".result");
113+
var result_stderr_block = code_block.querySelector(".result.stderr");
114+
if (!result_stderr_block) {
115+
result_stderr_block = document.createElement('code');
116+
result_stderr_block.className = 'result stderr hljs nohighlight hidden';
117+
118+
code_block.append(result_stderr_block);
119+
}
120+
var result_block = code_block.querySelector(".result.stdout");
114121
if (!result_block) {
115122
result_block = document.createElement('code');
116-
result_block.className = 'result hljs language-bash';
123+
result_block.className = 'result stdout hljs nohighlight';
117124

118125
code_block.append(result_block);
119126
}
@@ -127,21 +134,27 @@ function playground_text(playground, hidden = true) {
127134
edition = "2021";
128135
}
129136
var params = {
130-
version: "stable",
131-
optimize: "0",
137+
backtrace: true,
138+
channel: "stable",
132139
code: text,
133-
edition: edition
140+
edition: edition,
141+
mode: "debug",
142+
tests: false,
143+
crateType: "bin",
134144
};
135145

136146
if (text.indexOf("#![feature") !== -1) {
137147
params.version = "nightly";
138148
}
139149

140150
result_block.innerText = "Running...";
151+
// hide stderr block while running
152+
result_stderr_block.innerText = "";
153+
result_stderr_block.classList.add("hidden");
141154

142155
const playgroundModified = isPlaygroundModified(code_block);
143156
const startTime = window.performance.now();
144-
fetch_with_timeout("https://play.rust-lang.org/evaluate.json", {
157+
fetch_with_timeout("https://play.rust-lang.org/execute", {
145158
headers: {
146159
'Content-Type': "application/json",
147160
},
@@ -158,13 +171,26 @@ function playground_text(playground, hidden = true) {
158171
"latency": (endTime - startTime) / 1000,
159172
});
160173

161-
if (response.result.trim() === '') {
174+
if (response.stdout.trim() === '') {
162175
result_block.innerText = "No output";
163176
result_block.classList.add("result-no-output");
164177
} else {
165-
result_block.innerText = response.result;
178+
result_block.innerText = response.stdout;
166179
result_block.classList.remove("result-no-output");
167180
}
181+
182+
// trim compile message
183+
// ====================
184+
// Compiling playground v0.0.1 (/playground)
185+
// Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.60s
186+
// Running `target/debug/playground`
187+
// ====================
188+
const compileMsgRegex = /^\s+Compiling(.+)\s+Finished(.+)\s+Running(.+)\n/;
189+
response.stderr = response.stderr.replace(compileMsgRegex, "");
190+
if (response.stderr.trim() !== '') {
191+
result_stderr_block.classList.remove("hidden");
192+
result_stderr_block.innerText = response.stderr;
193+
}
168194
})
169195
.catch(error => {
170196
const endTime = window.performance.now();

0 commit comments

Comments
 (0)