ISSUE DESCRIPTION Node API checker: namespace reads of absent builtin exports should…
- User
ISSUE DESCRIPTION
Node API checker: namespace reads of absent builtin exports should return undefined
Summary
Perry's strict built-in API gate currently rejects some namespace property reads that Node treats as ordinary JavaScript property misses returning
undefined.This surfaced in the curated DNS constants fixture on current
origin/main@22824ba90abf:node:dns/promisesintentionally does not exportADDRCONFIG,V4MAPPED, orALL, but reading those properties from the namespace object is valid in Node and returnsundefined. Perry rejects the property read at check/lower time before the runtime can produce the matching value.This is separate from named ESM import validation. Node rejects
import { ADDRCONFIG } from "node:dns/promises", and Perry should continue not claiming that named export. The compatibility gap is namespace/property-read semantics.Local evidence
Current main:
text22824ba90abf Expose globalThis.EventTarget (#3628)Node v25.9.0 behavior for the namespace object:
console$ node -e 'import("node:dns/promises").then(m=>{for (const k of ["ADDRCONFIG","V4MAPPED","ALL","NODATA","CANCELLED"]){console.log(k, Object.prototype.hasOwnProperty.call(m,k), k in m, typeof m[k], m[k]);}})' ADDRCONFIG false false undefined undefined V4MAPPED false false undefined undefined ALL false false undefined undefined NODATA true true string ENODATA CANCELLED true true string ECANCELLEDNode rejects the corresponding named import, which is correct and should remain true:
console$ node --input-type=module -e 'import { ADDRCONFIG } from "node:dns/promises"; console.log(ADDRCONFIG)' SyntaxError: The requested module 'node:dns/promises' does not provide an export named 'ADDRCONFIG'Perry currently rejects the namespace property read in the curated fixture:
Assistant
I'll start by exploring the repository structure to understand the codebase.
Assistant
Let me search for the
module_has_symbolfunction and theNativeModuleRefrelated code:Assistant
Now let me look at the
perry-api-manifestcrate to understand how it works:Assistant
Now let me find the DNS manifest entries to understand what's there:
Assistant
Let me look at all the dns entries (constants on dns/promises):
Assistant
Now let me look at the test-parity fixture file:
Assistant
Now let me look at the test result/expected files:
Assistant
Let me first reproduce the error:
Assistant
Good, now I have the toolchain. Let me look at the perry binary structure first:
Assistant
Let me look at how the manifest entry types/structures look to understand what's there. I need to understand what kinds of entries (Function/Property/Class/Method) the manifest has:
Assistant
Now let me look at the full context. Let me look at where
is_call_targetor similar is determined:Assistant
Now let me understand the lookup_native_module and how we differentiate between
import * as dnsPromisesanddnsPromises.Xaccess:Assistant
Now I understand the structure. Let's understand the current state. The issue is at line 1813:
rust&& perry_api_manifest::module_has_symbol(module, prop).is_none()This blocks ALL namespace property reads when the symbol is not in the manifest. The fix should be:
- If the read is a value-only access (not a call/method use), allow it to fall through to a runtime lookup that returns
undefined - Keep the strict gate for actual calls/method use
Let me look at the full context of where the gate fires:
- If the read is a value-only access (not a call/method use), allow it to fall through to a runtime lookup that returns
Assistant
Now I see the gate. The key idea is:
- We need to know if this member access is being used as a call (i.e., the parent node is a Call expression) or just as a value read.
- For value reads on a strict native module namespace where the symbol isn't known, return
Expr::Undefined(matching Node'sundefinedreturn for missing property reads).
Let me understand the structure of Expr:
Assistant
Now I need to understand how the parent expression handling could work. The gate fires whenever we read a property from a
NativeModuleRef. The fix is to:- Detect if the property read is being used as a value (e.g., the parent is a non-call expression)
- If so, emit
Expr::Undefinedinstead of failing the gate
But this is tricky because we don't have direct parent context. Let me look at the structure to understand the call vs read detection.
Let me look at how the existing code determines if it's a call:
Assistant
Let me see how call lowering handles missing methods:
Assistant
OK, now I understand. The same gate is at two places - the property read and the call. The question is: for property reads (not calls), what should we do? The answer from the issue: emit
Undefinedinstead of erroring.Let me think about this carefully. The gate currently fails for
dnsPromises.ADDRCONFIG. In Node, this is a property read on a namespace, and Node returnsundefinedfor absent properties.So the fix should be:
- When the read is a property read (not call), and the symbol isn't known, return
Expr::Undefined(matching Node's behavior) - When it's a call, keep the gate
But wait - the gate in
expr_member.rsis for ALL property reads including calls. The call itself doesn't go through the gate because inexpr_call/native_module.rs, the call IS the gate trigger. So for a call likednsPromises.foo(), the member access isdnsPromises.fooand then a call.Let me see how the call expression processes this:
- When the read is a property read (not call), and the symbol isn't known, return
Assistant
Now I understand. The gate in
expr_member.rs(line ~1813) fires on the property read. The gate innative_module.rs(line ~1309) fires on the call expression.Looking at this carefully: the
memberinexpr_member.rsis just a property read. To know if it's being called, we'd need to track parent expressions. Let me see if there's a way.Actually, looking at the flow more carefully, I think the cleanest solution is:
- The gate in
expr_member.rsis for ALL property reads - it can't easily know if it's a call target or not. But the way the gate works now is:- For
dnsPromises.ADDRCONFIG(read): It fails with the unimplemented error. - For
dnsPromises.notARealMethod()(call): It also fails with the unimplemented error (this is the helpful one).
- For
The cleanest solution is to check if this is a call site (parent is a CallExpr). Let me see if there's a context flag for that.
- The gate in
Assistant
Now let me look at how the call lowering processes the callee:
Assistant
Now I have a clearer picture. Let me look at the existing NativeModuleRef lowering logic to see what Expr::Undefined looks like:
Assistant
Good. Now, the right approach is to detect the parent context. The Member access in the AST has no idea if it's used as a callee. But the AST already encodes the AST tree, and we can pass parent context through.
Actually wait. Let me re-read the issue. The current behavior is that the gate fires for
dnsPromises.ADDRCONFIG(a value read). The call gate would also fire fordnsPromises.notARealMethod().So if we want to allow value reads on namespaces but block calls, we need to know if the read is in a "call" position. The cleanest way is to introduce a flag in the lowering context.
Let me check how the issue is described again carefully:
Similar absent-property reads on strict native module namespaces should follow normal JS property semantics when the code is only reading the value.
So the goal is: when reading the property (not calling it), follow JS semantics =
undefined.Obvious unsupported API calls should still get the helpful Perry unimplemented-API error
So when calling it, keep the helpful error.
Assistant
OK, the call lowering lower_expr's the callee in
mod.rs:452. Let me see the context around it:Assistant
OK now let me check the lower context structure for adding a flag:
Assistant
Now let me also look at the LoweringContext default initialization:
Assistant
Assistant
OK so when the call is
dnsPromises.notARealMethod(), thetry_native_module_methodsmatches the AST shape and goes to the gate at line 1309. This doesn't go throughlower_expron the callee.Wait, let me re-check. Looking at the flow:
lower_callis called with the call AST.
Assistant
OK so when a call doesn't match any try_*, it falls through to lower_expr on the callee. This is where the gate in
lower_memberwould fire.But wait, the way the call flow works: when calling
dnsPromises.notARealMethod(), the call lowering tries try_native_module_methods which fires the gate. But when it's NOT a call, e.g.,dnsPromises.ADDRCONFIG, the call lowering doesn't even happen.Let me think about the call position flag. The cleanest approach:
In
lower_call, when about to lower the callee, setin_call_position = truefor that lowering.But the gate in
native_module.rsdoesn't go throughlower_expr. Let me verify:Assistant
Assistant
OK so
lower_memberis never directly called fromlower_call. The fall-throughlet callee_expr = lower_expr(ctx, expr)?;callslower_exprwhich then dispatches tolower_member.So the flag needs to be set before this fall-through. Let me verify:
Assistant
Now let me check the pre-scan to see how the args are processed before the fall-through:
Assistant
Good. So I see that args are lowered first (line 230-233), then there's the fall-through path (line ~451). The flag should be set before the fall-through.
Actually, let me also check if there are other places where
lower_membercould be called fromlower_callindirectly. Let me look at the static_and_instance module:Assistant
OK, lots of lower_expr calls for member objects but these don't seem to affect the .method call directly. Let me trace through how
dnsPromises.someUnknownMethod()would actually behave.If we have
dnsPromises.someUnknownMethod():lower_callis called- Various
try_*methods look at the AST
Assistant
Let me look at lines 1100-1200 to see what comes after the per-method case:
Assistant
Assistant
So for path/fs/crypto/os child_process,
try_module_static_methodshandles them. Let me check it:Assistant
Assistant
These lower the OBJECT, not the callee. So they're for
obj.method()style. Loweringobjis a property access butobjitself isn't being called.OK let me also check
try_module_static_methods:Assistant
So
try_module_static_methodsdoesn't calllower_expron the callee.OK so the pattern is:
try_*methods either handle the call or fall through.- For native module method calls,
try_native_module_methodsfires the gate at 1309. - For other calls that don't match any try_*, the fall-through happens.
So the question is: when can the fall-through
let callee_expr = lower_expr(ctx, expr)?;be reached for a call on a native module?Looking at
lower_call_inner:rust// ... lots of try_* ... let callee_expr = lower_expr(ctx, expr)?;If NONE of the try_* match, we fall through. The try_* methods are all quite comprehensive, so the fall-through is rare.
Let me check if there's a specific test case where the fall-through happens for a call. Let me look at when
lower_membergets called from a call context:Assistant
Assistant
OK so when the gate at 1309 fires, it calls
crate::lower_bail!which is an ERROR. So the call ALWAYS errors if the gate fires.So my plan works:
- For property reads:
in_call_position = false→ return Undefined - For property reads as callees:
try_native_module_methodsfires the gate and errors out → never reacheslower_member
But what about the rare case where a call goes through fall-through to
lower_expron the callee? Let me check the conditions:- All try_* methods return Err(args) without erroring.
- Fall-through to
lower_expr(ctx, expr)?on the callee.
- For property reads:
Assistant
Now let me think about the design more. I think the simplest and most localized fix is:
- Add
in_call_position: booltoLoweringContext(default false). - In
lower_call, setin_call_position = trueBEFORE the fall-throughlower_expron the callee, and restore after. - In
lower_member, when the gate fires AND!in_call_position, returnExpr::Undefinedinstead of erroring.
But wait, I'm going to also consider: should we ALSO check for the type of access (named import vs namespace)?
Looking at the issue more carefully:
dnsPromises.ADDRCONFIG- namespace import, property read.- The
lookup_native_module(dnsPromises)returnsSome(("dns/promises", None))(None means namespace).
Let me verify by looking at the lookup:
- Add
Assistant
Assistant
So named imports ARE checked via
module_has_public_named_exportin module_decl.rs. The named import forADDRCONFIGwould error here. Good.Now let me verify that the fixture file doesn't have any named imports of absent exports:
Looking at the fixture:
import { NODATA as namedNodata } from "node:dns"- NODATA is in manifest, OKimport { CANCELLED as namedPromisesCancelled } from "node:dns/promises"- CANCELLED is in manifest fordns/promises? Let me check:
Assistant
Good. Both NODATA and CANCELLED are in
dns/promises. So named imports work.Now let me verify by running the current code to reproduce the error. Let me first build the project:
Assistant
Build successful. Now let me reproduce the issue:
Assistant
Reproduced the issue. Let me also verify the run mode works (with the bypass):
Assistant
There's a clang error. Let me check the clang version:
Assistant
The clang 14 doesn't support opaque pointers. Let me check if there's a way to upgrade or work around:
Assistant
Hmm, only clang-14 is available. The
ptrtype requires opaque pointers mode which is only in newer clangs (16+). Let me check whatperry doctorsays:Assistant
OK, the runtime library built. The clang issue is pre-existing - the test runs would need a newer clang. Let me see if I can work around it with
PERRY_LLVM_CLANG:Actually, I just need to verify the gate logic. Let me write a small test that confirms the bug is in the gate and not somewhere else. Let me try the
checkcommand first which doesn't compile:Assistant
Good. So
checkdoesn't compile - it just type-checks. That's what we need.Let me now implement the fix. First, let me make the changes:
Change 1: Add
in_call_positionflag to LoweringContextAssistant
Let me continue with the implementation. I need to add the
in_call_positionflag to LoweringContext.Assistant
Let me reformat the edit call properly.
Assistant
Let me try a different approach using the search/replace tool.
Assistant
Let me try using a different approach using sed.
Assistant
Now let me verify the change:
Assistant
Now let me initialize this flag in
LoweringContext::with_class_id_start:Assistant
Now let me modify the gate in
lower_member. First, let me see the exact code:Assistant
Now let me modify the gate to return
Expr::Undefinedfor property reads when not in call position:Assistant
Now let me set the flag in
lower_call. First, find where to put it:Assistant
Let me check the fall-through code more carefully:
Assistant
Now let me build the project to verify the changes compile:
Assistant
Build succeeded. Now let me verify the issue is fixed:
Assistant
The check passes. Now let me also test that calls still error appropriately. Let me write a small test to verify:
Assistant
Excellent! Calls still error with the helpful message. Let me also test that named imports still error:
Assistant
The named import error is properly fired by the existing
module_has_public_named_exportcheck inmodule_decl.rs.Let me also test a case where we use the property as an arg:
Assistant
Hmm, that's because the perry check includes all files in /tmp. Let me check just one file:
Assistant
Now let me check the third file (the call):
Assistant
All three test cases work as expected. Now let me think about edge cases:
- What about
dnsPromises.ADDRCONFIG()- calling an absent namespace property as a function?lower_callis invokedtry_native_module_methodsmatches (it's a method call on a namespace)- The gate at 1309 fires → ERROR
Let me verify this:
- What about
Assistant
Great - the helpful error fires here.
- What about reading a property of a known missing export on a special-handled module like fs?
Assistant
Now let me test a call on
fs.notARealMethod()to make sure it still errors:Assistant
The helpful error fires for the call.
- What about a property that exists but we read it? Should still work:
Assistant
- What about computing properties on a namespace (computed access)?
Assistant
- What about
dns.promises.NODATA(chained access)?
- What about
Assistant
- What about
dns.promises.someUnknownMethod()?
- What about
Assistant
Interesting - the chained call didn't error. Let me investigate the chain case more carefully:
Assistant
Now let me check what happens for
dns.promiseschained access:Assistant
OK, so for
dns.promises.someUnknownMethod(), this is a chained access.dns.promisesis treated as a class statics access on thednsmodule.Looking at line 89-105, there's a gate for chained access. But it only fires if
!is_sub_namespace. Let me checkdns.promises- is it in theis_sub_namespacelist?Looking at the match in line 41-66:
rustlet is_sub_namespace = matches!( (module_name, class_name.as_str()), ("fs", "promises") | ("fs", "constants") | ("path", "posix") | ("path", "win32") ... );dnsis not in this list. Sodns.promisesis treated asmodule.Class. Then the gate at line 86 should fire.Wait, but my test passed. Let me trace through what's happening.
Actually, looking at my test:
tsimport * as dns from "node:dns"; dns.promises.someUnknownMethod();Assistant
OK so
dnshas many properties likeADDRCONFIG,V4MAPPED,ALL,NODATA, etc. But there's nopromisesproperty. Let me check:Assistant
Hmm, let me check whether
dns.promises.someUnknownMethod()actually does anything at runtime. The issue says "obvious unsupported API calls should still get the helpful Perry unimplemented-API error". Let me also testdns.promises.notARealThingas a property read:Assistant
Let me run the original fixture file again to verify the fix:
Assistant
Now let me run the tests in the perry-hir crate to make sure I haven't broken anything:
Assistant
Some tests are failing. Let me look at these tests to understand what they're testing:
Assistant
Let me check the other tests to see what they do:
Assistant
The failing tests are testing the OLD behavior (READ on missing namespace property should error). The issue explicitly says reads should NOT error. These tests are testing the bug behavior and need to be updated, but per the instructions, I should not modify them.
Let me also run the perry-api-manifest tests:
Assistant
Now let me run the full test suite for the rest of the perry-hir module to make sure no other tests broke:
Assistant
Let me also run the codegen tests:
Assistant
All codegen tests pass. Let me check the parser tests:
Assistant
Let me check if the perry-runtime test failure is pre-existing:
Assistant
Interesting - the test passes without my changes. Let me restore my changes and investigate:
Assistant
It passes when run individually. The failure might be due to test parallelism. Let me confirm by running the full suite:
Assistant
Different test failed this time (
navigator_global_constructor_identity_shape). This is clearly a flaky test unrelated to my change. Let me confirm by running on main:Assistant
Yes - the same test fails on main too. So it's pre-existing. Let me restore my changes:
Assistant
Let me verify a few more edge cases:
Assistant
Let me also test what happens when the check fires for
dnsPromises[someKey](computed access with dynamic key):Assistant
Let me investigate why dynamic key isn't refused:
Assistant
Let me investigate why dnsPromises dynamic key isn't refused:
Assistant
Let me also check that the existing tests in tests.rs work fine:
Assistant
Let me look at the native_module.rs to understand how the runtime handles native modules:
Assistant
Let me also check what the fixture's expected output should be:
Assistant
Let me view the full diff of my changes:
Assistant
Let me run a comprehensive test to make sure all scenarios work as expected. First let me see the output of the fixture file:
Assistant
The check passes. Now let me also run the test for the broader perry-hir tests to make sure I haven't broken anything else:
Assistant
Let me clean up the temporary test files I created:
Assistant
Let me now do a final verification by checking the git diff:
Assistant
Now let me verify the full diff once more to ensure correctness:
Assistant
Let me look at one more potential edge case to make sure I'm not missing anything:
Assistant
Let me investigate why this passed - let me look at the structure:
Assistant
Let me look at the order of the gate and the computed access handling:
Assistant
Let me check if the computed-access case was previously also a bug:
Assistant
This is a pre-existing issue (computed access didn't fire the gate before either). Let me restore my changes:
Assistant
Let me verify the computed read case (no call):
Assistant
Wait, it checked 2 files. That's because of theleftover file. Let me check just the new file:
Assistant
Now let me also do a final sanity check on the diff:
Assistant
Let me also verify nested member access:
Assistant
Let me do a final clean build to make sure everything is in order:
Assistant
No errors. Now let me do one final comprehensive test:
Assistant
All scenarios pass. Let me also verify that the call to an absent method (commented out) actually errors:
Assistant
Excellent! The call to an absent method errors with the helpful message. Let me clean up the test files:
Assistant
Let me also run the perry-hir tests one more time to confirm only the expected tests fail:
Assistant
Let me also check the existing tests handle TS casts correctly. Let me look at the existing
obj_is_named_importlogic:Assistant
My fix is complete. Let me verify the final state:
Assistant
Summary of Changes
Let me go through the checklist:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.