-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
util: possibly confusing util.inspect() output #15474
Comments
More complicated case: 'use strict';
function autoVivify(obj) {
return new Proxy(obj, {
get(target, property) {
if (target[property] === undefined) target[property] = autoVivify({});
return target[property];
},
});
}
const object = {};
const proxy = autoVivify(object);
proxy.a.b.c = 'I am alive!';
console.log(proxy.a.b.c);
console.log(object.a.b.c);
console.log(object);
console.log(proxy); I am alive!
I am alive!
{ a:
{ b: { c: 'I am alive!', [Symbol(util.inspect.custom)]: [Object] },
[Symbol(util.inspect.custom)]: { [Symbol(util.inspect.custom)]: [Object] } } }
{ a:
{ b:
{ c: 'I am alive!',
[Symbol(util.inspect.custom)]: [Object],
[Symbol(Symbol.toStringTag)]: [Object] },
[Symbol(util.inspect.custom)]:
{ [Symbol(util.inspect.custom)]: [Object],
[Symbol(Symbol.toStringTag)]: [Object] },
[Symbol(Symbol.toStringTag)]: { [Symbol(util.inspect.custom)]: [Object] } },
[Symbol(util.inspect.custom)]: { [Symbol(util.inspect.custom)]: { [Symbol(util.inspect.custom)]: [Object] } } } |
I do not think that this is intended. It changed from 7 -> 8 because of #9726. But showing symbols is not the issue here out of my perspective but that the I plan on having a look at this. |
IMHO the issue is that Line 420 in 3c65a83
P.S. @vsemozhetbyt autovivification with proxies is tricky exactly because of this - frameworks, and language features test for fields on the proxy and trigger vivification. When I use this pattern, I run it once or twice, see which fields are accessed, then filter them out in the proxy 🤷♂️ |
@refack you are absolutely right. There is little that can be done on utils side to improve the situation. Even though I think it is weird that |
@refack In addition, objects autovivified with proxies appear not to be serializable ( |
@BridgeAR we could do something less performant, but "safer": const maybeCustomInspect = customInspectSymbol in value ? value[customInspectSymbol] :
'inspect' in value ? value.inspect : null; @vsemozhetbyt you need to filter out function autoVivify(obj) {
return new Proxy(obj, {
get(target, property) {
if (property == 'toJSON') return;
if (property == util.inspect.custom) return;
if (!property in target) target[property] = autoVivify({});
return target[property];
},
});
} |
@refack is that really safer? It moves the check to a different trap and in this case the trap would not be called but I do not yet see a big advantage for this approach. |
@BridgeAR You're right. Actually I now think the inspection should go to a lower level V8 API, and not done in JS. |
I don’t think this is very different from e.g. when |
Well, after thinking about this further I guess everything is working as expected and also my comment about changing the default is probably not a good idea as it should probably "just work" when ever a user wants a individual inspection instead of having to explicitly activate it. Therefore I think there is little left to do here and I close the issue. If someone disagrees - please feel free to reopen. |
I am trying to make a simple object autovivification with Proxy:
I understand that this is an edge case and maybe the output is correct. But I feel somehow that I should report this in case it is not completely intended. Feel free to close if it is by design.
The text was updated successfully, but these errors were encountered: