Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkgs/mcpx/internal/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions pkgs/mcpx/internal/e2e/effect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 21 additions & 4 deletions pkgs/mcpx/internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 "/// <reference path=\"./" + GlobalsFileName + "\" />\n" + out, nil
}

func referencesEntry(t launcher.Template) bool {
Expand Down