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: traverse parent folders while running --run #53154

Merged
merged 1 commit into from
Jun 7, 2024

Conversation

anonrig
Copy link
Member

@anonrig anonrig commented May 26, 2024

This adds the most requested 2 features to node --run:

  1. Traversing parent folders until package.json file is found.
  2. Prepending node_modules/.bin for each subdirectory of the current folder to PATH variable (specifically for monorepos)

I've also updated the documentation. I think we are in good hands, and getting ready to make the runner stable.

With the current changes and the overhead of checking if it's a directory for each node_modules/.bin inside each ancestor folder, here's the updated benchmark results:

❯ hyperfine '../node/out/Release/node --run test' 'npm run test' -i -w 10
Benchmark 1: ../node/out/Release/node --run test
  Time (mean ± σ):      22.7 ms ±   1.2 ms    [User: 17.0 ms, System: 2.7 ms]
  Range (min … max):    20.9 ms …  25.8 ms    118 runs

Benchmark 2: npm run test
  Time (mean ± σ):     106.0 ms ±   4.0 ms    [User: 82.5 ms, System: 14.1 ms]
  Range (min … max):   101.3 ms … 116.1 ms    28 runs

Summary
  ../node/out/Release/node --run test ran
    4.67 ± 0.31 times faster than npm run test

cc @nodejs/cpp-reviewers

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels May 26, 2024
@anonrig anonrig force-pushed the traverse-until-root branch 2 times, most recently from 6a4d9b0 to 595b3f5 Compare May 26, 2024 00:38
@anonrig anonrig added the semver-minor PRs that contain new features and should be released in the next minor version. label May 26, 2024
@GeoffreyBooth
Copy link
Member

Appending node_modules/.bin for each subdirectory of the current folder to PATH variable (specifically for monorepos)

I thought this one was hard/next to impossible to do safely for Windows because of the short limit of the path variable there? There was a comment to this effect on an earlier thread.

@anonrig
Copy link
Member Author

anonrig commented May 26, 2024

Appending node_modules/.bin for each subdirectory of the current folder to PATH variable (specifically for monorepos)

I thought this one was hard/next to impossible to do safely for Windows because of the short limit of the path variable there? There was a comment to this effect on an earlier thread.

I found the following solution online, might fix it:

  • Create a new system environment variable, say 'NEWPATH'
  • Assign the bin directory location to 'NEWPATH'
  • Now append '; %NEWPATH%' to the PATH environment variable

@GeoffreyBooth
Copy link
Member

Since the path part is potentially problematic, should it be a separate PR? The traversing up the tree part is straightforward and could just land I'd think.

doc/api/cli.md Outdated Show resolved Hide resolved
@ryanccn
Copy link

ryanccn commented May 26, 2024

Also, checking if the node_modules/.bin directory for each ancestor actually exists before adding it to PATH could possibly alleviate the issue.

src/node_task_runner.cc Outdated Show resolved Hide resolved
@anonrig
Copy link
Member Author

anonrig commented May 26, 2024

Also, checking if the node_modules/.bin directory for each ancestor actually exists before adding it to PATH could possibly alleviate the issue.

This would reduce the performance by a significant margin, and isn't in par with npm (but that's not a supportive argument just a fact)

@ryanccn
Copy link

ryanccn commented May 26, 2024

This would reduce the performance by a significant margin

It's a very minor cost compared to the other work that's being done, and skipping these checks would really just offload the work to the subprocesses being run (such as the shell), which would have to look through all of these nonexistent paths for binaries to execute, so it doesn't prevent the performance cost.

@isaacs
Copy link
Contributor

isaacs commented May 27, 2024

Appending node_modules/.bin for each subdirectory of the current folder to PATH variable (specifically for monorepos)

I think you mean each parent directory, not each sub directory?

I haven't checked the implementation, but you can probably get away with only adding node_modules/.bin to the path for every parent directory you find containing a package.json file, and can also further check if {dir}/node_modules/.bin is a directory. It adds some stats, but it'll help keep the PATH env within limits, and if you don't do those stats, then the OS will end up doing them anyway when it goes to look up executables. (This is close to what npm does.)

Also, the node_modules/.bin should be pre-pended to the PATH env, not appended. Eg, if I have eslint installed globally, and a different version in the current project, I want the one in the closest containing project that has it, not the one at /usr/local/bin/eslint.

@anonrig
Copy link
Member Author

anonrig commented May 27, 2024

@isaacs Thanks for the comment!

I think you mean each parent directory, not each sub directory?

Yes, I've updated the CLI to use ancestor keyword.

I haven't checked the implementation, but you can probably get away with only adding node_modules/.bin to the path for every parent directory you find containing a package.json file, and can also further check if {dir}/node_modules/.bin is a directory. It adds some stats, but it'll help keep the PATH env within limits, and if you don't do those stats, then the OS will end up doing them anyway when it goes to look up executables. (This is close to what npm does.)

Why do you think checking package_json is good? I'm currently checking if node_modules/.bin is a directory.

Also, the node_modules/.bin should be pre-pended to the PATH env, not appended. Eg, if I have eslint installed globally, and a different version in the current project, I want the one in the closest containing project that has it, not the one at /usr/local/bin/eslint.

Yes, I'm pre-pending although I'm not sure how did you come to this conclusion? If it's my choice of words in the docs, can you point me to the correct place to fix it?

@anonrig anonrig force-pushed the traverse-until-root branch 3 times, most recently from 0a32d40 to 8213c4f Compare May 27, 2024 19:22
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@anonrig anonrig added the commit-queue Add this label to land a pull request using GitHub Actions. label May 31, 2024
@nodejs-github-bot nodejs-github-bot added commit-queue-failed An error occurred while landing this pull request using GitHub Actions. and removed commit-queue Add this label to land a pull request using GitHub Actions. labels May 31, 2024
@nodejs-github-bot
Copy link
Collaborator

Commit Queue failed
- Loading data for nodejs/node/pull/53154
✔  Done loading data for nodejs/node/pull/53154
----------------------------------- PR info ------------------------------------
Title      src: traverse parent folders while running `--run` (#53154)
Author     Yagiz Nizipli  (@anonrig)
Branch     anonrig:traverse-until-root -> nodejs:main
Labels     c++, semver-minor, needs-ci
Commits    1
 - src: traverse parent folders while running `--run`
Committers 1
 - Yagiz Nizipli 
PR-URL: https://github.com/nodejs/node/pull/53154
Reviewed-By: Benjamin Gruenbaum 
Reviewed-By: Daniel Lemire 
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/53154
Reviewed-By: Benjamin Gruenbaum 
Reviewed-By: Daniel Lemire 
--------------------------------------------------------------------------------
   ℹ  This PR was created on Sun, 26 May 2024 00:20:07 GMT
   ✔  Approvals: 2
   ✔  - Benjamin Gruenbaum (@benjamingr) (TSC): https://github.com/nodejs/node/pull/53154#pullrequestreview-2086340450
   ✔  - Daniel Lemire (@lemire): https://github.com/nodejs/node/pull/53154#pullrequestreview-2088970099
   ✘  Last GitHub CI failed
   ℹ  Last Full PR CI on 2024-05-31T11:06:06Z: https://ci.nodejs.org/job/node-test-pull-request/59561/
- Querying data for job/node-test-pull-request/59561/
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu
https://github.com/nodejs/node/actions/runs/9318146805

@nodejs-github-bot
Copy link
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/59571/

@nodejs-github-bot
Copy link
Collaborator

@anonrig anonrig added request-ci Add this label to start a Jenkins CI on a PR. and removed commit-queue-failed An error occurred while landing this pull request using GitHub Actions. labels Jun 7, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 7, 2024
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@anonrig anonrig added the commit-queue Add this label to land a pull request using GitHub Actions. label Jun 7, 2024
@anonrig anonrig mentioned this pull request Jun 7, 2024
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Jun 7, 2024
@nodejs-github-bot nodejs-github-bot merged commit 5731e2b into nodejs:main Jun 7, 2024
65 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 5731e2b

RafaelGSS pushed a commit that referenced this pull request Jun 10, 2024
PR-URL: #53154
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: James M Snell <jasnell@gmail.com>
RafaelGSS added a commit that referenced this pull request Jun 10, 2024
Notable changes:

buffer:
  * (SEMVER-MINOR) add .bytes() method to Blob (Matthew Aitken) #53221
cli:
  * (SEMVER-MINOR) add `NODE_RUN_PACKAGE_JSON_PATH` env (Yagiz Nizipli) #53058
  * (SEMVER-MINOR) add `NODE_RUN_SCRIPT_NAME` env to `node --run` (Yagiz Nizipli) #53032
doc:
  * (SEMVER-MINOR) add context.assert docs (Colin Ihrig) #53169
  * (SEMVER-MINOR) improve explanation about built-in modules (Joyee Cheung) #52762
  * add StefanStojanovic to collaborators (StefanStojanovic) #53118
  * add Marco Ippolito to TSC (Rafael Gonzaga) #53008
fs:
  * mark recursive cp methods as stable (Théo LUDWIG) #53127
lib:
  * (SEMVER-MINOR) add EventSource Client (Aras Abbasi) #51575
  * (SEMVER-MINOR) replace MessageEvent with undici's (Matthew Aitken) #52370
module:
  * (SEMVER-MINOR) print amount of load time of a cjs module (Vinicius Lourenço) #52213
net:
  * (SEMVER-MINOR) add new net.server.listen tracing channel (Paolo Insogna) #53136
process:
  * (SEMVER-MINOR) add process.getBuiltinModule(id) (Joyee Cheung) #52762
src:
  * (SEMVER-MINOR) traverse parent folders while running `--run` (Yagiz Nizipli) #53154
src,permission:
  * (SEMVER-MINOR) --allow-wasi & prevent WASI exec (Rafael Gonzaga) #53124
test_runner:
  * (SEMVER-MINOR) add snapshot testing (Colin Ihrig) #53169
  * (SEMVER-MINOR) add context.fullName (Colin Ihrig) #53169
  * (SEMVER-MINOR) support module mocking (Colin Ihrig) #52848

PR-URL: #53379
RafaelGSS added a commit that referenced this pull request Jun 10, 2024
Notable changes:

buffer:
  * (SEMVER-MINOR) add .bytes() method to Blob (Matthew Aitken) #53221
cli:
  * (SEMVER-MINOR) add `NODE_RUN_PACKAGE_JSON_PATH` env (Yagiz Nizipli) #53058
  * (SEMVER-MINOR) add `NODE_RUN_SCRIPT_NAME` env to `node --run` (Yagiz Nizipli) #53032
doc:
  * (SEMVER-MINOR) add context.assert docs (Colin Ihrig) #53169
  * (SEMVER-MINOR) improve explanation about built-in modules (Joyee Cheung) #52762
  * add StefanStojanovic to collaborators (StefanStojanovic) #53118
  * add Marco Ippolito to TSC (Rafael Gonzaga) #53008
fs:
  * mark recursive cp methods as stable (Théo LUDWIG) #53127
lib:
  * (SEMVER-MINOR) add EventSource Client (Aras Abbasi) #51575
  * (SEMVER-MINOR) replace MessageEvent with undici's (Matthew Aitken) #52370
module:
  * (SEMVER-MINOR) print amount of load time of a cjs module (Vinicius Lourenço) #52213
net:
  * (SEMVER-MINOR) add new net.server.listen tracing channel (Paolo Insogna) #53136
process:
  * (SEMVER-MINOR) add process.getBuiltinModule(id) (Joyee Cheung) #52762
src:
  * (SEMVER-MINOR) traverse parent folders while running `--run` (Yagiz Nizipli) #53154
src,permission:
  * (SEMVER-MINOR) --allow-wasi & prevent WASI exec (Rafael Gonzaga) #53124
test_runner:
  * (SEMVER-MINOR) add snapshot testing (Colin Ihrig) #53169
  * (SEMVER-MINOR) add context.fullName (Colin Ihrig) #53169
  * (SEMVER-MINOR) support module mocking (Colin Ihrig) #52848

PR-URL: #53379
RafaelGSS added a commit that referenced this pull request Jun 11, 2024
Notable changes:

buffer:
  * (SEMVER-MINOR) add .bytes() method to Blob (Matthew Aitken) #53221
cli:
  * (SEMVER-MINOR) add `NODE_RUN_PACKAGE_JSON_PATH` env (Yagiz Nizipli) #53058
  * (SEMVER-MINOR) add `NODE_RUN_SCRIPT_NAME` env to `node --run` (Yagiz Nizipli) #53032
doc:
  * (SEMVER-MINOR) add context.assert docs (Colin Ihrig) #53169
  * (SEMVER-MINOR) improve explanation about built-in modules (Joyee Cheung) #52762
  * add StefanStojanovic to collaborators (StefanStojanovic) #53118
  * add Marco Ippolito to TSC (Rafael Gonzaga) #53008
fs:
  * mark recursive cp methods as stable (Théo LUDWIG) #53127
lib:
  * (SEMVER-MINOR) add EventSource Client (Aras Abbasi) #51575
  * (SEMVER-MINOR) replace MessageEvent with undici's (Matthew Aitken) #52370
module:
  * (SEMVER-MINOR) print amount of load time of a cjs module (Vinicius Lourenço) #52213
net:
  * (SEMVER-MINOR) add new net.server.listen tracing channel (Paolo Insogna) #53136
process:
  * (SEMVER-MINOR) add process.getBuiltinModule(id) (Joyee Cheung) #52762
src:
  * (SEMVER-MINOR) traverse parent folders while running `--run` (Yagiz Nizipli) #53154
src,permission:
  * (SEMVER-MINOR) --allow-wasi & prevent WASI exec (Rafael Gonzaga) #53124
test_runner:
  * (SEMVER-MINOR) add snapshot testing (Colin Ihrig) #53169
  * (SEMVER-MINOR) add context.fullName (Colin Ihrig) #53169
  * (SEMVER-MINOR) support module mocking (Colin Ihrig) #52848

PR-URL: #53379
eliphazbouye pushed a commit to eliphazbouye/node that referenced this pull request Jun 20, 2024
PR-URL: nodejs#53154
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: James M Snell <jasnell@gmail.com>
eliphazbouye pushed a commit to eliphazbouye/node that referenced this pull request Jun 20, 2024
Notable changes:

buffer:
  * (SEMVER-MINOR) add .bytes() method to Blob (Matthew Aitken) nodejs#53221
cli:
  * (SEMVER-MINOR) add `NODE_RUN_PACKAGE_JSON_PATH` env (Yagiz Nizipli) nodejs#53058
  * (SEMVER-MINOR) add `NODE_RUN_SCRIPT_NAME` env to `node --run` (Yagiz Nizipli) nodejs#53032
doc:
  * (SEMVER-MINOR) add context.assert docs (Colin Ihrig) nodejs#53169
  * (SEMVER-MINOR) improve explanation about built-in modules (Joyee Cheung) nodejs#52762
  * add StefanStojanovic to collaborators (StefanStojanovic) nodejs#53118
  * add Marco Ippolito to TSC (Rafael Gonzaga) nodejs#53008
fs:
  * mark recursive cp methods as stable (Théo LUDWIG) nodejs#53127
lib:
  * (SEMVER-MINOR) add EventSource Client (Aras Abbasi) nodejs#51575
  * (SEMVER-MINOR) replace MessageEvent with undici's (Matthew Aitken) nodejs#52370
module:
  * (SEMVER-MINOR) print amount of load time of a cjs module (Vinicius Lourenço) nodejs#52213
net:
  * (SEMVER-MINOR) add new net.server.listen tracing channel (Paolo Insogna) nodejs#53136
process:
  * (SEMVER-MINOR) add process.getBuiltinModule(id) (Joyee Cheung) nodejs#52762
src:
  * (SEMVER-MINOR) traverse parent folders while running `--run` (Yagiz Nizipli) nodejs#53154
src,permission:
  * (SEMVER-MINOR) --allow-wasi & prevent WASI exec (Rafael Gonzaga) nodejs#53124
test_runner:
  * (SEMVER-MINOR) add snapshot testing (Colin Ihrig) nodejs#53169
  * (SEMVER-MINOR) add context.fullName (Colin Ihrig) nodejs#53169
  * (SEMVER-MINOR) support module mocking (Colin Ihrig) nodejs#52848

PR-URL: nodejs#53379
bmeck pushed a commit to bmeck/node that referenced this pull request Jun 22, 2024
PR-URL: nodejs#53154
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: James M Snell <jasnell@gmail.com>
bmeck pushed a commit to bmeck/node that referenced this pull request Jun 22, 2024
Notable changes:

buffer:
  * (SEMVER-MINOR) add .bytes() method to Blob (Matthew Aitken) nodejs#53221
cli:
  * (SEMVER-MINOR) add `NODE_RUN_PACKAGE_JSON_PATH` env (Yagiz Nizipli) nodejs#53058
  * (SEMVER-MINOR) add `NODE_RUN_SCRIPT_NAME` env to `node --run` (Yagiz Nizipli) nodejs#53032
doc:
  * (SEMVER-MINOR) add context.assert docs (Colin Ihrig) nodejs#53169
  * (SEMVER-MINOR) improve explanation about built-in modules (Joyee Cheung) nodejs#52762
  * add StefanStojanovic to collaborators (StefanStojanovic) nodejs#53118
  * add Marco Ippolito to TSC (Rafael Gonzaga) nodejs#53008
fs:
  * mark recursive cp methods as stable (Théo LUDWIG) nodejs#53127
lib:
  * (SEMVER-MINOR) add EventSource Client (Aras Abbasi) nodejs#51575
  * (SEMVER-MINOR) replace MessageEvent with undici's (Matthew Aitken) nodejs#52370
module:
  * (SEMVER-MINOR) print amount of load time of a cjs module (Vinicius Lourenço) nodejs#52213
net:
  * (SEMVER-MINOR) add new net.server.listen tracing channel (Paolo Insogna) nodejs#53136
process:
  * (SEMVER-MINOR) add process.getBuiltinModule(id) (Joyee Cheung) nodejs#52762
src:
  * (SEMVER-MINOR) traverse parent folders while running `--run` (Yagiz Nizipli) nodejs#53154
src,permission:
  * (SEMVER-MINOR) --allow-wasi & prevent WASI exec (Rafael Gonzaga) nodejs#53124
test_runner:
  * (SEMVER-MINOR) add snapshot testing (Colin Ihrig) nodejs#53169
  * (SEMVER-MINOR) add context.fullName (Colin Ihrig) nodejs#53169
  * (SEMVER-MINOR) support module mocking (Colin Ihrig) nodejs#52848

PR-URL: nodejs#53379
@marco-ippolito marco-ippolito added the backport-blocked-v20.x PRs that should land on the v20.x-staging branch but are blocked by another PR's pending backport. label Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-blocked-v20.x PRs that should land on the v20.x-staging branch but are blocked by another PR's pending backport. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.