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

Fix mismatching tsc binary versions in expo-module-scripts in monorepos #27779

Conversation

peter-jozsa
Copy link
Contributor

Why

If you have a monorepo in which you have different versions of typescript installed you may see that expo-module build plugin fails because of unknown TS compiler options. I think it happens because wrong version of tsc picked up by npx.

Related GitHub issues:
npm/cli#6765

How

As far as I understand the root cause of this issue is that if we simply run npx tsc ... npm does not know tsc binary belongs to the typescript package and it will simply try to resolve it from the root workspace's node_modules/.bin where it will find the wrong version of tsc because of dependency hoisting. It should start looking up tsc in the workspace's node_modules/.bin (from where we're executing expo-module build plugin) but that is not the case for some reason. We can only achieve this by giving a hint to npx that tsc belongs to the package called typescript. In this case it will start the binary lookup from the workspace's node_modules/.bin folder and use the correct version to build our module plugin.

Test Plan

  1. Create a Yarn v2 Berry monorepo.
  2. Create a module (packages/cool-module) by following the official tutorial with a config plugin (https://docs.expo.dev/modules/config-plugin-and-native-module-tutorial/)
  3. Add typescript@^4.5.5 to your workspace root as a dev dependency (or add you could have multiple other workspaces that depend on typescript@^4.5.5, e.g. packages/my-api)
  4. At this point you should see that the 4.5.5 tsc is installed in your root node_modules and 5.3.0 tsc is installed in your packages/cool-module/node_modules folder.
  5. If you try to run cd packages/cool-module && yarn run build plugin you should receive TypeScript config errors since packages/cool-module/plugin/tsconfig.json inherits configuration options that are unknown to 4.5.5 tsc. (e.g. "lib": ["es2023"]). At this point you know something is wrong since expo-module-scripts depends on typescript": "^5.1.3" that should be aware of es2023 and node16 module resolution logic.
  6. Go to your module workspace folder (cd packages/cool-module) and execute the same command that is executed by expo-module build plugin under the hood: yarn exec -- npx tsc --version. It should give the mismatched 4.5.5 version.
  7. Stay in the module workspace folder (packages/cool-module) and execute now the following command: yarn exec -- npx --package=typescript tsc --version. It should give the correct 5.3.0 version.

Checklist

@peter-jozsa peter-jozsa requested a review from ide as a code owner March 21, 2024 00:12
@expo-bot expo-bot added the bot: suggestions ExpoBot has some suggestions label Mar 21, 2024
@ide
Copy link
Member

ide commented Mar 21, 2024

I'll need some time to review this more closely but wanted to say what a great PR summary this is, explaining the motivation and providing your test plan.

@expo-bot expo-bot added bot: passed checks ExpoBot has nothing to complain about and removed bot: suggestions ExpoBot has some suggestions labels Mar 21, 2024
@ide ide merged commit 0e9edc5 into expo:main Mar 26, 2024
14 of 15 checks passed
@brentvatne brentvatne added the published Changes from the PR have been published to npm label Apr 18, 2024
brentvatne added a commit that referenced this pull request Apr 22, 2024
brentvatne added a commit that referenced this pull request Apr 22, 2024
brentvatne added a commit that referenced this pull request Apr 22, 2024
brentvatne added a commit that referenced this pull request Apr 22, 2024
@tsapeta and I found that #27779 may lead to very unpredictable
behavior, for example - if you have the latest version of eas-cli
installed globally, then `npx --package=typescript tsc` will not in fact
look up the `tsc` within your monorepo but may actually try to use the
version from the global eas-cli pkg.

This has caused us confusion in several cases including blocking a
release right now, so I am going to revert it
@ide
Copy link
Member

ide commented Apr 23, 2024

We had to revert this change. See the linked revert PR. From the description it sounds like npx --package xxx will look for xxx in your global packages, hence needing to revert.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: passed checks ExpoBot has nothing to complain about published Changes from the PR have been published to npm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants