Skip to content

src: add --run-from runtime flag#57523

Closed
mertcanaltin wants to merge 10 commits into
nodejs:mainfrom
mertcanaltin:mert/added-run-in
Closed

src: add --run-from runtime flag#57523
mertcanaltin wants to merge 10 commits into
nodejs:mainfrom
mertcanaltin:mert/added-run-in

Conversation

@mertcanaltin

@mertcanaltin mertcanaltin commented Mar 17, 2025

Copy link
Copy Markdown
Member

add --run-from runtime flag

Fixes #57489

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/config

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Mar 17, 2025
@mertcanaltin mertcanaltin requested a review from jasnell March 17, 2025 19:50
@aduh95

aduh95 commented Mar 17, 2025

Copy link
Copy Markdown
Contributor

The commit message doesn't adhere to our guidelines, it should start with an imperative verb so e.g. src: add `--run-in` runtime flag

Comment thread src/node_task_runner.cc Outdated
Comment thread src/node_task_runner.cc Outdated
Comment thread doc/api/cli.md Outdated
Comment thread doc/api/cli.md Outdated
@anonrig

anonrig commented Mar 17, 2025

Copy link
Copy Markdown
Member

cc @flakey5

@anonrig

anonrig commented Mar 17, 2025

Copy link
Copy Markdown
Member

We need to also change the pull-request title and description to reflect the changes.

@anonrig anonrig added semver-minor PRs that contain new features and should be released in the next minor version. notable-change PRs with changes that should be highlighted in changelogs. labels Mar 17, 2025
@github-actions

Copy link
Copy Markdown
Contributor

The notable-change PRs with changes that should be highlighted in changelogs. label has been added by @anonrig.

Please suggest a text for the release notes if you'd like to include a more detailed summary, then proceed to update the PR description with the text or a link to the notable change suggested text comment. Otherwise, the commit will be placed in the Other Notable Changes section.

@mertcanaltin mertcanaltin changed the title src: added node --run-in src: add --run-in runtime flag Mar 17, 2025
@mertcanaltin mertcanaltin changed the title src: add --run-in runtime flag src: add --run-directory runtime flag Mar 17, 2025
@mertcanaltin mertcanaltin changed the title src: add --run-directory runtime flag src: add --run-in-directory runtime flag Mar 17, 2025
Comment thread test/parallel/test-node-run.js
Comment thread src/node_task_runner.cc Outdated
Comment thread src/node_options.cc Outdated
Comment thread src/node_options.cc Outdated
Comment thread test/parallel/test-node-run.js Outdated

@anonrig anonrig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM if tests pass (and the new requested test is added)

@mertcanaltin mertcanaltin changed the title src: add --run-in-directory runtime flag src: add --run-dir runtime flag Mar 17, 2025
@GeoffreyBooth

Copy link
Copy Markdown
Member

so e.g. src: add `--run-in` runtime flag

I find the name --run-dir confusing; you're not running a directory, you're specifying the folder to look in to find the package.json file. Why not just ask for the path to the package.json file directly? Or allow either a path to the file or the path to the containing folder. Something like:

node --run-path=./app/package.json --run test:backend
# Or
node --run-path=./app --run test:backend

Comment thread test/parallel/test-node-run.js Outdated
Comment thread test/parallel/test-node-run.js Outdated
@mertcanaltin

Copy link
Copy Markdown
Member Author

so e.g. src: add `--run-in` runtime flag

I find the name --run-dir confusing; you're not running a directory, you're specifying the folder to look in to find the package.json file. Why not just ask for the path to the package.json file directly? Or allow either a path to the file or the path to the containing folder. Something like:

node --run-path=./app/package.json --run test:backend
# Or
node --run-path=./app --run test:backend

I understand your concern, I will wait for a few more ideas to make a collective decision, thank you very much for your suggestion 🙏

@GeoffreyBooth

Copy link
Copy Markdown
Member

the ask is for a way of setting the working directory of the --run command, not the process as a whole.

What does it mean to apply only to --run? Does that just tell it where to find package.json or are there other effects?

@mertcanaltin

mertcanaltin commented Mar 21, 2025

Copy link
Copy Markdown
Member Author

the ask is for a way of setting the working directory of the --run command, not the process as a whole.

What does it mean to apply only to --run? Does that just tell it where to find package.json or are there other effects?

only determines the directory where the --run command finds the package.json file; it does not change the process-wide working directory.

@GeoffreyBooth

GeoffreyBooth commented Mar 21, 2025

Copy link
Copy Markdown
Member

only determines the directory where the --run command finds the package.json file; it does not change the process-wide working directory.

Okay, can it accept as input either the path to the containing folder or the path directly to the package.json file?

(As in, if this isn't the current behavior is it possible to update the PR so that this is possible.)

@targos

targos commented Mar 21, 2025

Copy link
Copy Markdown
Member

I'm not sure this feature will work as expected if it doesn't set the process's cwd. Most scripts I write wouldn't work FWIW

@mertcanaltin

Copy link
Copy Markdown
Member Author

only determines the directory where the --run command finds the package.json file; it does not change the process-wide working directory.

Okay, can it accept as input either the path to the containing folder or the path directly to the package.json file?

(As in, if this isn't the current behavior is it possible to update the PR so that this is possible.)

yes, I'm trying now

@mertcanaltin

Copy link
Copy Markdown
Member Author

hello i sent an improvement @GeoffreyBooth @ovflowd @targos

I tried to implement that if given a package.json directly, this directory is now selected as the parent directory

Comment thread doc/api/cli.md Outdated
Co-authored-by: Geoffrey Booth <webadmin@geoffreybooth.com>
Comment thread doc/api/cli.md
Comment thread doc/api/cli.md
Co-authored-by: Geoffrey Booth <webadmin@geoffreybooth.com>
@GeoffreyBooth

Copy link
Copy Markdown
Member

I'm not sure this feature will work as expected if it doesn't set the process's cwd. Most scripts I write wouldn't work FWIW

I'm wondering how this can work in practice too. Does it solve #57489? Are the scripts in that package.json runnable from a different CWD?

Comment thread src/node_task_runner.cc

void ProcessRunner::Run() {
// keeps the string alive until destructor
cwd = package_json_path_.parent_path().string();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not work since this doesn't add the current node_modules to path environment variable.

@anonrig

anonrig commented Mar 21, 2025

Copy link
Copy Markdown
Member

only determines the directory where the --run command finds the package.json file; it does not change the process-wide working directory.

@mertcanaltin this is wrong. you're also changing the cwd of the process that we are spawning.

To answer other reviewers: I think this solution has lots of flaws. It doesn't properly propagate path variable (according to the given --run-from), and makes lots of unnecessary system calls.

@mertcanaltin

Copy link
Copy Markdown
Member Author

only determines the directory where the --run command finds the package.json file; it does not change the process-wide working directory.

@mertcanaltin this is wrong. you're also changing the cwd of the process that we are spawning.

To answer other reviewers: I think this solution has lots of flaws. It doesn't properly propagate path variable (according to the given --run-from), and makes lots of unnecessary system calls.

now only the cwd of the spawned process (options_.cwd) is set, without changing the process-wide cwd. Also, if the file path is given for --run-from, the parent directory is used. I hope this addresses your concerns.

Comment thread src/node_task_runner.cc
Comment on lines +224 to +229
// Adding node_modules/.bin under cwd to PATH environment variable
std::filesystem::path nmBin =
std::filesystem::path(cwd) / "node_modules" / ".bin";
if (std::filesystem::is_directory(nmBin)) {
path_env_var_ = nmBin.string() + env_var_separator + path_env_var_;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anonrig I tried to process the path given with --run-from and add the node_modules/.bin directory under the cwd to the PATH Without changing the cwd of the parent process, only the cwd of the spawned process will be set, is this a good solution ?

@mertcanaltin

mertcanaltin commented Mar 23, 2025

Copy link
Copy Markdown
Member Author

Thank you very much for your valuable contributions and feedback on this PR. I just found out that @flakey5 is already working on it, so I was unknowingly working on it. Therefore, I will close this PR 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. notable-change PRs with changes that should be highlighted in changelogs. review wanted PRs that need reviews. semver-minor PRs that contain new features and should be released in the next minor version. tsc-agenda Issues and PRs to discuss during the meetings of the TSC.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Specify working directory for node --run