Skip to content

Commit 899f988

Browse files
committed
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.
1 parent 9547aa3 commit 899f988

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

extensions/ql-vscode/src/archive-filesystem-provider.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ export function encodeSourceArchiveUri(ref: ZipFileReference): vscode.Uri {
9090
});
9191
}
9292

93+
/**
94+
* Convenience method to create a codeql-zip-archive with a path to the root
95+
* archive
96+
*
97+
* @param pathToArchive the filesystem path to the root of the archive
98+
*/
99+
export function encodeArchiveBasePath(sourceArchiveZipPath: string) {
100+
return encodeSourceArchiveUri({
101+
sourceArchiveZipPath,
102+
pathWithinSourceArchive: ''
103+
});
104+
}
105+
93106
const sourceArchiveUriAuthorityPattern = /^(\d+)-(\d+)$/;
94107

95108
class InvalidSourceArchiveUriError extends Error {

extensions/ql-vscode/src/contextual/locationFinder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
22

3-
import { decodeSourceArchiveUri, zipArchiveScheme } from '../archive-filesystem-provider';
3+
import { decodeSourceArchiveUri, encodeArchiveBasePath } from '../archive-filesystem-provider';
44
import { ColumnKindCode, EntityValue, getResultSetSchema, ResultSetSchema } from '../bqrs-cli-types';
55
import { CodeQLCliServer } from '../cli';
66
import { DatabaseManager, DatabaseItem } from '../databases';
@@ -44,7 +44,7 @@ export async function getLocationsForUriString(
4444
filter: (src: string, dest: string) => boolean
4545
): Promise<FullLocationLink[]> {
4646
const uri = decodeSourceArchiveUri(vscode.Uri.parse(uriString));
47-
const sourceArchiveUri = vscode.Uri.file(uri.sourceArchiveZipPath).with({ scheme: zipArchiveScheme });
47+
const sourceArchiveUri = encodeArchiveBasePath(uri.sourceArchiveZipPath);
4848

4949
const db = dbm.findDatabaseItemBySourceArchive(sourceArchiveUri);
5050
if (!db) {

extensions/ql-vscode/src/contextual/templateProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
22

3-
import { decodeSourceArchiveUri, zipArchiveScheme } from '../archive-filesystem-provider';
3+
import { decodeSourceArchiveUri, encodeArchiveBasePath, zipArchiveScheme } from '../archive-filesystem-provider';
44
import { CodeQLCliServer } from '../cli';
55
import { DatabaseManager } from '../databases';
66
import { CachedOperation, ProgressCallback, withProgress } from '../helpers';
@@ -148,7 +148,7 @@ export class TemplatePrintAstProvider {
148148
}
149149

150150
const zippedArchive = decodeSourceArchiveUri(uri);
151-
const sourceArchiveUri = vscode.Uri.file(zippedArchive.sourceArchiveZipPath).with({ scheme: zipArchiveScheme });
151+
const sourceArchiveUri = encodeArchiveBasePath(zippedArchive.sourceArchiveZipPath);
152152
const db = this.dbm.findDatabaseItemBySourceArchive(sourceArchiveUri);
153153

154154
if (!db) {

extensions/ql-vscode/src/databases.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as vscode from 'vscode';
55
import * as cli from './cli';
66
import { ExtensionContext } from 'vscode';
77
import { showAndLogErrorMessage, showAndLogWarningMessage, showAndLogInformationMessage } from './helpers';
8-
import { zipArchiveScheme, encodeSourceArchiveUri, decodeSourceArchiveUri } from './archive-filesystem-provider';
8+
import { zipArchiveScheme, encodeArchiveBasePath, decodeSourceArchiveUri, encodeSourceArchiveUri } from './archive-filesystem-provider';
99
import { DisposableObject } from './vscode-utils/disposable-object';
1010
import { QueryServerConfig } from './config';
1111
import { Logger, logger } from './logging';
@@ -122,10 +122,7 @@ async function findSourceArchive(
122122
if (await fs.pathExists(basePath)) {
123123
return vscode.Uri.file(basePath);
124124
} else if (await fs.pathExists(zipPath)) {
125-
return encodeSourceArchiveUri({
126-
pathWithinSourceArchive: '',
127-
sourceArchiveZipPath: zipPath
128-
});
125+
return encodeArchiveBasePath(zipPath);
129126
}
130127
}
131128
if (!silent) {
@@ -439,10 +436,7 @@ export class DatabaseItemImpl implements DatabaseItem {
439436
const sourceArchive = this.sourceArchive;
440437
if (sourceArchive === undefined || !sourceArchive.fsPath.endsWith('.zip'))
441438
return undefined;
442-
return encodeSourceArchiveUri({
443-
pathWithinSourceArchive: '/',
444-
sourceArchiveZipPath: sourceArchive.fsPath,
445-
});
439+
return encodeArchiveBasePath(sourceArchive.fsPath);
446440
}
447441

448442
/**

extensions/ql-vscode/src/vscode-tests/no-workspace/archive-filesystem-provider.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { expect } from 'chai';
22
import * as path from 'path';
33

4-
import { encodeSourceArchiveUri, ArchiveFileSystemProvider, decodeSourceArchiveUri, ZipFileReference, zipArchiveScheme } from '../../archive-filesystem-provider';
4+
import {
5+
encodeSourceArchiveUri,
6+
encodeArchiveBasePath,
7+
ArchiveFileSystemProvider,
8+
decodeSourceArchiveUri,
9+
ZipFileReference,
10+
zipArchiveScheme
11+
} from '../../archive-filesystem-provider';
512
import { FileType, FileSystemError, Uri } from 'vscode';
613

714
describe('archive-filesystem-provider', () => {
@@ -146,6 +153,15 @@ describe('source archive uri encoding', function() {
146153
});
147154
}
148155

156+
it('should encode a uri at the root of the archive', () => {
157+
const path = '/a/b/c/src.zip';
158+
const uri = encodeArchiveBasePath(path);
159+
expect(uri.path).to.eq(path);
160+
expect(decodeSourceArchiveUri(uri).pathWithinSourceArchive).to.eq('');
161+
expect(decodeSourceArchiveUri(uri).sourceArchiveZipPath).to.eq(path);
162+
expect(uri.authority).to.eq('0-14');
163+
});
164+
149165
it('should handle malformed uri with no authority', () => {
150166
// This handles codeql-zip-archive uris generated using the `with` method
151167
const uri = Uri.parse('file:/a/b/c/src.zip').with({ scheme: zipArchiveScheme });

0 commit comments

Comments
 (0)