Repository navigation
fix(system proxy): app needs restart to fetch system proxy when set from terminal - #8159
abhishekp-bruno wants to merge 14 commits into
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughAdds ChangesShell Proxy Environment Refresh Integration
Sequence DiagramsequenceDiagram
participant ElectronStore as Electron fetchSystemProxy
participant LoadProxy as loadSystemProxy()
participant RefreshUtil as refreshShellEnvProxyVars()
participant ProcessEnv as process.env
participant GetSystemProxy as getSystemProxy()
ElectronStore->>LoadProxy: call with {refreshShellEnv}
alt refreshShellEnv true
LoadProxy->>RefreshUtil: await refreshShellEnvProxyVars()
RefreshUtil->>ProcessEnv: sync proxy keys
RefreshUtil-->>LoadProxy: return shell env
end
LoadProxy->>GetSystemProxy: fetch system proxy
GetSystemProxy-->>LoadProxy: return proxy config
LoadProxy-->>ElectronStore: cache and return
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning Review ran into problems🔥 ProblemsStopped waiting for pipeline failures after 30000ms. One of your pipelines takes longer than our 30000ms fetch window to run, so review may not consider pipeline-failure results for inline comments if any failures occurred after the fetch window. Increase the timeout if you want to wait longer or run a Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/bruno-requests/src/utils/shell-env.ts (1)
20-33:⚠️ Potential issue | 🟠 Major | ⚡ Quick winAsymmetric error handling between Windows and Unix paths.
The
win32branch returnsfetchWindowsShellEnv()outside thetry/catch, whereas the Unix path swallows failures and returns{}. If the Windows registry fetch throws, it propagates into bothinitializeShellEnv()(startup) andrefreshShellEnvProxyVars(), instead of degrading gracefully like the Unix path. Consider wrapping the Windows branch too.🛡️ Proposed fix
const fetchShellEnv = async (): Promise<Record<string, string>> => { - if (process.platform === 'win32') { - return fetchWindowsShellEnv(); - } - try { + if (process.platform === 'win32') { + return await fetchWindowsShellEnv(); + } // shell-env is ESM-only, so we use dynamic import const { shellEnv } = await import('shell-env'); const env = await shellEnv(); return env; } catch (error) { return {}; } };🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/bruno-requests/src/utils/shell-env.ts` around lines 20 - 33, fetchShellEnv currently calls fetchWindowsShellEnv() outside the try/catch so Windows errors propagate while Unix errors are swallowed; modify fetchShellEnv to wrap the Windows branch in the same try/catch (or surround the fetchWindowsShellEnv() call with its own try/catch) so any thrown error is caught and the function returns an empty {} like the Unix path, ensuring callers such as initializeShellEnv and refreshShellEnvProxyVars degrade gracefully.
🧹 Nitpick comments (1)
packages/bruno-requests/src/utils/shell-env-windows.ts (1)
34-37: 💤 Low valueHeads-up: case-sensitive lookup vs case-insensitive Windows env names.
%VAR%resolution usesexpanded[name]with exact casing. Registry value names usually match (%SystemRoot%,%USERPROFILE%), and theprocess.env[name]fallback is case-insensitive on Windows, so this mostly works — but a%path%reference against aPathkey would skip the local map and only resolve viaprocess.env. Acceptable for now; calling it out in case mixed-case configs surface.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/bruno-requests/src/utils/shell-env-windows.ts` around lines 34 - 37, The replacement currently looks up expanded[name] with exact casing which can miss Windows env keys with different case (e.g., '%path%' vs 'Path'); update the lookup inside the RegExp replacer in the loop over expanded so it performs a case-insensitive lookup: derive a normalized key (e.g., lowercased name) and check expanded for that normalized key before falling back to process.env (and optionally check process.env with the same normalization), ensuring references like '%path%' resolve against expanded and process.env regardless of casing; update any creation of expanded to also populate a lowercase-key map or perform normalized lookups when accessing expanded and process.env.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@packages/bruno-requests/src/utils/shell-env.ts`:
- Around line 20-33: fetchShellEnv currently calls fetchWindowsShellEnv()
outside the try/catch so Windows errors propagate while Unix errors are
swallowed; modify fetchShellEnv to wrap the Windows branch in the same try/catch
(or surround the fetchWindowsShellEnv() call with its own try/catch) so any
thrown error is caught and the function returns an empty {} like the Unix path,
ensuring callers such as initializeShellEnv and refreshShellEnvProxyVars degrade
gracefully.
---
Nitpick comments:
In `@packages/bruno-requests/src/utils/shell-env-windows.ts`:
- Around line 34-37: The replacement currently looks up expanded[name] with
exact casing which can miss Windows env keys with different case (e.g., '%path%'
vs 'Path'); update the lookup inside the RegExp replacer in the loop over
expanded so it performs a case-insensitive lookup: derive a normalized key
(e.g., lowercased name) and check expanded for that normalized key before
falling back to process.env (and optionally check process.env with the same
normalization), ensuring references like '%path%' resolve against expanded and
process.env regardless of casing; update any creation of expanded to also
populate a lowercase-key map or perform normalized lookups when accessing
expanded and process.env.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 65e8826c-f110-4f55-83d9-0ba4b743159d
📒 Files selected for processing (3)
packages/bruno-requests/src/utils/shell-env-windows.tspackages/bruno-requests/src/utils/shell-env.spec.tspackages/bruno-requests/src/utils/shell-env.ts
💤 Files with no reviewable changes (1)
- packages/bruno-requests/src/utils/shell-env.spec.ts
| "version": "7.26.3", | ||
| "resolved": "https://registry.npmjs.org/@babel/helper-create-regexp-features-plugin/-/helper-create-regexp-features-plugin-7.26.3.tgz", | ||
| "integrity": "sha512-G7ZRb40uUgdKOQqPLjfD12ZmGA54PzqDFUv2BKImnC9QIfGhIHKvVML0oN8IUiDq4iRqpq74ABpvOaerfWdong==", | ||
| "devOptional": true, |
| if (value) { | ||
| process.env[key] = value; | ||
| } else { | ||
| delete process.env[key]; |
There was a problem hiding this comment.
the delete seems to be redundant, we are already deleting them above,if block will be enough!
e9417ee to
502027f
Compare
502027f to
442e75f
Compare
a3898fb to
5fa6785
Compare
f3cecf1 to
46d23e9
Compare
|
|
||
| // Clear stale proxy vars first so shell-env does not inherit them into the | ||
| // login shell subprocess (removed .zshrc exports would otherwise persist). | ||
| for (const key of PROXY_ENV_KEYS) { |
There was a problem hiding this comment.
Since we are actually deleting all the PROXY_ENVs here, then calling the fetchShellEnv, if any error the happens in fetchShellEnv, as it returns {} within the catch, refresh will remove all the existing values. Edge case, but something to be mindful about. @abhishekp-bruno cc: @vijayh-bruno
something like this can work, may also need to update the failing test accordingly
// New helper that surfaces failure. Keep fetchShellEnv as-is for initializeShellEnv's additive path.
const tryFetchShellEnv = async (): Promise<Record<string, string> | null> => {
if (process.platform === 'win32') return {};
try {
const { shellEnv } = await import('shell-env');
return await shellEnv();
} catch {
return null;
}
};
export const refreshShellEnvProxyVars = async (): Promise<Record<string, string>> => {
if (process.platform === 'win32') return {};
const snapshot: Record<string, string | undefined> = {};
for (const key of PROXY_ENV_KEYS) {
snapshot[key] = process.env[key];
delete process.env[key];
}
const shellEnvVars = await tryFetchShellEnv();
if (shellEnvVars === null) {
// Subprocess failed — restore prior values rather than leave the user unproxied.
for (const key of PROXY_ENV_KEYS) {
if (snapshot[key] !== undefined) process.env[key] = snapshot[key];
}
return {};
}
for (const key of PROXY_ENV_KEYS) {
const value = shellEnvVars[key];
if (value) process.env[key] = value;
}
return shellEnvVars;
};There was a problem hiding this comment.
@abhishekp-bruno this is still valid in a situation where the race function causes this one to just be left in the timeout, we'll end up with a partial snapshot which isn't something we want.
Needs to be addressed.
There was a problem hiding this comment.
I have updated the code to handle the race condition inside the function itself
There was a problem hiding this comment.
Timeout shouldn't be in bruno-requests, it shouldn't be holding business and rate limiting in here but should be in the app / cli layer only. you can just move the delete logic to be run after fetching has finished instead.
The rest looks good, let me discuss with @bijin-bruno and get back
07e41cd to
7f7a0c7
Compare
7f7a0c7 to
c752f79
Compare
… update shell environment initialization logic
…ons to prevent indefinite hangs
036c530 to
05a779b
Compare
…ction for clarity
|
Closed as merged via #8535 |
JIRA Link - https://usebruno.atlassian.net/browse/BRU-3202
Fixes: #7559
Description
When users configured proxy settings via shell environment variables (e.g.
http_proxy,https_proxyin.zshrc/.bashrc), Bruno would only pick them up at startup. Changing or removing those exports in the terminal had no effect until the app was fully restarted — the cachedprocess.envproxy values wentstale, and the "refresh" path never re-read the shell.
Fix
Added
refreshShellEnvProxyVars()inbruno-requests, which re-syncs proxy-related variables from the user's login shell on demand:delete process.env[key]) before spawning the login shell, so a removed export in.zshrcdoesn't persie subprocess.http_proxy/HTTP_PROXY,https_proxy/HTTPS_PROXY,no_proxy/NO_PROXY,all_proxy/ALL_PROXY.Wired this into the Electron system-proxy store:
fetchSystemProxy({ refresh: true })now passesrefreshShellEnv: truedown to `loadSystemP re-reads the terminal environment rather than serving cached values.Changes
packages/bruno-requests/src/utils/shell-env.ts— newrefreshShellEnvProxyVars()+ exportedPROXY_ENV_KEYS.packages/bruno-requests/src/index.ts— export the new function.packages/bruno-electron/src/store/system-proxy.js— refresh shell env on proxy refresh.Testing
Added unit tests covering the refresh behavior (re-syncing updated vars and clearing removed vars).
Contribution Checklist:
Note: Keeping the PR small and focused helps make it easier to review and merge. If you have multiple changes you want to make, please consider submitting them as separate pull requests.
Publishing to New Package Managers
Please see here for more information.
Summary by CodeRabbit
Improvements
Public API
Tests