-
Notifications
You must be signed in to change notification settings - Fork 84
Suppress compose code lenses for documents outside the workspace #544
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
Brandon Waterloo [MSFT] (bwateratmsft)
merged 7 commits into
main
from
copilot/fix-code-lens-error
Aug 13, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a01bf38
Initial plan
Copilot fa96df2
Initial plan: gate compose code lenses on workspace folder membership
Copilot 6d4aca8
Suppress compose code lenses for documents outside the workspace
Copilot af77fe0
Reorder ServiceStartupCodeLensProvider early exits
Copilot 4ff3b36
Use structured URI comparison for workspace folder containment
bwateratmsft a066774
Restore original file mode on docker-compose-langserver
bwateratmsft 9202fbd
Make workspace path matching case-insensitive on macOS and Windows
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
packages/compose-language-service/src/service/utils/isDocumentInWorkspace.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /*!-------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See LICENSE in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import type { WorkspaceFolder } from 'vscode-languageserver'; | ||
| import type { DocumentUri } from 'vscode-languageserver-textdocument'; | ||
| import type { ActionContext } from './ActionContext'; | ||
|
|
||
| const useCaseInsensitivePathComparison = process.platform === 'darwin' || process.platform === 'win32'; | ||
|
|
||
| /** | ||
| * Determines whether a document is located within one of the workspace folders open in the client. | ||
| * If the client does not support the `workspace/workspaceFolders` request, the document is | ||
| * optimistically treated as being within the workspace (so behavior is unchanged for such clients). | ||
| * @param ctx The current action context (used to access client capabilities and the connection) | ||
| * @param documentUri The URI of the document | ||
| * @returns True if the document is within a workspace folder (or the capability is unsupported), false otherwise | ||
| */ | ||
| export async function isDocumentInWorkspace(ctx: ActionContext, documentUri: DocumentUri): Promise<boolean> { | ||
| // If the client doesn't support workspace folders, we can't verify, so optimistically show code lenses | ||
| if (!ctx.clientCapabilities?.workspace?.workspaceFolders) { | ||
| return true; | ||
| } | ||
|
|
||
| const folders = await ctx.connection.workspace.getWorkspaceFolders(); | ||
| return isDocumentInWorkspaceFolders(documentUri, folders); | ||
| } | ||
|
|
||
| /** | ||
| * Determines whether a document is located within one of the given workspace folders. | ||
| * @param documentUri The URI of the document | ||
| * @param folders The workspace folders reported by the client (may be `null`/`undefined` if none are open) | ||
| * @returns True if the document is within one of the workspace folders, false otherwise | ||
|
bwateratmsft marked this conversation as resolved.
|
||
| * @internal Exported only for tests | ||
| */ | ||
| export function isDocumentInWorkspaceFolders(documentUri: DocumentUri, folders: WorkspaceFolder[] | null | undefined): boolean { | ||
| if (!folders?.length) { | ||
| return false; | ||
| } | ||
|
|
||
| const document = parseUri(documentUri); | ||
| if (!document) { | ||
| return false; | ||
| } | ||
|
|
||
| return folders.some(folder => { | ||
| const parsedFolder = parseUri(folder.uri); | ||
| if (!parsedFolder) { | ||
| return false; | ||
| } | ||
|
|
||
| // The scheme and authority must match exactly, so that (for example) a `file://` document is | ||
| // never considered to be within a `vscode-vfs://` folder, or a folder on a different host | ||
| if (parsedFolder.scheme !== document.scheme || parsedFolder.authority !== document.authority) { | ||
| return false; | ||
| } | ||
|
|
||
| // The document is within the folder if the folder's path segments are a prefix of the document's. | ||
| // Comparing whole segments (rather than raw strings) means a folder at `/foo` is correctly seen as | ||
| // containing `/foo/compose.yaml`, but not the sibling `/foobar/compose.yaml`. | ||
| return parsedFolder.segments.every((segment, i) => { | ||
| const documentSegment = document.segments[i]; | ||
|
|
||
| if (useCaseInsensitivePathComparison) { | ||
| return segment.toLowerCase() === documentSegment?.toLowerCase(); | ||
| } | ||
|
|
||
| return segment === documentSegment; | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Parses a URI into the pieces needed to compare it against another URI. LSP models URIs as plain | ||
| * strings (`DocumentUri` is just a `string` alias) and offers no URI type of its own, so the | ||
| * platform's WHATWG `URL` is used: it separates the scheme and authority from the path, and | ||
| * normalizes dot segments (e.g. `/a/b/../c` becomes `/a/c`). | ||
| * @param uri The URI to parse | ||
| * @returns The parsed pieces, or `undefined` if the URI is malformed | ||
| */ | ||
| function parseUri(uri: string): { scheme: string, authority: string, segments: string[] } | undefined { | ||
| try { | ||
| const url = new URL(uri); | ||
|
|
||
| return { | ||
| scheme: url.protocol, | ||
| authority: url.host, | ||
| // Decoding is done per-segment (after splitting) so that an encoded separator within a | ||
| // segment can never be mistaken for a real one. This also makes comparison insensitive | ||
| // to differences in percent-encoding between the document and folder URIs. | ||
| segments: url.pathname.split('/').filter(segment => !!segment).map(decodeURIComponent), | ||
| }; | ||
| } catch { | ||
| // Malformed URI, or malformed percent-encoding within it | ||
| return undefined; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
packages/compose-language-service/src/test/utils/isDocumentInWorkspace.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /*!-------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See LICENSE in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import type { WorkspaceFolder } from 'vscode-languageserver'; | ||
| import { isDocumentInWorkspaceFolders } from '../../service/utils/isDocumentInWorkspace'; | ||
|
|
||
| function folder(uri: string): WorkspaceFolder { | ||
| return { uri, name: uri }; | ||
| } | ||
|
|
||
| const itOnCaseInsensitivePlatforms = process.platform === 'darwin' || process.platform === 'win32' ? it : it.skip; | ||
| const itOnCaseSensitivePlatforms = process.platform === 'darwin' || process.platform === 'win32' ? it.skip : it; | ||
|
|
||
| describe('(Unit) isDocumentInWorkspaceFolders', () => { | ||
| describe('Common scenarios', () => { | ||
| it('Should return true when the document is directly within a workspace folder', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/compose.yaml', [folder('file:///workspace')]).should.be.true; | ||
| }); | ||
|
|
||
| it('Should return true when the document is nested within a workspace folder', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/sub/dir/compose.yaml', [folder('file:///workspace')]).should.be.true; | ||
| }); | ||
|
|
||
| it('Should return true when the folder URI has a trailing slash', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/compose.yaml', [folder('file:///workspace/')]).should.be.true; | ||
| }); | ||
|
|
||
| it('Should return true when the document is within one of several workspace folders', () => { | ||
| isDocumentInWorkspaceFolders('file:///second/compose.yaml', [folder('file:///first'), folder('file:///second')]).should.be.true; | ||
| }); | ||
|
|
||
| it('Should return true when the document and folder differ in percent-encoding', () => { | ||
| // `%3A` and `%3a` encode the same character, as do `:` and `%3A` in a path | ||
| isDocumentInWorkspaceFolders('file:///c%3a/workspace/compose.yaml', [folder('file:///c%3A/workspace')]).should.be.true; | ||
| }); | ||
|
|
||
| it('Should return true when the document URI contains dot segments resolving into the folder', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/sub/../compose.yaml', [folder('file:///workspace')]).should.be.true; | ||
| }); | ||
|
|
||
| itOnCaseInsensitivePlatforms('Should compare path segments case-insensitively on Mac and Windows', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/compose.yaml', [folder('file:///Workspace')]).should.be.true; | ||
| }); | ||
|
|
||
| itOnCaseSensitivePlatforms('Should compare path segments case-sensitively on Linux', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/compose.yaml', [folder('file:///Workspace')]).should.be.false; | ||
| }); | ||
|
|
||
| it('Should return true for any document when the workspace folder is the root', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/compose.yaml', [folder('file:///')]).should.be.true; | ||
| }); | ||
| }); | ||
|
|
||
| describe('Negative scenarios', () => { | ||
| it('Should return false when the document is outside all workspace folders', () => { | ||
| isDocumentInWorkspaceFolders('file:///elsewhere/compose.yaml', [folder('file:///workspace')]).should.be.false; | ||
| }); | ||
|
|
||
| it('Should return false for a sibling folder with a matching prefix', () => { | ||
| // `file:///workspace` should not match `file:///workspace-other` | ||
| isDocumentInWorkspaceFolders('file:///workspace-other/compose.yaml', [folder('file:///workspace')]).should.be.false; | ||
| }); | ||
|
|
||
| it('Should return false when the document URI contains dot segments resolving out of the folder', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/../elsewhere/compose.yaml', [folder('file:///workspace')]).should.be.false; | ||
| }); | ||
|
|
||
| it('Should return false when the schemes differ', () => { | ||
| isDocumentInWorkspaceFolders('vscode-vfs:///workspace/compose.yaml', [folder('file:///workspace')]).should.be.false; | ||
| }); | ||
|
|
||
| it('Should return false when the authorities differ', () => { | ||
| isDocumentInWorkspaceFolders('vscode-vfs://other/workspace/compose.yaml', [folder('vscode-vfs://github/workspace')]).should.be.false; | ||
| }); | ||
|
|
||
| it('Should return false when the document URI is malformed', () => { | ||
| isDocumentInWorkspaceFolders('not a uri', [folder('file:///workspace')]).should.be.false; | ||
| }); | ||
|
|
||
| it('Should return false when a workspace folder URI is malformed', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/compose.yaml', [folder('not a uri')]).should.be.false; | ||
| }); | ||
|
|
||
| it('Should return false when there are no workspace folders', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/compose.yaml', []).should.be.false; | ||
| }); | ||
|
|
||
| it('Should return false when the folders are null', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/compose.yaml', null).should.be.false; | ||
| }); | ||
|
|
||
| it('Should return false when the folders are undefined', () => { | ||
| isDocumentInWorkspaceFolders('file:///workspace/compose.yaml', undefined).should.be.false; | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.