Skip to content

Commit 2f3be92

Browse files
committed
Make functions async + other review comments
1 parent a8fd6cc commit 2f3be92

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

extensions/ql-vscode/src/extension.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export async function activate(ctx: ExtensionContext): Promise<CodeQLExtensionIn
155155
}
156156

157157
const distributionConfigListener = new DistributionConfigListener();
158-
initializeLogging(ctx);
158+
await initializeLogging(ctx);
159159
await initializeTelemetry(extension, ctx);
160160
languageSupport.install();
161161

@@ -767,10 +767,10 @@ function getContextStoragePath(ctx: ExtensionContext) {
767767
return ctx.storagePath || ctx.globalStoragePath;
768768
}
769769

770-
function initializeLogging(ctx: ExtensionContext): void {
770+
async function initializeLogging(ctx: ExtensionContext): Promise<void> {
771771
const storagePath = getContextStoragePath(ctx);
772-
logger.setLogStoragePath(storagePath, false);
773-
ideServerLogger.setLogStoragePath(storagePath, false);
772+
await logger.setLogStoragePath(storagePath, false);
773+
await ideServerLogger.setLogStoragePath(storagePath, false);
774774
ctx.subscriptions.push(logger);
775775
ctx.subscriptions.push(queryServerLogger);
776776
ctx.subscriptions.push(ideServerLogger);

extensions/ql-vscode/src/logging.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@ export interface Logger {
2828
removeAdditionalLogLocation(location: string | undefined): void;
2929

3030
/**
31-
* The base location location where all side log files are stored.
31+
* The base location where all side log files are stored.
3232
*/
3333
getBaseLocation(): string | undefined;
34+
35+
/**
36+
* Sets the location where logs are stored.
37+
* @param storagePath The path where logs are stored.
38+
* @param isCustomLogDirectory Whether the logs are stored in a custom, user-specified directory.
39+
*/
40+
setLogStoragePath(storagePath: string, isCustomLogDirectory: boolean): Promise<void>;
3441
}
3542

3643
export type ProgressReporter = Progress<{ message: string }>;
@@ -49,14 +56,14 @@ export class OutputChannelLogger extends DisposableObject implements Logger {
4956
this.isCustomLogDirectory = false;
5057
}
5158

52-
setLogStoragePath(storagePath: string, isCustomLogDirectory: boolean): void {
59+
async setLogStoragePath(storagePath: string, isCustomLogDirectory: boolean): Promise<void> {
5360
this.additionalLogLocationPath = path.join(storagePath, this.title);
5461

5562
this.isCustomLogDirectory = isCustomLogDirectory;
5663

5764
if (!this.isCustomLogDirectory) {
5865
// clear out any old state from previous runs
59-
fs.remove(this.additionalLogLocationPath);
66+
await fs.remove(this.additionalLogLocationPath);
6067
}
6168
}
6269

extensions/ql-vscode/src/queryserver-client.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Disposable, CancellationToken, commands } from 'vscode';
55
import { createMessageConnection, MessageConnection, RequestType } from 'vscode-jsonrpc';
66
import * as cli from './cli';
77
import { QueryServerConfig } from './config';
8-
import { Logger, ProgressReporter, queryServerLogger } from './logging';
8+
import { Logger, ProgressReporter } from './logging';
99
import { completeQuery, EvaluationResult, progress, ProgressMessage, WithProgressId } from './pure/messages';
1010
import * as messages from './pure/messages';
1111
import { ProgressCallback, ProgressTask } from './commandRunner';
@@ -89,26 +89,28 @@ export class QueryServerClient extends DisposableObject {
8989
this.evaluationResultCallbacks = {};
9090
}
9191

92-
initLogger() {
92+
async initLogger() {
9393
let storagePath = this.opts.contextStoragePath;
9494
let isCustomLogDirectory = false;
9595
if (this.config.customLogDirectory) {
9696
try {
97-
fs.mkdirSync(this.config.customLogDirectory);
98-
helpers.showAndLogInformationMessage(`Storing query server logs to user-specified directory: ${this.config.customLogDirectory}.`);
97+
if (!(await fs.pathExists(this.config.customLogDirectory))) {
98+
await fs.mkdir(this.config.customLogDirectory);
99+
}
100+
this.logger.log(`Saving query server logs to user-specified directory: ${this.config.customLogDirectory}.`);
99101
storagePath = this.config.customLogDirectory;
100102
isCustomLogDirectory = true;
101103
} catch (e) {
102104
helpers.showAndLogErrorMessage(`${this.config.customLogDirectory} is not a valid directory. Logs will be stored in a temporary workspace directory instead.`);
103105
}
104106
}
105107

106-
queryServerLogger.setLogStoragePath(storagePath, isCustomLogDirectory);
108+
await this.logger.setLogStoragePath(storagePath, isCustomLogDirectory);
107109

108110
}
109111

110112
get logger(): Logger {
111-
return queryServerLogger;
113+
return this.opts.logger;
112114
}
113115

114116
/** Stops the query server by disposing of the current server process. */
@@ -148,6 +150,7 @@ export class QueryServerClient extends DisposableObject {
148150

149151
/** Starts a new query server process, sending progress messages to the given reporter. */
150152
private async startQueryServerImpl(progressReporter: ProgressReporter): Promise<void> {
153+
await this.initLogger();
151154
const ramArgs = await this.cliServer.resolveRam(this.config.queryMemoryMb, progressReporter);
152155
const args = ['--threads', this.config.numThreads.toString()].concat(ramArgs);
153156

@@ -172,7 +175,6 @@ export class QueryServerClient extends DisposableObject {
172175
args.push('-J=-agentlib:jdwp=transport=dt_socket,address=localhost:9010,server=y,suspend=n,quiet=y');
173176
}
174177

175-
this.initLogger();
176178
const child = cli.spawnServer(
177179
this.config.codeQlPath,
178180
'CodeQL query server',

extensions/ql-vscode/test/pure-tests/logging.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('OutputChannelLogger tests', () => {
4848
});
4949

5050
it('should create a side log in the workspace area', async () => {
51-
logger.setLogStoragePath(tempFolders.storagePath.name, false);
51+
await logger.setLogStoragePath(tempFolders.storagePath.name, false);
5252

5353
await logger.log('xxx', { additionalLogLocation: 'first' });
5454
await logger.log('yyy', { additionalLogLocation: 'second' });
@@ -65,7 +65,7 @@ describe('OutputChannelLogger tests', () => {
6565
});
6666

6767
it('should delete side logs on dispose', async () => {
68-
logger.setLogStoragePath(tempFolders.storagePath.name, false);
68+
await logger.setLogStoragePath(tempFolders.storagePath.name, false);
6969
await logger.log('xxx', { additionalLogLocation: 'first' });
7070
await logger.log('yyy', { additionalLogLocation: 'second' });
7171

@@ -80,7 +80,7 @@ describe('OutputChannelLogger tests', () => {
8080
});
8181

8282
it('should not delete side logs on dispose in a custom directory', async () => {
83-
logger.setLogStoragePath(tempFolders.storagePath.name, true);
83+
await logger.setLogStoragePath(tempFolders.storagePath.name, true);
8484
await logger.log('xxx', { additionalLogLocation: 'first' });
8585
await logger.log('yyy', { additionalLogLocation: 'second' });
8686

@@ -95,7 +95,7 @@ describe('OutputChannelLogger tests', () => {
9595
});
9696

9797
it('should remove an additional log location', async () => {
98-
logger.setLogStoragePath(tempFolders.storagePath.name, false);
98+
await logger.setLogStoragePath(tempFolders.storagePath.name, false);
9999
await logger.log('xxx', { additionalLogLocation: 'first' });
100100
await logger.log('yyy', { additionalLogLocation: 'second' });
101101

@@ -110,7 +110,7 @@ describe('OutputChannelLogger tests', () => {
110110
});
111111

112112
it('should not remove an additional log location in a custom directory', async () => {
113-
logger.setLogStoragePath(tempFolders.storagePath.name, true);
113+
await logger.setLogStoragePath(tempFolders.storagePath.name, true);
114114
await logger.log('xxx', { additionalLogLocation: 'first' });
115115
await logger.log('yyy', { additionalLogLocation: 'second' });
116116

@@ -126,17 +126,17 @@ describe('OutputChannelLogger tests', () => {
126126

127127
it('should delete an existing folder when setting the log storage path', async () => {
128128
fs.createFileSync(path.join(tempFolders.storagePath.name, 'test-logger', 'xxx'));
129-
logger.setLogStoragePath(tempFolders.storagePath.name, false);
129+
await logger.setLogStoragePath(tempFolders.storagePath.name, false);
130130
// should be empty dir
131131

132+
await waitABit();
132133
const testLoggerFolder = path.join(tempFolders.storagePath.name, 'test-logger');
133-
// TODO: Why does this test pass? I'd expect the length to be 0 if it's correctly deleted the existing folder.
134-
expect(fs.readdirSync(testLoggerFolder).length).to.equal(1);
134+
expect(fs.existsSync(testLoggerFolder)).to.be.false;
135135
});
136136

137137
it('should not delete an existing folder when setting the log storage path for a custom directory', async () => {
138138
fs.createFileSync(path.join(tempFolders.storagePath.name, 'test-logger', 'xxx'));
139-
logger.setLogStoragePath(tempFolders.storagePath.name, true);
139+
await logger.setLogStoragePath(tempFolders.storagePath.name, true);
140140
// should not be empty dir
141141

142142
const testLoggerFolder = path.join(tempFolders.storagePath.name, 'test-logger');

0 commit comments

Comments
 (0)