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

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

mertcanaltin
Copy link
Member

@mertcanaltin mertcanaltin commented Mar 17, 2025

add --run-from runtime flag

Fixes #57489

@nodejs-github-bot
Copy link
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
Copy link
Contributor

aduh95 commented Mar 17, 2025

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

@anonrig
Copy link
Member

anonrig commented Mar 17, 2025

cc @flakey5

@anonrig
Copy link
Member

anonrig commented Mar 17, 2025

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
Copy link
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
Copy link
Member

@anonrig anonrig left a comment

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
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

@mertcanaltin
Copy link
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 🙏

@mertcanaltin mertcanaltin changed the title src: add --run-dir runtime flag src: add --run-from runtime flag Mar 21, 2025
@mertcanaltin
Copy link
Member Author

thanks a lot, now I am merging all commits and fixing my first commit message 🚀

@GeoffreyBooth
Copy link
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
Copy link
Member Author

mertcanaltin commented Mar 21, 2025

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
Copy link
Member

GeoffreyBooth commented Mar 21, 2025

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
Copy link
Member

targos commented Mar 21, 2025

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
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
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

Co-authored-by: Geoffrey Booth <webadmin@geoffreybooth.com>
Co-authored-by: Geoffrey Booth <webadmin@geoffreybooth.com>
@GeoffreyBooth
Copy link
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?

@@ -206,7 +206,35 @@ void ProcessRunner::OnExit(int64_t exit_status, int term_signal) {

void ProcessRunner::Run() {
// keeps the string alive until destructor
cwd = package_json_path_.parent_path().string();
Copy link
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
Copy link
Member

anonrig commented Mar 21, 2025

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
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 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
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 ?

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