Skip to content

fix: fallbacks for when env variables are disallowed - #5

Merged
sxzz merged 2 commits into
sxzz:mainfrom
vrugtehagel:main
Jul 12, 2026
Merged

sxzz merged 2 commits into
sxzz:mainfrom
vrugtehagel:main

Conversation

@vrugtehagel

Copy link
Copy Markdown
Contributor
  • This PR contains AI-generated code, but I have carefully reviewed it myself. Otherwise, my PR may be closed.
    • I understand that my PR is more likely to be rejected or requested for changes if it contains AI-generated code that I do not fully understand.

Description

This creates some try-catch blocks around bits that ask for access to environment variables. Functionally, this allows the host environment to reject access to env variables, while maintaining obug's functionality.

Linked Issues

Resolves #4.

Additional context

I opened this PR fully aware that it may be rejected, as my issue has just been opened and neither the feature nor its solution has been discussed with the maintainer.

@sxzz sxzz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid wrapping everything in try blocks. Instead, extract a env variable for reading environment variables.

// simple example
let env
try {
  env = process.env
} catch {}

Also, please provide a minimal reproduction demonstrating why accessing process.env causes an error.

Comment thread src/node.ts Resolved
@vrugtehagel

vrugtehagel commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the (speedy) review! Updated as per your suggestion. It's much cleaner indeed 🙂

Note that env = process.env itself doesn't throw an error, because no environment variables are accessed. I now set env = { ...process.env } instead, which means that changes to the environment variables during the lifetime of the program won't be taken into account. Do you think this is problematic? Most of the work happens synchronously when the module loads, though the enabling/saving could happen at any time.

I did amend and force-push the commit, to align it better with your commit message style. Not sure if it matters for you, but if you're wondering about the force pushing, that's that 😉

As for the reproduction case, consider a file repro.js with the following contents:

import 'npm:[email protected]'

Then, run it using deno run --deny-env repro.js. This throws an error. Alternatively run it with deno run repro.js and deny access to the variables manually. Note that it doesn't matter what you import because the environment variables are accessed in the top level of the package.

@sxzz sxzz changed the title Fallbacks for when env variables are disallowed fix: fallbacks for when env variables are disallowed Jul 12, 2026
@sxzz
sxzz merged commit faf1f47 into sxzz:main Jul 12, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support context without access to env variables

2 participants