From 035526fe04a781e33129eef2a8af1ce7b175eb2d Mon Sep 17 00:00:00 2001 From: von Heydebrand Julian Date: Wed, 13 Mar 2024 16:21:22 +0100 Subject: [PATCH] Gracefully handle outdated .ninja_log during '-t recompact' When we explicitly unlink the file we should return LOAD_NOT_FOUND instead of LOAD_SUCCESS --- misc/output_test.py | 23 +++++++++++++++++++++++ src/build_log.cc | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/misc/output_test.py b/misc/output_test.py index 78848cbd4c..13b09269e0 100755 --- a/misc/output_test.py +++ b/misc/output_test.py @@ -127,6 +127,29 @@ def test_pr_1685(self): self.assertEqual(run('', flags='-t recompact'), '') self.assertEqual(run('', flags='-t restat'), '') + def test_issue_2048(self): + with tempfile.TemporaryDirectory() as d: + with open(os.path.join(d, 'build.ninja'), 'w'): + pass + + with open(os.path.join(d, '.ninja_log'), 'w') as f: + f.write('# ninja log v4\n') + + try: + output = subprocess.check_output([NINJA_PATH, '-t', 'recompact'], + cwd=d, + env=default_env, + stderr=subprocess.STDOUT, + text=True + ) + + self.assertEqual( + output.strip(), + "ninja: warning: build log version is too old; starting over" + ) + except subprocess.CalledProcessError as err: + self.fail("non-zero exit code with: " + err.output) + def test_status(self): self.assertEqual(run(''), 'ninja: no work to do.\n') self.assertEqual(run('', pipe=True), 'ninja: no work to do.\n') diff --git a/src/build_log.cc b/src/build_log.cc index cf2118251c..54a3a35183 100644 --- a/src/build_log.cc +++ b/src/build_log.cc @@ -293,7 +293,7 @@ LoadStatus BuildLog::Load(const string& path, string* err) { unlink(path.c_str()); // Don't report this as a failure. An empty build log will cause // us to rebuild the outputs anyway. - return LOAD_SUCCESS; + return LOAD_NOT_FOUND; } }