This is a large file. The key thing I see is that calls go through lower_call_inner. For member expression calls (like fs.notARealMethod()), it would eventually reach the fall-through tail where lower_expr is called on the callee, which would call lower_member_inner.
Now I need to understand the flow better. The issue says:
- Property reads of absent exports should return
undefined
- Named imports should still be rejected (handled elsewhere)
- Method calls on absent methods should still error
The current gate in lower_member_inner rejects ALL property accesses when the symbol is not in the manifest. The fix should:
- Allow property reads (return
undefined)
- Still reject method calls
But lower_member_inner doesn't know if the result is being used in a call or just read. So I need to think about where to add the check.
Looking at the call lowering code, after all the special cases, it falls through to:
let callee_expr = lower_expr(ctx, expr)?;
This lowers the callee expression, which would be the member expression. So if the callee is fs.notARealMethod, lower_member_inner is called, and if we remove the gate there, it would emit a PropertyGet that evaluates to undefined. Then the call would be undefined(...) which would be a runtime error.
But the issue says we want a compile-time error for calls. So we need to add a check in lower_call_inner for when the callee is a member expression on a native module namespace.
Let me think about this more carefully. The issue says:
Obvious unsupported API calls should still get the helpful Perry unimplemented-API error; this issue should not silently allow fs.notARealMethod()-style call gaps unless the runtime has a deliberate JS-compatible fallback.
So the idea is that fs.notARealMethod() should still get a compile-time error. But fs.notARealMethod (just the read) should be allowed and evaluate to undefined.
I think the approach should be:
- In
lower_member_inner, instead of erroring for absent properties on namespace imports, emit a PropertyGet that will evaluate to undefined at runtime.
- In
lower_call_inner, add a check for when the callee is a member expression on a native module namespace, and if the method is not in the manifest, emit an error.
Actually, looking at the code more carefully, I think there's a simpler approach. The current gate fires in lower_member_inner. Instead of removing the gate entirely, we can modify it to:
- For namespace property reads (when the module has the symbol), continue as before
- For namespace property reads (when the module does NOT have the symbol), instead of erroring, emit
Expr::Undefined directly
- The call check will happen in
lower_call_inner
Wait, but if we emit Expr::Undefined in lower_member_inner, then the call lowering will see the callee as Undefined and the call will be Undefined(...) which might not give a helpful error message.
Let me think again... Actually, looking at the runtime behavior mentioned in the issue:
With the gate bypassed, Perry's runtime output matches Node for this fixture, including the absent namespace properties
This suggests that if we just remove the gate, the runtime handles it correctly. So the fix might be simpler:
- Remove the gate for property reads on namespace imports (let it emit
PropertyGet)
- Add a check in
lower_call_inner for when the callee is a member expression on a native module namespace, and error if the method is not in the manifest
Let me look for where try_native_module_methods is called, as this might be a good place to add the check.
Actually, I think I'm overcomplicating this. Let me re-read the issue more carefully:
The relevant gate is in crates/perry-hir/src/lower/expr_member.rs: namespace PropertyGet on a NativeModuleRef(module) fails whenever module_has_symbol(module, prop) is None, even when the Node-compatible value for a namespace property miss is simply undefined.
So the issue is specifically about property reads. The fix should be to not reject property reads of absent exports, and instead let them evaluate to undefined.
Obvious unsupported API calls should still get the helpful Perry unimplemented-API error; this issue should not silently allow fs.notARealMethod()-style call gaps unless the runtime has a deliberate JS-compatible fallback.
This is a separate concern - calls should still be rejected. But this is handled in the call lowering, not in the member expression lowering.
So my approach is:
- Modify the gate in
lower_member_inner to not error for absent properties on namespace imports, but instead emit PropertyGet which will evaluate to undefined at runtime.
- Add a check in
lower_call_inner (or try_native_module_methods) to reject calls on absent methods.
Let me start with step 1 and see what happens.
Actually, wait. Looking at the current code in lower_member_inner:
if !allow_unimplemented
&& !obj_is_named_import
&& perry_api_manifest::module_has_any_entries(module)
&& perry_api_manifest::module_has_symbol(module, prop).is_none()
{
// error
}
The condition is:
!allow_unimplemented: the environment variable is not set
!obj_is_named_import: this is a namespace import (not a named import)
module_has_any_entries(module): the module has some entries in the manifest
module_has_symbol(module, prop).is_none(): this specific property is not in the manifest
So the gate fires for namespace imports of modules that have some entries, but the specific property is not in the manifest.
The fix should be to change the behavior when the gate fires. Instead of erroring, we should just let it pass through to emit PropertyGet, which will evaluate to undefined at runtime.
But we also need to make sure calls are still rejected. So I need to add a check in the call lowering.
Let me look at try_native_module_methods to see how it handles calls.
Actually, I think the simplest approach is:
- In
lower_member_inner, change the gate to not error, but just let it pass through. This allows property reads to emit PropertyGet, which evaluates to undefined.
- In
lower_call_inner, before lowering the callee, check if it's a member expression on a native module namespace, and if the method is not in the manifest, emit an error.
But wait, the call lowering has a lot of special cases. It tries various handlers like try_native_module_methods, try_module_static_methods, etc. If none of them handle the call, it falls through to the generic case where it lowers the callee and emits a Call expression.
So if I remove the gate in lower_member_inner, then for a call like fs.notARealMethod(), the lowering would:
- Try various handlers in
lower_call_inner
- None of them would match (because the method is not in the manifest)
- Fall through to the generic case
- Lower the callee using
lower_expr, which calls lower_member_inner
lower_member_inner would emit PropertyGet (because we removed the gate)
- The call would be
Call { callee: PropertyGet { object: NativeModuleRef("fs"), property: "notARealMethod" }, args: [...] }
- At runtime, this would evaluate
fs.notARealMethod to undefined and then try to call it, resulting in a runtime error
But the issue says we want a compile-time error for calls. So I need to add a check in the call lowering.
Looking at the call lowering code, I see that after all the special cases, it falls through to:
let callee_expr = lower_expr(ctx, expr)?;
This is where the callee is lowered. At this point, expr is the AST expression of the callee. If it's a member expression on a native module namespace, I can check if the method is in the manifest before lowering.
Let me find a good place to add this check.
Actually, I think the best approach is to add the check right before the lower_expr call for the callee. I'll check if the callee is a member expression on a native module namespace, and if so, check if the method is in the manifest. If not, emit an error.
Let me write the code:
// Check for unsupported native module method calls (#463 followup)
// If the callee is a member expression on a native module namespace, and the
// method is not in the manifest, emit an error. This preserves the helpful
// compile-time error for calls while allowing property reads to return undefined.
if let ast::Expr::Member(member_expr) = unwrap_call_callee_ts_wrappers(callee_expr.as_ref()) {
if let ast::MemberProp::Ident(method_ident) = &member_expr.prop {
if let ast::Expr::Ident(obj_ident) = member_expr.obj.as_ref() {
if let Some((module, None)) = ctx.lookup_native_module(obj_ident.sym.as_ref()) {
let method = method_ident.sym.as_ref();
let allow_unimplemented = std::env::var_os("PERRY_ALLOW_UNIMPLEMENTED").is_some();
if !allow_unimplemented
&& perry_api_manifest::module_has_any_entries(module)
&& perry_api_manifest::module_has_symbol(module, method).is_none()
{
let hint = super::unimpl_hints::module_member_hint(module, method)
.map(|h| format!(" {h}"))
.unwrap_or_default();
let msg = format!(
"`{}.{}` is not implemented in Perry — see `perry --print-api-manifest` for the supported surface, \
or set `PERRY_ALLOW_UNIMPLEMENTED=1` to ignore. (#463){}",
module, method, hint,
);
crate::lower_bail!(call.span, "{}", msg);
}
}
}
}
}
let callee_expr = lower_expr(ctx, expr)?;
Wait, but this is after args are lowered. Let me find the exact location in the call lowering code.
Looking at the code again, I see that the fall-through tail starts around line 320 (in the truncated output). Let me look at the context around lower_expr for the callee.
Actually, I think I should look for the pattern where it lowers the callee in the generic case. Let me search for it.
I see it now. After all the special cases (try_*_methods), there's a section:
// ---------------------------------------------------------------
// Fall-through tail: lower the callee, fill in default arguments
// for known callees, fold namespace-function calls into
// StaticMethodCall, and emit Call / CallSpread.
// ---------------------------------------------------------------
let callee_expr = lower_expr(ctx, expr)?;
This is where I should add the check. But I need to be careful because this is inside a match on call.callee. Let me look at the context.
Looking at the code, I see that this fall-through is inside a match on &call.callee:
if let ast::Callee::Expr(callee_expr) = &call.callee {
let expr = unwrap_call_callee_ts_wrappers(callee_expr.as_ref());
// ... various try_* handlers ...
// ---------------------------------------------------------------
// Fall-through tail: lower the callee, fill in default arguments
// for known callees, fold namespace-function calls into
// StaticMethodCall, and emit Call / CallSpread.
// ---------------------------------------------------------------
let callee_expr = lower_expr(ctx, expr)?;
So callee_expr (the AST) is available, and expr is the unwrapped version. I should add the check right before lower_expr(ctx, expr).
But wait, I need to be careful. The expr is already unwrapped from TS wrappers, but it might still be a MemberExpr wrapped in a Paren or other node. Let me check what unwrap_call_callee_ts_wrappers does.
Looking at the function, it unwraps TsAs, TsNonNull, TsSatisfies, TsTypeAssertion, TsConstAssertion, and Paren. So after unwrapping, expr should be the core expression.
If the callee is fs.notARealMethod, then expr should be a MemberExpr. I can check if it's a MemberExpr and then check if the object is a native module namespace.
Let me write the code:
// Check for unsupported native module method calls (#463 followup)
// If the callee is a member expression on a native module namespace, and the
// method is not in the manifest, emit an error. This preserves the helpful
// compile-time error for calls while allowing property reads to return undefined.
if let ast::Expr::Member(member_expr) = expr {
if let ast::MemberProp::Ident(method_ident) = &member_expr.prop {
// Check if the object is a native module namespace
// We need to handle the case where the object might be wrapped in TS casts
let obj = unwrap_call_callee_ts_wrappers(member_expr.obj.as_ref());
if let ast::Expr::Ident(obj_ident) = obj {
if let Some((module, None)) = ctx.lookup_native_module(obj_ident.sym.as_ref()) {
let method = method_ident.sym.as_ref();
let allow_unimplemented = std::env::var_os("PERRY_ALLOW_UNIMPLEMENTED").is_some();
if !allow_unimplemented
&& perry_api_manifest::module_has_any_entries(module)
&& perry_api_manifest::module_has_symbol(module, method).is_none()
{
let hint = super::unimpl_hints::module_member_hint(module, method)
.map(|h| format!(" {h}"))
.unwrap_or_default();
let msg = format!(
"`{}.{}` is not implemented in Perry — see `perry --print-api-manifest` for the supported surface, \
or set `PERRY_ALLOW_UNIMPLEMENTED=1` to ignore. (#463){}",
module, method, hint,
);
crate::lower_bail!(call.span, "{}", msg);
}
}
}
}
}
Wait, but there's a problem. The unwrap_call_callee_ts_wrappers function is defined in this module, so I can use it. But I need to make sure I'm checking the right thing.
Actually, looking at the code in lower_member_inner, I see that it already handles TS casts when checking obj_is_named_import. So I should do something similar here.
Let me simplify and use the same pattern as in lower_member_inner:
// Check for unsupported native module method calls (#463 followup)
// If the callee is a member expression on a native module namespace, and the
// method is not in the manifest, emit an error. This preserves the helpful
// compile-time error for calls while allowing property reads to return undefined.
if let ast::Callee::Expr(callee_expr) = &call.callee {
let expr = unwrap_call_callee_ts_wrappers(callee_expr.as_ref());
if let ast::Expr::Member(member_expr) = expr {
if let ast::MemberProp::Ident(method_ident) = &member_expr.prop {
// Check if the object is a native module namespace
let obj = member_expr.obj.as_ref();
let is_namespace_import = match obj {
ast::Expr::Ident(obj_ident) => matches!(
ctx.lookup_native_module(obj_ident.sym.as_ref()),
Some((_, None))
),
ast::Expr::TsAs(ts_as) => match ts_as.expr.as_ref() {
ast::Expr::Ident(obj_ident) => matches!(
ctx.lookup_native_module(obj_ident.sym.as_ref()),
Some((_, None))
),
_ => false,
},
ast::Expr::TsNonNull(ts_nn) => match ts_nn.expr.as_ref() {
ast::Expr::Ident(obj_ident) => matches!(
ctx.lookup_native_module(obj_ident.sym.as_ref()),
Some((_, None))
),
_ => false,
},
ast::Expr::TsTypeAssertion(ts_ta) => match ts_ta.expr.as_ref() {
ast::Expr::Ident(obj_ident) => matches!(
ctx.lookup_native_module(obj_ident.sym.as_ref()),
Some((_, None))
),
_ => false,
},
ast::Expr::Paren(paren) => match paren.expr.as_ref() {
ast::Expr::Ident(obj_ident) => matches!(
ctx.lookup_native_module(obj_ident.sym.as_ref()),
Some((_, None))
),
ast::Expr::TsAs(ts_as) => match ts_as.expr.as_ref() {
ast::Expr::Ident(obj_ident) => matches!(
ctx.lookup_native_module(obj_ident.sym.as_ref()),
Some((_, None))
),
_ => false,
},
_ => false,
},
_ => false,
};
if is_namespace_import {
let method = method_ident.sym.as_ref();
// We need to get the module name
// ...
}
}
}
}
Hmm, this is getting complicated. Let me think of a simpler approach.
Actually, looking at the code in lower_member_inner, it extracts the module name like this:
if let (Expr::NativeModuleRef(module), ast::MemberProp::Ident(prop_ident)) =
(&*object, &member.prop)
{
let prop = prop_ident.sym.as_ref();
// ...
}
So it checks if the lowered object is a NativeModuleRef. This is after the object has been lowered.
For the call check, I should do a similar check, but on the AST level, before lowering the callee. I can check if the callee's object is an Ident that maps to a native module namespace.
Actually, I think I can simplify this by using a helper function. But for now, let me write the code directly.
Wait, I realize that I should also consider the case where the callee is a nested member expression, like path.posix.join(). In that case, the callee is path.posix.join, and the object is path.posix, which is itself a member expression. So I need to handle that case too.
But looking at the code in lower_call_inner, I see that nested namespace calls are handled by try_path_subnamespace. So by the time we reach the fall-through tail, nested namespace calls should have already been handled.
So I think I only need to handle the simple case where the callee is a member expression with an Ident object.
Let me write the code. I'll add a helper function to extract the module name and method name from a member expression callee.
Actually, thinking about it more, I think the simplest approach is:
- In
lower_member_inner, change the gate to not error, but just emit Expr::Undefined directly for absent properties. This way, the property read evaluates to undefined immediately.
- In
lower_call_inner, add a check before lowering the callee to see if it's a member expression on a native module namespace with an absent method, and error if so.
Wait, but if I emit Expr::Undefined in lower_member_inner, then the PropertyGet expression is never emitted. This might break other things, like the runtime behavior.
Let me re-read the issue:
With the gate bypassed, Perry's runtime output matches Node for this fixture, including the absent namespace properties
This suggests that if we just remove the gate (i.e., let the code pass through to emit PropertyGet), the runtime handles it correctly. So I should just remove the gate and let it emit PropertyGet.
OK so my plan is:
- In
lower_member_inner, remove the gate (or change it to not error) so that property reads of absent exports emit PropertyGet, which evaluates to undefined at runtime.
- In
lower_call_inner, add a check before lowering the callee to see if it's a member expression on a native module namespace with an absent method, and error if so.
Let me start by modifying lower_member_inner.
Actually, wait. I just realized something. The issue says:
Named imports for absent exports should remain rejected, matching Node's ESM behavior.
This is handled in the import lowering, not in the member expression lowering. So I don't need to worry about it here.
Also, the issue says:
A named import probe for the absent constants remains rejected in a way consistent with Node's missing named export behavior.
So I need to make sure that named imports of absent exports are still rejected. But this is a separate check, probably in the import lowering code.
OK so my plan is confirmed:
- In
lower_member_inner, remove the gate so that property reads of absent exports emit PropertyGet, which evaluates to undefined at runtime.
- In
lower_call_inner, add a check before lowering the callee to see if it's a member expression on a native module namespace with an absent method, and error if so.
Let me start implementing.
First, let me look at the exact code in lower_member_inner that needs to be modified.
The gate is around line 1811-1837:
if !allow_unimplemented
&& !obj_is_named_import
&& perry_api_manifest::module_has_any_entries(module)
&& perry_api_manifest::module_has_symbol(module, prop).is_none()
{
// error
}
I need to remove this gate. But wait, I should keep the gate for calls. But how do I know if this is a call?
Actually, I think the issue is that the gate is too broad. It rejects all property reads, including those that are just reads (not calls). The fix is to remove the gate here and add a more specific check in lower_call_inner for calls.
So I'll just remove the gate in lower_member_inner and add the check in lower_call_inner.
Let me look at the exact lines to modify.
Looking at the file, the gate starts around line 1811:
if !allow_unimplemented
&& !obj_is_named_import
&& perry_api_manifest::module_has_any_entries(module)
&& perry_api_manifest::module_has_symbol(module, prop).is_none()
{
// error
}
I need to remove this entire block. Let me see what lines to remove.
Looking at the code, I see that the gate is:
if !allow_unimplemented
&& !obj_is_named_import
&& perry_api_manifest::module_has_any_entries(module)
&& perry_api_manifest::module_has_symbol(module, prop).is_none()
{
// #925: when there's a known supported equivalent for this
// shape, append it to the error so the user doesn't have to
// grep through the manifest to find the replacement.
let hint = super::unimpl_hints::module_member_hint(module, prop)
.map(|h| format!(" {h}"))
.unwrap_or_default();
let msg = format!(
"`{}.{}` is not implemented in Perry — see `perry --print-api-manifest` for the supported surface, \
or set `PERRY_ALLOW_UNIMPLEMENTED=1` to ignore. (#463){}",
module, prop, hint,
);
// #2309: defer when tree-shaking (sink armed for this node_modules
// module); re-raised only if the module survives pruning.
if !crate::try_defer_refusal(msg.clone(), member.span.lo.0) {
crate::lower_bail!(member.span, "{}", msg);
}
}
This is the block I need to remove. Let me find the exact line numbers.
From the earlier output, I saw:
1813: && perry_api_manifest::module_has_symbol(module, prop).is_none()
So the condition is on line 1813. Let me find the start and end of the block.
From the earlier output:
- Line 1811-1813: the condition
- Line 1814-1833: the error handling
Actually, let me re-check. Looking at the earlier output:
if !allow_unimplemented
&& !obj_is_named_import
&& perry_api_manifest::module_has_any_entries(module)
&& perry_api_manifest::module_has_symbol(module, prop).is_none()
{
// #925: when there's a known supported equivalent for this
// shape, append it to the error so the user doesn't have to
// grep through the manifest to find the replacement.
let hint = super::unimpl_hints::module_member_hint(module, prop)
.map(|h| format!(" {h}"))
.unwrap_or_default();
let msg = format!(
"`{}.{}` is not implemented in Perry — see `perry --print-api-manifest` for the supported surface, \
or set `PERRY_ALLOW_UNIMPLEMENTED=1` to ignore. (#463){}",
module, prop, hint,
);
// #2309: defer when tree-shaking (sink armed for this node_modules
// module); re-raised only if the module survives pruning.
if !crate::try_defer_refusal(msg.clone(), member.span.lo.0) {
crate::lower_bail!(member.span, "{}", msg);
}
}
So the block starts at the if and ends at the closing }. I need to remove this entire block.
But wait, I should think about this more carefully. The issue says that property reads should return undefined, but calls should still error. If I remove the gate entirely, then both reads and calls will pass through, and calls will be handled in lower_call_inner.
So I should:
- Remove the gate in
lower_member_inner (so reads pass through)
- Add the gate in
lower_call_inner (so calls still error)
This is the approach I'll take.
Now, let me also think about what happens to the object_is_named_import check. Looking at the code, this check is to skip the gate for named imports, because for named imports, the member access is reading a property of the imported value, not the module namespace.
If I remove the gate, this check becomes irrelevant. So I can also remove the code that calculates obj_is_named_import.
But wait, looking at the code, I see that the gate is inside a larger block that checks if the object is a NativeModuleRef:
if let (Expr::NativeModuleRef(module), ast::MemberProp::Ident(prop_ident)) =
(&*object, &member.prop)
{
let prop = prop_ident.sym.as_ref();
let allow_unimplemented = std::env::var_os("PERRY_ALLOW_UNIMPLEMENTED").is_some();
// ... obj_is_named_import calculation ...
// ... gate ...
}
If I remove the gate, the entire block becomes unnecessary, except for the object variable which is used later. So I should just remove the gate block, keeping the object variable assignment.
Wait, looking at the code, I see that object_expr is assigned before the gate, and then object is assigned:
let object = Box::new(object_expr);
// Unimplemented-API gate (#463). ...
if let (Expr::NativeModuleRef(module), ast::MemberProp::Ident(prop_ident)) =
(&*object, &member.prop)
{
// ...
}
match &member.prop {
ast::MemberProp::Ident(ident) => {
let property = ident.sym.to_string();
Ok(Expr::PropertyGet { object, property })
}
// ...
}
So the object variable is used in the PropertyGet expression. If I remove the gate block, the code still works because object is already assigned.
OK so my plan is:
- In
lower_member_inner, remove the gate block (lines 1811-1833 approximately).
- In
lower_call_inner, add a check before lowering the callee to see if it's a member expression on a native module namespace with an absent method, and error if so.
Let me implement this.
First, let me see the exact lines to remove in expr_member.rs.
Let me get the exact line numbers by looking at the file.
Let me look at the lines around 1800-1840.
I see the code now. Let me identify the exact lines to remove.
From the earlier output:
- Line 1811:
if !allow_unimplemented
- Line 1812:
&& !obj_is_named_import
- Line 1813:
&& perry_api_manifest::module_has_any_entries(module)
- Line 1814:
&& perry_api_manifest::module_has_symbol(module, prop).is_none()
- Line 1815:
{
- ... (error handling)
- Line 1833:
}
Wait, the line numbers might not be exact. Let me re-check.
From the earlier grep:
1813: && perry_api_manifest::module_has_symbol(module, prop).is_none()
So line 1813 is the last condition. Let me look at the surrounding lines.
From the earlier output, I saw:
if !allow_unimplemented
&& !obj_is_named_import
&& perry_api_manifest::module_has_any_entries(module)
&& perry_api_manifest::module_has_symbol(module, prop).is_none()
{
// #925: when there's a known supported equivalent for this
// shape, append it to the error so the user doesn't have to
// grep through the manifest to find the replacement.
let hint = super::unimpl_hints::module_member_hint(module, prop)
.map(|h| format!(" {h}"))
.unwrap_or_default();
let msg = format!(
"`{}.{}` is not implemented in Perry — see `perry --print-api-manifest` for the supported surface, \
or set `PERRY_ALLOW_UNIMPLEMENTED=1` to ignore. (#463){}",
module, prop, hint,
);
// #2309: defer when tree-shaking (sink armed for this node_modules
// module); re-raised only if the module survives pruning.
if !crate::try_defer_refusal(msg.clone(), member.span.lo.0) {
crate::lower_bail!(member.span, "{}", msg);
}
}
Let me use sed to look at the exact lines.
Actually, let me think about this differently. Instead of removing the gate entirely, I can modify it to not error but instead just skip the check. This way, the code flow remains the same, and the PropertyGet expression is still emitted.
Actually, I realize I can just comment out or remove the entire if block. Let me do that.
But wait, I should also remove the code that calculates obj_is_named_import, since it's only used by the gate. Let me check if it's used elsewhere.
Looking at the code, obj_is_named_import is only used in the gate condition. So if I remove the gate, I can also remove the code that calculates obj_is_named_import.
Let me also check if allow_unimplemented is used elsewhere. Looking at the code, it's only used in the gate condition. So I can remove it too.
But wait, allow_unimplemented is defined inside the if let (Expr::NativeModuleRef(module), ...) block. So if I remove the gate, I should also remove the allow_unimplemented definition.
Let me look at the entire block again:
if let (Expr::NativeModuleRef(module), ast::MemberProp::Ident(prop_ident)) =
(&*object, &member.prop)
{
let prop = prop_ident.sym.as_ref();
let allow_unimplemented = std::env::var_os("PERRY_ALLOW_UNIMPLEMENTED").is_some();
// Skip the gate when `member.obj` is an Ident that was a
// *named* import binding from the module (e.g. `import {
// EventEmitter } from "node:events"; EventEmitter.prototype`).
// ... (long comment) ...
let obj_is_named_import = match member.obj.as_ref() {
ast::Expr::Ident(obj_ident) => matches!(
ctx.lookup_native_module(obj_ident.sym.as_ref()),
Some((_, Some(_)))
),
// ... (handling TS casts) ...
_ => false,
};
if !allow_unimplemented
&& !obj_is_named_import
&& perry_api_manifest::module_has_any_entries(module)
&& perry_api_manifest::module_has_symbol(module, prop).is_none()
{
// error
}
}
If I remove the gate and the obj_is_named_import calculation, the block becomes:
if let (Expr::NativeModuleRef(module), ast::MemberProp::Ident(prop_ident)) =
(&*object, &member.prop)
{
// No-op: the gate for namespace property reads has been moved to
// lower_call_inner to distinguish reads (undefined) from calls (error).
}
But this is unnecessary code. I should just remove the entire if let block, since it no longer does anything.
Wait, but looking at the code, the if let block doesn't affect the rest of the function. After the block, the code continues with:
match &member.prop {
ast::MemberProp::Ident(ident) => {
let property = ident.sym.to_string();
Ok(Expr::PropertyGet { object, property })
}
// ...
}
So I can safely remove the entire if let block.
OK so my plan is:
- In
lower_member_inner, remove the entire if let (Expr::NativeModuleRef(module), ...) block.
- In
lower_call_inner, add a check before lowering the callee to see if it's a member expression on a native module namespace with an absent method, and error if so.
Let me implement this now.
First, let me get the exact line numbers for the block to remove.
I'll use grep to find the start of the block.
OK let me find the exact block to remove.
Good, now I can see that the block starts at line 1721 and ends at line 1837. But wait, the comment at line 1721 is part of the block. Let me look at the structure more carefully.
Looking at the code:
- Line 1720:
let object = Box::new(object_expr);
- Line 1722-1760: Comment explaining the gate
- Line 1761:
if let (Expr::NativeModuleRef(module), ast::MemberProp::Ident(prop_ident)) =
- Line 1762:
(&*object, &member.prop)
- Line 1763:
{
- ... (block content)
- Line 1837:
}
- Line 1839:
match &member.prop {
So the block is from line 1761 to 1837. I need to remove lines 1761-1837.
But wait, I should also remove the comment that explains the gate (lines 1722-1760). Let me check what the comment says.
Looking at the earlier output:
// Unimplemented-API gate (#463). When the receiver is a
// `NativeModuleRef("crypto")`-style import binding and the user is
// reading a named property, fail loudly if the manifest doesn't
// know about that property. The check is gated on the module
// having at least one entry in `API_MANIFEST`, so modules whose
// surface hasn't been enumerated yet (incremental coverage) keep
// working — adding entries to a module promotes it to strict mode
// automatically.
//
// Stubs (`stub: true` in the manifest) are NOT treated as
// unimplemented — those are intentional no-ops surfaced by #464's
// runtime first-call warning. The call only checks that
// `module_has_symbol` returns Some; the stub flag is consulted by
// the docs serializer, not by the gate.
//
// Escape hatch: setting `PERRY_ALLOW_UNIMPLEMENTED=1` skips the
// check entirely (downgrades to existing silent-undefined
// behavior). Useful when the manifest has a real gap that a
// followup will fix; documents the bypass instead of forcing an
// unrelated change in this PR.
This comment explains the gate. If I remove the gate, I should also remove or update this comment. Let me remove the comment as well.
So I need to remove lines 1722-1837.
Wait, let me double-check. Line 1720 is let object = Box::new(object_expr);, which I need to keep. So I should remove lines 1722-1837.
Actually, let me re-check. The comment starts at line 1722 and the if let starts at line 1761. So I should remove lines 1722-1837.
But wait, I should be careful about the blank line at line 1721. Let me check if there's a blank line between let object = Box::new(object_expr); and the comment.
From the earlier output, I see:
let object = Box::new(object_expr);
// Unimplemented-API gate (#463). ...
So there's a blank line at 1721. I should keep line 1720 (let object = Box::new(object_expr);) and the blank line, and remove the rest.
Actually, let me re-count. From the earlier output, I saw:
let object = Box::new(object_expr);
// Unimplemented-API gate (#463). When the receiver is a
So:
- Line 1720:
let object = Box::new(object_expr);
- Line 1721: (blank)
- Line 1722:
// Unimplemented-API gate (#463). ...
So I should remove lines 1722-1837, keeping lines 1-1721 and 1838 onwards.
But wait, line 1721 is a blank line, and line 1838 is also likely a blank line. So after removal, there would be two consecutive blank lines. I should keep one blank line for readability.
Let me just remove lines 1722-1837 and see what happens. I can adjust later if needed.
Actually, let me reconsider. The issue says:
Obvious unsupported API calls should still get the helpful Perry unimplemented-API error; this issue should not silently allow fs.notARealMethod()-style call gaps unless the runtime has a deliberate JS-compatible fallback.
So I need to add the check in lower_call_inner. Let me think about where to add it.
Looking at lower_call_inner, after all the special cases, there's the fall-through tail where lower_expr is called on the callee. I should add the check right before lower_expr.
But I need to be careful. The expr variable is the unwrapped callee expression. I should check if it's a member expression on a native module namespace, and if so, check if the method is in the manifest.
Let me write the code:
// Check for unsupported native module method calls (#463 followup).
// When the callee is a namespace property read on a strict native module, and
// the method is not in the manifest, emit the helpful unimplemented-API error.
// This preserves the compile-time error for calls while allowing property reads
// (which return `undefined`) to pass through in `lower_member_inner`.
if let ast::Callee::Expr(callee_ast) = &call.callee {
let unwrapped = unwrap_call_callee_ts_wrappers(callee_ast.as_ref());
if let ast::Expr::Member(member_expr) = unwrapped {
if let ast::MemberProp::Ident(method_ident) = &member_expr.prop {
let method = method_ident.sym.as_ref();
// Check if the object is a namespace import of a native module.
// We need to peel TS casts from the object to find the Ident.
let obj_expr = member_expr.obj.as_ref();
if let Some((module, None)) = extract_native_module_ns_from_expr(ctx, obj_expr) {
let allow_unimplemented = std::env::var_os("PERRY_ALLOW_UNIMPLEMENTED").is_some();
if !allow_unimplemented
&& perry_api_manifest::module_has_any_entries(module)
&& perry_api_manifest::module_has_symbol(module, method).is_none()
{
let hint = super::unimpl_hints::module_member_hint(module, method)
.map(|h| format!(" {h}"))
.unwrap_or_default();
let msg = format!(
"`{}.{}` is not implemented in Perry — see `perry --print-api-manifest` for the supported surface, \
or set `PERRY_ALLOW_UNIMPLEMENTED=1` to ignore. (#463){}",
module, method, hint,
);
crate::lower_bail!(call.span, "{}", msg);
}
}
}
}
}
But I need to define the extract_native_module_ns_from_expr helper. Or I can inline the logic.
Looking at the code in lower_member_inner, I see that it uses a match to peel TS casts:
let obj_is_named_import = match member.obj.as_ref() {
ast::Expr::Ident(obj_ident) => matches!(
ctx.lookup_native_module(obj_ident.sym.as_ref()),
Some((_, Some(_)))
),
ast::Expr::TsAs(ts_as) => match ts_as.expr.as_ref() {
ast::Expr::Ident(obj_ident) => matches!(
ctx.lookup_native_module(obj_ident.sym.as_ref()),
Some((_, Some(_)))
),
_ => false,
},
// ...
_ => false,
};
For my case, I need to check if it's a namespace import (not a named import). So I should check for Some((_, None)) instead of Some((_, Some(_))).
Let me write a helper function or inline the logic.
Actually, I think I can just inline the logic:
// Check if the object is a namespace import of a native module.
let is_ns_import = match member_expr.obj.as_ref() {
ast::Expr::Ident(obj_ident) => matches!(
ctx.lookup_native_module(obj_ident.sym.as_ref()),
Some((_, None))
),
ast::Expr::TsAs(ts_as) => match ts_as.expr.as_ref() {
ast::Expr::Ident(obj_ident) => matches!(
ctx.lookup_native_module(obj_ident.sym.as_ref()),
Some((_, None))
),
_ => false,
},
ast::Expr::TsNonNull(ts_nn) => match ts_nn.expr.as_ref() {
ast::Expr::Ident(obj_ident) => matches!(
ctx.lookup_native_module(obj_ident.sym.as_ref()),
Some((_, None))
),
_ => false,
},
ast::Expr::TsTypeAssertion(ts_ta) => match ts_ta.expr.as_ref() {
ast::Expr::Ident(obj_ident) => matches!(
ctx.lookup_native_module(obj_ident.sym.as_ref()),
Some((_, None))
),
_ => false,
},
ast::Expr::Paren(paren) => match paren.expr.as_ref() {
ast::Expr::Ident(obj_ident) => matches!(
ctx.lookup_native_module(obj_ident.sym.as_ref()),
Some((_, None))
),
ast::Expr::TsAs(ts_as) => match ts_as.expr.as_ref() {
ast::Expr::Ident(obj_ident) => matches!(
ctx.lookup_native_module(obj_ident.sym.as_ref()),
Some((_, None))
),
_ => false,
},
_ => false,
},
_ => false,
};
if is_ns_import {
// Extract the module name and check if the method is in the manifest.
let (module, method) = match member_expr.obj.as_ref() {
ast::Expr::Ident(obj_ident) => {
if let Some((m, None)) = ctx.lookup_native_module(obj_ident.sym.as_ref()) {
(m.to_string(), method_ident.sym.as_ref())
} else {
continue; // Not a namespace import, skip
}
}
// ... (handle TS casts)
};
// ...
}
Hmm, this is getting complicated. Let me think of a simpler approach.
Actually, I can extract the module name by calling ctx.lookup_native_module and then using the returned module name. Let me write a helper function:
fn extract_native_module_from_expr(ctx: &LoweringContext, expr: &ast::Expr) -> Option<String> {
match expr {
ast::Expr::Ident(obj_ident) => {
if let Some((module, None)) = ctx.lookup_native_module(obj_ident.sym.as_ref()) {
Some(module.to_string())
} else {
None
}
}
ast::Expr::TsAs(ts_as) => extract_native_module_from_expr(ctx, ts_as.expr.as_ref()),
ast::Expr::TsNonNull(ts_nn) => extract_native_module_from_expr(ctx, ts_nn.expr.as_ref()),
ast::Expr::TsTypeAssertion(ts_ta) => extract_native_module_from_expr(ctx, ts_ta.expr.as_ref()),
ast::Expr::Paren(paren) => extract_native_module_from_expr(ctx, paren.expr.as_ref()),
_ => None,
}
}
This function recursively peels TS casts and parentheses, and returns the module name if the expression is a namespace import of a native module.
Let me use this helper in the call check:
// Check for unsupported native module method calls (#463 followup).
if let ast::Callee::Expr(callee_ast) = &call.callee {
let unwrapped = unwrap_call_callee_ts_wrappers(callee_ast.as_ref());
if let ast::Expr::Member(member_expr) = unwrapped {
if let ast::MemberProp::Ident(method_ident) = &member_expr.prop {
if let Some(module) = extract_native_module_from_expr(ctx, member_expr.obj.as_ref()) {
let method = method_ident.sym.as_ref();
let allow_unimplemented = std::env::var_os("PERRY_ALLOW_UNIMPLEMENTED").is_some();
if !allow_unimplemented
&& perry_api_manifest::module_has_any_entries(&module)
&& perry_api_manifest::module_has_symbol(&module, method).is_none()
{
let hint = super::unimpl_hints::module_member_hint(&module, method)
.map(|h| format!(" {h}"))
.unwrap_or_default();
let msg = format!(
"`{}.{}` is not implemented in Perry — see `perry --print-api-manifest` for the supported surface, \
or set `PERRY_ALLOW_UNIMPLEMENTED=1` to ignore. (#463){}",
module, method, hint,
);
crate::lower_bail!(call.span, "{}", msg);
}
}
}
}
}
OK now let me implement this. I'll:
- Add the helper function
extract_native_module_from_expr in expr_call/mod.rs.
- Add the call check before
lower_expr(ctx, expr).
- Remove the gate in
expr_member.rs.
Let me start with the implementation.
First, let me modify expr_member.rs to remove the gate.
Let me find the exact lines to remove.
Good. So the block I need to remove is:
- Lines 1722-1837 (the comment and the
if let block)
Wait, let me look at the line numbers more carefully. From the sed output, I see:
- Line 1720:
let object = Box::new(object_expr);
- Line 1721: (blank line)
- Line 1722-1760: Comment
- Line 1761-1837:
if let block
- Line 1838: (blank line)
- Line 1839:
match &member.prop {
So I should remove lines 1722-1837. This keeps lines 1720-1721 and 1838-1839.
Actually, let me double-check. After removing lines 1722-1837, the file would look like:
- Line 1720:
let object = Box::new(object_expr);
- Line 1721: (blank line)
- Line 1722: (previously line 1838, which is blank)
- Line 1723: (previously line 1839,
match &member.prop {)
So there would be two blank lines. I should remove one of them. Let me remove line 1722-1838 instead, which would leave:
- Line 1720:
let object = Box::new(object_expr);
- Line 1721: (blank line)
- Line 1722:
match &member.prop {
This looks good.
Actually, wait. Let me re-read the sed output. Looking at the output, I see:
1838-
1839- match &member.prop {
So line 1838 is a blank line (the - at the beginning indicates it was removed). And line 1839 is match &member.prop {.
So if I remove lines 1722-1837, I get:
- Line 1720:
let object = Box::new(object_expr);
- Line 1721: (blank)
- Line 1722: (previously 1838, blank)
- Line 1723: (previously 1839,
match &member.prop {)
To avoid two blank lines, I should remove lines 1722-1838. But wait, that would remove the blank line at 1838, leaving:
- Line 1720:
let object = Box::new(object_expr);
- Line 1721: (blank)
- Line 1722: (previously 1839,
match &member.prop {)
This looks good. So I'll remove lines 1722-1838.
Actually, let me re-check the sed output. The output shows lines being removed (prefixed with -). Let me look at the context:
1837- }
1838-
1839- match &member.prop {
So:
- Line 1837:
} (closing the if let block)
- Line 1838: (blank)
- Line 1839:
match &member.prop {
If I remove lines 1722-1837, the result is:
let object = Box::new(object_expr);
match &member.prop {
This looks good. There's one blank line between let object = ... and match &member.prop {.
Wait, but line 1721 is also blank. So the result is:
let object = Box::new(object_expr);
match &member.prop {
Hmm, there are two blank lines (1721 and 1838). Let me remove lines 1722-1838 to get one blank line.
Actually, looking at the original code, I see:
let object = Box::new(object_expr);
// Unimplemented-API gate (#463). ...
So line 1720 is let object = Box::new(object_expr); and line 1721 is blank. After the block, line 1838 is also blank.
If I remove lines 1722-1837, I get:
let object = Box::new(object_expr);
match &member.prop {
Wait, that's only if I remove 1722-1837. But line 1721 is blank, and line 1838 is blank. So after removal:
- Line 1720:
let object = Box::new(object_expr);
- Line 1721: (blank)
- Line 1722: (previously 1838, blank)
- Line 1723: (previously 1839,
match &member.prop {)
So there are two consecutive blank lines. To fix this, I should remove lines 1722-1838:
- Line 1720:
let object = Box::new(object_expr);
- Line 1721: (blank)
- Line 1722: (previously 1839,
match &member.prop {)
This has one blank line, which is good.
OK so I'll remove lines 1722-1838.
Now let me implement this.
First, let me check if there's a blank line at 1838.
Good, so line 1838 is blank. Let me remove lines 1722-1838.
Now let me use edit to remove the block. I'll replace lines 1722-1838 with an empty string.
Actually, the edit command requires replacement text. Let me replace the block with nothing, or with a blank line to maintain spacing.
Looking at the original code:
let object = Box::new(object_expr);
// Unimplemented-API gate (#463). ...
...
}
match &member.prop {
After removal, I want:
let object = Box::new(object_expr);
match &member.prop {
So I need to keep the blank line at 1721 and remove the rest. Let me remove lines 1722-1838 (inclusive).
Now let me do the edit.
Good, I've removed the gate block from expr_member.rs. Now I need to add the call check in expr_call/mod.rs.
Let me find the exact location in expr_call/mod.rs where I need to add the check. Looking at the fall-through tail:
// ---------------------------------------------------------------
// Fall-through tail: lower the callee, fill in default arguments
// for known callees, fold namespace-function calls into
// StaticMethodCall, and emit Call / CallSpread.
// ---------------------------------------------------------------
let callee_expr = lower_expr(ctx, expr)?;
I should add the check right before let callee_expr = lower_expr(ctx, expr)?;.
Let me find the exact line number.
Good, the lower_expr call is at line 348. I need to add the check before this line.
Now let me write the code. First, I need to add the helper function. Let me add it at the top of the file, near the other helper functions.
Actually, looking at the file, I see there's already a helper function unwrap_call_callee_ts_wrappers. I should add my helper function near it.
Let me add the helper function after unwrap_call_callee_ts_wrappers.
Good, I've added the helper function. Now let me add the call check before lower_expr(ctx, expr).
Let me replace the comment and the lower_expr call with the check and then the lower_expr call.
Good, I've added the call check. Now let me build the project to see if it compiles.
Good, the project compiles successfully. Now let me test if the fix works by running the check command on the DNS constants fixture.
Perfect! The check passes now. Let me also run the fixture to make sure it produces the expected output.
Let me compare with the Node.js output to make sure they match. According to the issue, the expected output for absent properties is undefined.
Let me also verify that calls on absent methods still get an error. Let me create a test file.
Good! The call on an absent method still gets the helpful error message. Now let me verify that named imports of absent exports are still rejected.
Great! Named imports of absent exports are still rejected, which is the expected behavior.
Now let me run the existing tests to make sure I didn't break anything.
The tests pass. Let me also run a broader set of tests to make sure everything is working.
Good, the tests pass. Let me also verify that the DNS constants fixture produces the expected output by running it.
Perfect! The output matches the expected behavior:
- Absent properties (
ADDRCONFIG, V4MAPPED, ALL) return undefined
- Present properties (
NODATA, CANCELLED) return their values
Let me also verify that the output matches what the issue describes.
Excellent! The output matches the expected behavior from the issue:
dnsPromises.ADDRCONFIG: undefined
dnsPromises.V4MAPPED: undefined
dnsPromises.ALL: undefined
named aliases: ENODATA ECANCELLED
Now let me also make sure that calls on absent methods still get an error. Let me test this.
Perfect! The call on an absent method (dnsPromises.notARealMethod()) still gets the helpful error message.
Let me also test that property reads on absent methods (without calling them) work correctly.
Good, the property read without calling works and returns undefined.
Let me also verify that the fix handles edge cases, such as nested member expressions.
Let me clean up the test files I created.
Good, I've cleaned up the test files. Now let me review my changes and make sure they're correct.
Let me check the git diff to see my changes.
Perfect! Let me summarize my changes:
-
In crates/perry-hir/src/lower/expr_member.rs:
- Removed the unimplemented-API gate that was rejecting namespace property reads of absent exports.
- This allows property reads like
dnsPromises.ADDRCONFIG to compile and evaluate to undefined at runtime, matching Node.js behavior.
-
In crates/perry-hir/src/lower/expr_call/mod.rs:
- Added a helper function
extract_native_module_ns_from_expr to extract the native module name from an expression, peeling TS casts and parentheses.
- Added a call check before lowering the callee. If the callee is a member expression on a native module namespace and the method is not in the manifest, emit the helpful unimplemented-API error.
- This preserves the compile-time error for calls on absent methods while allowing property reads to pass through.
These changes satisfy all the acceptance criteria:
- ✅
target/release/perry check test-parity/node-suite/dns/constants/error-aliases.ts passes without PERRY_ALLOW_UNIMPLEMENTED=1.
- ✅ The DNS constants fixture prints the same shape as Node for absent namespace reads (
undefined) and present DNS error aliases (NODATA, CANCELLED, etc.).
- ✅ Named imports for absent constants remain rejected (consistent with Node's missing named export behavior).
- ✅ The strict unimplemented-API gate still catches actual unsupported builtin method usage (calls on absent methods still get the error).
Let me now submit my changes.
Actually, let me first verify once more that all tests pass.
Great, all tests pass. Let me submit my changes.
Actually, let me first review the checklist:
Let me go through each item:
-
Reproduced the error: Yes, I ran target/release/perry check test-parity/node-suite/dns/constants/error-aliases.ts and confirmed the error.
-
Confirmed the fix resolves the error: Yes, after my fix, the check passes without PERRY_ALLOW_UNIMPLEMENTED=1.
-
Thought about edge cases: Yes, I tested:
- Property reads of absent exports (return
undefined)
- Calls on absent methods (still get error)
- Named imports of absent exports (still rejected)
- Present exports work correctly
-
Ran existing tests: Yes, I ran cargo test and all tests pass.
I'm confident that my fix is correct and complete. Let me submit.
Wait, I should also verify that the --test-release tests pass, since the issue mentions running the release build.
Good, the release tests pass. Let me also run a quick sanity check on the cargo test output to make sure no tests failed.
Great, all tests pass. Let me submit my changes.
Hmm, actually looking at the output, I see test result: ok. 0 passed; 0 failed which means no tests ran. Let me run the tests without the release filter.
I see that there are many tests passing. The output shows 351 passed; 0 failed. Let me scroll up to see if there are any failures.
Actually, the output is truncated. Let me run the tests again and check for failures.
Actually, looking at the output, I see test result: ok. 351 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 16.32s. This means all 351 tests passed, which is great.
Let me also verify that the DNS constants fixture runs correctly with the release build.
I see the output is truncated. Let me check if there are any errors.
Great! The output shows that the DNS constants fixture runs correctly, with absent properties returning undefined and present properties returning their values.
Now let me submit my changes.
Actually, let me first verify one more time that my changes are correct by looking at the git diff.
Perfect! My changes look correct. Let me summarize:
-
crates/perry-hir/src/lower/expr_member.rs: Removed the unimplemented-API gate (lines 1722-1838 in the original file) that was rejecting namespace property reads of absent exports. This allows property reads to pass through and emit PropertyGet expressions, which evaluate to undefined at runtime.
-
crates/perry-hir/src/lower/expr_call/mod.rs: Added a helper function extract_native_module_ns_from_expr (lines 97-112) to extract the native module name from an expression, and added a call check (lines 348-376) before lowering the callee. If the callee is a member expression on a native module namespace and the method is not in the manifest, emit the helpful unimplemented-API error. This preserves the compile-time error for calls while allowing property reads to pass through.
These changes satisfy all the acceptance criteria from the issue. Let me submit.