From 331e4f94c80e997825777fcafb8920445516cf7e Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 20:52:16 +0000 Subject: [PATCH] fix(mcpx): generated launchers type check now that run checks by default run defaults script.check on (d3772a58c, #1868 point 4), which turned four e2e tests red: - The @entry fill emitted `"" ? mod[""] : mod.default`, a ternary on a string literal that deno check rejects (TS2873/TS2872). The export is known when the launcher is built, so pick it in Go. - Custom launchers lacked the mcpx-globals.d.ts reference the built-in launcher carries, so any script using `log` failed with TS2304. Prepend the same directive to every expanded custom launcher. - TestScriptTypecheckOnRefusesATypeError assumed run was off by default; it now asks for off explicitly. - The concurrent-launcher test uses a deliberately headerless launcher, which cannot type check; it passes --script-check off. Co-Authored-By: Claude Opus 5.5 --- pkgs/mcpx/internal/e2e/e2e_test.go | 4 +++- pkgs/mcpx/internal/e2e/effect_test.go | 9 ++++++--- pkgs/mcpx/internal/runner/runner.go | 25 +++++++++++++++++++++---- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/pkgs/mcpx/internal/e2e/e2e_test.go b/pkgs/mcpx/internal/e2e/e2e_test.go index 92aa5439ae..df9bd6fd8f 100644 --- a/pkgs/mcpx/internal/e2e/e2e_test.go +++ b/pkgs/mcpx/internal/e2e/e2e_test.go @@ -3280,7 +3280,9 @@ func TestConcurrentRunsOfOneScriptKeepTheirOwnLauncher(t *testing.T) { for i := 0; i < n; i++ { go func(i int) { mark := fmt.Sprintf("LAUNCHER-%d", i) - o, err := e.try("run", "--launcher", + // The launcher is deliberately headerless, so it does not + // type check; run checks by default (#1868 point 4). + o, err := e.try("run", "--script-check", "off", "--launcher", fmt.Sprintf("console.log(%q); @entry", mark), script) out <- res{mark, o, err} }(i) diff --git a/pkgs/mcpx/internal/e2e/effect_test.go b/pkgs/mcpx/internal/e2e/effect_test.go index a61718cfe5..237b79c5cc 100644 --- a/pkgs/mcpx/internal/e2e/effect_test.go +++ b/pkgs/mcpx/internal/e2e/effect_test.go @@ -195,14 +195,17 @@ func TestScriptTypecheckOnRefusesATypeError(t *testing.T) { t.Fatal(err) } - // Off by default: the annotation is stripped and the script runs. + saved := e.envVars + defer func() { e.envVars = saved }() + + // Off: the annotation is stripped and the script runs. run checks by + // default (#1868 point 4), so off has to be asked for. + e.envVars = append(append([]string{}, saved...), "MCPX_SCRIPT_TYPECHECK=off") if out := e.run("run", script); !strings.Contains(out, "not a number") { t.Fatalf("with typecheck off the script should run:\n%s", out) } - saved := e.envVars e.envVars = append(append([]string{}, saved...), "MCPX_SCRIPT_TYPECHECK=on") - defer func() { e.envVars = saved }() out, err := e.try("run", script) if err == nil { diff --git a/pkgs/mcpx/internal/runner/runner.go b/pkgs/mcpx/internal/runner/runner.go index d36c17092f..24458b39fd 100644 --- a/pkgs/mcpx/internal/runner/runner.go +++ b/pkgs/mcpx/internal/runner/runner.go @@ -1437,17 +1437,25 @@ func expandCustom(text string, opts Options, scriptPath, base, export string, // Braced, so that a template naming @entry twice produces two runs rather // than a redeclaration error. Allowing a repeat and then emitting code // that cannot compile would be a worse answer than refusing it outright. + // The export is known here, so the selection is made in Go rather + // than emitted as a ternary on a string literal: a constant condition + // is TS2872/TS2873 under deno check, and run checks by default + // (#1868 point 4), so the generated launcher must itself type check. + pick, call := "(mod as any).default", "await entry(argv)" + if export != "" { + pick, call = "(mod as any)["+jsonString(export)+"]", "await entry(...argv)" + } entryBlock := fmt.Sprintf(`{ %s - const entry = %s ? (mod as any)[%s] : (mod as any).default; + const entry = %s; if (typeof entry === "function") { - const __v = %s ? await entry(...argv) : await entry(argv); + const __v = %s; result.value = __v; if (__v !== undefined) { console.log(typeof __v === "string" ? __v : JSON.stringify(__v, null, 2)); } } -}`, importLine, jsonString(export), jsonString(export), jsonString(export)) +}`, importLine, pick, call) fill := launcher.Fill{ launcher.Header: launcherHeader(scriptPath, base, export), @@ -1494,7 +1502,16 @@ func expandCustom(text string, opts Options, scriptPath, base, export string, fmt.Fprintf(os.Stderr, "mcpx: %s refers to neither @entry nor @import, so the script will not run\n", name) } - return launcher.Expand(tpl, fill, launcher.Options{AllowRepeat: allow, Defs: defs}) + out, err := launcher.Expand(tpl, fill, launcher.Options{AllowRepeat: allow, Defs: defs}) + if err != nil { + return "", err + } + // The same ambient reference the built-in launcher carries: without + // it a checked run (the default for run, #1868 point 4) reports + // "Cannot find name 'log'" for every script that uses a global. A + // triple-slash directive only counts at the top of the file, so it + // goes first whatever the template puts there. + return "/// \n" + out, nil } func referencesEntry(t launcher.Template) bool {