[gnome-builder] flatpak: fix wrong cwd when running a command through "flatpak build"



commit 2d339299c8dd6e7101633951912d5ba687431a11
Author: Giovanni Campagna <gcampagn cs stanford edu>
Date:   Fri Mar 23 11:59:22 2018 -0700

    flatpak: fix wrong cwd when running a command through "flatpak build"
    
    "flatpak build" will set the cwd of the run command to the build
    dir, regardless of what directory it is run from.
    Previously, this would cause us to ignore the cwd set on subprocess
    launchers created in a flatpak runtime.
    In turn, this would break subprocesses that rely on the cwd to
    find configuration, such as eslint, which uses the cwd to resolve
    the location of the source file and from there the .eslintrc.yml
    file.

 src/plugins/flatpak/gbp-flatpak-runtime.c          |  3 --
 .../flatpak/gbp-flatpak-subprocess-launcher.c      | 59 +++++++++++++---------
 2 files changed, 36 insertions(+), 26 deletions(-)
---
diff --git a/src/plugins/flatpak/gbp-flatpak-runtime.c b/src/plugins/flatpak/gbp-flatpak-runtime.c
index f19c297d8..132e3992a 100644
--- a/src/plugins/flatpak/gbp-flatpak-runtime.c
+++ b/src/plugins/flatpak/gbp-flatpak-runtime.c
@@ -205,17 +205,14 @@ gbp_flatpak_runtime_create_launcher (IdeRuntime  *runtime,
           g_autofree gchar *filesystem_option_src = NULL;
           g_autofree gchar *filesystem_option_build = NULL;
           g_autofree gchar *filesystem_option_cache = NULL;
-          g_autofree gchar *build_dir_option = NULL;
 
           filesystem_option_src = g_strdup_printf ("--filesystem=%s", project_path);
           filesystem_option_build = g_strdup_printf ("--filesystem=%s", builddir);
           filesystem_option_cache = g_strdup_printf ("--filesystem=%s/gnome-builder", g_get_user_cache_dir 
());
-          build_dir_option = g_strdup_printf ("--build-dir=%s", builddir);
           ide_subprocess_launcher_push_argv (ret, "--nofilesystem=host");
           ide_subprocess_launcher_push_argv (ret, filesystem_option_cache);
           ide_subprocess_launcher_push_argv (ret, filesystem_option_src);
           ide_subprocess_launcher_push_argv (ret, filesystem_option_build);
-          ide_subprocess_launcher_push_argv (ret, build_dir_option);
         }
       new_environ = ide_configuration_get_environ (IDE_CONFIGURATION (configuration));
       if (g_strv_length (new_environ) > 0)
diff --git a/src/plugins/flatpak/gbp-flatpak-subprocess-launcher.c 
b/src/plugins/flatpak/gbp-flatpak-subprocess-launcher.c
index e8edca708..d3762a07d 100644
--- a/src/plugins/flatpak/gbp-flatpak-subprocess-launcher.c
+++ b/src/plugins/flatpak/gbp-flatpak-subprocess-launcher.c
@@ -34,44 +34,57 @@ gbp_flatpak_subprocess_launcher_spawn (IdeSubprocessLauncher  *launcher,
 {
   const gchar * const * envp;
   IdeSubprocess *ret;
+  const gchar * const * argv;
+  guint argpos = 0;
+  g_autofree gchar *build_dir_option = NULL;
 
   IDE_ENTRY;
 
   g_assert (IDE_IS_SUBPROCESS_LAUNCHER (launcher));
   g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
 
+
   /*
    * The "flatpak build" command will filter out all of our environment variables
-   * from the subprocess. So we need to look at our configured environment and
-   * convert the KEY=VALUE pairs into --env=key=value command line arguments.
+   * from the subprocess, and change the current directory to the build dir.
+   * So we need to look at our configured environment and convert the
+   * KEY=VALUE pairs into --env=key=value command line arguments, and set the appropriate
+   * --build-dir
    */
 
-  envp = ide_subprocess_launcher_get_environ (launcher);
+  argv = ide_subprocess_launcher_get_argv (launcher);
 
-  if (envp != NULL)
+  /*
+   * Locate the position after our ["flatpak", "build"] arguments.
+   */
+  for (argpos = 0; argv[argpos] != NULL; argpos++)
     {
-      const gchar * const * argv;
-      guint argpos = 0;
-
-      argv = ide_subprocess_launcher_get_argv (launcher);
-
-      /*
-       * Locate the position after our ["flatpak", "build"] arguments.
-       */
-      for (argpos = 0; argv[argpos] != NULL; argpos++)
-        {
-          if (g_strcmp0 (argv[argpos], "flatpak") == 0)
-            break;
-        }
-      for (; argv[argpos] != NULL; argpos++)
+      if (g_strcmp0 (argv[argpos], "flatpak") == 0)
+        break;
+    }
+  for (; argv[argpos] != NULL; argpos++)
+    {
+      if (g_strcmp0 (argv[argpos], "build") == 0)
         {
-          if (g_strcmp0 (argv[argpos], "build") == 0)
-            {
-              argpos++;
-              break;
-            }
+          argpos++;
+          break;
         }
+    }
 
+  build_dir_option = g_strdup_printf ("--build-dir=%s",
+                                      ide_subprocess_launcher_get_cwd (launcher));
+
+  /*
+   * Since this can be called multiple times, we have to avoid re-adding
+   * the --build-dir= parameters a second (or third, or fourth) time.
+   */
+  if (!g_strv_contains (argv, build_dir_option))
+    ide_subprocess_launcher_insert_argv (launcher, argpos, build_dir_option);
+
+  envp = ide_subprocess_launcher_get_environ (launcher);
+
+  if (envp != NULL)
+    {
       /*
        * Since this can be called multiple times, we have to avoid re-adding
        * the --env= parameters a second (or third, or fourth) time.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]