fix(提交): C 代码编译不过时,提交前的自动格式化把出错的几行并成一行,学生认不出自己的代码
Deploy / deploy (push) Canceled after 0s

clang-format 不认中文分号、缺了分号的语句,会把它和下一行并起来(int n; 和 scanf 成了
一行),报错的行号也对不上学生记得的位置。现在 C / C++ 格式化之前先套上题目模板、在
判题机上编一次,编不过就原样返回、不格式化,代码照常提交、照常判成编译错误。

api 容器里没有 gcc,判题机的编译器和参数本来就是判的标准,所以借它编。这一下不进判题
队列、不建提交记录;判题机连不上时照旧格式化。代价是每次交 C 多一次编译,零点几秒。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
xuyueandClaude Opus 5.5 committed 2026-09-30 11:29:37 +08:00
1 parent fa092d334c
commit c2ef55b10b
5 files changed
+51 -3

No files matched your search

+23 -1
View File
@@ -17,7 +17,9 @@ import { optionalAuth, requireAuth, requireTeacher } from "../auth/middleware"
import type { AuthUser } from "../auth/session"
import { db, schema } from "../db"
import { failure, parseBody, success } from "../http"
import { compileFails, templateForLanguage } from "../judge/run"
import { JudgeStatus } from "../judge/status"
import { parseProblemTemplate } from "../judge/template"
import { judgeQueue } from "../queue"
import {
canAccessContest,
@@ -244,8 +246,28 @@ submissionRoutes.post("/submissions/:id/rejudge", requireTeacher, async (c) => {
submissionRoutes.post("/code/format", requireAuth, async (c) => {
const parsed = await parseBody(c, formatCodeRequestSchema, "Invalid format payload")
if (!parsed.success) return parsed.response
// C / C++ 编不过就原样返回,不格式化(理由见 judge/run.ts 的 compileFails)。
// 代码照常提交、照常判成编译错误,只是不改动学生的代码
const { code: source, language, problemId } = parsed.data
if (language === "c" || language === "cpp") {
const judgeLanguage = language === "c" ? "C" : "C++"
const [problem] =
problemId === undefined
? []
: await db
.select({ template: schema.problem.template })
.from(schema.problem)
.where(eq(schema.problem.id, problemId))
.limit(1)
const rawTemplate = problem ? templateForLanguage(problem.template, judgeLanguage) : null
const template = rawTemplate ? parseProblemTemplate(rawTemplate) : null
const full = template ? `${template.prepend}\n${source}\n${template.append}` : source
if (await compileFails(judgeLanguage, full)) {
return success(c, { code: source } satisfies FormatCodeResponse)
}
}
try {
const code = await formatCode(parsed.data.code, parsed.data.language)
const code = await formatCode(source, language)
return success(c, { code } satisfies FormatCodeResponse)
} catch (error) {
if (error instanceof CodeFormatError) {