react-native-community / react-native-community/cli

Warnings emitted on `init` when ran from within a non-installed package

Open
#1,493 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug report no-stale-bot
Dominant language
TypeScript
Stars
2.9k
Forks
949
PR merge metrics
No merged PRs in 30d

Description

Environment

System:
    OS: macOS 11.6
    CPU: (8) x64 Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
    Memory: 983.04 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.16.0 - ~/.nvm/versions/node/v14.16.0/bin/node
    Yarn: 1.22.11 - ~/.nvm/versions/node/v14.16.0/bin/yarn
    npm: 7.19.1 - ~/.nvm/versions/node/v14.16.0/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.10.1 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 15.0, DriverKit 20.4, macOS 11.3, tvOS 15.0, watchOS 8.0
    Android SDK:
      Android NDK: 21.0.6113669
  IDEs:
    Android Studio: 2020.3 AI-203.7717.56.2031.7784292
    Xcode: 13.0/13A233 - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.12 - /usr/local/opt/openjdk@11/bin/javac
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: Not Found
    react-native: Not Found
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Description

I want to react-native init a new app while my current working directory is a (a sub-directory of) another package.
But when I do this from within a package that has not yet been installed (or linked in the case of a Lerna mono-repo), the CLI emits confusing warnings (see example below).

I believe the root cause of these warnings is that an attempt is made to locate a root package and load its configuration (https://github.com/react-native-community/cli/blob/master/packages/cli/src/index.ts#L217) independently on what command is being executed.

The default behaviour of loadConfig is to find the nearest package.json up the tree: https://github.com/react-native-community/cli/blob/master/packages/cli/src/tools/config/index.ts#L61 which will find the enclosing package with uninstalled dependencies.

Ideally running react-native init shouldn't traverse my file system to determine a configuration.
Most likely the commands meant to be executed "detached" from a package, should have an early return code-path to make sure the configuration is never loaded when running those.

Reproducible Demo

# Create a package
cat << EOF > package.json
{
  "name": "wrong-root-package",
  "dependencies": {
    "unresolvable-package": "^1.0.0"
  }
}
EOF

# Try running the "react-native" CLI to initialize a new app
npx --yes react-native init MyTestApp

When running this, notice the warning emitted from the CLI:

warn Package unresolvable-package has been ignored because it contains invalid configuration. Reason: Cannot find module 'unresolvable-package/package.json'
Require stack:
- /Users/kraen.hansen/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/tools/config/resolveNodeModuleDir.js
- /Users/kraen.hansen/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/tools/releaseChecker/index.js
- /Users/kraen.hansen/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/commands/start/runServer.js
- /Users/kraen.hansen/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/commands/start/start.js
- /Users/kraen.hansen/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/commands/index.js
- /Users/kraen.hansen/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/index.js
- /Users/kraen.hansen/.npm/_npx/7930a8670f922cdb/node_modules/@react-native-community/cli/build/bin.js

Workaround

Users can place a package.json containing an empty object ({}) in the current working directory before executing the react-native init command.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with packages/cli/src/index.ts and packages/cli/src/tools/config/index.ts, then reproduce the issue by running npx --yes react-native init MyTestApp from a package containing an unresolved dependency. Trace when configuration is loaded for init; done means initialization from that directory no longer emits the unrelated invalid-configuration warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, react-native, typescript
Domain
cli, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.