thingworx-field-work / ThingworxVSCodeProject

Develop thingworx models using a real IDE
MIT License
33 stars 17 forks source link

Transitive dependencies of top level functions are not always copied in services #66

Closed kklorenzotesta closed 6 months ago

kklorenzotesta commented 7 months ago

If I have a top level function that call another one like:

function f2() {
  logger.error("F2");
}

function f1() {
  f2();
}

@ThingDefinition
class ThingB extends GenericThing {}

and f1 is called by a service in a Thing in a file that precedes alphabetically the file containing the function definitions, then f2 is not copied inside the service. Instead the service

@ThingDefinition
class ThingA extends GenericThing {
  test() {
    f1();
  }
}

is just:

var result = (function () {const METHOD_NAME = "test";
function f1() {
    f2();
}

    f1();
}).apply(me, [])

In the transformer f2 is correcly collected as a dependency of the service but is not included in the transpiled service since f2 is not transpiled yet. I didn't understood the Transformer code good enough, but it seems like when visiting the nodes it compiles only the first level of dependencies discovered, maybe to avoid incidental infinite recursion.

A full ready to use example that reproduces the issue is on this fork: github.com/kklorenzotesta/ThingworxVSCodeProjectFunctionsOrderBug

BogdanMihaiciuc commented 7 months ago

Thanks I will look into this

BogdanMihaiciuc commented 6 months ago

You should be able to fix this by updating your bm-thing-cli/bm-thing-transformer dependencies to version 2.0.1.

Please reopen if you're still seeing this issue after updating.

The fix is in this commit: https://github.com/BogdanMihaiciuc/ThingTransformer/commit/7520b5697fda9016eb1fac6de5e783f7b035da14

Normally, the thing transformer creates separate "code transformers" for global functions that are in files that haven't yet been processed and each of these can create their own code transformers for any other global functions they encounter. However these are not created when the global function is defined in the same file as the code that calls it since that transformer will end up visiting that global function at some point in its processing. The bug was that this rule was also in place for those code transformers specific to the global functions so they didn't properly process dependencies that were defined in the same file.

kklorenzotesta commented 6 months ago

Thank you very much for the quick fix! I'll try it on monday