NativeScript / nativescript-cli

Command-line interface for building NativeScript apps
https://www.npmjs.com/package/nativescript
Apache License 2.0
1.04k stars 195 forks source link

TNS syncAllFiles on Android frequently crashes with error #4264

Closed NathanaelA closed 5 years ago

NathanaelA commented 5 years ago

Environment Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

Describe the bug I use tns run android --syncAllFiles while developing my plugin; but the issue is about every 4 to 10 updates; the CLI comes back with the error: Unable to apply changes for device:<DEVICE_ID>. Error is: The parser encountered an unexpected exception.. Sometimes it does not report any error but just stops without any messages.

To Reproduce Make changes to a plugin in the node_modules folder and let tns resync the app.

Expected behavior It to continue working. :grinning:

Additional context When it dies; I can't hit CTRL-C to abort it; have to find the tns process and kill it... (Which is also very annoying).

DimitarTachev commented 5 years ago

Hi @NathanaelA,

I tried to reproduce the issue using the image picker plugin but everything is working as expected. I've changed a few js plugin files, using a linked plugin and the typescript demo. Also, we've never seen such error in our plugins. Could you please specify the following details: 1) What kind of demo application do you use - js, ts, ng, vue? 2) Is your plugin linked in node_modules or installed from a local .tgz? 3) What kind of files do you change in order to trigger the LiveSyncs (ts, js, xml, css)?

Also, a sample application reproducing the problematic behavior will be appreciated.

NathanaelA commented 5 years ago

Hi @DimitarTachev,

  1. This is a pure JS demo app.
  2. Plugin is npm linked from the ../src directory into the node_modules folder.
  3. Changing the plugin's JS code typically.

Additional information;

  1. I am seeing the same issue (no error printed typically) on iOS also, though it isn't as often as I was seeing on Android. But I also have to use ps -Af | grep tns to find the tns command to kill it; because ctrl-c doesn't work...

  2. About 30% of the time on android when this happens, I have to nuke the platforms folder; as the platforms folder is now in a corrupted state.

DimitarTachev commented 5 years ago

Hi @NathanaelA,

We tried the same steps both on Ubuntu and MacOS: 1) Clone the Image Picker plugin. 2) Execute npm i in its src folder. 3) Execute tns run android --syncAllFiles in its demo folder. 4) Apply some change in the plugins ts/js file (e.g. in https://github.com/NativeScript/nativescript-imagepicker/blob/master/src/imagepicker.android.ts) 5) Repeat (4) 30+ times.

but everything is working as expected on our side.

As far as I see this error can be thrown during .apk installation, which is not an expected operation during LiveSync of JS files. We've seen such behavior when the Android Emulator's folders limit is reached and we are not able to execute the LiveSync operation.

Could you please provide the trace logs from a problematic LiveSync of your demo app (tns run android --syncAllFiles --log trace) in order to allow us investigate this issue further? Also, I suggest you try the above-mentioned steps with the Image Picker repository in order to check if this is something related to a specific code in your plugin.

NathanaelA commented 5 years ago

I'll get you a log... This isn't related to the any specific plugin as I've seen it on several plugins, I've worked on. (I actually think I've seen it on all the plugins I've worked on recently; because it has become second nature to do ps -Af | grep tns then kill -9 x Actually have seriously considered making a killtns command macro to handle it as it has become such an annoyance.

NathanaelA commented 5 years ago

Here is a log of the ctrl-c issue on linux:

Project dir from hooksArgs is: /home/nathanael/projects/NativeScript/repos/nativescript-sqlite/demo.
Hooks directories: [ '/home/nathanael/projects/NativeScript/repos/nativescript-sqlite/demo/hooks' ]
AfterHookName for command watch is after-watch
Executing after-watch hook from /home/nathanael/projects/NativeScript/repos/nativescript-sqlite/demo/hooks/after-watch/nativescript-dev-webpack.js
Executing after-watch hook at location /home/nathanael/projects/NativeScript/repos/nativescript-sqlite/demo/hooks/after-watch/nativescript-dev-webpack.js in-process
Validating after-watch arguments.
Stopping webpack watch
spawn: /home/nathanael/Android/Sdk/platform-tools/adb "-s" "emulator-5554" "shell" "rm" "-rf" "/data/local/tmp/org.nativescript.demo-livesync-in-progress"
Result when throw error is false:
{ stdout: '', stderr: '', exitCode: 0 }

Each time I hit CTRL-C it repeats this.

DimitarTachev commented 5 years ago

Unfortunately, these are the regular logs from our CTRL + C handling. If the CLI process does not exit after these logs, this means that there is a leaked child process or a socket.

In order to investigate both the CTRL + C handling and the Unable to apply changes for device:<DEVICE_ID>. Error is: The parser encountered an unexpected exception.., we need your trace logs from the whole LiveSync process.

Also, do you get these error on the same device/emulator or they are not depending on a specific device?

NathanaelA commented 5 years ago

@DimitarTachev

Well, I am working on another plugin right now; so I'll capture the logs for the entire session -- do you have a place to upload them -- the logs that are generated with trace are massive.

DimitarTachev commented 5 years ago

@NathanaelA

Maybe the easiest approach is to use tns run android --syncAllFiles --log trace | tee log.txt and attach (drag & drop) the generated log.txt in a comment here.

NathanaelA commented 5 years ago

@DimitarTachev - I have since that point upgraded the CLI to v5.2.1; so the issue now which happens very frequently is attached in this log file. I do not know if this is the same root cause; but it is related in that it kills the sync'ng and requires me the majority of the time to have to delete the platforms/android folder to continue.

Attached is the logfile.
log.txt

A couple additional notes: CTRL-C seems much more reliable in 5.2.1, I haven't had any issues cancelling TNS. So that sub-issue might be fixed in the 5.2's.

The error thrown: Unable to apply changes on device: emulator-5554. Error is: cp: cannot create directory '/home/nathanael/projects/NativeScript/repos/nativescript-sqlite/demo/platforms/android/app/src/main/assets/app/App_Resources': No such file or directory.

The ../assets/app folder is missing at this point, not sure what deleted it... It is their during the first run.

One thing I just thought of -- I do have installed nativescript-dev-webpack": "^0.20.2" so maybe one of its hooks is running and breaking things even though I am not passing the --bundle command.
Update: I tried deleting webpack and see if it makes any differences -- npm removing webpack and removing all its hooks did not change this issue. The error is still the same; the assets/app folder seems to be being deleted by something in tns. And since the assets/app folder is gone, this causes all future run's to fail until I manually create a new "assets/app" folder, or delete the platforms/android folder.

DimitarTachev commented 5 years ago

Thanks for the logs and the additional details.

Unfortunately, we were not able to find the reason for the missing assets/app folder from the logs. The folder is not deleted from a hook as the webpack hooks are skipped internally when you run without --bundle and it seems that you don't have any additional hooks.

We also tried to reproduce this behavior with the free version of the SQLite plugin and exactly the same CLI and webpack versions but everything is working as expected when we run tns run android --syncAllFiles and make changes in node_modules/nativescript-sqlite/sqlite.android.js.

Could you try the following actions: 1) Ensure there aren't any leaked CLI or Webpack process before you run tns run android --syncAllFiles - the issue could be caused by a leaked CLI process which is deleting the above-mentioned folder (if you don't have any important node processes, the easiest solution is killall -9 node). 2) Try to reproduce the issue in the demo of the free version of your SQLite plugin (reviewing your logs, we found out that you are making changes in node_modules/nativescript-sqlite-sync/sync.js which is not available for testing).

NathanaelA commented 5 years ago

@DimitarTachev - This specific issue in this case appears to be related to having a .git directory inside one of the plugins paths. So for example in my sqlite plugin, I have a .git at the root folder; but I also have a .git folder in the "encrypted" and a .git folder in my commercial folders. It appears that NativeScript is still having issues sync'ing when their is a .git folder inside with the source. I would have figured this would have been fixed by v5 as not ignoring hidden folders is crazy...

So this is related to: https://github.com/NativeScript/nativescript-cli/issues/3028
Be nice if this was actually fixed at some point. Nasty gotcha still... before I close it, I'll do some tests on a plugin that doesn't have a .git folder inside it...

rosen-vladimirov commented 5 years ago

Hey @NathanaelA , As in NativeScript 6.0 we are going to support only bundle workflow and syncAllFiles option is not supported anymore, this issue should not be reproduced anymore. Can we consider it as resolved?

NathanaelA commented 5 years ago

@rosen-vladimirov - Yes, we can close it since it is a moot point... :)

rynop commented 5 years ago

With 6.0, how do I pick up a change I made manually in a node_modules dir? Blow away platforms dir and re-run tns run ..?