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:
22824ba90abf Expose globalThis.EventTarget (#3628)Node v25.9.0 behavior for the namespace object:
$ 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:
$ 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:
$ target/release/perry check test-parity/node-suite/dns/constants/error-aliases.ts error[U006]: `dns/promises.ADDRCONFIG` is not implemented in Perry — see `perry --print-api-manifest` for the supported surface, or set `PERRY_ALLOW_UNIMPLEMENTED=1` to ignore. (#463) --> test-parity/node-suite/dns/constants/error-aliases.ts:9:48The manifest matches Node for named exports:
dns/promisesincludes DNS error aliases likeNODATAandCANCELLED, but does not includeADDRCONFIG,V4MAPPED, orALL.With the gate bypassed, Perry's runtime output matches Node for this fixture, including the absent namespace properties:
Assistant
Let me start by exploring the repository structure to understand what we're working with.
Assistant
Now let me find the test fixture:
Assistant
Let me see what the build looks like and check the relevant crates:
Assistant
Now I understand the structure. Let me look at the testfor the issue:
Assistant
Let me look at the issue more thoroughly to understand the existingstructure for
dns:Assistant
Now let me look at how the gate works and how to address the issue. The fix should make namespace property reads of absent builtin exports return
undefined:Assistant
Let me look at how native module names are registered to understand the namespace vs named-import distinction:
Assistant
Let me check how calls on namespace references work:
Assistant
Let me look at how
lower_callhandles the case:Assistant
Let me understand the full call path. Let me look at the member call dispatch:
Assistant
Let me check how the runtime handles
NativeModuleRefproperty reads:Assistant
Let me check if there's a gate for the handled modules (path, fs, etc.):
Assistant
Let me look at the call paths for handled modules:
Assistant
Let me first reproduce the issue, then plan my fix:
Assistant
Let me check if perry is already built:
Assistant
Now let me reproduce the issue:
Assistant
I've reproduced the issue. Now let me check the gate behavior with
PERRY_ALLOW_UNIMPLEMENTED=1:Assistant
The runtime needs a different clang. Let me check the doctor:
Assistant
Let me focus on the
checkstep which is what's broken. Let me look at the existing tests:Assistant
Let me look at how handled modules handle unknown methods:
Assistant
Let me look at the broader call path:
Assistant
Let me trace what happens for
fs.notARealMethod():Assistant
OK so the codegen has its own dispatch. Let me look at how
Call { callee: PropertyGet { object: NativeModuleRef, property } }is handled. Iflower_memberdoesn't error, thenlower_callproduces aCallwith aPropertyGetcallee. The codegen must then handle this.Let me look at the codegen:Assistant
Let me look at the property_get codegen to see how
PropertyGet { NativeModuleRef, prop }is lowered:Assistant
Let me check what
js_native_module_property_by_namereturns for missing properties:Assistant
So
js_native_module_property_by_namereturnsf64::from_bits(crate::value::TAG_UNDEFINED)(i.e., undefined) for missing properties. So if we remove the gate fromlower_member,dnsPromises.ADDRCONFIGwould compile and return undefined at runtime.But we still need to catch
fs.notARealMethod(). Let me trace what happens to that.When
fs.notARealMethod()is lowered:lower_call(fs.notARealMethod(), args)is calledexprisMemberExpr { obj: Identifier("fs"), prop: Ident("notARealMethod") }- The member lowering would now NOT fire the gate (after my fix)
- So
exprlowers toPropertyGet { object: NativeModuleRef("fs"), property: "notARealMethod" } - The call lowers to
Call { callee: PropertyGet{...}, args } - Codegen has to handle this somehow
Let me checkif there's a codegen path for Call with a NativeModuleRef PropertyGet callee:
Assistant
Let me look at how a Call with PropertyGet on NativeModuleRef gets dispatched:
Assistant
Let me look at how handled module calls fall through:
Assistant
Let me trace what happens after
try_lower_native_method_str_dispatchis skipped:Assistant
Let me look at the structure of
lower_call_innermore carefully:Assistant
Let me check what
js_closure_calldoes with undefined:Assistant
Let me check the test for
crypto.hmacSha256:Assistant
Let me look at how
lower_expris called to understand how to pass a "callee" flag:Assistant
Now I need to understand the approach better. Let me think about a cleaner fix.
Actually, looking at the problem again, the simplest approach is:
Keep the gate in
lower_member, but ONLY fire it for actualCallexpressions. This means we need to check the AST context.But
lower_memberonly sees aMemberExpr, not the surroundingCallExpr.OK, here's another approach:
Have
lower_callcalllower_memberfor the callee first, then check the result and fire the gate if needed.Wait, that wouldn't work either because the gate fires inside
lower_member.Let me try yet another approach:
Add a context flag
is_calleeto the lowering context. When we're processing a callee of aCallExpr, set this flag.lower_membercan then check this flag and fire the gate accordingly.This requires:
- Adding
is_callee: booltoLoweringContext - Setting it in
lower_callbefore lowering the callee - Checking it in
lower_member
Let me look atthe lowering context:
- Adding
Assistant
Let me look at how the existing
unresolved_ident_as_globalflag works inlower_member:Assistant
Let me look at how the
unresolved_ident_as_globalflag is set inlower_call:Assistant
Let me look at
lower_call_innermore carefully:Assistant
Let me look at how
lower_call_innerfalls through to thelower_exprfor the callee:Assistant
Let me look at the existing named import validation:
Assistant
Let me check how named imports are lowered:
Assistant
Let me look at where exactly
lower_call_innerfalls through:Assistant
OK let me start implementing. First, let me understand the init for
LoweringContext:Assistant
Now let me look at the issue 463's gate in
lower_member.rsmore carefully:Assistant
Now I have a clear plan. Let me first verify my understanding by writing a reproduction script:
Assistant
Good. Now let me start the implementation. First, let me look at the current state of the gate in
expr_member.rs:Assistant
Now I'll implement the fix. Let me start by modifying the gate in
lower_member.rsto skip for namespace reads. I'll add a flagis_calleeto the context:Assistant
Now let me make the changes. First, let me think about the simplest approach more carefully.
Looking at this again, the cleanest approach is to use the
is_calleeflag, but we need to be careful with how we set/restore it. Let me look at thelower_call_innerfall-through:Assistant
Let me now implement my plan. I'll:
- Modify the gate in
lower_member.rsto skip for namespace reads (usingis_calleeflag) - Add the
is_calleeflag toLoweringContext - Set the flag in
lower_call_inner - Add a centralized call-side gate in
lower_call_inneras a fallback
First, let me look at the
LoweringContextstruct:- Modify the gate in
Assistant
Now let me add the
is_calleeflag to the context. First, let me look at the context's init:Assistant
Now let me add the
is_calleeflag. I'll add it to the context struct first:Assistant
Let me use sed to make the edit precisely:
Assistant
Now I need to initialize this flag in
LoweringContext::new:Assistant
Now let me modify the gate in
lower_member.rsto skip for namespace reads (whenis_calleeis false):Assistant
Now I need to set
is_callee = trueinlower_call_innerbefore the fall-through. Let me also look at this more carefully to understand the call structure:Assistant
Now let me modify
lower_call_innerto setis_calleebefore lowering the callee:Assistant
Now let me build the project and see if it compiles:
Assistant
It compiled. Now let me test:
Assistant
Excellent! The fix is working correctly:
dnsPromises.ADDRCONFIG,V4MAPPED,ALL(reads) - no errordnsPromises.notARealMethod()(call) - errors with the helpful message
Let me also test the original fixture:
Assistant
The fixture passes. Now let me test some additional cases:
Assistant
All edge cases pass. Now let me test that unsupported method calls on handled modules still error:
Assistant
fs.notARealMethod()errors. Let me test thecrypto.notARealMethod()case separately:Assistant
All the unsupported method calls are still rejected. Now let me test some more edge cases:
Assistant
Named imports are still rejected. Now let me run the existing HIR tests:
Assistant
3 tests fail because they expect reads to error, but my fix correctly allows reads. According to the instructions, I can ignore these failures. Let me verify that the call-form tests still pass:
Assistant
The call-form test still passes. Now let me also check other tests to make sure I haven't broken anything:
Assistant
Only the 3 tests fail that expected reads to error. Let me now run a broader set of tests to make sure my fix doesn't break anything:
Assistant
All codegen tests pass. Let me check the perry-api-manifest tests:
Assistant
Let me find which perry-runtime test failed:
Assistant
The 2 failing tests don't seem related to my fix. Let me check if they are pre-existing failures:
Assistant
Without my changes, the test passes when run alone. Let me check if it's an order-dependent test or a flaky test:
Assistant
The test passes when run alone. Let me run a wider check to confirm this is a pre-existing flaky test:
Assistant
The perry-runtime tests timeout (124 = timeout) due to the test suite being slow. This is a pre-existing issue unrelated to my fix.
Let me also run the larger test suite to make sure my fix doesn't break anything else:
Assistant
Only the 3 expected failures. Now let me also run the codegen tests:
Assistant
All codegen tests pass. Now let me also check edge cases I might have missed. Let me think about potential issues:
- What if
lower_memberis called from a non-lower_call_innerpath that should also fire the gate? - What about
lower_membercalled fromlower_call_inner's specialized handlers (liketry_module_class_static)?
- What if
Assistant
Let me test some more edge cases:
Assistant
All edge cases pass. Let me also check the case where a call is made through a chain like
dnsPromises.someMethod().foo():Assistant
Let me also test the case where a method is called via a chain:
Assistant
Excellent! The gate fires for
dnsPromises.someMethod()even when chained with.foo(). Let me also test a case where the chained method IS in the manifest:Assistant
Let me also test the case where the property read is a sub-namespace access for a handled module:
Assistant
All the cases work correctly. Now let me also think about a subtle edge case: what about
lower_memberbeing called recursively fromlower_memberitself for a chained access?Assistant
Let me also verify a case where a method on a sub-namespace is called:
Assistant
The gate fires for
crypto.subtle.notARealMethod()because it's a call. The existingtry_module_class_staticgate catches this case.Let me also verify the test case with
test2removed:Assistant
Let me clean up and verify all the issues are addressed:
Assistant
Now let me also think about a subtle issue: the
is_calleeflag is set inlower_call_inner, but what if the call is asuper()or some special form that goes throughlower_call_inner's fall-through but doesn't actually invoke the member? Let me check:Assistant
Let me also check the runtime behavior. Let me look at the related tests in the issue:
Assistant
Let me clean up and verify the fix:
Assistant
All four acceptance criteria are met:
- ✓ The DNS constants fixture passes
- ✓ Reads of absent namespace properties compile (return undefined at runtime)
- ✓ Named imports for absent constants are rejected
- ✓ Unsupported method calls are still rejected with helpful errors
Let me also check that the fix doesn't introduce any regressions in the perry-hir tests (other than the expected ones):
Assistant
Let me verify the call test specifically:
Assistant
The call test still passes (the regression test for the distinction is preserved). Let me now think about whether there are any other edge cases I should handle. Let me look at one more scenario - the case where a runtime function value is checked:
Assistant
Good - the cast form
(crypto as any).notARealMethod()still errors because it's a call. Let me also test the chained call through a class method:Assistant
All edge cases work correctly. Now let me look at my changes once more to ensure they're complete:
Assistant
The changes look good. Let me verify all four changes are minimal and correct:
Assistant
Let me run the original test fixture once more to verify:
Assistant
Let me also check that the test results are reproducible. Let me look at the diff once more:
Assistant
The fix is minimal and focused. Let me verify by running the final tests one more time:
Assistant
Let me also verify that
PERRY_ALLOW_UNIMPLEMENTED=1still works:Assistant
The escape hatch still works. Let me now also think about whether I need to handle the case where the call site is inside a TS cast expression:
Assistant
Let me also verify that the dynamic dispatch guard still works:
Assistant
The dynamic dispatch guard still works.
Now let me also look at the runtime side. Looking at the original issue:
With the gate bypassed, Perry's runtime output matches Node for this fixture, including the absent namespace properties
This means the runtime already correctly returns
undefinedfor absent namespace properties. My fix just makes thecheckstep pass withoutPERRY_ALLOW_UNIMPLEMENTED=1.Let me verify by looking at the runtime function:
Assistant
Good. The runtime correctly returns
f64::from_bits(TAG_UNDEFINED)(i.e., JavaScriptundefined) when a property is missing from the manifest. My fix allows the compile step to pass for these reads.Let me now also do a sanity check on the entire changes once more, including the codepath of the lowering:
Assistant
The changes are clean and minimal. Let me run the full perry-hir test suite one final time to ensure everything is working:
Assistant
All my changes are in place. The 3 failing tests are testing the old behavior (reads should error) that the issue explicitly says needs to change (reads should compile and return undefined). The call tests () still pass, providing the regression test for the distinction.
Assistant
The changes are minimal (46 lines added across 4 files). Let me submit:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.