Repository navigation
Fixed: TypeScript scripts importing the same module as a global script, #2341 - #2408
Conversation
Run non-global TypeScript output in a CommonJS-like scope to prevent generated import bindings from colliding with bindings from shared scripts.
Run each compiled global script in a separate CommonJS scope and publish its shared exports globally to prevent generated import-name collisions.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…2341 The wrapper of a global script now only takes the first line of the compiled source and the line after it, which is empty without the wrapper. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@agross this PR takes your two commits b03172f and 25ffad0 (authorship kept), with a changed wrapper for global scripts so the line numbers in errors stay the same. Could you test the branch |
|
@GermanBluefox Installed and tested once more. Despite no mass recompilations (the adapter version hasn't changed since yesterday), I ran a couple of one-off scripts that recompiled successfully. Other scripts with deliberate (but unrelated) code changes still work. |
|
@agross thanks for testing and for the original fix. Merging. |
Fixes the remaining case from #2341: a TypeScript script that imports the same module as a global script (or two global scripts importing the same module) failed with
Global scripts and the script itself run as one
vm.Script, and TypeScript emits the same CommonJS binding for every script that imports a module.Based on the two fix commits from @agross (fork, branch
issue-2341), authorship kept. The first two commits of that branch were not taken: they only touched stalebuild/srcoutput, andmasteralready contains a more complete fix for the typings part.Changes
wrapCompiledScriptInCommonJsScope: compiled non-global TS scripts run in their own function scope (.call(exports)).wrapCompiledGlobalScriptInCommonJsScope: every compiled global TS script gets its own scope andexportsobject. Its exports are published onglobalThisas getter/setter, so later changes to exportedletvariables stay visible.main.ts.common.compiledstays unwrapped, so the compile cache is unaffected.test/testTypeScript.js: one reproduces the error without wrapping; the others cover global + normal script, two globals plus a liveletexport, and the line numbers.Line numbers in error messages
Both wrappers keep the line numbers unchanged (checked by comparing the stack traces before and after):
:6:11:6:11:8:11:8:11globalScriptLinesis counted after the global scripts are combined, sofixLineNostill works.fixLineNodoes not correct lines inside the global part).at script.js.x:10:4, pointing at the line after the script. The frame above now readsat Object.<anonymous> (…:8:1)instead ofat …:8:1. Only cosmetic.Behaviour changes to be aware of
const/letfrom global TS scripts are now reachable as globals. Before, they only ended up onsandbox.exports."use strict"stood in the middle of the combined code and had no effect. In the other direction, a"use strict"from a leading global TS script no longer makes the following JS code strict.thisin a TS script is nowexports({}) as in real CommonJS, instead of the sandbox context.ReferenceError.Cannot redefine propertyerror (checked: the properties of the VM context stay configurable). But the order changes: withvar foo = 1in a JS global andfunction fooin a later TS global,foois now the TS function. Before, it was1.Tests
test/testTypeScript.jsandtest/testResolveTypings.js: 37 passing.Test JSintegration tests could not run locally because an ioBroker instance was already running on the machine.^13.4.0required); not related to this change.🤖 Generated with Claude Code