Skip to content

fix: add support for determing workspace folder with root uri/path on initialize #1210

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

Merged
merged 6 commits into from
May 1, 2025

Conversation

shruti0085
Copy link
Contributor

Description

LSP lifecycle for VS extension is controlled by Visual Studio. It currently does not set the workspaceFolder as part of the Initialize Params of the LSP initialize request, however it does send this information via rootUri and rootPath properties. The workspaceFolders is utilized for the local project context indexing and so in order to support that, this change extends logic to determine the workspaceFolder to use the root properties when workspaceFolders is not set.

License

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@shruti0085 shruti0085 requested a review from a team as a code owner April 30, 2025 01:55
@shruti0085 shruti0085 requested review from breedloj and a team and removed request for a team April 30, 2025 01:56
return []
}
const getFolderName = (uri: string) => path.basename(URI.parse(uri).path) || 'workspace'
if (params.workspaceFolders) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Also check if the array is empty, so that we avoid returning an empty list too early

import * as path from 'path'
import { URI } from 'vscode-uri'

export function getWorkspaceFolders(params?: InitializeParams): WorkspaceFolder[] {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this method be wrapped in a try catch, returning an empty array if there is an error? In cases like VS, the inputs here would be out of the extension and user's control but we may not want the server to crash (or fail to initialize).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

import * as path from 'path'
import { URI } from 'vscode-uri'

export function getWorkspaceFolders(logging: Logging, params?: InitializeParams): WorkspaceFolder[] {
Copy link
Contributor

@justinmk3 justinmk3 Apr 30, 2025

Choose a reason for hiding this comment

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

why is this in localProjectContext/ instead of shared/ ? seems generally applicable , not specific to project context.


export function getWorkspaceFolders(logging: Logging, params?: InitializeParams): WorkspaceFolder[] {
try {
if (!params) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: we can move the early return statements outside of the try catch block

if(!params){
   return []
}
if (params.workspaceFolders && params.workspaceFolders.length > 0) {
   return params.workspaceFolders
}

try{
...

if (!params) {
return []
}
const getFolderName = (uri: string) => path.basename(URI.parse(uri).fsPath) || 'workspace'
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we default to workspace here? Couldn't it lead to some unexpected side effects?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

just as a default incase a folder name cannot be extracted. I can default it to the full path if that makes more sense

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to return the uri instead as default

return [{ name: folderName, uri: params.rootUri }]
}
if (params.rootPath) {
const pathUri = URI.parse(params.rootPath).toString()
Copy link
Contributor

Choose a reason for hiding this comment

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

it is slightly redundant that for this code path we do uri.parse twice. We do uri.parse() -> toString -> uri.parse()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reduced the duplication and refactored it a bit

}
return []
} catch (error) {
logging.error(`Error occurred when determine workspace folders: ${error}`)
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: when determine

@shruti0085 shruti0085 merged commit 5fd3174 into aws:main May 1, 2025
6 checks passed
shruti0085 added a commit to aws/language-server-runtimes that referenced this pull request May 15, 2025
…ChangeEvent (#509)

Currently in a lot of downstream servers, the `workspace.getWorkspaceFolder` method is used for path resolution. This only relied on the `initializeParams.workspaceFolder` value which did not take into account workspace folder changes(did not listen to didChangeWorkspaceFolders) that could have happened during the session.
In addition with VS client as mentioned in aws/language-servers#1210, the workspaceFolder value is not set(more details in that PR) however it does specify that information via `rootUri` and rootPath. The utility class from that PR has now been moved centrally to this repo.
A new `getAllWorkspaceFolders()` call has been added as a common utility that servers downstream can use as single source of truth to get latest information of workspaceFolders available to the lsp server.
A follow-up PR to language-servers repo will utilize this change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants