ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
11.31k stars 962 forks source link

bug: 'sh' is not recognized as an internal or external command #4947

Closed tigohenryschultz closed 1 year ago

tigohenryschultz commented 2 years ago

Bug Report

Capacitor Version

Latest Dependencies:

  @capacitor/cli: 3.2.0
  @capacitor/core: 3.2.0
  @capacitor/android: 3.2.0
  @capacitor/ios: 3.2.0

Installed Dependencies:

  @capacitor/ios: not installed
  @capacitor/cli: 3.2.0
  @capacitor/core: 3.2.0
  @capacitor/android: 3.2.0

Platform(s)

Andriod/Windows

Current Behavior

Attempts to run an 'sh' command when deploying to an Android phone

Expected Behavior

Don't run an SH command

Code Reproduction

Fresh Ionic/Capacitor project and just ionic cap run android -l --external from Windows 10 WSL(PowerShell)

Other Technical Details

npm --version output: 6.16.3 node --version output: v14.17.3 pod --version output (iOS issues only):

Additional Context

[vue-cli-service] Issues checking in progress...
[vue-cli-service] No issues found.
[capacitor] × Running Gradle build - failed!
[capacitor] [error] 'sh' is not recognized as an internal or external command,
[capacitor]         operable program or batch file.
[capacitor]
[ERROR] An error occurred while running subprocess capacitor.

        capacitor.cmd run android --no-sync --target R5CR114FT6N exited with exit code 1.

        Re-running this command with the --verbose flag may provide more information.
Ionitron commented 2 years ago

This issue may need more information before it can be addressed. In particular, it will need a reliable Code Reproduction that demonstrates the issue.

Please see the Contributing Guide for how to create a Code Reproduction.

Thanks! Ionitron 💙

tigohenryschultz commented 2 years ago

I have it reproduced on Windows 11 with WSL2 in a powershell terminal

jcesarmobile commented 2 years ago

Can you provide a sample app or more information about your configuration? I can't reproduce on Windows 10 with powershell 7.1.4 downloaded from https://github.com/PowerShell/PowerShell/releases/tag/v7.1.4

tigohenryschultz commented 2 years ago

Info: npm -v 6.14.13

ionic -v 6.16.3

npx cap -V 3.2.2

powershell:

PS C:\repos\test> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      22000  65

Steps:

Step 1)

PS C:\repos\ionic-capacitor-test> ionic start
Select Vue
name: test
type: tabs
free account: no

Step 2)

cd test && mkdir dist
Create file in dist/index.html

Step 3)

ionic cap run android -l --external
Select Nexus 7 as device
Select ethernet

Results:

[vue-cli-service]  INFO  Starting development server...

[INFO] Development server running!

       Local: http://localhost:8100
       External: http://10.9.1.x:8100, http://x.x.176.1:8100

       Use Ctrl+C to quit this process

[vue-cli-service]   App running at:
[vue-cli-service]   - Local:   http://localhost:8100/
[vue-cli-service]   - Network: http://10.9.1.16:8100/
[vue-cli-service]   Note that the development build is not optimized.
[vue-cli-service]   To create a production build, run npm run build.
[vue-cli-service] No issues found.
> capacitor.cmd run android --no-sync --target Nexus_7_2012_API_30
[capacitor] × Running Gradle build - failed!
[capacitor] [error] 'sh' is not recognized as an internal or external command,
[capacitor]         operable program or batch file.
[capacitor]
[ERROR] An error occurred while running subprocess capacitor.

        capacitor.cmd run android --no-sync --target Nexus_7_2012_API_30 exited with exit code 1.

        Re-running this command with the --verbose flag may provide more information.

Notes:

Running in powershell on a windows directory: C:\repos

jcesarmobile commented 2 years ago

I still can't reproduce, tested on powershell 5 and 7 on two different computers.

Can you check the node_modules/cross-spawn/package.json and see which version is being used?

tigohenryschultz commented 2 years ago

"version": "7.0.3"

tigohenryschultz commented 2 years ago

Also note my machine is Windows 11 preview

jcesarmobile commented 2 years ago

Yeah, my guess is that it’s some bug on windows 11 since it’s still preview.

The only similar issue I’ve found was a bug in cross-spawn, but should be fixed in 7.0.3.

Does it work if you use the regular cmd instead of powershell?

tigohenryschultz commented 2 years ago

From regular cmd.exe same thing:

C:\repos\test>ionic cap run android -l --external
? Which device would you like to target? samsung SM-G998U1 (R5CRXX4FT6N)
> capacitor.cmd sync android
[capacitor] √ Copying web assets from dist to android\app\src\main\assets\public in 7.04ms
[capacitor] √ Creating capacitor.config.json in android\app\src\main\assets in 891.30μp
[capacitor] √ copy android in 25.62ms
[capacitor] √ Updating Android plugins in 4.16ms
[capacitor] [info] Found 4 Capacitor plugins for android:
[capacitor]        @capacitor/app@1.0.3
[capacitor]        @capacitor/haptics@1.0.3
[capacitor]        @capacitor/keyboard@1.0.3
[capacitor]        @capacitor/status-bar@1.0.3
[capacitor] √ update android in 62.50ms
[capacitor] [info] Sync finished in 0.091s
[WARN] Multiple network interfaces detected!

       You will be prompted to select an external-facing IP for the dev server that your device or emulator can access.
       Make sure your device is on the same Wi-Fi network as your computer. Learn more about Live Reload in the docs[1].
       To bypass this prompt, use the --public-host option (e.g. --public-host=10.9.1.16). You can alternatively bind
       the dev server to a specific IP (e.g. --host=10.9.1.16).

       [1]: https://ion.link/livereload-docs

? Please select which IP to use: 10.9.1.16 (Ethernet)
> vue-cli-service.cmd serve
[vue-cli-service]  INFO  Starting development server...

[INFO] Development server running!

       Local: http://localhost:8100
       External: http://10.9.X.XX:8100, http://XXX.XX.176.1:8100

       Use Ctrl+C to quit this process

[vue-cli-service]   App running at:
[vue-cli-service]   - Local:   http://localhost:8100/
[vue-cli-service]   - Network: http://10.9.1.16:8100/
[vue-cli-service]   Note that the development build is not optimized.
[vue-cli-service]   To create a production build, run npm run build.
> capacitor.cmd run android --no-sync --target R5CR114FT6N
[vue-cli-service] Issues checking in progress...
[vue-cli-service] No issues found.
[capacitor] × Running Gradle build - failed!
[capacitor] [error] 'sh' is not recognized as an internal or external command,
[capacitor]         operable program or batch file.
[capacitor]
[ERROR] An error occurred while running subprocess capacitor.

        capacitor.cmd run android --no-sync --target R5CR114FT6N exited with exit code 1.

        Re-running this command with the --verbose flag may provide more information.

C:\repos\test>
jcesarmobile commented 2 years ago

Ok, good to know it’s not powershell related then. What if you run npx cap run android?

tigohenryschultz commented 2 years ago

Same:

C:\repos\test>npx cap run android
√ Copying web assets from dist to android\app\src\main\assets\public in 7.53ms
√ Creating capacitor.config.json in android\app\src\main\assets in 1.16ms
√ copy android in 26.11ms
√ Updating Android plugins in 3.07ms
[info] Found 4 Capacitor plugins for android:
       @capacitor/app@1.0.3
       @capacitor/haptics@1.0.3
       @capacitor/keyboard@1.0.3
       @capacitor/status-bar@1.0.3
√ update android in 50.82ms
√ Please choose a target device: » samsung SM-G998U1 (R5CR114FT6N)
× Running Gradle build - failed!
[error] 'sh' is not recognized as an internal or external command,
        operable program or batch file.

C:\repos\test>
jcesarmobile commented 2 years ago

if you go to android folder and run gradlew assembleDebug on cmd or ./gradlew assembleDebug in powershell does it work?

tigohenryschultz commented 2 years ago

PS C:\repos\test> cd .\android\
PS C:\repos\test\android> ./gradlew assembleDebug

ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
PS C:\repos\test\android>

jcesarmobile commented 2 years ago

That means you don't have JAVA_HOME environment variable set and pointing to a valid java JDK installation. If you are using a recent Android Studio it already includes the JDK, so you can set the variable to use that JDK. If you installed Android Studio in the default place it should be at: C:\Program Files\Android\Android Studio\jre You should also add a new entry for the existing Path system variable like this %JAVA_HOME%\bin\ and close and reopen the cmd/powershell

tigohenryschultz commented 2 years ago

I currently use Capacitor 2.5.0 with no problems.

After setting JAVA_HOME here are the results for ./gradlew assembleDebug


PS C:\repos\test\android> ./gradlew assembleDebug                                                                      

Welcome to Gradle 7.0!                                                                                                 

Here are the highlights of this release:                                                                               
 - File system watching enabled by default                                                                             
 - Support for running with and building Java 16 projects                                                              
 - Native support for Apple Silicon processors                                                                         
 - Dependency catalog feature preview                                                                                  

For more details see https://docs.gradle.org/7.0/release-notes.html                                                    

Starting a Gradle Daemon (subsequent builds will be faster)                                                            

> Configure project :app                                                                                               
google-services.json not found, google-services plugin not applied. Push Notifications won't work                      
WARNING:: Using flatDirs should be avoided because it doesn't support any meta-data formats.                           
Currently detected usages:                                                                                             
- repository flatDir used in: project ':app', project ':capacitor-cordova-android-plugins'                             
WARNING:: Please remove usages of `jcenter()` Maven repository from your build scripts and migrate your build to other 
Maven repositories.                                                                                                    
This repository is deprecated and it will be shut down in the future.                                                  
See http://developer.android.com/r/tools/jcenter-end-of-service for more information.                                  
Currently detected usages in: root project 'android', project ':app', project ':capacitor-android', ...                

FAILURE: Build failed with an exception.                                                                               

* What went wrong:                                                                                                     
Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.                                         
> SDK location not found. Define location with an ANDROID_SDK_ROOT environment variable or by setting the sdk.dir path 
in your project's local properties file at 'C:\repos\test\android\local.properties'.                                   

* Try:                                                                                                                 
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with
 --scan to get full insights.                                                                                          

* Get more help at https://help.gradle.org                                                                             

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.                            
Use '--warning-mode all' to show the individual deprecation warnings.                                                  
See https://docs.gradle.org/7.0/userguide/command_line_interface.html#sec:command_line_warnings                        

BUILD FAILED in 10s                                                                                                    
PS C:\repos\test\android>                                                                                              
jcesarmobile commented 2 years ago

Capacitor 2.5.0 doesn’t have run command, capacitor 3 added run command which will create an apk, install it to the device and launch the app, and that’s what is failing.

For fixing the ANDROID_SDK_ROOT error you have to configure that environment variable too. https://developer.android.com/studio/command-line/variables

Anyway, you don’t need to keep testing, I don’t think that’s the problem, looks like it fails before running the gradlew command, I still think it’s a problem on cross-spawn

tigohenryschultz commented 2 years ago

Okay, thanks for looking into this!

shapebuster commented 2 years ago

I am also having this issue @tigohenryschultz did you manage to find a solution?

tigohenryschultz commented 2 years ago

@paulosllvn nope I had to run it on linux to get it to work.

shapebuster commented 2 years ago

Ok thanks. Seems like this error has made it not possible to run a development server with live reload on windows.

jcesarmobile commented 2 years ago

@paulosllvn, can you provide more information? I wasn't able to reproduce with the information provided by tigohenryschultz.

shapebuster commented 2 years ago

@jcesarmobile Thanks for your reply!

I have spent a couple of days now trying to pinpoint the issue with no success. I also am struggling to create an environment that replicates this issue.

What I can tell you is that this error occurs on the project that I am migrating from Cordova. I have attempted to create a brand new ionic capacitor project to test and ionic cap run android -l runs perfectly with no issues with the emulator.

ionic cap build android and running the app from android studio works fine. However, ionic cap run android -l fails with the above error that tigohenryschultz got.

I have tried comparing the android folders of the broken project and the test project to see any differences. All of the gradle files are identical. I have even tried copying the android folder of the test project into my broken project and I still get the same error. Grasping at straws at this stage. Happy to provide any further info at your request

tigohenryschultz commented 2 years ago

I have recently been forced to upgrade to capacitor 3 and ran into this issue again:

I have to specify the host or my phone doesn't show up as a device target:

ionic cap run android -l --host=10.9.1.16

Half way in compiling:

[npm] <s> [webpack.Progress] 64% building 595/652 modules 57 active C:\repos\tigosmart\node_modules\core-js\internals\array-method-is-strict.js
[capacitor] [error] 'sh' is not recognized as an internal or external command,
[capacitor]         operable program or batch file.
[capacitor]

Then further down:

[npm] <s> [webpack.Progress] 65% building 611/662 modules 51 active C:\repos\tigosmart\node_modules\css-loader\dist\cjs.js??ref--8-oneOf-1-1!C:\repos\tigosmart\node_modules\vue-loader-v16\dist\stylePostLoader.js!C:\repos\tigosmart\node_modules\postcss-loader\src\index.js??ref--8-oneOf-1-2!C:\repos\tigosmart\node_modules\sass-loader\dist\cjs.js??ref--8-oneOf-1-3!C:\repos\tigosmart\node_modules\cache-loader\dist\cjs.js??ref--0-0!C:\repos\tigosmart\node_modules\vue-loader-v16\dist\index.js??ref--0-1!C:\repos\tigosmart\src\components\TgInputWithValidation2.vue?vue&type=style&index=0&id=4e5e6768&lang=scss&scoped=true
[ERROR] An error occurred while running subprocess capacitor.

        capacitor.cmd run android --no-sync --target R5CR114FT6N exited with exit code 1.

Then it just stops proceeding and exits

tigohenryschultz commented 2 years ago

Running this command I immediately get the error:

PS C:\repos\tigosmart> capacitor.cmd run android --no-sync --target R5CR114FT6N
× Running Gradle build - failed!
[error] 'sh' is not recognized as an internal or external command,
        operable program or batch file.
tigohenryschultz commented 2 years ago

Clearly there is a bug in: https://github.com/ionic-team/ionic-cli/blob/5ca557bad2e04a147fc5aed9fbf0298def628a0e/packages/%40ionic/utils-terminal/src/info.ts

tigohenryschultz commented 2 years ago

Hard coded ./gradle to ./gradlew.bat here:

image

Fix my 'sh' command issue

jcesarmobile commented 2 years ago

That shouldn't be necessary, cross-spawn is supposed to handle that, as long as you have version 7.0.3 (that you said you have).

Make sure you have the PATHEXT environment variable configured and that it includes .BAT entry. It's included by default, but maybe it got deleted or modified. You can check from CMD by running echo %PATHEXT% or from PowerShell by running echo $env:PATHEXT, or checking the system variables on windows configuration. cross-spawn checks that variable for knowing which executable to search for.

What's your @ionic/cli version? it also uses cross-spawn and if you have an old version it might conflict with the version that capacitor tries to use. Latest is 6.18.1.

tigohenryschultz commented 2 years ago

Still running into this bug:

Here is my ionic info:

PS C:\repos\router-undefined-reference> ionic info [WARN] Error loading @capacitor/ios package.json: Error: Cannot find module '@capacitor/ios/package'

   Require stack:
   - C:\Users\Henry\AppData\Roaming\npm\node_modules\@ionic\cli\lib\project\index.js
   - C:\Users\Henry\AppData\Roaming\npm\node_modules\@ionic\cli\lib\index.js
   - C:\Users\Henry\AppData\Roaming\npm\node_modules\@ionic\cli\index.js
   - C:\Users\Henry\AppData\Roaming\npm\node_modules\@ionic\cli\bin\ionic

Ionic:

Ionic CLI : 6.16.3 (C:\Users\Henry\AppData\Roaming\npm\node_modules\@ionic\cli) Ionic Framework : @ionic/vue 5.9.2

Capacitor:

Capacitor CLI : 3.3.2 @capacitor/android : 3.3.3 @capacitor/core : 3.3.2 @capacitor/ios : not installed

Utility:

cordova-res : not installed globally native-run : 1.5.0

System:

NodeJS : v14.17.3 (C:\Program Files\nodejs\node.exe) npm : 6.14.13 OS : Windows 10

echo $env:PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL

jcesarmobile commented 1 year ago

closing since I couldn't reproduce and there has not been any recent activity in the last year and half. to me, it looks like a bug on cross-spawn that might have been fixed since nobody else has faced this issue.

If anybody else faces this issue, please, report a new one and provide all the possible information about windows versions, powershell version, node versions, etc. and a sample app.