Skip to content

feat(cli) check and warn if npx react-native version is using an old cached version - #37510

Closed
blakef wants to merge 1 commit into
mainfrom
npx-check-version
Closed

feat(cli) check and warn if npx react-native version is using an old cached version#37510
blakef wants to merge 1 commit into
mainfrom
npx-check-version

Conversation

@blakef

@blakefblakef commented May 22, 2023

Copy link
Copy Markdown
Contributor

Summary:

Currently npx has a variety of caching strategies to avoid having to pull a version of the package from a registry. These are often unexpected to our users, who may fall behind. After looking at a variety of fancy approaches to dealing with this (the high end of which was intelligently forking npx to run npx react-native@latest <args>, the best possible tradeoff for time and simplicity was to warn the user when they weren't running the latest release:

CleanShot 2023-05-22 at 14 07 09@2x

Problem Details

On my laptop when you run npx <package> <arguments> this it eventually calls libnpmexec, which applies this lookup algorithm for package@version:

  • is package available in local modules (npm root → ~/project/node_modules/<package>)?. Importantly it will walk all the way down to / looking for node_modules/<package>.
  • is package available in global modules (npm root -g → /Users/blakef/.nvm/versions/node/v17.9.0/lib/node_modules)?
  • is package available in npx cache (~/.npm/_npx)?
  • is package available in your registry? Download to the npx cache ~/.npm/_npx/<hash>/
    At this point you'll have a cached copy, which then has its bin script run with the arguments you originally provided.

How this works against React-Native users

Users can get their development environment into a persistent pickle with a bunch of unintended side-effects of npx / npm exec’s caching model:

  • It matters where you run npx react-native, since it’ll default to the version of react-native in a node package's folder. This works well for us in a React Native project, but not when initializing a project outside of a package folder.
  • Global and relative node_modules really matter. If your users runs npx react-native init and they have a version of react-native installed globally, it’ll use that version.
  • If the user has a node_modules/react-native installation anywhere in the directory hierarchy it’ll be used. For example if I run npx react-native init Foobar in /home/blakef/src/example , npx will look for versions of react-native like this before searching globals or the npx cache:
    • /home/blakef/src/example/node_modules
    • /home/blakef/src/node_modules
    • /home/blakef/node_modules
    • /home/node_modules
    • /node_modules
      nvm just makes things harder if your user switches between versions of node it can be hard to determine if they're affected by a globally installed version. Examples include having a .nvmrc file in the directory they run the command which transparently switches node version (and globals location).

Changelog:

[General][Added] - Log a warning if npx react-native uses old cached version

Test Plan:

Ran this directly from the project, defining the npm_lifecycle_event=npx to mock directly running using npx.

@facebook-github-botfacebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Facebook Partner: Facebook Partner labels May 22, 2023
@blakefblakef changed the title feat(cli) check and warn if react-native version is oldfeat(cli) check and warn if npx react-native version is oldMay 22, 2023
@blakefblakef changed the title feat(cli) check and warn if npx react-native version is oldfeat(cli) check and warn if npx react-native version is using an old cached versionMay 22, 2023
@blakef
blakefforce-pushed the npx-check-version branch from 3e8a13b to 241f26aCompareMay 22, 2023 13:04
npx react-native commands can be cached in unexpected ways. Adding a
warning to users if this is the case to encourage them to use npx
react-native@latest (per the docs).
[General][Added] - Log a warning if npx react-native uses old cached version
@blakef
blakefforce-pushed the npx-check-version branch from 241f26a to 95b0dbbCompareMay 22, 2023 13:15
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@blakef has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@analysis-bot

Copy link
Copy Markdown
PlatformEngineArchSize (bytes)Diff
androidhermesarm64-v8a8,733,278-94
androidhermesarmeabi-v7a8,044,461-97
androidhermesx869,223,383-97
androidhermesx86_649,075,371-90
androidjscarm64-v8a9,298,078-103
androidjscarmeabi-v7a8,486,814-100
androidjscx869,359,240-109
androidjscx86_649,615,336-91

Base commit: 92c7219
Branch: main

@facebook-github-botfacebook-github-bot added the Merged This PR has been merged. label May 22, 2023
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@blakef merged this pull request in bfca23a.

facebook-github-bot pushed a commit that referenced this pull request Jan 10, 2024
Summary:
Dependency on `chalk` was introduced in #37510, but was never declared. In pnpm setups, the CLI fails to run because of this.
This needs to be picked to 0.73.
## Changelog:
[GENERAL] [FIXED] - Declare missing dependency `chalk`
Pull Request resolved: #42235
Test Plan: n/a
Reviewed By: huntie
Differential Revision: D52660337
Pulled By: cortinico
fbshipit-source-id: 1cd45fcff72045c127773566a27103f1b38262b3
blakef pushed a commit that referenced this pull request Jan 25, 2024
Summary:
Dependency on `chalk` was introduced in #37510, but was never declared. In pnpm setups, the CLI fails to run because of this.
This needs to be picked to 0.73.
[GENERAL] [FIXED] - Declare missing dependency `chalk`
Pull Request resolved: #42235
Test Plan: n/a
Reviewed By: huntie
Differential Revision: D52660337
Pulled By: cortinico
fbshipit-source-id: 1cd45fcff72045c127773566a27103f1b38262b3
facebook-github-bot pushed a commit that referenced this pull request May 23, 2024
…44644)
Summary:
In #37510, a check was introduced to check if user is using `latest` version of `npx`, but right now it checks for every command executed, but it should only ensure that `latest` is included when creating a new project.
In this Pull Request I've added a condition to only warn if `init` was fired.
## Changelog:
[GENERAL] [FIXED] - Warn only in `init` command when CLI uses cached `npx` version
Pull Request resolved: #44644
Test Plan: Warning about using `latest` version CLI should only be presented when running `init` command.
Reviewed By: arushikesarwani94
Differential Revision: D57681864
Pulled By: blakef
fbshipit-source-id: 5c81b9a08141396efcd24539b2560cea16028dd9
cipolleschi pushed a commit that referenced this pull request Jun 3, 2024
…44644)
Summary:
In #37510, a check was introduced to check if user is using `latest` version of `npx`, but right now it checks for every command executed, but it should only ensure that `latest` is included when creating a new project.
In this Pull Request I've added a condition to only warn if `init` was fired.
[GENERAL] [FIXED] - Warn only in `init` command when CLI uses cached `npx` version
Pull Request resolved: #44644
Test Plan: Warning about using `latest` version CLI should only be presented when running `init` command.
Reviewed By: arushikesarwani94
Differential Revision: D57681864
Pulled By: blakef
fbshipit-source-id: 5c81b9a08141396efcd24539b2560cea16028dd9
@cortinico
cortinico deleted the npx-check-version branch June 10, 2024 13:09
kosmydel pushed a commit to kosmydel/react-native that referenced this pull request Jun 11, 2024
…eact#44644)
Summary:
In react#37510, a check was introduced to check if user is using `latest` version of `npx`, but right now it checks for every command executed, but it should only ensure that `latest` is included when creating a new project.
In this Pull Request I've added a condition to only warn if `init` was fired.
## Changelog:
[GENERAL] [FIXED] - Warn only in `init` command when CLI uses cached `npx` version
Pull Request resolved: react#44644
Test Plan: Warning about using `latest` version CLI should only be presented when running `init` command.
Reviewed By: arushikesarwani94
Differential Revision: D57681864
Pulled By: blakef
fbshipit-source-id: 5c81b9a08141396efcd24539b2560cea16028dd9
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.💻CLIMergedThis PR has been merged.p: FacebookPartner: FacebookPartner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@blakef@facebook-github-bot@analysis-bot@Pranav-yadav