ts-node icon indicating copy to clipboard operation
ts-node copied to clipboard

Weird directory is created when using nodejs Worker threads API with `env` option

Open chulman444 opened this issue 3 years ago • 3 comments

Search Terms

v8-compile-cache worker worker thread

Expected Behavior

Not to have undefined\temp\v8-compile-cache\10.2.154.15-node.12 directory

Actual Behavior

I get one.

Steps to reproduce the problem

Use NodeJS Worker API with typescript using env option.

Run node index.js. You get no undefined because it uses argv intead of env.

Uncomment env one and comment argv you get the directory.

It seems like env is the problem. Running index-jsonly.js doesn't create undefined directory.

Minimal reproduction

https://github.com/chulman444/ts-node-reacreation

Specifications

ts-node v10.9.1
node v18.12.1
compiler v4.9.4
  • package.json:
{
  "name": "test0",
  "version": "1.0.0",
  "description": "",
  "main": "worker.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "^18.11.18",
    "tsconfig-paths": "^4.1.2"
  },
  "dependencies": {
    "ts-node": "^10.9.1"
  }
}

Just plain Windows 10 Pro 19056.2486

chulman444 avatar Jan 21 '23 10:01 chulman444

For some reason, if you pass env, it will not pass the global env, as a result of which the worker cannot find the LOCALAPPDATA in which temp is located. You can temporarily fix it by passing process.env:

new Worker('./worker.js', {
	env: {
		...process.env,
		foo: "bar"
	}
});

newTomas avatar Mar 04 '23 12:03 newTomas

@newTomas I wasn't aware that I got a reply from you. Had to recreate a project some reason and ran into the same problem, looked at my notes at it had a link to this issue I created and now I finally found yours.

This time, I was using just workerData and I was getting undefined/blah/blah directory.

Like you said, passing { env: { ...process.env } } solves the issue.

So my code is just:

const worker = new Worker(path.join(__dirname, "./worker.js"), { env: { ...process.env }, workerData: { worker_abs_path } })

I just need to comment now.

Because the solution you provided worked, I hope you could recommend me whether I should close this issue or not.

Like you said this is just a "temporary fix". When I search in issues with "v8-compile-cache" I don't think there exists any similar issues? Also I had no clue how LOCALAPPDATA was involved. Could you hint me on that? Is that ts-node thing or Node thing, etc?

chulman444 avatar Mar 13 '23 16:03 chulman444

@chulman444 I think the problem is in Nodejs. I created a test project in pure Nodejs:

//worker.js
console.log(process.env);
//index.js
const { Worker } = require("worker_threads");
const worker = new Worker("./worker.js", { env: { test: "123" } });

Here the worker outputs { test: '123' }. And if you do const worker = new Worker("./worker.js"); Then the worker will output the system env.

Pure nodejs does not create a v8-compile-cache folder neither in the same directory nor in temp. But ts-node creates, because uses v8-compile-cache-lib. And when creating it, it specifies the path process.env["LOCALAPPDATA"] + "\temp\v8-compile-cache\blah" and if any env was specified in the worker, then LOCALAPPDATA will not be in process.env and it will turn out undefined + "\temp\v8-compile-cache\blah".

I believe that Nodejs should merge the system env and what is passed to the worker. Therefore, it is worth redirecting the issue to them.

temp is a common folder for storing temporary files. You can open %LOCALAPPDATA%\temp in Explorer and see its contents. LOCALAPPDATA is a global variable that points to C:\Users\*your login*\AppData\Local.

newTomas avatar Mar 14 '23 05:03 newTomas