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

Supports NVM in sentry-cli.js when build from XCode (React Native) - node no such file or directory #421

Closed
duyleekun opened this issue Oct 31, 2018 · 21 comments

Comments

@duyleekun
Copy link

duyleekun commented Oct 31, 2018

Dependencies

{
    "@babel/runtime": "^7.1.2",
    "add": "^2.0.6",
    "axios": "^0.18.0",
    "babel-plugin-transform-flow-strip-types": "^6.22.0",
    "lodash": "^4.17.10",
    "mobx": "^4.3.1",
    "mobx-react": "^5.1.0",
    "moment": "^2.22.2",
    "native-base": "^2.8.1",
    "numeral": "^2.0.6",
    "react": "16.6.0-alpha.8af6728",
    "react-native": "0.57.1",
    "react-native-contacts": "^2.2.4",
    "react-native-fast-image": "^5.0.11",
    "react-native-firebase": "^5.0.0",
    "react-native-image-crop-picker": "^0.21.2",
    "react-native-loading-spinner-overlay": "^0.5.2",
    "react-native-modal-datetime-picker": "^6.0.0",
    "react-native-sentry": "^0.39.1",
    "react-navigation": "^2.12.1",
    "schedule": "0.4.0"
}

Currently, the sentry wizard made following change to ProjectName.xcodeproj/project.pbxproj

- shellScript = "export NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh";
+ shellScript = "export SENTRY_PROPERTIES=sentry.properties\nexport NODE_BINARY=node\n../node_modules/@sentry/cli/bin/sentry-cli react-native xcode ../node_modules/react-native/scripts/react-native-xcode.sh\n";

This breaks RN apps with RVM with following error
image

The reason is scripts/react-native-xcode.sh can leverage nvm node executable in their script but the sentry-cli script didn't

The fix was to copy the NVM handling part inside scripts/react-native-xcode.sh to build script to fix the above error.

@export-mike
Copy link

export-mike commented Nov 11, 2018

@export-mike
Copy link

Looks like this is still a temporary solution that works:
getsentry/sentry-react-native#247

run this in your terminal - no edits required to project.pbxproj

n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local

@ManAnRuck
Copy link

https://docs.sentry.io/platforms/react-native/manual-setup/#using-node-with-nvm-or-notion
this works for me locally, but on travis-ci.com it does not work. (I use nvm on travis)
also the workaround #421 (comment) doesn't work for me on travis

@kamilogorek
Copy link
Contributor

@ManAnRuck what's the exact problem on CI you're running into?

@twelve17
Copy link

twelve17 commented Jun 19, 2020

It appears that the "no such file or directory" error is coming from the sentry-cli script, as it is a javascript script, and also runs with node:

$ head ./node_modules/@sentry/cli/bin/sentry-cli 
#!/usr/bin/env node
/* eslint-disable no-console */

The error is actually coming from /usr/bin/env, hence why the error message starts with "env:", because "/usr/bin/env" is not finding "node".

To confirm this, I changed this line of the build phase script :

../node_modules/@sentry/cli/bin/sentry-cli react-native xcode ../node_modules/react-native/scripts/react-native-xcode.sh

To

../node_modules/@sentry/cli/bin/sentry-cli -h

Which should just print the help page of the command. I still see the error:

env: node: No such file or directory

So the problem is that the sentry-cli script cannot find our nvm's node.

Why?

This is because for each build phase script, XCode will create a wrapper shell script and run it with a preset environment.

To see the the wrapper script, go to XCode's report navigator after the build fails:

xcode-report-navigator-1

There are two notable things about this output. First, the PATH is missing the path to the nvm's node:

xcode-report-navigator-2

Second is this:

xcode-report-navigator-3

After setting up a bunch of variables via export ..., XCode runs the temporary script via sh -c:

/bin/sh -c /Users/..../Script-030D431E0DE5414187450AD2.sh

The shell that runs the script is ran in non-interactive mode, which typically means it won't read the "profile" files, including the call to initialize nvm.

I don't know if this is the best solution, but one way to fix this is to initialize nvm in the build phase itself and then use it to set the node version to use:

# this could be different for you
export NVM_DIR=$HOME/.nvm

if [ -n "$NVM_DIR" ] && [ -d "$NVM_DIR" ]; then
  echo "nvm detected"

  # This is the path to the nvm initializer when installed via homebrew. 
  # This might be different for you.
  source /usr/local/opt/nvm/nvm.sh

  # If the project root has an .nvmrc file, change to that node  
  # version first.
  if [ -f "../.nvmrc" ]; then
    NODE_VERSION=$(cat ../.nvmrc)
    echo "nvmrc found at project root, using node version: ${NODE_VERSION}"
    nvm use ${NODE_VERSION}
  fi
  export NODE_BINARY=$(nvm which node)
else
  export NODE_BINARY=node
fi

Be sure to include the snippet in both Sentry build phases that call out to ../node_modules/@sentry/cli/bin/sentry-cli.

Update: I was curious as to why this doesn't affect the react-native-xcode.sh script itself. It is because it has built-in support for nvm.

@twelve17
Copy link

@export-mike would you be open to a PR to add nvm support to the sentry build phases? It would probably require a similar approach to what the react native team did, and would need to be wrapped in a shell script.

@export-mike
Copy link

Not really I don't think this is an issue for me anymore. @twelve17 this was 2 years ago

@twelve17
Copy link

twelve17 commented Sep 17, 2020

@export-mike This indeed is an old issue, but it is still reproducible. Are you sure it's not an issue for you because you've done the temporary workaround of copying node to /usr/local?

@zkrige
Copy link

zkrige commented Oct 23, 2020

This is indeed still an issue

@airtonix
Copy link

really.. i can not fathom why the react-native team opt'd to be o proscriptive about the node version manager tooling .

simply could of instead source $REPOROOT/.react-native-env.sh where we put our own choice of node version manager...

@twelve17
Copy link

really.. i can not fathom why the react-native team opt'd to be o proscriptive about the node version manager tooling

Is it really proscriptive? From the time the script was revived, it looked not just for nvm but also for nodenv:

facebook/react-native@b8c86b8#diff-0eeea47fa4bace26fa6c492a03fa0ea3923a2d8d54b7894f7760cb9131ab65ebR64

It doesn't seem like the script is only allowing node to be ran from those version managers.

where we put our own choice of node version

That would be a super nice solution for this project!

On Facebook's side, I can see how adding a few popular options out of the box might save some headaches internally and externally.

@nickyleach
Copy link

Inspired by @twelve17 and the find-node.sh script in React Native, I created a wrapper script for node that lives under /usr/local/bin/node:

#!/usr/bin/env bash

# I don't expect this file to get used often! nvm has been added to the path
# and in most cases will be accessed directly. I'm just adding this file
# because the React Native Sentry package doesn't seem to handle nvm very
# well. It expects /usr/local/bin/node to be a valid executable and I want
# to make sure that executable handles .nvmrc files.
#
# https://github.com/getsentry/sentry-cli/issues/421

if [[ -s "$HOME/.nvm/nvm.sh" ]]; then
  . "$HOME/.nvm/nvm.sh"
elif [[ -x "$(command -v brew)" && -s "$(brew --prefix nvm)/nvm.sh" ]]; then
  . "$(brew --prefix nvm)/nvm.sh"
fi

nvm use

exec node "$@"

@Natteke
Copy link

Natteke commented Jun 8, 2021

2021, still an issue

@duyleekun
Copy link
Author

Because of this issue, I chose to ditch the NVM in CI environment 2 years ago. I just stick to normal node installation since it's the most natural way to use node anyway. Haven't looked back since.

@twelve17
Copy link

twelve17 commented Jul 3, 2021

Technically you have looked back if you’re here again ;)

@sungsong88
Copy link

Thanks, @twelve17! By the way, I didn't get the "initialize nvm in the build phase" part from your instruction, please. So, where should I put your script in Xcode?

@kamilogorek
Copy link
Contributor

Closing the issue, as it seems like the original issue has been partially resolved or there is a working solution. I'd prefer someone to create a new issue with a fresh description if it's still an issue.
Please do not hesitate to ping me if it is still relevant, and I will happily reopen it.
Cheers!

@fqf
Copy link

fqf commented Oct 20, 2021

Looks like this is still a temporary solution that works: getsentry/sentry-react-native#247

run this in your terminal - no edits required to project.pbxproj

n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local

thanks man! it really helps!

@tobiastornros
Copy link

Reporting from 2022. Still an issue

@airtonix
Copy link

airtonix commented Feb 3, 2022

We're just going to move to using ASDF as it's the superior choice to any other specific language engine manager and is based on shims so not affected by problems that nvm has.

@Norcy
Copy link

Norcy commented Jul 13, 2023

Looks like this is still a temporary solution that works: getsentry/sentry-react-native#247

run this in your terminal - no edits required to project.pbxproj

n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local

Thanks for saving my time.

For me, it can simplify to this

n=$(which node);sudo cp -r $n /usr/local

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests