From 32f2b75b360de288e034f6cac6e5d12117558f66 Mon Sep 17 00:00:00 2001 From: Dylan Leclair Date: Wed, 25 Oct 2023 16:23:38 +0000 Subject: [PATCH] Fix path failure preventing trace from opening When doing some testing on an internal extension relying, I noticed that we could no longer use the Open With TraceViewer command on Windows due to an invalid path failing (worked on Linux). I was able to trace the cause of this it to some code that uses .path instead of .fsPath, which is intended for use with the filesystem and scrubs out improper path values (which is what caused our error!). I also changed some other path calculations to be less error prone across platforms by using the path API. This fixes a critical issue which prevents users from opening traces in some cases. Signed-off-by: Dylan Leclair QVSC-760: fix basename calculation --- .../src/trace-explorer/trace-tree.ts | 18 +++++++++--------- .../trace-viewer-webview-panel.ts | 3 ++- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/vscode-trace-extension/src/trace-explorer/trace-tree.ts b/vscode-trace-extension/src/trace-explorer/trace-tree.ts index 5b869da2..49febfd8 100644 --- a/vscode-trace-extension/src/trace-explorer/trace-tree.ts +++ b/vscode-trace-extension/src/trace-explorer/trace-tree.ts @@ -166,8 +166,8 @@ export const fileHandler = return; } - const uri: string = resolvedTraceURI.path; - if (!uri) { + const filePath: string = resolvedTraceURI.fsPath; + if (!filePath) { traceLogger.addLogMessage( 'Cannot open trace: could not retrieve path from URI for trace ' + resolvedTraceURI, fileHandler.name @@ -175,7 +175,7 @@ export const fileHandler = return; } - const name = uri.substring(uri.lastIndexOf('/') + 1); + const name = path.basename(filePath); const panel = TraceViewerPanel.createOrShow(context.extensionUri, name, undefined); progress.report({ message: ProgressMessages.FINDING_TRACES, increment: 10 }); @@ -187,11 +187,11 @@ export const fileHandler = if (fileStat) { if (fileStat.type === vscode.FileType.Directory) { // Find recursively CTF traces - const foundTraces = await findTraces(uri); + const foundTraces = await findTraces(filePath); foundTraces.forEach(trace => tracesArray.push(trace)); } else { // Open single trace file - tracesArray.push(uri); + tracesArray.push(filePath); } } @@ -208,7 +208,7 @@ export const fileHandler = progress.report({ message: ProgressMessages.OPENING_TRACES, increment: 20 }); const traces = new Array(); for (let i = 0; i < tracesArray.length; i++) { - const traceName = tracesArray[i].substring(tracesArray[i].lastIndexOf('/') + 1); + const traceName = path.basename(tracesArray[i]); const trace = await traceManager.openTrace(tracesArray[i], traceName); if (trace) { traces.push(trace); @@ -291,7 +291,7 @@ const findTraces = async (directory: string): Promise => { const childrenArr = await vscode.workspace.fs.readDirectory(uri); for (const child of childrenArr) { if (child[1] === vscode.FileType.Directory) { - const subTraces = await findTraces(directory + '/' + child[0]); + const subTraces = await findTraces(path.join(directory, child[0])); subTraces.forEach(trace => traces.push(trace)); } } @@ -311,8 +311,8 @@ const isCtf = async (directory: string): Promise => { }; function getProgressBarTitle(traceUri: vscode.Uri | undefined): string { - if (!traceUri || !traceUri.path) { + if (!traceUri || !traceUri.fsPath) { return 'undefined'; } - return traceUri.path.substring(traceUri.path.lastIndexOf('/') + 1); + return path.basename(traceUri.fsPath); } diff --git a/vscode-trace-extension/src/trace-viewer-panel/trace-viewer-webview-panel.ts b/vscode-trace-extension/src/trace-viewer-panel/trace-viewer-webview-panel.ts index 912c372d..dc9c9f68 100644 --- a/vscode-trace-extension/src/trace-viewer-panel/trace-viewer-webview-panel.ts +++ b/vscode-trace-extension/src/trace-viewer-panel/trace-viewer-webview-panel.ts @@ -1,4 +1,5 @@ import * as vscode from 'vscode'; +import * as path from 'path'; import { Experiment } from 'tsp-typescript-client/lib/models/experiment'; import { getTspClientUrl, getTraceServerUrl } from '../utils/tspClient'; import { TraceServerConnectionStatusService } from '../utils/trace-server-status'; @@ -115,7 +116,7 @@ export class TraceViewerPanel { private static async saveTraceCsv(csvData: string, defaultFileName: string) { const saveDialogOptions = { defaultUri: vscode.workspace.workspaceFolders - ? vscode.Uri.file(vscode.workspace.workspaceFolders[0].uri.path + '/' + defaultFileName) + ? vscode.Uri.file(path.join(vscode.workspace.workspaceFolders[0].uri.fsPath, defaultFileName)) : undefined, saveLabel: 'Save as CSV', filters: {