Skip to content

Commit 2798f43

Browse files
Copilotalexr00
andcommitted
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>
1 parent e253b53 commit 2798f43

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/common/temporaryState.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,22 @@ export class TemporaryState extends vscode.Disposable {
3939
}
4040
}
4141

42-
private async writeState(subpath: string, filename: string, contents: Uint8Array, persistInSession: boolean): Promise<vscode.Uri> {
42+
private async writeState(subpath: string, filename: string, contents: Uint8Array, persistInSession: boolean, repositoryUri?: vscode.Uri): Promise<vscode.Uri> {
4343
let filePath: vscode.Uri = this.path;
44-
const workspace = (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0)
45-
? vscode.workspace.workspaceFolders[0].name : undefined;
44+
let workspace: string | undefined;
45+
46+
// If repositoryUri is provided, find the matching workspace folder
47+
if (repositoryUri && vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
48+
const matchingFolder = vscode.workspace.workspaceFolders.find(folder =>
49+
folder.uri.toString() === repositoryUri.toString()
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);
@@ -99,12 +111,12 @@ export class TemporaryState extends vscode.Disposable {
99111
}
100112
}
101113

102-
static async write(subpath: string, filename: string, contents: Uint8Array, persistInSession: boolean = false): Promise<vscode.Uri | undefined> {
114+
static async write(subpath: string, filename: string, contents: Uint8Array, persistInSession: boolean = false, repositoryUri?: vscode.Uri): Promise<vscode.Uri | undefined> {
103115
if (!tempState) {
104116
return;
105117
}
106118

107-
return tempState.writeState(subpath, filename, contents, persistInSession);
119+
return tempState.writeState(subpath, filename, contents, persistInSession, repositoryUri);
108120
}
109121

110122
static async read(subpath: string, filename: string): Promise<Uint8Array | undefined> {

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)