Drop `workspaceService.hasWorkspaceFolders` · Issue #6237 · microsoft/vscode-python · GitHub
Skip to content

Drop workspaceService.hasWorkspaceFolders #6237

Description

@DonJayamanne

Goal: Do not get into the habit of ignoring ts compiler warnings. We don't always know better.

Currently we have a helper property that checks whether we have workspace folders:

  • Checks whether vscode.workspace.workspaceFolders is an array
  • Checks whether length of array vscode.workspace.workspaceFolders is > 0

The use of this property leads to a poor programming practice:
Lets look at the following code:

if (this.workspaceService.hasWorkspaceFolder){
	return
}

const workspaceFolder = this.workspaceService.workspaceFolders[0];
// Do something with the above item
  • Based on the property hasWorkspaceFolders, we (developers of exension) know that we will have at least one folder.
  • However typescript doesn't know this, it throws a compiler warning about the fact that workspaceFolders could be undefined or could have a length of 0
  • To get around this, we basically tell typescript that we know better and ignore the warning as follows:
const workspaceFolder = this.workspaceService.workspaceFolders![0]!;
  • Note the use of the !.
  • This is us telling typescript that we know better and it is known not to be null.

Unfortunately, i've found that we've been using this even in cases when we shouldn't. I.e. we think we know better, and we're ignoring the warning, when we shoudln't.

Solution: Delete the hasWorkspaceFolders property

Goal: Do not get into the habit of ignoring ts compiler warnings. We don't always know better.

  • Old code
if (this.workspaceService.hasWorkspaceFolder){
	return
}
  • New code
// Variations of the following code.
if ((this.workspaceService.workspaceFolders || []).length === 0){
	return
}

@microsoft/pvsc-team

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions