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

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

Closed
wants to merge 1 commit into from

Conversation

blakef
Copy link
Contributor

@blakef blakef commented May 22, 2023

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-bot facebook-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
@blakef blakef changed the title feat(cli) check and warn if react-native version is old feat(cli) check and warn if npx react-native version is old May 22, 2023
@blakef blakef changed the title feat(cli) check and warn if npx react-native version is old feat(cli) check and warn if npx react-native version is using an old cached version May 22, 2023
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
@facebook-github-bot
Copy link
Contributor

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

@analysis-bot
Copy link

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 8,733,278 -94
android hermes armeabi-v7a 8,044,461 -97
android hermes x86 9,223,383 -97
android hermes x86_64 9,075,371 -90
android jsc arm64-v8a 9,298,078 -103
android jsc armeabi-v7a 8,486,814 -100
android jsc x86 9,359,240 -109
android jsc x86_64 9,615,336 -91

Base commit: 92c7219
Branch: main

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label May 22, 2023
@facebook-github-bot
Copy link
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
gokul1099 pushed a commit to gokul1099/react-native-visionos that referenced this pull request Jan 17, 2024
Summary:
Dependency on `chalk` was introduced in facebook#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: facebook#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
…acebook#44644)

Summary:
In facebook#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: facebook#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 free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. 💻CLI Merged This PR has been merged. p: Facebook Partner: Facebook Partner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants