MCPcopy Index your code
hub / github.com/deepnote/deepnote / withRestoredEnv

Function withRestoredEnv

packages/cli/src/commands/lint.ts:55–70  ·  view source on GitHub ↗

* Run `fn` with a snapshot of `process.env` taken beforehand, restoring the original environment * afterwards (even if `fn` throws). Linting loads `.env` via `dotenv.config` and injects generated * integration env vars into `process.env`; without this a lint run would permanently mutate the * pro

(fn: () => Promise<T>)

Source from the content-addressed store, hash-verified

53 * was reported missing would silently start passing) and clobbering caller-provided env values.
54 */
55async function withRestoredEnv<T>(fn: () => Promise<T>): Promise<T> {
56 const originalEnv = { ...process.env }
57 try {
58 return await fn()
59 } finally {
60 // Reset to the snapshot: delete keys added during the run, then restore originals. Assigning
61 // `process.env = originalEnv` would replace the live env object some libraries hold a reference
62 // to, so mutate the existing object in place instead.
63 for (const key of Object.keys(process.env)) {
64 if (!(key in originalEnv)) {
65 delete process.env[key]
66 }
67 }
68 Object.assign(process.env, originalEnv)
69 }
70}
71
72/**
73 * Derive a stable, snake_case issue code from an error's class name.

Callers 2

lintFileFunction · 0.85
lintIntegrationsFileFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected