feat: support DisableBuiltin("$env") to hide the $env variable#949
Open
boinger wants to merge 2 commits intoexpr-lang:masterfrom
Open
feat: support DisableBuiltin("$env") to hide the $env variable#949boinger wants to merge 2 commits intoexpr-lang:masterfrom
boinger wants to merge 2 commits intoexpr-lang:masterfrom
Conversation
$env is a hardcoded special variable, not a regular builtin, so
DisableBuiltin("$env") previously had no effect. Check config.Disabled
before each $env special-case handler in the checker and compiler.
When disabled, $env falls through to normal identifier resolution
(errors in strict mode as "unknown name $env"), consistent with how
DisableBuiltin works for regular builtins.
Fixes expr-lang#710
There was a problem hiding this comment.
Pull request overview
This PR makes the hardcoded special variable $env respect DisableBuiltin("$env"), allowing users to hide $env (e.g., to avoid naming conflicts or to restrict environment access) and have it fall back to normal identifier resolution.
Changes:
- Guard
$envspecial-case handling in the checker so it only applies when$envis not disabled. - Guard
$envbytecode emission (OpLoadEnv) in the compiler so it only applies when$envis not disabled. - Add regression tests covering
$envdisabled/enabled behaviors across identifier, member access, call, andget($env, ...)paths.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
checker/checker.go |
Adds !config.Disabled["$env"] guards around $env special handling so disabling causes normal name resolution/errors. |
compiler/compiler.go |
Prevents OpLoadEnv emission when $env is disabled, aligning runtime behavior with checker resolution. |
test/issues/710/issue_test.go |
Adds coverage for strict and non-strict behavior when $env is disabled, plus default enabled regressions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
$envis a hardcoded special variable with custom handling in the checker and compiler. It's not in theBuiltinsmap, soDisableBuiltin("$env")previously had no effect.This PR checks
config.Disabled["$env"]before each$envspecial-case handler. When disabled,$envfalls through to normal identifier resolution, which errors in strict mode as"unknown name $env". This is consistent with howDisableBuiltinworks for regular builtins.Changes
checker/checker.go: Guard$envhandling inidentifierNode,memberNode,callNode, andcallBuiltin(theget($env, ...)path) with!v.config.Disabled["$env"]compiler/compiler.go: GuardOpLoadEnvemission inIdentifierNodewith!c.config.Disabled["$env"]test/issues/710/issue_test.go: 7 subtests covering all disabled paths (standalone, member access, call,get()), non-strict mode behavior, and regression tests for default behaviorUse case
Users embedding expr as a DSL may have domain variables that conflict with
$env, or may want to restrict access to the full environment object for sandboxing.Fixes #710