From 899f988df871c4923af01508b7efd06169888b90 Mon Sep 17 00:00:00 2001 From: Andrew Eisenberg Date: Wed, 21 Oct 2020 09:07:26 -0700 Subject: [PATCH] Fix other locations where we create an invalid codeql-zip-archive uri Also, create a convenience function for generating a codeql-zip-archive at the root of the archive. --- .../src/archive-filesystem-provider.ts | 13 +++++++++++++ .../ql-vscode/src/contextual/locationFinder.ts | 4 ++-- .../src/contextual/templateProvider.ts | 4 ++-- extensions/ql-vscode/src/databases.ts | 12 +++--------- .../archive-filesystem-provider.test.ts | 18 +++++++++++++++++- 5 files changed, 37 insertions(+), 14 deletions(-) diff --git a/extensions/ql-vscode/src/archive-filesystem-provider.ts b/extensions/ql-vscode/src/archive-filesystem-provider.ts index 8ff04febb..e28a17ce6 100644 --- a/extensions/ql-vscode/src/archive-filesystem-provider.ts +++ b/extensions/ql-vscode/src/archive-filesystem-provider.ts @@ -90,6 +90,19 @@ export function encodeSourceArchiveUri(ref: ZipFileReference): vscode.Uri { }); } +/** + * Convenience method to create a codeql-zip-archive with a path to the root + * archive + * + * @param pathToArchive the filesystem path to the root of the archive + */ +export function encodeArchiveBasePath(sourceArchiveZipPath: string) { + return encodeSourceArchiveUri({ + sourceArchiveZipPath, + pathWithinSourceArchive: '' + }); +} + const sourceArchiveUriAuthorityPattern = /^(\d+)-(\d+)$/; class InvalidSourceArchiveUriError extends Error { diff --git a/extensions/ql-vscode/src/contextual/locationFinder.ts b/extensions/ql-vscode/src/contextual/locationFinder.ts index a60018180..942a2297a 100644 --- a/extensions/ql-vscode/src/contextual/locationFinder.ts +++ b/extensions/ql-vscode/src/contextual/locationFinder.ts @@ -1,6 +1,6 @@ import * as vscode from 'vscode'; -import { decodeSourceArchiveUri, zipArchiveScheme } from '../archive-filesystem-provider'; +import { decodeSourceArchiveUri, encodeArchiveBasePath } from '../archive-filesystem-provider'; import { ColumnKindCode, EntityValue, getResultSetSchema, ResultSetSchema } from '../bqrs-cli-types'; import { CodeQLCliServer } from '../cli'; import { DatabaseManager, DatabaseItem } from '../databases'; @@ -44,7 +44,7 @@ export async function getLocationsForUriString( filter: (src: string, dest: string) => boolean ): Promise { const uri = decodeSourceArchiveUri(vscode.Uri.parse(uriString)); - const sourceArchiveUri = vscode.Uri.file(uri.sourceArchiveZipPath).with({ scheme: zipArchiveScheme }); + const sourceArchiveUri = encodeArchiveBasePath(uri.sourceArchiveZipPath); const db = dbm.findDatabaseItemBySourceArchive(sourceArchiveUri); if (!db) { diff --git a/extensions/ql-vscode/src/contextual/templateProvider.ts b/extensions/ql-vscode/src/contextual/templateProvider.ts index b2d744993..78e07eff4 100644 --- a/extensions/ql-vscode/src/contextual/templateProvider.ts +++ b/extensions/ql-vscode/src/contextual/templateProvider.ts @@ -1,6 +1,6 @@ import * as vscode from 'vscode'; -import { decodeSourceArchiveUri, zipArchiveScheme } from '../archive-filesystem-provider'; +import { decodeSourceArchiveUri, encodeArchiveBasePath, zipArchiveScheme } from '../archive-filesystem-provider'; import { CodeQLCliServer } from '../cli'; import { DatabaseManager } from '../databases'; import { CachedOperation, ProgressCallback, withProgress } from '../helpers'; @@ -148,7 +148,7 @@ export class TemplatePrintAstProvider { } const zippedArchive = decodeSourceArchiveUri(uri); - const sourceArchiveUri = vscode.Uri.file(zippedArchive.sourceArchiveZipPath).with({ scheme: zipArchiveScheme }); + const sourceArchiveUri = encodeArchiveBasePath(zippedArchive.sourceArchiveZipPath); const db = this.dbm.findDatabaseItemBySourceArchive(sourceArchiveUri); if (!db) { diff --git a/extensions/ql-vscode/src/databases.ts b/extensions/ql-vscode/src/databases.ts index 182d9a8a8..35b285fec 100644 --- a/extensions/ql-vscode/src/databases.ts +++ b/extensions/ql-vscode/src/databases.ts @@ -5,7 +5,7 @@ import * as vscode from 'vscode'; import * as cli from './cli'; import { ExtensionContext } from 'vscode'; import { showAndLogErrorMessage, showAndLogWarningMessage, showAndLogInformationMessage } from './helpers'; -import { zipArchiveScheme, encodeSourceArchiveUri, decodeSourceArchiveUri } from './archive-filesystem-provider'; +import { zipArchiveScheme, encodeArchiveBasePath, decodeSourceArchiveUri, encodeSourceArchiveUri } from './archive-filesystem-provider'; import { DisposableObject } from './vscode-utils/disposable-object'; import { QueryServerConfig } from './config'; import { Logger, logger } from './logging'; @@ -122,10 +122,7 @@ async function findSourceArchive( if (await fs.pathExists(basePath)) { return vscode.Uri.file(basePath); } else if (await fs.pathExists(zipPath)) { - return encodeSourceArchiveUri({ - pathWithinSourceArchive: '', - sourceArchiveZipPath: zipPath - }); + return encodeArchiveBasePath(zipPath); } } if (!silent) { @@ -439,10 +436,7 @@ export class DatabaseItemImpl implements DatabaseItem { const sourceArchive = this.sourceArchive; if (sourceArchive === undefined || !sourceArchive.fsPath.endsWith('.zip')) return undefined; - return encodeSourceArchiveUri({ - pathWithinSourceArchive: '/', - sourceArchiveZipPath: sourceArchive.fsPath, - }); + return encodeArchiveBasePath(sourceArchive.fsPath); } /** diff --git a/extensions/ql-vscode/src/vscode-tests/no-workspace/archive-filesystem-provider.test.ts b/extensions/ql-vscode/src/vscode-tests/no-workspace/archive-filesystem-provider.test.ts index 71ea748a9..5c874783c 100644 --- a/extensions/ql-vscode/src/vscode-tests/no-workspace/archive-filesystem-provider.test.ts +++ b/extensions/ql-vscode/src/vscode-tests/no-workspace/archive-filesystem-provider.test.ts @@ -1,7 +1,14 @@ import { expect } from 'chai'; import * as path from 'path'; -import { encodeSourceArchiveUri, ArchiveFileSystemProvider, decodeSourceArchiveUri, ZipFileReference, zipArchiveScheme } from '../../archive-filesystem-provider'; +import { + encodeSourceArchiveUri, + encodeArchiveBasePath, + ArchiveFileSystemProvider, + decodeSourceArchiveUri, + ZipFileReference, + zipArchiveScheme +} from '../../archive-filesystem-provider'; import { FileType, FileSystemError, Uri } from 'vscode'; describe('archive-filesystem-provider', () => { @@ -146,6 +153,15 @@ describe('source archive uri encoding', function() { }); } + it('should encode a uri at the root of the archive', () => { + const path = '/a/b/c/src.zip'; + const uri = encodeArchiveBasePath(path); + expect(uri.path).to.eq(path); + expect(decodeSourceArchiveUri(uri).pathWithinSourceArchive).to.eq(''); + expect(decodeSourceArchiveUri(uri).sourceArchiveZipPath).to.eq(path); + expect(uri.authority).to.eq('0-14'); + }); + it('should handle malformed uri with no authority', () => { // This handles codeql-zip-archive uris generated using the `with` method const uri = Uri.parse('file:/a/b/c/src.zip').with({ scheme: zipArchiveScheme });