(Name Collision) UiPath Parser/Compiler Bug with Fix Proposal

I ran into an issue which caused an uncaught illegal code state. In the issue I was dealing with, I had moved some code around and created a state which effectively behaved like the following C# code sample (note the name collision):

var aastate = GetAttribute("button1", "aastate");
while (!aastate.ToString().Contains("unavailable"))
{
    // snip
    var aastate = GetAttribute("button1", "aastate");
}

I should expect a situation like this to throw a compiler error in a normal environment and likely an equivalent error in UiPath Studio due to the aastate variable within the while scope having the same name as a member of a parent scope. Instead, I got a warning (read: not an error) about duplicate names in my variables menu, which I usually leave closed to avoid clutter. This lead me to not be aware of the issue for some time until I opened the menu for a completely unrelated reason.

Despite this clearly illegal code, the bot was allowed to compile and run. This resulted in a NullReferenceException as explained below.

In a standard code environment, if someone were to remove the first line from my sample code, a compiler warning would be thrown stating that aastate “does not exist or is not accessible.” However, in the situation introduced above, this catch somehow does not trigger. The observed behavior is that the while loop instead grabs the value of the uninitialized internal aastate instead of the aastate in the parent scope, causing the aforementioned NRE.

One of the Microsoft employees I have contact with commented on the observed behavior:

I think I have no choice but to say that this is complete magic, and clearly doesn’t follow the rules I’d expect.

While the NRE is technically correct behavior (since it’s attempting to grab the value of an uninitialized variable), this situation should not have been able to exist in the first place given the illegal nature of the code.

I propose three things:

  1. Change name collisions warnings into errors. This is how it is treated in Visual Studio and it is what I would expect to happen here. This should prevent the code from “compiling” or executing as it would otherwise introduce an illegal state.
  2. Disallow creating variables of the same name where name collisions would occur. This should include creating names using the ctrl+k shortcut.
  3. In the event of a name collision (and the only remaining situation should be moving code around), warnings should be displayed before allowing the code to be moved and an opportunity provided to rename or merge the problem variables.

Hi,
Thank you for your suggestion. I added it to our internal ideas tracker for our team to consider.

@Foxtrek_64 thanks for the detailed feedback.

There is a chance that we might break already existing automations if we enforce the naming collision to error. So we have the Workflow Analyzer Rule [ST-NMG-005](https://docs.uipath.com/studio/lang-en/v2020.10/docs/st-nmg-005) that allows a user to control the error level. If this is set to Error you can use it to prevent name collisions.

Let me know if this works for you.