Skip to content

Commit

Permalink
Merge pull request #145 from semgrep/austin/cdx-505-error-enoent-no-s…
Browse files Browse the repository at this point in the history
…uch-file-or-directory-open

fix: log error issue
  • Loading branch information
ajbt200128 authored Jun 6, 2024
2 parents 3a93e06 + 0c51267 commit 32fbd6b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Extension no longer errors out if a log can't be written

## v1.8.0 - 2024-06-05

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export const DIAGNOSTIC_COLLECTION_NAME = "semgrep-findings";
export const CLIENT_ID = "semgrep";
export const CLIENT_NAME = "Semgrep";
export const DEFAULT_RULESET = "p/r2c";
export const LSP_LOG_FILE = "semgrep_lsp.log";
export const VSCODE_CONFIG_KEY = "semgrep";
export const VSCODE_EXT_NAME = CLIENT_NAME;

export type VersionInfo = {
latest: SemVer;
min: SemVer;
Expand Down
7 changes: 2 additions & 5 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {
ExtensionContext,
OutputChannel,
Uri,
WorkspaceConfiguration,
} from "vscode";
import { window, workspace } from "vscode";
import * as fs from "fs";

import { LSP_LOG_FILE, VSCODE_CONFIG_KEY, VSCODE_EXT_NAME } from "./constants";
import { DEFAULT_LSP_LOG_URI, Logger } from "./utils";
import { VSCODE_CONFIG_KEY, VSCODE_EXT_NAME } from "./constants";
import { Logger } from "./utils";
import { SemgrepDocumentProvider } from "./showAstDocument";
import { LanguageClient } from "vscode-languageclient/node";
import { EventEmitter } from "stream";
Expand Down Expand Up @@ -40,7 +39,6 @@ export class Config {
}

export class Environment {
semgrep_log: Uri = DEFAULT_LSP_LOG_URI;
public semgrepVersion: string | undefined;

/* The scan ID is the (hopefully) unique identifier associated to each
Expand All @@ -64,7 +62,6 @@ export class Environment {
// rulesRefreshedEmitter is used to notify if rules are refreshed, i.e. after startup, a login, or a manual refresh
private rulesRefreshedEmitter: EventEmitter = new EventEmitter(),
) {
this.semgrep_log = Uri.joinPath(context.logUri, LSP_LOG_FILE);
setSentryContext(this);
}

Expand Down
8 changes: 5 additions & 3 deletions src/lsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,11 @@ async function lspOptions(
env.channel,
env.globalStoragePath,
);
const errorHandler = new SentryErrorHandler(5, () => [
outputChannel.logAsAttachment(),
]);
const errorHandler = new SentryErrorHandler(5, () => {
const attachment = outputChannel.logAsAttachment();

return attachment ? [attachment] : [];
});
const clientOptions: LanguageClientOptions = {
diagnosticCollectionName: DIAGNOSTIC_COLLECTION_NAME,
// TODO: should we limit to support languages and keep the list manually updated?
Expand Down
36 changes: 31 additions & 5 deletions src/telemetry/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "vscode-languageclient";
import { Environment } from "../env";
import * as fs from "fs";
import { DEFAULT_LSP_LOG_FOLDER } from "../utils";

// global here so if user opts out the functions below don't do anything
let sentryEnabled = false;
Expand Down Expand Up @@ -170,20 +171,41 @@ export class SentryErrorHandler implements ErrorHandler {
export class ProxyOutputChannel implements vscode.OutputChannel {
readonly name: string;
private logFile: string;
private writeLog = true;
constructor(
public readonly baseOutputChannel: vscode.OutputChannel,
private readonly logPath: string,
logPath: string,
) {
this.baseOutputChannel = baseOutputChannel;
this.name = baseOutputChannel.name;
// Check if logPath exists, if not create a random one
if (!fs.existsSync(logPath)) {
logPath = DEFAULT_LSP_LOG_FOLDER.fsPath;
// ensure logPath exists
if (!fs.existsSync(logPath)) {
fs.mkdirSync(logPath);
}
}
this.logFile = `${logPath}/lsp-output.log`;
// create/clear log file
fs.writeFileSync(this.logFile, "");
try {
fs.writeFileSync(this.logFile, "");
} catch (e) {
if (sentryEnabled) {
Sentry.captureException(e);
}
console.error(`Failed to write to log file: ${e}`);
this.writeLog = false;
}
}

logAsAttachment(): Attachment {
logAsAttachment(): Attachment | null {
if (!this.writeLog) {
return null;
}
const timestamp = new Date().toISOString();
const filename = `lsp-output-${timestamp}.log`;

const data = fs.readFileSync(this.logFile, "utf8");
const attachment = {
filename,
Expand All @@ -198,13 +220,17 @@ export class ProxyOutputChannel implements vscode.OutputChannel {
append(value: string): void {
this.baseOutputChannel.append(value);
// write to log file
fs.appendFileSync(this.logFile, value);
if (this.writeLog) {
fs.appendFileSync(this.logFile, value);
}
}

appendLine(value: string): void {
this.baseOutputChannel.appendLine(value);
// write to log file
fs.appendFileSync(this.logFile, `${value}\n`);
if (this.writeLog) {
fs.appendFileSync(this.logFile, `${value}\n`);
}
}

clear = this.baseOutputChannel.clear;
Expand Down
13 changes: 6 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { tmpdir } from "os";
import { OutputChannel, Uri } from "vscode";
import { Uri } from "vscode";
// Can't put this in constants for some reason??
export const DEFAULT_LSP_LOG_FOLDER = Uri.file(tmpdir());

import { OutputChannel } from "vscode";
import * as vscode from "vscode";

import { getVersionInfo, LSP_LOG_FILE } from "./constants";
import { ViewResults } from "./webview-ui/src/types/results";
import * as semver from "semver";

export const DEFAULT_LSP_LOG_URI = Uri.joinPath(
Uri.file(tmpdir()),
LSP_LOG_FILE,
);
import { getVersionInfo } from "./constants";

export class Logger {
enabled: boolean;
Expand Down

0 comments on commit 32fbd6b

Please sign in to comment.