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

process.argv vanishing when --import flag used #50719

Closed
bpstrngr opened this issue Nov 14, 2023 · 4 comments
Closed

process.argv vanishing when --import flag used #50719

bpstrngr opened this issue Nov 14, 2023 · 4 comments

Comments

@bpstrngr
Copy link

bpstrngr commented Nov 14, 2023

Version

20.9.0

Platform

linux-x64

I'm trying to upgrade from the old --loader to the new --import flag for module customization hooks now, but i'm encountering an issue with process.argv going missing when the --import flag is used (both in the loader registration worker, and the main process), unlike without the flag being used:
image
image
( for full picture, this is where the hooks get called in each case: )
image
I have seen similar occur with the --watch flag not being present in execArgv when used, but that is less critical and i can imagine a reason for it.
Is there a reason for process.argv disappearing with --import modules?
Tested with Node 20.9 and 21.1, happens in both versions.

Additional information

first reported under --import flag's concluded feature request: #40110 (comment)

@aduh95
Copy link
Contributor

aduh95 commented Nov 15, 2023

Could you share a minimal repro? Please note that images makes it very hard fir folks using screen reader, or those unable to decipher text with your color scheme – it also makes it pointlessly harder to copy paste your code to debug the issue.

@bpstrngr
Copy link
Author

bpstrngr commented Nov 15, 2023

@aduh95 Thanks for looking, so the only thing to see in the screenshots is the commands being run, which are:
node ./loader.js 1 2 3
and
node --import=./loader.js ./loader.js 1 2 3,
the first of which has process.argv available as passed,
but the second loses them from at least version 20.9,
given a loader.js file with such contents:

// loader.js
 if(process.execArgv.some(flag=>flag.startsWith("--import")))
 // register loader module. 
 await Promise.all(["module","worker_threads"].map(module=>
 import(module))).then(([{register},{MessageChannel}])=>
 [new MessageChannel(),import.meta.url].reduce(({port1,port2},parentURL)=>
 register(new URL(parentURL).pathname,parentURL,{data:{socket:port2},transferList:[port2]})));
 else console.trace(process.argv);

 export function initialize({socket}){console.trace(process.argv);socket.postMessage("Module interface ready");}
 
 export function resolve(source,context,next){console.trace(process.argv);return next(source,context);}

expected logs:
['/path/to/node', '/path/to/loader.js', '1', '2', '3']
actual:
['/path/to/node']

@aduh95
Copy link
Contributor

aduh95 commented Nov 16, 2023

I think it is your responsibility to pass the argv from the main thread to the loader thread.

/cc @nodejs/loaders

@bpstrngr
Copy link
Author

bpstrngr commented Nov 16, 2023

@aduh95 ah right argv are actually intact on the main thread - i just got used to the main thread running the resolution with the old --loader flag, and thought the worker was only alive during the loader registration/initialization. It all actually remains on a separate thread now, got it. So it's a similar reason to why that --watch flag disappears from execArgv on the main thread itself too.

Now i'll just need to figure out how to get the result of what i use the argv for in module resolution back to the main thread somehow... hope it's possible.

Thanks!

@bpstrngr bpstrngr closed this as not planned Won't fix, can't repro, duplicate, stale Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants