periodic "TypeError: Cannot redefine property" after switching branch



  • It seems that some servers will not re-init code environment properly after a branch change through the screeps script UI.

     

    My guess is that the file-global scopes are re-ran, but previous results are still cached. This means that a call to Object.defineProperty is seen as a duplicate call, even though there is only one instance of this line and only one instance of a call to require() for the file.

    Workaround is to re-upload all code, or make a whitespace change in the UI.

    code snippet:

    proto.creep.js
    Object.defineProperty(Creep.prototype, "debug", { get: function () { return this.memory.debug; }, set: function (x) { this.memory.debug = x; } });

    main.js

    require('proto.creep');

     

    example console error:

    proto.creep:13
    Object.defineProperty(Creep.prototype, "debug", { get: function () { return this.memory.debug; }, set: function (x) { this.memory.debug = x; } });
    ^
    TypeError: Cannot redefine property: debug
    at Function.defineProperty (native)
    at proto.creep:13:8
    at main:2:1
    [11:01:31 AM]
    proto.creep:13
    Object.defineProperty(Creep.prototype, "debug", { get: function () { return this.memory.debug; }, set: function (x) { this.memory.debug = x; } });
    ^
    TypeError: Cannot redefine property: debug
    at Function.defineProperty (native)
    at proto.creep:13:8
    at main:2:1
     


  • This has been a random bug since the dawn of .tick() for this game. Luckily it doesn't appear that often. But I'm guessing the devs have quite a few other things farther up the totem pole keeping this from being fixed.

    Since it's so infrequent it may be hard to track down.



  • Just in case anyone else got here via google, I can 100% reproduce this bug when there is an error in the initial parsing of main.js (and anything it calls directly on that parse).

    This seems to cause the instance to re-parse the file, which will of course try to re-define the properties that you have already defined.