-
-
Notifications
You must be signed in to change notification settings - Fork 34.6k
module: fix extensionless entry with explicit type=commonjs #61600
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| 'use strict'; | ||
|
|
||
| const test = require('node:test'); | ||
| const assert = require('node:assert/strict'); | ||
| const fs = require('node:fs'); | ||
| const path = require('node:path'); | ||
| const { spawnSync } = require('node:child_process'); | ||
|
|
||
| const tmpdir = require('../common/tmpdir'); | ||
|
|
||
| test('extensionless entry point with ESM syntax under type=commonjs should not silently exit 0', () => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to use node:test if there is only one test - it would unnecessarily clutter the CI output with nested tap (and if more tests are added they should be in separate files to avoid building test monoliths). |
||
| tmpdir.refresh(); | ||
|
|
||
| const dir = tmpdir.resolve('extensionless-esm-commonjs'); | ||
| fs.mkdirSync(dir, { recursive: true }); | ||
|
|
||
| // package.json with type: commonjs | ||
| fs.writeFileSync( | ||
| path.join(dir, 'package.json'), | ||
| '{\n "type": "commonjs"\n}\n', | ||
| 'utf8' | ||
| ); | ||
|
|
||
| // Extensionless executable with shebang + ESM syntax. | ||
| // NOTE: Execute via process.execPath to avoid PATH/env differences. | ||
| const scriptPath = path.join(dir, 'script'); // no extension | ||
| fs.writeFileSync( | ||
| scriptPath, | ||
| '#!/usr/bin/env node\n' + | ||
| "console.log('script STARTED')\n" + | ||
| "import { version } from 'node:process'\n" + | ||
| 'console.log(version)\n', | ||
| 'utf8' | ||
| ); | ||
| fs.chmodSync(scriptPath, 0o755); | ||
|
|
||
| const r = spawnSync(process.execPath, ['./script'], { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use spawnSyncAndAssert from test/common/child_process.js for this? Otherwise when the expectation is not met in the CI there won't be a lot of information about what's going on. |
||
| cwd: dir, | ||
| encoding: 'utf8' | ||
| }); | ||
|
|
||
| assert.ifError(r.error); | ||
|
|
||
| assert.notEqual( | ||
| r.status, | ||
| 0, | ||
| `unexpected exit code 0; stdout=${JSON.stringify(r.stdout)} stderr=${JSON.stringify(r.stderr)}` | ||
| ); | ||
| assert.ok(r.stderr && r.stderr.length > 0, 'expected stderr to contain an error message'); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
N/B: I think we should also allow passing the explicit "module" type field in package.json here.