Skip to content
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

registered load hook fails to print consecutive console.log #55307

Closed
iambumblehead opened this issue Oct 7, 2024 · 1 comment
Closed

registered load hook fails to print consecutive console.log #55307

iambumblehead opened this issue Oct 7, 2024 · 1 comment
Labels
invalid Issues and PRs that are invalid. wontfix Issues that will not be fixed.

Comments

@iambumblehead
Copy link

Version

v20.15.1

Platform

Linux lenovo-21bx 6.11.0 #1-lenovo-21bx SMP PREEMPT Thu Sep 26 11:38:13 UTC 2024 aarch64 Linux

Subsystem

No response

What steps will reproduce the bug?

run the attached file with: touch file.js && node mytest.js, observe console.log('1', { loadurl: url }) is printed to process output, but consecutive calls including console.log('2', { loadurl: url }) are never seen.

mytest.js
import { register } from 'node:module';
import assert from 'node:assert';

async function resolve(referrer, context, next) {
 const result = await next(referrer);
 const url = new URL(result.url)

 console.log({
   url: result.url,
   parentURL: context.parentURL,
   result
 })

 return result
}

async function load(url, context, next) {
 console.log('1', { loadurl: url })
 console.log('2', { loadurl: url })
 console.log('3', { loadurl: url })
 console.log('4', { loadurl: url })

 if (url.endsWith('file.js')) {
   return {
     shortCircuit: true,
     format: 'commonjs',
     responseURL: url,
     source: `
       const fs = require('fs')

       module.exports = (path) => {
         //fs.readFileSync(path, 'utf8')
       }`
    }
 }

 return next(url, context);
}

register(`data:text/javascript,export ${encodeURIComponent(resolve)};export ${encodeURIComponent(load)}`);

const testread = (await import('./file.js')).default
console.log(testread('testpath'));

How often does it reproduce? Is there a required condition?

every time

What is the expected behavior? Why is that the expected behavior?

each console.log call should be printed to the console, otherwise the behaviour is unpredictable

What do you see instead?

only one console.log call is seen

Additional information

No response

@avivkeller avivkeller added invalid Issues and PRs that are invalid. wontfix Issues that will not be fixed. labels Oct 7, 2024
@avivkeller avivkeller closed this as not planned Won't fix, can't repro, duplicate, stale Oct 7, 2024
@avivkeller
Copy link
Member

This is expected behavior. console.log does not have time to complete, as it's not a synchnorous function. Use fs when logging from hooks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid Issues and PRs that are invalid. wontfix Issues that will not be fixed.
Projects
None yet
Development

No branches or pull requests

2 participants