diff mbox series

[v2] cooker: make the event log path absolute

Message ID 20261118215423.7-1-bbnpreetsingh@gmail.com
State New
Headers show
Series [v2] cooker: make the event log path absolute | expand

Commit Message

Babanpreet Singh Nov. 18, 2026, 9:54 p.m. UTC
The server's current directory may not be the one bitbake was run
from, so a relative -w path is not safe to hand over. A bare file name
fails outright:

  FileNotFoundError: [Errno 2] No such file or directory: ''

Resolve the -w path in main.py, and show an error if
BB_DEFAULT_EVENTLOG is relative.

Fixes: ee174b231897 ("cooker: Add support for BB_DEFAULT_EVENTLOG")
AI-Generated: Uses Claude (claude-opus-5-5)
Signed-off-by: Babanpreet Singh <bbnpreetsingh@gmail.com>
---

 lib/bb/cooker.py       |  2 ++
 lib/bb/main.py         |  4 ++++
 lib/bb/tests/cooker.py | 45 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+)
diff mbox series

Patch

diff --git a/lib/bb/cooker.py b/lib/bb/cooker.py
index a6bc2827d..54f87278f 100644
--- a/lib/bb/cooker.py
+++ b/lib/bb/cooker.py
@@ -314,6 +314,8 @@  class BBCooker:
 
         eventlog = self.data.getVar("BB_DEFAULT_EVENTLOG")
         if not self.configuration.writeeventlog and eventlog:
+            if not os.path.isabs(eventlog):
+                bb.fatal("BB_DEFAULT_EVENTLOG must be an absolute path, not \"%s\"" % eventlog)
             self.setupEventLog(eventlog)
 
         if consolelog:
diff --git a/lib/bb/main.py b/lib/bb/main.py
index 5cce8eb27..5a49f73b3 100755
--- a/lib/bb/main.py
+++ b/lib/bb/main.py
@@ -338,6 +338,10 @@  class BitBakeConfigParameters(cookerdata.ConfigParameters):
             eventlog = "bitbake_eventlog_%s.json" % datetime.now().strftime("%Y%m%d%H%M%S")
             options.writeeventlog = eventlog
 
+        # the server may not share our current directory
+        if options.writeeventlog:
+            options.writeeventlog = os.path.abspath(options.writeeventlog)
+
         if options.bind:
             try:
                 #Checking that the port is a number and is a ':' delimited value
diff --git a/lib/bb/tests/cooker.py b/lib/bb/tests/cooker.py
index db4104d40..923adc156 100644
--- a/lib/bb/tests/cooker.py
+++ b/lib/bb/tests/cooker.py
@@ -409,3 +409,48 @@  addtask marker
             with open(os.path.join(builddir, "marker.log")) as f:
                 self.assertEqual(f.read(), "bbappend-applied",
                                  "bitbake -b did not apply the recipe's bbappend")
+
+
+class EventLogTest(_BitbakeSubprocessTestCase):
+    """Tests for the event log ("bitbake -w" and BB_DEFAULT_EVENTLOG)."""
+
+    _parsetests = os.path.realpath(os.path.join(os.path.dirname(__file__), "parse-tests"))
+
+    def _make_env(self, builddir):
+        env = os.environ.copy()
+        env.pop("BBEVENTLOG", None)
+        env["BBPATH"] = self._parsetests
+        env["BB_ENV_PASSTHROUGH_ADDITIONS"] = "TOPDIR BB_DEFAULT_EVENTLOG"
+        env["TOPDIR"] = builddir
+        return env
+
+    def test_write_log_path_is_absolute(self):
+        """The -w path is resolved against the client's directory, since the
+        server may run in another one."""
+        import bb.main
+        for arg in ("events.json", ""):
+            with self.subTest(arg=arg):
+                params = bb.main.BitBakeConfigParameters(["bitbake", "-w", arg])
+                self.assertTrue(os.path.isabs(params.writeeventlog))
+                self.assertEqual(os.path.dirname(params.writeeventlog), os.getcwd())
+
+    def test_write_log_bare_filename(self):
+        """bitbake -w must accept a file name without a directory."""
+        with self._build_dir(prefix="eventlogtest") as builddir:
+            self._run_subprocess(["bitbake", "-w", "events.json", "-e"],
+                                 self._make_env(builddir), builddir)
+
+            self.assertTrue(os.path.exists(os.path.join(builddir, "events.json")))
+
+    def test_default_eventlog_relative_is_error(self):
+        """A relative BB_DEFAULT_EVENTLOG is refused with an error."""
+        with self._build_dir(prefix="eventlogtest") as builddir:
+            env = self._make_env(builddir)
+            env["BB_DEFAULT_EVENTLOG"] = "events.json"
+            proc = subprocess.run(["bitbake", "-e"], env=env, cwd=builddir,
+                                  stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
+                                  universal_newlines=True)
+
+            self.assertNotEqual(proc.returncode, 0, proc.stdout)
+            self.assertRegex(proc.stdout, r"ERROR: .*BB_DEFAULT_EVENTLOG")
+            self.assertFalse(os.path.exists(os.path.join(builddir, "events.json")))