From a874e1db6bad83d1f6609609f26bbf85e0e2bf9d Mon Sep 17 00:00:00 2001 From: Everett Date: Thu, 19 Nov 2026 00:39:15 -0800 Subject: [PATCH 1/2] Runtime: a source ending in a // comment compiles The entry wrapper appended its closing code directly to the source, so a last line that was a comment without a line break swallowed it and the widget failed to compile. The wrapper now ends the source with a line break, in the Swift runtime and in the shared JavaScript that Android, Windows, Linux and the browser preview run. No source line number moves. The compiler fingerprint stays: artifacts compiled before remain correct for their source, so no widget needs recompiling. Co-Authored-By: Claude Fable 5.2 --- .../Widget/Runtime/ScriptWidgetRuntime.swift | 5 +++++ .../RuntimeUnitTests.swift | 15 +++++++++++++++ Web/preview-engine/src/contract.js | 2 ++ Web/preview-engine/test/contract.test.js | 4 +++- Web/preview-engine/test/kernel.test.js | 7 +++++++ 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Shared/ScriptWidgetRuntime/Widget/Runtime/ScriptWidgetRuntime.swift b/Shared/ScriptWidgetRuntime/Widget/Runtime/ScriptWidgetRuntime.swift index 763cfd56..15040792 100644 --- a/Shared/ScriptWidgetRuntime/Widget/Runtime/ScriptWidgetRuntime.swift +++ b/Shared/ScriptWidgetRuntime/Widget/Runtime/ScriptWidgetRuntime.swift @@ -329,6 +329,11 @@ enum ScriptWidgetCompiler { static func wrappedSource(_ source: String, callFunction: String = "") -> String { var result = "async function $main() { try {" result += source + // A source may end in a `//` comment without a line break; what follows + // must not become part of that comment. A line break at the end moves + // no line number of the source. The fingerprint is not bumped for it: + // every artifact compiled before stays correct for its source. + result += "\n" if !callFunction.isEmpty { result += "await \(callFunction)();" } result += "} catch(e){ console.error(e); $error(`${e}`) } }" return result diff --git a/Tests/ScriptWidgetRuntimeTests/RuntimeUnitTests.swift b/Tests/ScriptWidgetRuntimeTests/RuntimeUnitTests.swift index 0316d45c..49f7e78f 100644 --- a/Tests/ScriptWidgetRuntimeTests/RuntimeUnitTests.swift +++ b/Tests/ScriptWidgetRuntimeTests/RuntimeUnitTests.swift @@ -905,6 +905,21 @@ final class CompiledArtifactTests: XCTestCase { XCTAssertTrue(artifact.javaScript.contains("$main")) XCTAssertNotNil(ScriptWidgetCompiledArtifactStore.load(package: package, source: source)) } + + /// The wrapper used to append its own closing code directly to the source, + /// so a last line that was a comment swallowed it and nothing compiled. + func testSourceEndingInALineCommentWithoutLineBreakCompiles() throws { + let source = "$render(ready); // the end" + let result = ScriptWidgetPrecompiler.compileNow(package: package, source: source) + guard case .success(let artifact) = result else { + return XCTFail("precompile failed: \(result)") + } + XCTAssertTrue(artifact.javaScript.contains("$main")) + XCTAssertEqual( + ScriptWidgetCompiler.wrappedSource("x // note", callFunction: "render"), + "async function $main() { try {x // note\nawait render();} catch(e){ console.error(e); $error(`${e}`) } }" + ) + } } final class ExecutionSessionTests: XCTestCase { diff --git a/Web/preview-engine/src/contract.js b/Web/preview-engine/src/contract.js index 291c0f65..1048d759 100644 --- a/Web/preview-engine/src/contract.js +++ b/Web/preview-engine/src/contract.js @@ -42,6 +42,8 @@ export function makeError(kind, message) { export function wrappedSource(source, callFunction = "") { let result = "async function $main() { try {"; result += source; + // A source may end in a `//` comment without a line break. + result += "\n"; if (callFunction !== "") result += `await ${callFunction}();`; result += "} catch(e){ console.error(e); $error(`${e}`) } }"; return result; diff --git a/Web/preview-engine/test/contract.test.js b/Web/preview-engine/test/contract.test.js index fb6a1a5d..05389fd8 100644 --- a/Web/preview-engine/test/contract.test.js +++ b/Web/preview-engine/test/contract.test.js @@ -27,5 +27,7 @@ test("native compiler fingerprint and entry wrapper match ScriptWidgetCompiler", assert.match(packageSwift, new RegExp(`static let compilerFingerprint = "${runtimeContract.nativeCompilerFingerprint}"`)); assert.ok(runtimeSwift.includes('var result = "async function $main() { try {"')); assert.ok(runtimeSwift.includes('result += "} catch(e){ console.error(e); $error(`${e}`) } }"')); - assert.equal(wrappedSource("X"), "async function $main() { try {X} catch(e){ console.error(e); $error(`${e}`) } }"); + assert.ok(runtimeSwift.includes('result += "\\n"')); + assert.equal(wrappedSource("X"), "async function $main() { try {X\n} catch(e){ console.error(e); $error(`${e}`) } }"); + assert.equal(wrappedSource("X // note", "render"), "async function $main() { try {X // note\nawait render();} catch(e){ console.error(e); $error(`${e}`) } }"); }); diff --git a/Web/preview-engine/test/kernel.test.js b/Web/preview-engine/test/kernel.test.js index 9476e43d..785dc819 100644 --- a/Web/preview-engine/test/kernel.test.js +++ b/Web/preview-engine/test/kernel.test.js @@ -35,6 +35,13 @@ test("missing $render text yields the native placeholder element", async () => { assert.equal(result.tree, '{"$element":"text","props":null,"children":["#UI Not Found#"]}'); }); +test("a source that ends in a line comment without a line break runs", async () => { + // The wrapper's closing code used to land inside the comment. + const result = await run("$render(ready); // the end"); + assert.equal(result.error, null); + assert.equal(result.tree, '{"$element":"text","props":null,"children":["ready"]}'); +}); + test("errors use the native categories", async () => { assert.equal((await run('throw new Error("boom")')).error.kind, "scriptException"); assert.equal((await run("$render()")).error.kind, "scriptException"); From d0fe178028a36d48a33f6d6eca046fbc42d45b5b Mon Sep 17 00:00:00 2001 From: Everett Date: Thu, 19 Nov 2026 00:41:58 -0800 Subject: [PATCH 2/2] Android: rebuild the generated JavaScript for the wrapper change Co-Authored-By: Claude Fable 5.2 --- Android/core/src/main/assets/scriptwidget/prepare.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Android/core/src/main/assets/scriptwidget/prepare.js b/Android/core/src/main/assets/scriptwidget/prepare.js index 4c06b715..faf56c35 100644 --- a/Android/core/src/main/assets/scriptwidget/prepare.js +++ b/Android/core/src/main/assets/scriptwidget/prepare.js @@ -44,6 +44,8 @@ function makeError(kind, message) { function wrappedSource(source, callFunction = "") { let result = "async function $main() { try {"; result += source; + // A source may end in a `//` comment without a line break. + result += "\n"; if (callFunction !== "") result += `await ${callFunction}();`; result += "} catch(e){ console.error(e); $error(`${e}`) } }"; return result;