Skip to content

Commit cc3d03a

Browse files
committed
inherit from electron
1 parent 655ceda commit cc3d03a

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

packages/core/src/amazonq/lsp/lspClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ export async function activate(extensionContext: ExtensionContext, resourcePaths
262262
// TODO(jmkeyes): we always use the debug options...?
263263
execArgv: debugOptions.execArgv,
264264
warnThresholds: { memory: memoryWarnThreshold },
265+
env: (resourcePaths as any).env,
265266
})
266267

267268
const documentSelector = [{ scheme: 'file', language: '*' }]

packages/core/src/shared/lsp/baseLspInstaller.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ export abstract class BaseLspInstaller<T extends ResourcePaths = ResourcePaths,
2424
this.logger = getLogger(loggerName)
2525
}
2626

27+
/**
28+
* Gets environment settings for the Node.js process
29+
*/
30+
protected getProxyEnvironment(): Record<string, string> {
31+
const env: Record<string, string> = {}
32+
33+
// Get SSL settings from VS Code
34+
const httpSettings = vscode.workspace.getConfiguration('http')
35+
const strictSSL = httpSettings.get<boolean>('proxyStrictSSL', true)
36+
37+
// Handle SSL certificate verification
38+
if (!strictSSL) {
39+
env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
40+
}
41+
42+
return env
43+
}
44+
2745
async resolve(): Promise<LspResolution<T>> {
2846
const { id, manifestUrl, supportedVersions, path, suppressPromptPrefix } = this.config
2947
if (path) {
@@ -35,6 +53,7 @@ export abstract class BaseLspInstaller<T extends ResourcePaths = ResourcePaths,
3553
location: 'override',
3654
version: '0.0.0',
3755
resourcePaths: this.resourcePaths(),
56+
env: this.getProxyEnvironment(),
3857
}
3958
}
4059

@@ -73,6 +92,7 @@ export abstract class BaseLspInstaller<T extends ResourcePaths = ResourcePaths,
7392
// }
7493
// ```
7594
resourcePaths: this.resourcePaths(assetDirectory),
95+
env: this.getProxyEnvironment(),
7696
}
7797
return r
7898
}

packages/core/src/shared/lsp/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ export interface LspResolution<T extends ResourcePaths> extends LspResult {
5252
* ```
5353
*/
5454
resourcePaths: T
55+
/**
56+
* Environment variables to pass to the Node.js process, including proxy settings
57+
*/
58+
env?: Record<string, string>
5559
}
5660

5761
export interface TargetContent {

packages/core/src/shared/lsp/utils/platform.ts

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,97 @@ export async function validateNodeExe(nodePath: string[], lsp: string, args: str
8181
}
8282
}
8383

84+
/**
85+
* Gets Electron settings from VS Code
86+
*/
87+
export function getElectronSettings(): { caCerts?: string; proxyRules?: string; proxyBypassRules?: string } {
88+
try {
89+
// Access Electron's modules through VSCode's API
90+
// @ts-ignore - This is a valid access pattern in VSCode extensions
91+
const electron = require('electron')
92+
const result: { caCerts?: string; proxyRules?: string; proxyBypassRules?: string } = {}
93+
94+
// Get certificates
95+
if (electron?.net?.getCACertificates) {
96+
const certs = electron.net.getCACertificates()
97+
if (certs && certs.length > 0) {
98+
// Convert the certificates to PEM format
99+
result.caCerts = certs
100+
.map((cert: any) => cert.pemEncoded)
101+
.filter(Boolean)
102+
.join('\n')
103+
}
104+
}
105+
106+
// Get proxy settings from Electron
107+
if (electron?.session?.defaultSession?.getProxyRules) {
108+
result.proxyRules = electron.session.defaultSession.getProxyRules()
109+
}
110+
111+
if (electron?.session?.defaultSession?.getProxyBypassRules) {
112+
result.proxyBypassRules = electron.session.defaultSession.getProxyBypassRules()
113+
}
114+
115+
return result
116+
} catch (err) {
117+
// Silently fail if we can't access Electron
118+
return {}
119+
}
120+
}
121+
84122
export function createServerOptions({
85123
encryptionKey,
86124
executable,
87125
serverModule,
88126
execArgv,
89127
warnThresholds,
128+
env,
90129
}: {
91130
encryptionKey: Buffer
92131
executable: string[]
93132
serverModule: string
94133
execArgv: string[]
95134
warnThresholds?: { cpu?: number; memory?: number }
135+
env?: Record<string, string>
96136
}) {
97137
return async () => {
98138
const bin = executable[0]
99139
const args = [...executable.slice(1), serverModule, ...execArgv]
100140
if (isDebugInstance()) {
101141
args.unshift('--inspect=6080')
102142
}
103-
const lspProcess = new ChildProcess(bin, args, { warnThresholds })
143+
144+
// Merge environment variables
145+
const processEnv = { ...process.env }
146+
if (env) {
147+
Object.assign(processEnv, env)
148+
}
149+
150+
// Get Electron settings (certificates and proxy)
151+
const electronSettings = getElectronSettings()
152+
153+
// Add system CA certificates to the Node process if not already set
154+
if (!processEnv.NODE_EXTRA_CA_CERTS && electronSettings.caCerts) {
155+
processEnv.NODE_EXTRA_CA_CERTS = electronSettings.caCerts
156+
}
157+
158+
// Add Electron proxy settings to the Node process
159+
if (electronSettings.proxyRules && !processEnv.HTTP_PROXY) {
160+
processEnv.HTTP_PROXY = electronSettings.proxyRules
161+
processEnv.HTTPS_PROXY = electronSettings.proxyRules
162+
}
163+
164+
// Add proxy bypass rules if available
165+
if (electronSettings.proxyBypassRules) {
166+
processEnv.NO_PROXY = electronSettings.proxyBypassRules
167+
}
168+
169+
const lspProcess = new ChildProcess(bin, args, {
170+
warnThresholds,
171+
spawnOptions: {
172+
env: processEnv,
173+
},
174+
})
104175

105176
// this is a long running process, awaiting it will never resolve
106177
void lspProcess.run()

0 commit comments

Comments
 (0)