Skip to content

Commit

Permalink
fix: suppress experimental warning for node 23
Browse files Browse the repository at this point in the history
  • Loading branch information
nirinchev committed Nov 12, 2024
1 parent c29b2fe commit cfbd54b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/smoke-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: "Run Smoke Tests"

on:
push:
branches:
- main
pull_request:
schedule:
- cron: "0 0 * * *"

jobs:
smoke-tests:
strategy:
matrix:
runner: [ubuntu-latest, macos-latest, windows-latest]
node: [20.x, 22.x, 23.x]
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm run test-smoke
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"test-evergreen-expansions": "bash .evergreen/compilation-context-expansions.test.sh",
"replace-package": "node scripts/replace-package.js",
"test-nodedriver": "bash .evergreen/test-node-driver.sh",
"pretest-smoke": "npm run compile-cli",
"test-smoke": "npm run test-smoke -w @mongosh/cli-repl",
"compile": "npm run compile --workspaces --if-present",
"compile-cli": "lerna run compile --scope @mongosh/cli-repl --include-dependencies",
"prestart-cli": "npm run compile-cli",
Expand Down Expand Up @@ -167,4 +169,4 @@
"overrides": {
"cookie": "^0.7.2"
}
}
}
1 change: 1 addition & 0 deletions packages/cli-repl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"test-coverage": "nyc --no-clean --cwd ../.. --reporter=none npm run test",
"test-ci-coverage": "nyc --no-clean --cwd ../.. --reporter=none npm run test-ci",
"test-apistrict-ci": "cross-env MONGOSH_TEST_FORCE_API_STRICT=1 npm run test-ci",
"test-smoke": "node bin/mongosh.js --smokeTests",
"eslint": "eslint",
"lint": "npm run eslint . && npm run prettier -- --check .",
"check": "npm run lint && npm run depcheck",
Expand Down
30 changes: 30 additions & 0 deletions packages/cli-repl/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ if ((v8 as any)?.startupSnapshot?.isBuildingSnapshot?.()) {
// eslint-disable-next-line complexity
async function main() {
markTime(TimingCategories.Main, 'entered main');
suppressExperimentalWarnings();
if (process.env.MONGOSH_RUN_NODE_SCRIPT) {
// For uncompiled mongosh: node /path/to/this/file script ... -> node script ...
// FOr compiled mongosh: mongosh mongosh script ... -> mongosh script ...
Expand Down Expand Up @@ -311,3 +312,32 @@ async function ask(prompt: string): Promise<string> {
process.stdin.unpipe(stdinCopy);
}
}

/**
* Helper to suppress experimental warnings emitted by node if necessary.
*
* In Node.js 23 require()ing ESM modules will work, but emit an experimental warning like
* CommonJS module ABC is loading ES Module XYZ using require(). This is causing problems for
* the way we import fetch - see relevant comments here:
* https://github.com/mongodb-js/devtools-shared/blob/29ceeb5f51d29883d4a69c83e68ad37b0965d49e/packages/devtools-proxy-support/src/fetch.ts#L12-L17
*/
function suppressExperimentalWarnings() {
const nodeMajorVersion = process.versions.node.split('.').map(Number)[0];
if (nodeMajorVersion >= 23) {
const originalEmit = process.emitWarning;
process.emitWarning = (warning, ...args: any[]): void => {
if (args[0] === 'ExperimentalWarning') {
return;
}

if (
typeof args[0] === 'object' &&
args[0].type === 'ExperimentalWarning'
) {
return;
}

originalEmit(warning, ...args);
};
}
}
5 changes: 3 additions & 2 deletions packages/cli-repl/src/smoke-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ async function runSmokeTest({
// eslint-disable-next-line
const { spawn } = require('child_process') as typeof import('child_process');
const proc = spawn(executable, [...args], {
stdio: ['pipe', 'pipe', includeStderr ? 'pipe' : 'inherit'],
stdio: 'pipe',
});
let stdout = '';
let stderr = '';
Expand All @@ -420,13 +420,14 @@ async function runSmokeTest({
input,
output,
stdout,
stderr,
stderr: includeStderr ? stderr : '',
executable,
actualExitCode,
args: args.map((arg) => redactURICredentials(arg)),
};
try {
assert.match(includeStderr ? `${stdout}\n${stderr}` : stdout, output);
assert.doesNotMatch(stderr, /ExperimentalWarning/);
if (exitCode !== undefined) {
assert.strictEqual(actualExitCode, exitCode);
}
Expand Down

0 comments on commit cfbd54b

Please sign in to comment.