Repository: AppImageCrafters/appimage-builder

Issue:
Properly handle shebangs set in previous builds
WARNING:root:Required interpreter 'appimage-f5d659fa-db6f-4a84-b46b-8893153ca973-env' could not be found in the AppDir while processing /home/msalvatore/appimage/../monkey-appdir/usr/src/monkey_island/cc/ui/node_modules/node-sass/src/libsass/script/ci-install-compiler

Comments:

Patch:
diff --git a/appimagebuilder/builder/runtime/executables_wrapper.py b/appimagebuilder/builder/runtime/executables_wrapper.py
index 05033e7..949c8ae 100644
--- a/appimagebuilder/builder/runtime/executables_wrapper.py
+++ b/appimagebuilder/builder/runtime/executables_wrapper.py
@@ -27,6 +27,8 @@ from appimagebuilder.common import file_utils
 
 
 class ExecutablesWrapper:
+    EXPORTED_FILES_PREFIX = "/tmp/appimage-"
+
     def __init__(
         self,
         appdir_path: str,
@@ -115,7 +117,10 @@ class ExecutablesWrapper:
 
     def _rewrite_shebang_using_env(self, executable):
         logging.info("Replacing SHEBANG on: %s" % executable.path)
-        local_env_path = "/tmp/appimage-" + self.env.get("APPIMAGE_UUID") + "-env"
+        local_env_path = "%s%s-env" % (
+            self.EXPORTED_FILES_PREFIX,
+            self.env.get("APPIMAGE_UUID"),
+        )
         tmp_path = executable.path.__str__() + ".tmp"
         output = open(tmp_path, "wb")
         try:
@@ -136,7 +141,13 @@ class ExecutablesWrapper:
 
     def _write_rel_shebang(self, executable, local_env_path, output):
         output.write(b"#!%s" % local_env_path.encode())
-        args_start = 2 if executable.shebang[0] == "/usr/bin/env" else 1
+        shebang_main = executable.shebang[0]
+        if shebang_main.startswith("/usr/bin/env") or shebang_main.startswith(
+            self.EXPORTED_FILES_PREFIX
+        ):
+            args_start = 2
+        else:
+            args_start = 1
         bin_name = os.path.basename(executable.shebang[args_start - 1])
         output.write(b" ")
         output.write(bin_name.encode())


Test patch:
diff --git a/tests/builder/runtime/test_executables_wrapper.py b/tests/builder/runtime/test_executables_wrapper.py
index c8a1068..b9edf2e 100644
--- a/tests/builder/runtime/test_executables_wrapper.py
+++ b/tests/builder/runtime/test_executables_wrapper.py
@@ -89,6 +89,22 @@ class TestExecutablesWrapper(TestCase):
 
         self.assertEqual(expected, result)
 
+    def test_wrap_previously_wrpped_interpreted_executable(self):
+        resolver = FakeAppRunBinariesResolver()
+        environment = Environment()
+        environment.set("APPIMAGE_UUID", "UUID")
+        wrapper = ExecutablesWrapper(self.data_dir, resolver, environment)
+        executable = InterpretedExecutable(
+            self.script_path, ["/tmp/appimage-OLD-UUID-env", "python3"]
+        )
+        wrapper.wrap(executable)
+
+        result = self.script_path.read_text()
+        expected = "#!/tmp/appimage-UUID-env python3\n" "1234567890\n"
+        self.assertTrue(os.access(self.bin_path, os.X_OK | os.R_OK))
+
+        self.assertEqual(expected, result)
+
     def test_generate_executable_env(self):
         resolver = FakeAppRunBinariesResolver()
         environment = Environment()