Skip to content

workspace.dispose() throws "Non-empty variable map" error after changeVariableType #9337

Description

@michaelnixonau

Check for duplicates

  • I have searched for similar issues before opening a new one.

Description

Calling workspace.dispose() after using workspace.getVariableMap().changeVariableType() can trigger a "Non-empty variable map" error when the variable being changed is the last of its original type.

The issue stems from changeVariableType in variable_map.ts. When a variable’s type changes, it is removed from the old type’s map, but the code does not remove the map itself if it becomes empty. As a result, VariableMap.clear() removes all variables but leaves behind the empty map. Its final check if (this.variableMap.size !== 0) then fails, causing the error.

Suggested fix

in variable_map.ts:

 if (oldType === newType) return variable;
// start of fix -- delete old type map if our var was the last of its type
const oldTypeVariables = this.variableMap.get(oldType);
oldTypeVariables?.delete(variable.getId());
if (oldTypeVariables?.size === 0) {
this.variableMap.delete(oldType);
}
// end of fix
variable.setType(newType);

Reproduction steps

  1. Create a workspace.
  2. Create a single variable of a custom type, for example, "typeA".
  3. Change the type of this variable to a new type, for instance, "typeB".
  4. Attempt to dispose of the workspace.
  5. Observe the error thrown from the clear() method in variable_map.ts.

Stack trace

Screenshots

Error: Non-empty variable map
at VariableMap.clear (variable_map.ts:X:X)
at Workspace.dispose (workspace.ts:X:X)
at ...

Browsers

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions