Repository: napari/napari

Issue:
Bundles on Mac and Windows build inconsistently
## 🐛 Bug
Bundles on Mac and Windows build inconsistently, seemingly due to some changes since 0.4.0, and occasionally display the following error:

![image](https://user-images.githubusercontent.com/17995243/99015803-da791080-25a9-11eb-91a9-810915c0958a.png)

The bundle cannot find the `rcc.exe` and/or `pyside2-rcc.exe` files despite these both existing in the bundle installation `app-packages/pyside` and `app-packages/bin` folders respectively. This does not happen if you have PySide installed in your base environment, or if you first run the napari bundle from an environment which does have PySide installed.

## To Reproduce

Steps to reproduce the behavior:

1. Pull latest master
2. Ensure your base environment does not have PySide installed
3. Create a new environment with `python=3.7` and `pip`
4. Run
```
python -m pip install briefcase==0.3.1 tomlkit wheel dmgbuild>=1.4.2
python -m pip install -e .[pyside2]
python bundle.py
```
5. Install the resulting .msi
6. Try to run napari from start menu.

## Expected behavior

Bundle builds consistently regardless of what environments contain what packages on the build machine.

## Additional context
This could potentially be fixed by:
- Pre-building resources in the bundling script
- Limiting the available paths when bundling e.g. not allowing the bundle to rely on anything outside of the napari installation folders
- Explicitly listing the relative paths of the `rcc` files?

This issue does not occur with the 0.4.0 release on Windows as far as I'm aware.


Discussion:
@GenevieveBuckley @ziyangczi I think I've captured most of what we discussed in the meeting? But I'm sure I've missed some details so feel free to expand of course
My preferred solution would be to pre-build the resources in the bundling script.
> My preferred solution would be to pre-build the resources in the bundling script.

what's a little confusing here, is that this _should_ already be happening... or at least that was my intention with this line in bundle.py
https://github.com/napari/napari/blob/bf0c7d081b644c1c19488fc69df5f03460275f3e/bundle.py#L184-L185
(i.e. it runs `napari --info` prior to `briefcase create`, which, by importing napari, builds `_qt_resources.py` if it doesn't exist, prior to ).  In fact, I had to add that so that gatekeeper would allow the bundle to open (it can't change in anyway after code signing but before opening napari, or mac gatekeeper will reject).
@DragaDoncila: can you check whether there is a `napari/resources/_qt_resources*.py` in your bundle?
@tlambert03 I have the following potentially relevant files in the `resources` folder `C:\Users\ddonc\AppData\Local\Programs\napari\app\napari\resources`:

```
_qt_resources_0.4.0_pyside2_5.14.2.py
_qt_resources_0.4.1a1_pyside2_5.15.1.py
_qt_resources_0.4.1a2.dev0+gabdcfb2.d20201110_pyside2_5.15.1.py
_qt_resources_0.4.1a2.dev0+gabdcfb2.d20201112_pyqt5_5.15.1.py
_qt_resources_0.4.1a2.dev1+g4c632d6_pyside2_5.15.1.py
build_icons.py
```

Oops, this is in the working installation sorry - I'm getting the broken install now
oh! this could be relevant!  go back to your napari source folder, delete _all_ the `napari/resources/_qt_resources*.py` files (it's fine, they'll regenerate), then re-run `bundle.py`.  If that works, then we can add a line to `bundle.py` that deletes them.
This would never be the case in the github action, since it clones a fresh repo (and that file is not checked in), so that could partially explain the local-only issues?
I should say whenever I tried a new bundle, I'd delete the napari folder in AppData after uninstalling from Control Panel, and then I'd run the new msi
Ok, so just tried installing and running the broken install again. I got the same error as in the description.

These are the files inside the `resources` folder:

```
_qt_resources_0.2.3+283.g816b02e_pyside2_5.14.2.py
_qt_resources_0.3.1+10.g9566375.dirty_pyside2_5.14.2.py
_qt_resources_0.3.1+10.g9566375_pyside2_5.14.2.py
_qt_resources_0.3.1+11.g0e6a902.dirty_pyside2_5.14.2.py
_qt_resources_0.3.1+11.g0e6a902_pyside2_5.14.2.py
_qt_resources_0.3.1+12.g0cd943c_pyside2_5.14.2.py
_qt_resources_0.3.1+12.gaaab058_pyside2_5.14.2.py
_qt_resources_0.3.1+16.g987321a.dirty_pyside2_5.14.2.py
_qt_resources_0.3.1+17.g1733c94.dirty_pyside2_5.14.2.py
_qt_resources_0.3.1+21.g88c0a1a.dirty_pyside2_5.14.2.py
_qt_resources_0.3.1+22.g2634547.dirty_pyside2_5.14.2.py
_qt_resources_0.3.1+25.geb75ecc.dirty_pyside2_5.14.2.py
_qt_resources_0.3.1+25.geb75ecc_pyside2_5.14.2.py
_qt_resources_0.3.1+26.g884562f.dirty_pyside2_5.14.2.py
_qt_resources_0.3.1+27.gbb57edf.dirty_pyside2_5.14.2.py
_qt_resources_0.3.1+29.g1b9a8fa_pyside2_5.14.2.py
_qt_resources_0.3.1+29.g35daca5.dirty_pyside2_5.14.2.py
_qt_resources_0.3.1+29.g35daca5_pyside2_5.14.2.py
_qt_resources_0.3.1+31.g6832918_pyside2_5.14.2.py
_qt_resources_0.3.1+36.gd9421ae.dirty_pyside2_5.14.2.py
_qt_resources_0.3.1+36.gd9421ae_pyside2_5.14.2.py
_qt_resources_0.3.1+36.gf87e186.dirty_pyside2_5.14.2.py
_qt_resources_0.3.1+36.gf87e186_pyside2_5.14.2.py
_qt_resources_0.3.1+37.g93f9a0e.dirty_pyside2_5.14.2.py
_qt_resources_0.3.1+37.g9c2d9df.dirty_pyside2_5.14.2.py
_qt_resources_0.3.1+37.g9c2d9df_pyside2_5.14.2.py
_qt_resources_0.3.1+39.gbb87121_pyside2_5.14.2.py
_qt_resources_0.3.1+4.gf0ab04a.dirty_pyside2_5.14.2.py
_qt_resources_0.3.1+4.gf0ab04a_pyside2_5.14.2.py
_qt_resources_0.3.7.dev64+g504ef2f.d20200923_pyqt5_5.14.2.py
_qt_resources_0.3.9.dev16+g1cedd191_pyqt5_5.14.2.py
_qt_resources_0.3.9.dev53+gb230dc22_pyqt5_5.14.2.py
_qt_resources_0.3.9.dev81+g56d18b45_pyqt5_5.14.2.py
_qt_resources_0.3.9.dev81+g56d18b45_pyside2_5.14.2.py
_qt_resources_0.4.1.dev14+g205be9f3_pyqt5_5.14.2.py
_qt_resources_0.4.1.dev26+gabdcfb2d_pyside2_5.15.1.py
_qt_resources_not-installed_pyside2_5.14.2.py

```


good lord!  ok, try the deletion and rebundle that I [mentioned above](https://github.com/napari/napari/issues/1865#issuecomment-726455967) ...?
So those were newly generated by the msi I ran, but I'll try explicitly deleting the files in that folder and then re-running bundle.py and seeing if that changes anything. I'm out at the moment sorry, so will be later this afternoon. 
I was also suffering the same issue on mac bundled, somehow deleting the app and reinstall addressed the issue 🤔 so we should discourage people from upgrading in place by overwrite app?
I've got the same behaviour even when removing all mention of napari from start menu and deleting the folder completely, as well as on a completely different machine, so I don't think it's just the overwriting. 
> deleting the files in that folder

just in case it wasn't clear, I'm not talking about the files in the napari folder in AppData (though. it's not a bad idea to delete those too) ... I'm talking about deleting all of the `_qt_resources.py` files in your napari git repo before running `bundle.py`

> I was also suffering the same issue on mac bundled, somehow deleting the app and reinstall addressed the issue 🤔 so we should discourage people from upgrading in place by overwrite app?

hmm... on mac overwriting the `/Applications/napari.app` bundle should be absolute.  you saw the same py-rcc issue?
> hmm... on mac overwriting the `/Applications/napari.app` bundle should be absolute. you saw the same py-rcc issue?

yes, I originally saw the issue with 0.4.0 release, and then went on to debug the issue, I found that on mac bundle, build_icons.py actually have a bug where pyside2-rcc does not exist under the same bin as the python executable, because briefcase created a Support/bin/python, but everything we package is under app_packages/bin/ 
I think the reason it has been working is because the bundled app is able to find the pyside2 rcc from system path


> then went on to debug the issue, I found that on mac bundle, build_icons.py actually have a bug where pyside2-rcc does not exist under the same bin as the python executable, because briefcase created a Support/bin/python, but everything we package is under app_packages/bin/

did you create an issue for this?
> > then went on to debug the issue, I found that on mac bundle, build_icons.py actually have a bug where pyside2-rcc does not exist under the same bin as the python executable, because briefcase created a Support/bin/python, but everything we package is under app_packages/bin/
> 
> did you create an issue for this?

nope, we were talking about this in the meeting, I was hoping to get more report before making an issue, and it seems more complicated than the path I originally discovered, 0.4.0 release is somehow working again after I reinstall, which doesn't quite make sense. Is the build icon step skipped somehow when the resource is available?
> go back to your napari source folder, delete _all_ the `napari/resources/_qt_resources*.py` files (it's fine, they'll regenerate), then re-run `bundle.py`.

@tlambert03 I tried this and got the same error when running the installed app from start menu.

The only file replaced back into `napari/resources` was 

```
_qt_resources_0.4.1a2.dev2+gbf0c7d0_pyside2_5.15.1.py
```
I just want to add a note that this mainly seems to effect people building their own version of the bundle and not the version built via our CI (if I'm not mistaken)
@sofroniewn 

> (if I'm not mistaken)

You are, unfortunately. It's something to do with master. @DragaDoncila was able to build 0.4.0 fine on her machine, and the 0.4.1rc builds fail with the same error.

Resolving patch:
diff --git a/bundle.py b/bundle.py
--- a/bundle.py
+++ b/bundle.py
@@ -18,7 +18,7 @@
 # PySide2:
 # python bundle.py --add 'PySide2==5.15.0' 'ome-zarr'
 
-EXTRA_REQS = ["pip", "PySide2==5.14.2.2", "scikit-image", "zarr"]
+EXTRA_REQS = ["pip", "PySide2==5.14.2.3", "scikit-image", "zarr"]
 
 
 WINDOWS = os.name == 'nt'


Test patch: