Compare commits

...

1 Commits

Author SHA1 Message Date
Jarred Sumner
69557ddc5a feat(node): respect NODE_NO_WARNINGS 2025-05-29 22:43:38 -07:00
3 changed files with 83 additions and 0 deletions

View File

@@ -1379,6 +1379,27 @@ static bool isJSValueEqualToASCIILiteral(JSC::JSGlobalObject* globalObject, JSC:
return view == literal;
}
static bool processNoWarnings(JSC::JSGlobalObject* globalObject)
{
if (Bun__Node__ProcessNoWarnings)
return true;
static bool checked = false;
static bool result = false;
if (!checked) {
checked = true;
ZigString name = { reinterpret_cast<const unsigned char*>("NODE_NO_WARNINGS"), sizeof("NODE_NO_WARNINGS") - 1 };
ZigString value = { nullptr, 0 };
if (Bun__getEnvValue(globalObject, &name, &value)) {
if (value.len == 1 && value.ptr[0] == '1') {
result = true;
Bun__Node__ProcessNoWarnings = true;
}
}
}
return result;
}
extern "C" void Bun__Process__emitWarning(Zig::GlobalObject* globalObject, EncodedJSValue warning, EncodedJSValue type, EncodedJSValue code, EncodedJSValue ctor)
{
// ignoring return value -- emitWarning only ever returns undefined or throws
@@ -1397,6 +1418,10 @@ JSValue Process::emitWarningErrorInstance(JSC::JSGlobalObject* lexicalGlobalObje
auto scope = DECLARE_THROW_SCOPE(vm);
auto* process = globalObject->processObject();
if (processNoWarnings(globalObject)) {
return jsUndefined();
}
auto warningName = errorInstance.get(lexicalGlobalObject, vm.propertyNames->name);
RETURN_IF_EXCEPTION(scope, {});
if (isJSValueEqualToASCIILiteral(globalObject, warningName, "DeprecationWarning"_s)) {
@@ -1426,6 +1451,10 @@ JSValue Process::emitWarning(JSC::JSGlobalObject* lexicalGlobalObject, JSValue w
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue detail = jsUndefined();
if (processNoWarnings(globalObject)) {
return jsUndefined();
}
if (Bun__Node__ProcessNoDeprecation && isJSValueEqualToASCIILiteral(globalObject, type, "DeprecationWarning"_s)) {
return jsUndefined();
}
@@ -3309,6 +3338,17 @@ JSC_DEFINE_CUSTOM_SETTER(setProcessNoDeprecation, (JSC::JSGlobalObject * globalO
return true;
}
JSC_DEFINE_CUSTOM_GETTER(processNoProcessWarnings, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName name))
{
return JSValue::encode(jsBoolean(Bun__Node__ProcessNoWarnings));
}
JSC_DEFINE_CUSTOM_SETTER(setProcessNoProcessWarnings, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::EncodedJSValue encodedValue, JSC::PropertyName))
{
Bun__Node__ProcessNoWarnings = JSC::JSValue::decode(encodedValue).toBoolean(globalObject);
return true;
}
static JSValue constructFeatures(VM& vm, JSObject* processObject)
{
// {
@@ -3649,6 +3689,8 @@ extern "C" void Process__emitErrorEvent(Zig::GlobalObject* global, EncodedJSValu
moduleLoadList Process_stubEmptyArray PropertyCallback
nextTick constructProcessNextTickFn PropertyCallback
noDeprecation processNoDeprecation CustomAccessor
noProcessWarnings processNoProcessWarnings
CustomAccessor
openStdin Process_functionOpenStdin Function 0
pid constructPid PropertyCallback
platform constructPlatform PropertyCallback

View File

@@ -39,6 +39,7 @@ const OOM = bun.OOM;
export var Bun__Node__ZeroFillBuffers = false;
export var Bun__Node__ProcessNoDeprecation = false;
export var Bun__Node__ProcessThrowDeprecation = false;
export var Bun__Node__ProcessNoWarnings = false;
pub var Bun__Node__ProcessTitle: ?string = null;

View File

@@ -0,0 +1,40 @@
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');
if (process.argv[2] === 'child') {
process.emitWarning('foo');
} else {
function test(newEnv) {
const [cmd, opts] = common.escapePOSIXShell`"${process.execPath}" "${__filename}" child`;
cp.exec(cmd, { ...opts, env: { ...opts?.env, ...newEnv } }, common.mustCall((err, stdout, stderr) => {
assert.strictEqual(err, null);
assert.strictEqual(stdout, '');
if (newEnv.NODE_NO_WARNINGS === '1')
assert.strictEqual(stderr, '');
else
assert.match(stderr.trim(), /Warning: foo\n/);
}));
}
test({});
test(process.env);
test({ NODE_NO_WARNINGS: undefined });
test({ NODE_NO_WARNINGS: null });
test({ NODE_NO_WARNINGS: 'foo' });
test({ NODE_NO_WARNINGS: true });
test({ NODE_NO_WARNINGS: false });
test({ NODE_NO_WARNINGS: {} });
test({ NODE_NO_WARNINGS: [] });
test({ NODE_NO_WARNINGS: function() {} });
test({ NODE_NO_WARNINGS: 0 });
test({ NODE_NO_WARNINGS: -1 });
test({ NODE_NO_WARNINGS: '0' });
test({ NODE_NO_WARNINGS: '01' });
test({ NODE_NO_WARNINGS: '2' });
// Don't test the number 1 because it will come through as a string in the
// child process environment.
test({ NODE_NO_WARNINGS: '1' });
}