scratchfoundation / scratch-gui

Graphical User Interface for creating and running Scratch 3.0 projects.
https://scratchfoundation.github.io/scratch-gui/develop/
BSD 3-Clause "New" or "Revised" License
4.46k stars 3.55k forks source link

copy-and-paste code block copies private variables with unexpected behavior #5391

Open wyattscarpenter opened 4 years ago

wyattscarpenter commented 4 years ago

Expected Behavior

When copying a code block from sprite1 to sprite2 that contains a variable private to sprite1, a new variable of the same name should be made for sprite2. When this variable is checked in the variable menu, it should say: Sprite2: x [value]

Actual Behavior

When copypasting a code block from sprite1 to sprite2 that contains a variable private to sprite1, the resulting variables seem to be entangled somehow. When this variable is checked in the variable menu, it says: Sprite1: x [value in sprite1]. If you change the name of the variable in sprite2, it will say Sprite1: [name in sprite2] [value in sprite1]. (Un)checking one of the variables will (un)check it in the other sprite's menu. I think they are entangled in more ways, but since the value reporter is wrong it is hard to tell. I have a more complex program in which I think there's information leaking based on these shared variables, but I can't come up with a neat demo of it so I might be mistaken.

Notably, the correct behavior occurs when you copy the script via dragging it to the other sprite instead of copypasting.

Steps to Reproduce

Create a sprite with a private variable in a code block and copy-and-paste it into a different sprite. I made a project to demonstrate this (sorry it's a little convoluted, I was trying to test several cases that didn't end up being important): https://scratch.mit.edu/projects/360689189/ (note that in this project, all sprites have one variable checked, yet only two variables display)

Operating System and Browser

Windows 10 Firefox 72.0.1 64-bit

benjiwheeler commented 4 years ago

This relates to a larger problem, which is that we would ideally like to make sure that blocks always have some sort of validity -- that is, blocks we create/add to an editing space should never do absolutely nothing at all and be permanently useless.

Where this gets tricky is in deciding what to do about it. If you paste a big block of code that references a dozen variables and lists, do we really think the user wants us to create those variables and lists? What if there are already variables that share the names with some of the pasted variables -- should we go through and rename all of them, maybe adding an incrementing suffix like "velocity-2"? This gets very complicated, very fast...

We could remove all invalid blocks, but that would be surprising to users, and it would be hard to see where to put new, valid blocks in in their place.

We could somehow visually mark invalid blocks, maybe by making them gray...

It's a hard design problem!

wyattscarpenter commented 4 years ago

I agree, there's a lot of design space to be puzzled through here! However, I think an unambiguously good start is to make copy-paste have the same behavior as drag-and-drop, which is the behavior I expected.

BryceLTaylor commented 2 years ago

This is related tohttps://github.com/LLK/scratch-gui/issues/7935

hello-smile6 commented 2 years ago

7935