(settings?: Settings)
| 251 | } |
| 252 | |
| 253 | export function loadEnvironment(settings?: Settings): void { |
| 254 | const envFilePath = findEnvFile(process.cwd()); |
| 255 | |
| 256 | // Cloud Shell environment variable handling |
| 257 | if (process.env['CLOUD_SHELL'] === 'true') { |
| 258 | setUpCloudShellEnvironment(envFilePath); |
| 259 | } |
| 260 | |
| 261 | // If no settings provided, try to load workspace settings for exclusions |
| 262 | let resolvedSettings = settings; |
| 263 | if (!resolvedSettings) { |
| 264 | const workspaceSettingsPath = getWorkspaceSettingsPath(process.cwd()); |
| 265 | try { |
| 266 | if (fs.existsSync(workspaceSettingsPath)) { |
| 267 | const workspaceContent = fs.readFileSync( |
| 268 | workspaceSettingsPath, |
| 269 | 'utf-8', |
| 270 | ); |
| 271 | const parsedWorkspaceSettings = JSON.parse( |
| 272 | stripJsonComments(workspaceContent), |
| 273 | ) as Settings; |
| 274 | resolvedSettings = resolveEnvVarsInObject(parsedWorkspaceSettings); |
| 275 | } |
| 276 | } catch (_e) { |
| 277 | // Ignore errors loading workspace settings |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | if (envFilePath) { |
| 282 | // Manually parse and load environment variables to handle exclusions correctly. |
| 283 | // This avoids modifying environment variables that were already set from the shell. |
| 284 | try { |
| 285 | const envFileContent = fs.readFileSync(envFilePath, 'utf-8'); |
| 286 | const parsedEnv = dotenv.parse(envFileContent); |
| 287 | |
| 288 | const excludedVars = |
| 289 | resolvedSettings?.excludedProjectEnvVars || DEFAULT_EXCLUDED_ENV_VARS; |
| 290 | const isProjectEnvFile = !envFilePath.includes(ANUS_DIR); |
| 291 | |
| 292 | for (const key in parsedEnv) { |
| 293 | if (Object.hasOwn(parsedEnv, key)) { |
| 294 | // If it's a project .env file, skip loading excluded variables. |
| 295 | if (isProjectEnvFile && excludedVars.includes(key)) { |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | // Load variable only if it's not already set in the environment. |
| 300 | if (!Object.hasOwn(process.env, key)) { |
| 301 | process.env[key] = parsedEnv[key]; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | } catch (_e) { |
| 306 | // Errors are ignored to match the behavior of `dotenv.config({ quiet: true })`. |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 |
no test coverage detected