From 52d935c815e9f7eed6871bd01cbbd1d97c48e23e Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Mon, 12 Jan 2026 15:57:00 -0800 Subject: [PATCH] fix: Fix bug that prevented deleting a variable referenced by two connected blocks (#9563) * fix: Fix bug that prevented deleting a variable referenced by two connected blocks * fix: Remove inadvertent .only --- core/connection.ts | 5 +++- core/variable_map.ts | 2 ++ tests/mocha/blocks/variables_test.js | 34 ++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/core/connection.ts b/core/connection.ts index a79b7b9b1..a55c25059 100644 --- a/core/connection.ts +++ b/core/connection.ts @@ -291,7 +291,10 @@ export class Connection { } let event; - if (eventUtils.isEnabled()) { + if ( + eventUtils.isEnabled() && + !childConnection.getSourceBlock().isDeadOrDying() + ) { event = new (eventUtils.get(EventType.BLOCK_MOVE))( childConnection.getSourceBlock(), ) as BlockMove; diff --git a/core/variable_map.ts b/core/variable_map.ts index 3a6cf4026..88c28cbc3 100644 --- a/core/variable_map.ts +++ b/core/variable_map.ts @@ -315,6 +315,8 @@ export class VariableMap } try { for (let i = 0; i < uses.length; i++) { + if (uses[i].isDeadOrDying()) continue; + uses[i].dispose(true); } const variables = this.variableMap.get(variable.getType()); diff --git a/tests/mocha/blocks/variables_test.js b/tests/mocha/blocks/variables_test.js index 066de52b8..724e2e543 100644 --- a/tests/mocha/blocks/variables_test.js +++ b/tests/mocha/blocks/variables_test.js @@ -30,6 +30,25 @@ suite('Variables', function () { 'variableTypes': ['', 'type1', 'type2'], }, ], + 'output': null, + }, + // Block for variable setter. + { + 'type': 'set_var_block', + 'message0': '%{BKY_VARIABLES_SET}', + 'args0': [ + { + 'type': 'field_variable', + 'name': 'VAR', + 'variableTypes': ['', 'type1', 'type2'], + }, + { + 'type': 'input_value', + 'name': 'VALUE', + }, + ], + 'previousStatement': null, + 'nextStatement': null, }, ]); this.variableMap = this.workspace.getVariableMap(); @@ -59,6 +78,21 @@ suite('Variables', function () { return block; } + test('can be deleted when two connected blocks reference the same variable', function () { + const getter = new Blockly.Block(this.workspace, 'get_var_block'); + getter.getField('VAR').setValue('1'); + + const setter = new Blockly.Block(this.workspace, 'set_var_block'); + setter.getField('VAR').setValue('1'); + setter.getInput('VALUE').connection.connect(getter.outputConnection); + + this.variableMap.deleteVariable(this.variableMap.getVariableById('1')); + // Both blocks should have been deleted. + assert.equal(0, this.workspace.getAllBlocks(false).length); + // The variable itself should have been deleted. + assert.equal(this.variableMap.getVariableById('1'), undefined); + }); + suite('allUsedVarModels', function () { test('All used', function () { createTestVarBlock(this.workspace, '1');