Skip to content

Commit 7886871

Browse files
Copilotalexr00
andauthored
Fix file URIs using wrong workspace folder in multi-root workspaces (#8261)
* Initial plan * Initial plan for fixing wrong file URIs with multiple roots Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Fix wrong file URIs with multiple roots by using correct workspace folder Pass repository rootUri to TemporaryState.write so it can find the correct workspace folder name instead of always using the first one. Fixes issue where vscode-userdata URIs contained the wrong workspace folder path in multi-root workspace scenarios. Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Add comment documenting readState limitation for multi-root workspaces Added a comment to readState noting that it currently only supports the first workspace folder, and that it should accept a repositoryUri parameter if multi-root support is needed in the future. Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Add multi-root workspace support to readState method Updated readState to accept optional repositoryUri parameter and find the matching workspace folder, mirroring the logic in writeState. This ensures consistency when reading files written with the correct workspace folder path in multi-root workspaces. Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Fix some stuff --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent 8cfa2af commit 7886871

2 files changed

Lines changed: 34 additions & 11 deletions

File tree

src/common/temporaryState.ts

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import * as vscode from 'vscode';
66
import { disposeAll } from './lifecycle';
77
import Logger from './logger';
8+
import { isDescendant } from './utils';
89

910
let tempState: TemporaryState | undefined;
1011

@@ -39,10 +40,21 @@ export class TemporaryState extends vscode.Disposable {
3940
}
4041
}
4142

42-
private async writeState(subpath: string, filename: string, contents: Uint8Array, persistInSession: boolean): Promise<vscode.Uri> {
43+
private async writeState(subpath: string, filename: string, contents: Uint8Array, persistInSession: boolean, repositoryUri: vscode.Uri): Promise<vscode.Uri> {
4344
let filePath: vscode.Uri = this.path;
44-
const workspace = (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0)
45-
? vscode.workspace.workspaceFolders[0].name : undefined;
45+
let workspace: string | undefined;
46+
47+
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
48+
const matchingFolder = vscode.workspace.workspaceFolders.find(folder =>
49+
isDescendant(folder.uri.fsPath, repositoryUri.fsPath) || isDescendant(repositoryUri.fsPath, folder.uri.fsPath)
50+
);
51+
workspace = matchingFolder?.name;
52+
}
53+
54+
// Fall back to the first workspace folder if no match found
55+
if (!workspace && vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
56+
workspace = vscode.workspace.workspaceFolders[0].name;
57+
}
4658

4759
if (workspace) {
4860
filePath = vscode.Uri.joinPath(filePath, workspace);
@@ -68,10 +80,21 @@ export class TemporaryState extends vscode.Disposable {
6880
return file;
6981
}
7082

71-
private async readState(subpath: string, filename: string): Promise<Uint8Array> {
83+
private async readState(subpath: string, filename: string, repositoryUri: vscode.Uri): Promise<Uint8Array> {
7284
let filePath: vscode.Uri = this.path;
73-
const workspace = (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0)
74-
? vscode.workspace.workspaceFolders[0].name : undefined;
85+
let workspace: string | undefined;
86+
87+
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
88+
const matchingFolder = vscode.workspace.workspaceFolders.find(folder =>
89+
isDescendant(folder.uri.fsPath, repositoryUri.fsPath) || isDescendant(repositoryUri.fsPath, folder.uri.fsPath)
90+
);
91+
workspace = matchingFolder?.name;
92+
}
93+
94+
// Fall back to the first workspace folder if no match found
95+
if (!workspace && vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
96+
workspace = vscode.workspace.workspaceFolders[0].name;
97+
}
7598

7699
if (workspace) {
77100
filePath = vscode.Uri.joinPath(filePath, workspace);
@@ -99,19 +122,19 @@ export class TemporaryState extends vscode.Disposable {
99122
}
100123
}
101124

102-
static async write(subpath: string, filename: string, contents: Uint8Array, persistInSession: boolean = false): Promise<vscode.Uri | undefined> {
125+
static async write(subpath: string, filename: string, contents: Uint8Array, persistInSession: boolean = false, repositoryUri: vscode.Uri): Promise<vscode.Uri | undefined> {
103126
if (!tempState) {
104127
return;
105128
}
106129

107-
return tempState.writeState(subpath, filename, contents, persistInSession);
130+
return tempState.writeState(subpath, filename, contents, persistInSession, repositoryUri);
108131
}
109132

110-
static async read(subpath: string, filename: string): Promise<Uint8Array | undefined> {
133+
static async read(subpath: string, filename: string, repositoryUri: vscode.Uri): Promise<Uint8Array | undefined> {
111134
if (!tempState) {
112135
return;
113136
}
114137

115-
return tempState.readState(subpath, filename);
138+
return tempState.readState(subpath, filename, repositoryUri);
116139
}
117140
}

src/common/uri.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export async function asTempStorageURI(uri: vscode.Uri, repository: Repository):
195195

196196
if (ImageMimetypes.indexOf(mimetype) > -1) {
197197
const contents = await repository.buffer(ref, absolutePath);
198-
return TemporaryState.write(pathUtils.dirname(path), pathUtils.basename(path), contents);
198+
return TemporaryState.write(pathUtils.dirname(path), pathUtils.basename(path), contents, false, repository.rootUri);
199199
}
200200
} catch (err) {
201201
return;

0 commit comments

Comments
 (0)