Skip to content

src: traverse parent folders while running --run - #53154

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
anonrig:traverse-until-root
Jun 7, 2024
Merged

src: traverse parent folders while running --run#53154
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
anonrig:traverse-until-root

Conversation

@anonrig

@anonriganonrig commented May 26, 2024

Copy link
Copy Markdown
Member

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-botnodejs-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
anonrigforce-pushed the traverse-until-root branch 2 times, most recently from 6a4d9b0 to 595b3f5CompareMay 26, 2024 00:38
@anonriganonrig added the semver-minor PRs that contain new features and should be released in the next minor version. label May 26, 2024
@anonrig
anonrigforce-pushed the traverse-until-root branch from 595b3f5 to 94a5694CompareMay 26, 2024 00:43
@GeoffreyBooth

Copy link
Copy Markdown
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
Copy Markdown
MemberAuthor

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
Copy Markdown
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.

Comment threaddoc/api/cli.md Outdated
@ryanccn

Copy link
Copy Markdown

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

Comment threadsrc/node_task_runner.cc Outdated
@anonrig

Copy link
Copy Markdown
MemberAuthor

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

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.

Comment threadsrc/node_task_runner.cc Outdated
@isaacs

isaacs commented May 27, 2024

Copy link
Copy Markdown
Contributor

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
anonrigforce-pushed the traverse-until-root branch from 94a5694 to 09844aaCompareMay 27, 2024 15:40
@anonrig

Copy link
Copy Markdown
MemberAuthor

@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
anonrigforce-pushed the traverse-until-root branch 3 times, most recently from 0a32d40 to 8213c4fCompareMay 27, 2024 19:22
@anonrig
anonrig requested review from RafaelGSS and mcollinaMay 27, 2024 19:29
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@anonriganonrig added the commit-queue Add this label to land a pull request using GitHub Actions. label May 31, 2024
@nodejs-github-botnodejs-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
Copy Markdown
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
Copy Markdown
Collaborator

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

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@anonriganonrig 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-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 7, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@anonriganonrig added the commit-queue Add this label to land a pull request using GitHub Actions. label Jun 7, 2024
@anonriganonrig mentioned this pull request Jun 7, 2024
@nodejs-github-botnodejs-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:mainJun 7, 2024
@nodejs-github-bot

Copy link
Copy Markdown
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 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
soophoo pushed a commit to soophoo/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>
soophoo pushed a commit to soophoo/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
Sign up for freeto 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++.needs-ciPRs that need a full CI run.semver-minorPRs 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.

13 participants

@anonrig@GeoffreyBooth@ryanccn@isaacs@targos@nodejs-github-bot@benjamingr@avivkeller@mcollina@lemire@jasnell@RafaelGSS@marco-ippolito