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

fix(ext/node): process.argv0 #20925

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cli/tests/unit_node/process_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,23 @@ Deno.test({
},
});

Deno.test({
name: "process.argv0",
fn() {
assertEquals(typeof process.argv0, "string");
assert(
process.argv0.match(/[^/\\]*deno[^/\\]*$/),
"deno included in the file name of argv[0]",
);
// Setting should be a noop
process.argv0 = "foobar";
assert(
process.argv0.match(/[^/\\]*deno[^/\\]*$/),
"deno included in the file name of argv[0]",
);
},
});

Deno.test({
name: "process.execArgv",
fn() {
Expand Down
20 changes: 17 additions & 3 deletions ext/node/polyfills/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ import { isWindows } from "ext:deno_node/_util/os.ts";
import * as io from "ext:deno_io/12_io.js";
import { Command } from "ext:runtime/40_process.js";

let argv0Getter = () => "";
export let argv0 = "deno";

// TODO(kt3k): This should be set at start up time
export let arch = "";

Expand Down Expand Up @@ -408,6 +411,15 @@ class Process extends EventEmitter {
*/
argv = argv;

get argv0() {
if (!argv0) {
argv0 = argv0Getter();
}
return argv0;
}

set argv0(_val) {}

/** https://nodejs.org/api/process.html#process_process_chdir_directory */
chdir = chdir;

Expand Down Expand Up @@ -851,23 +863,25 @@ function synchronizeListeners() {
// Should be called only once, in `runtime/js/99_main.js` when the runtime is
// bootstrapped.
internals.__bootstrapNodeProcess = function (
argv0: string | undefined,
argv0Val: string | undefined,
args: string[],
denoVersions: Record<string, string>,
) {
// Overwrites the 1st item with getter.
if (typeof argv0 === "string") {
if (typeof argv0Val === "string") {
Object.defineProperty(argv, "0", {
get: () => {
return argv0;
return argv0Val;
},
});
argv0Getter = () => argv0Val;
} else {
Object.defineProperty(argv, "0", {
get: () => {
return Deno.execPath();
},
});
argv0Getter = () => Deno.execPath();
}

// Overwrites the 2st item with getter.
Expand Down