Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make IAction properties readonly #164846

Closed
wants to merge 2 commits into from
Closed

Conversation

mjbvz
Copy link
Collaborator

@mjbvz mjbvz commented Oct 28, 2022

The IAction interface is primarily used to passing around information about actions. However most properties on IAction are currently writable. This is bad as it makes code more difficult to reason about since callers are free to change these properties in sometimes unexpected ways.

Worse, some current implementers of IAction assume that the properties should are readonly. You can see this happening with MenuItemAction, which declares all of its properties as readonly but can then can see these properties be mutated if it is passed as an IAction


In order to prevent problems like the above, and also to hopefully increase the number of places where we can safely use simple a IAction instead of the more heavyweight Action class (see #164842 for an example of why this can be beneficial), I've updated IAction to be the readonly representation of an action. This also required updating a few types like ActionViewItems to let callers know which type of actions are being used

The `IAction` interface is primarily used to passing around information about actions. Many properties on this interface are currently writable however, which is not ideal as it means callers are free to change these properties in sometimes unexpected ways

Worse, the few places in our codebase that do need to write these properties assume that they actually have an `Action` instead of an `IAction`. You can see this happening with [`MenuItemAction`](https://github.com/microsoft/vscode/blob/9c802415e11bf11230720bc4a93397c338121da8/src/vs/platform/actions/common/actions.ts#L401), which declares all of its properties as `readonly` but can then can see these properties be mutated if it is passed as an `IAction`

In order to prevent problems like the above, and also to hopefully increase the number of places where we use simple `IActions` instead of the more heavyweight `Action` class, I've updated `IAction` to be the readonly representation of an action. This required updating a few types like `ActionViewItems` to let callers know which type of actions are being used
@mjbvz mjbvz added this to the November 2022 milestone Oct 28, 2022
@mjbvz mjbvz requested review from bpasero, Tyriar and alexdima October 28, 2022 04:45
@mjbvz mjbvz self-assigned this Oct 28, 2022
@@ -81,11 +80,6 @@ export class TerminalViewPane extends ViewPane {
) {
super(options, keybindingService, _contextMenuService, _configurationService, _contextKeyService, viewDescriptorService, _instantiationService, openerService, themeService, telemetryService);
this._register(this._terminalService.onDidRegisterProcessSupport(() => {
if (this._actions) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tyriar As far as I could tell, this property is unused

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we removed the usage and forgot to clean up 👍

@@ -405,12 +399,13 @@ class SingleTerminalTabActionViewItem extends MenuEntryActionViewItem {
this._register(this._terminalService.onDidChangeInstanceColor(e => this.updateLabel(e.instance)));
this._register(this._terminalService.onDidChangeInstanceTitle(e => {
if (e === this._terminalGroupService.activeInstance) {
this._action.tooltip = getSingleTabTooltip(e, this._terminalService.configHelper.config.tabs.separator);
// TODO: Fix types
(this._action as unknown as Action).tooltip = getSingleTabTooltip(e, this._terminalService.configHelper.config.tabs.separator);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tyriar This is the error I mentioned in the PR description. MenuItemAction says its properties are all readonly, but we then try writing them here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be readonly on MenuItemAction but we're dealing with an IAction here (surprised TS never gave us an error for MenuItemAction implements IAction). Changing the tooltip is important here as the terminal title is dynamic, can you come up with a solution for this too so this doesn't just create another debt item that probably won't get looked at for a long time.

@alexdima alexdima requested review from sandy081 and removed request for alexdima October 31, 2022 14:14
@sandy081 sandy081 requested a review from sbatten November 1, 2022 08:26
@bpasero bpasero requested a review from jrieken November 8, 2022 10:07
bpasero
bpasero previously approved these changes Nov 8, 2022
Copy link
Member

@bpasero bpasero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big stakeholder and I only glanced over the changes, but I am always 💯 for more readonly

@@ -405,12 +399,13 @@ class SingleTerminalTabActionViewItem extends MenuEntryActionViewItem {
this._register(this._terminalService.onDidChangeInstanceColor(e => this.updateLabel(e.instance)));
this._register(this._terminalService.onDidChangeInstanceTitle(e => {
if (e === this._terminalGroupService.activeInstance) {
this._action.tooltip = getSingleTabTooltip(e, this._terminalService.configHelper.config.tabs.separator);
// TODO: Fix types
(this._action as unknown as Action).tooltip = getSingleTabTooltip(e, this._terminalService.configHelper.config.tabs.separator);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be readonly on MenuItemAction but we're dealing with an IAction here (surprised TS never gave us an error for MenuItemAction implements IAction). Changing the tooltip is important here as the terminal title is dynamic, can you come up with a solution for this too so this doesn't just create another debt item that probably won't get looked at for a long time.

@sandy081
Copy link
Member

sandy081 commented Nov 9, 2022

Pushed a commit that handles Action safely in CompositeBarActions without changing previous behaviour. Otherwise ✅ from me.

@mjbvz mjbvz modified the milestones: November 2022, December 2022 Nov 29, 2022
@mjbvz mjbvz modified the milestones: January 2023, February 2023 Jan 24, 2023
@TylerLeonhardt TylerLeonhardt added the deferred Issues that were automatically moved out of the release milestone label Feb 22, 2023
@mjbvz mjbvz modified the milestones: March 2023, On Deck Mar 22, 2023
@mjbvz
Copy link
Collaborator Author

mjbvz commented Jun 3, 2024

Closing as out of date. May revisit at some point

@mjbvz mjbvz closed this Jun 3, 2024
@vs-code-engineering vs-code-engineering bot locked and limited conversation to collaborators Jul 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
deferred Issues that were automatically moved out of the release milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants