Skip to content

Commit 80d9baa

Browse files
committed
Attempt to require spdlog and node-pty
1 parent 59eec53 commit 80d9baa

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

packages/protocol/src/common/helpers.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { logger } from "@coder/logger";
77

88
// tslint:disable no-any
99

10+
declare var __non_webpack_require__: typeof require;
11+
declare var __webpack_require__: typeof require;
12+
1013
export type ForkProvider = (modulePath: string, args: string[], options: ForkOptions) => ChildProcess;
1114

1215
export interface Disposer extends IDisposable {
@@ -20,7 +23,7 @@ interface ActiveEvalEmitter {
2023
}
2124

2225
/**
23-
* Helper class for evaluations.
26+
* Helper class for server-side evaluations.
2427
*/
2528
export class EvalHelper {
2629
/**
@@ -35,15 +38,36 @@ export class EvalHelper {
3538
options.env = { ...process.env, ...options.env };
3639
}
3740
}
41+
42+
/**
43+
* Try a non-webpack require, then a webpack require if that fails.
44+
*/
45+
public require(modulePath: string): any {
46+
logger.info(`Attempting to require ${modulePath}`);
47+
try {
48+
return __non_webpack_require__(modulePath);
49+
} catch (error) { /* Nothing. */ }
50+
51+
logger.warn(`Non-webpack require failed for ${modulePath}`);
52+
try {
53+
return __webpack_require__(modulePath);
54+
} catch (error) { /* Nothing. */ }
55+
56+
logger.warn(`Webpack require failed for ${modulePath}`);
57+
try {
58+
return require(modulePath);
59+
} catch (error) {
60+
logger.error(`Failed to require ${modulePath}`);
61+
throw error;
62+
}
63+
}
3864
}
3965

4066
/**
41-
* Helper class for active evaluations.
67+
* Helper class for client-side active evaluations.
4268
*/
43-
export class ActiveEvalHelper extends EvalHelper implements ActiveEvalEmitter {
44-
public constructor(private readonly emitter: ActiveEvalEmitter) {
45-
super();
46-
}
69+
export class ActiveEvalHelper implements ActiveEvalEmitter {
70+
public constructor(private readonly emitter: ActiveEvalEmitter) {}
4771

4872
public removeAllListeners(event?: string): void {
4973
this.emitter.removeAllListeners(event);
@@ -100,9 +124,19 @@ export class ActiveEvalHelper extends EvalHelper implements ActiveEvalEmitter {
100124
/**
101125
* Helper class for server-side active evaluations.
102126
*/
103-
export class ServerActiveEvalHelper extends ActiveEvalHelper {
127+
export class ServerActiveEvalHelper extends ActiveEvalHelper implements EvalHelper {
128+
private readonly evalHelper: EvalHelper;
104129
public constructor(emitter: ActiveEvalEmitter, public readonly fork: ForkProvider) {
105130
super(emitter);
131+
this.evalHelper = new EvalHelper();
132+
}
133+
134+
public preserveEnv(options: SpawnOptions | ForkOptions): void {
135+
this.evalHelper.preserveEnv(options);
136+
}
137+
138+
public require(modulePath: string): any {
139+
return this.evalHelper.require(modulePath);
106140
}
107141

108142
/**

packages/vscode/src/fill/node-pty.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { client } from "@coder/ide/src/fill/client";
22
import { EventEmitter } from "events";
33
import * as nodePty from "node-pty";
44
import { ActiveEvalHelper } from "@coder/protocol";
5-
6-
// Use this to prevent Webpack from hijacking require.
7-
declare var __non_webpack_require__: typeof require;
5+
import { logger } from "@coder/logger";
86

97
/**
108
* Implementation of nodePty for the browser.
@@ -17,7 +15,7 @@ class Pty implements nodePty.IPty {
1715

1816
public constructor(file: string, args: string[] | string, options: nodePty.IPtyForkOptions) {
1917
this.ae = client.run((ae, file, args, options) => {
20-
const nodePty = __non_webpack_require__("node-pty") as typeof import("node-pty");
18+
const nodePty = ae.require("node-pty") as typeof import("node-pty");
2119

2220
ae.preserveEnv(options);
2321

@@ -54,6 +52,8 @@ class Pty implements nodePty.IPty {
5452
};
5553
}, file, args, options);
5654

55+
this.ae.on("error", (error) => logger.error(error.message));
56+
5757
this.ae.on("pid", (pid) => this._pid = pid);
5858
this.ae.on("process", (process) => this._process = process);
5959

packages/vscode/src/fill/spdlog.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import { RotatingLogger as NodeRotatingLogger } from "spdlog";
33
import { logger } from "@coder/logger";
44
import { client } from "@coder/ide/src/fill/client";
55

6-
declare var __non_webpack_require__: typeof require;
7-
86
const ae = client.run((ae) => {
9-
const spdlog = __non_webpack_require__("spdlog") as typeof import("spdlog");
7+
const spdlog = ae.require("spdlog") as typeof import("spdlog");
108
const loggers = new Map<number, NodeRotatingLogger>();
119

1210
ae.on("new", (id: number, name: string, filePath: string, fileSize: number, fileCount: number) => {

0 commit comments

Comments
 (0)