Skip to content

Commit b66c098

Browse files
fix(docs): continue partial i18n batches after file errors
1 parent 896fb6f commit b66c098

3 files changed

Lines changed: 59 additions & 5 deletions

File tree

extensions/microsoft-foundry/onboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export async function selectFoundryDeployment(
212212
})),
213213
});
214214
const selected =
215-
supported.find((deployment) => deployment.name === selectedDeploymentName) ?? supported[0]!;
215+
supported.find((deployment) => deployment.name === selectedDeploymentName) ?? supported[0];
216216
return { selected, supported };
217217
}
218218

scripts/docs-i18n/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func runDocSequential(ctx context.Context, ordered []string, translator docsTran
206206
start := time.Now()
207207
skip, outputPath, err := processFileDoc(ctx, translator, docsRoot, file, srcLang, tgtLang, overwrite)
208208
if err != nil {
209-
if shouldStopDocRun(ctx, allowPartial) {
209+
if shouldStopDocRun(ctx, err, allowPartial) {
210210
return processed, skipped, outputs, err
211211
}
212212
if firstErr == nil {
@@ -259,7 +259,7 @@ func runDocParallel(ctx context.Context, ordered []string, docsRoot, srcLang, tg
259259
skipped: skip,
260260
err: err,
261261
}
262-
if err != nil && shouldStopDocRun(ctx, allowPartial) {
262+
if err != nil && shouldStopDocRun(ctx, err, allowPartial) {
263263
cancel()
264264
return
265265
}
@@ -306,11 +306,11 @@ func runDocParallel(ctx context.Context, ordered []string, docsRoot, srcLang, tg
306306
return processed, skipped, outputs, firstErr
307307
}
308308

309-
func shouldStopDocRun(ctx context.Context, allowPartial bool) bool {
309+
func shouldStopDocRun(ctx context.Context, err error, allowPartial bool) bool {
310310
if !allowPartial {
311311
return true
312312
}
313-
return ctx.Err() != nil
313+
return ctx.Err() != nil || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded)
314314
}
315315

316316
func runSegmentSequential(ctx context.Context, ordered []string, translator docsTranslator, tm *TranslationMemory, docsRoot, srcLang, tgtLang string) (int, []string, error) {

scripts/docs-i18n/main_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,24 @@ func (cancelAwareTranslator) TranslateRaw(ctx context.Context, text, _, _ string
126126

127127
func (cancelAwareTranslator) Close() {}
128128

129+
type contextErrorTranslator struct{}
130+
131+
func (contextErrorTranslator) Translate(_ context.Context, text, _, _ string) (string, error) {
132+
if strings.Contains(text, "CANCEL") {
133+
return "", context.Canceled
134+
}
135+
return text, nil
136+
}
137+
138+
func (contextErrorTranslator) TranslateRaw(_ context.Context, text, _, _ string) (string, error) {
139+
if strings.Contains(text, "CANCEL") {
140+
return "", context.Canceled
141+
}
142+
return text, nil
143+
}
144+
145+
func (contextErrorTranslator) Close() {}
146+
129147
type cancelAfterFirstDocTranslator struct {
130148
cancel context.CancelFunc
131149
calls int
@@ -376,6 +394,42 @@ func TestRunDocsI18NAllowPartialReturnsCancellationAfterPartialSuccess(t *testin
376394
}
377395
}
378396

397+
func TestRunDocsI18NAllowPartialStopsAfterContextError(t *testing.T) {
398+
t.Parallel()
399+
400+
docsRoot := t.TempDir()
401+
writeFile(t, filepath.Join(docsRoot, ".i18n", "glossary.zh-CN.json"), "[]")
402+
writeFile(t, filepath.Join(docsRoot, "docs.json"), `{"redirects":[]}`)
403+
firstPath := filepath.Join(docsRoot, "aaa-first.md")
404+
cancelPath := filepath.Join(docsRoot, "bbb-cancel.md")
405+
laterPath := filepath.Join(docsRoot, "zzz-later.md")
406+
writeFile(t, firstPath, "# Gateway\n")
407+
writeFile(t, cancelPath, "# CANCEL\n")
408+
writeFile(t, laterPath, "# Gateway\n")
409+
410+
err := runDocsI18N(context.Background(), runConfig{
411+
targetLang: "zh-CN",
412+
sourceLang: "en",
413+
docsRoot: docsRoot,
414+
mode: "doc",
415+
thinking: "high",
416+
overwrite: true,
417+
allowPartial: true,
418+
parallel: 1,
419+
}, []string{firstPath, cancelPath, laterPath}, func(_, _ string, _ []GlossaryEntry, _ string) (docsTranslator, error) {
420+
return contextErrorTranslator{}, nil
421+
})
422+
if !errors.Is(err, context.Canceled) {
423+
t.Fatalf("expected cancellation error to remain terminal, got %v", err)
424+
}
425+
if got := mustReadFile(t, filepath.Join(docsRoot, "zh-CN", "aaa-first.md")); !strings.Contains(got, "# Gateway") {
426+
t.Fatalf("expected first output to be written before cancellation, got:\n%s", got)
427+
}
428+
if _, err := os.Stat(filepath.Join(docsRoot, "zh-CN", "zzz-later.md")); err == nil {
429+
t.Fatal("did not expect later output to be written after context error")
430+
}
431+
}
432+
379433
func TestRunDocsI18NRewritesLineTitleFromExactGlossaryWithoutModel(t *testing.T) {
380434
t.Parallel()
381435

0 commit comments

Comments
 (0)