-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRunCommand.cpp
More file actions
1867 lines (1501 loc) · 47.1 KB
/
Copy pathRunCommand.cpp
File metadata and controls
1867 lines (1501 loc) · 47.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
*
* @file RunCommand.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
*/
#include <vix/cli/commands/RunCommand.hpp>
#include <vix/cli/commands/InstallCommand.hpp>
#include <vix/cli/commands/run/RunDetail.hpp>
#include <vix/cli/commands/replay/ReplayCapture.hpp>
#include <vix/cli/commands/replay/ReplayRecorder.hpp>
#include <vix/cli/errors/RawLogDetectors.hpp>
#include <vix/cli/manifest/RunManifestMerge.hpp>
#include <vix/cli/manifest/VixManifest.hpp>
#include <vix/cli/app/AppProjectResolver.hpp>
#include <vix/cli/commands/run/detail/RunnableExecutableResolver.hpp>
#include <vix/engine/SanitizerMode.hpp>
#include <vix/cli/Style.hpp>
#include <vix/utils/Env.hpp>
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <optional>
#include <sstream>
#include <string>
#include <vector>
#ifndef _WIN32
#include <sys/stat.h>
#include <unistd.h>
#endif
using namespace vix::cli::style;
namespace fs = std::filesystem;
namespace app = vix::cli::app;
namespace
{
using vix::commands::RunCommand::detail::LiveRunResult;
using vix::commands::RunCommand::detail::Options;
int run_script_mode(Options &opt);
enum class RunTargetKind
{
Binary,
Script,
Project,
Container,
Unknown
};
struct RunTarget
{
RunTargetKind kind{RunTargetKind::Unknown};
fs::path path{};
};
bool should_clear_terminal_now()
{
return false;
}
std::vector<std::string> split_dep_spec(const std::string &spec)
{
std::vector<std::string> args;
if (spec.find(';') == std::string::npos || spec.find('=') == std::string::npos)
{
args.push_back(spec);
args.push_back("--yes");
return args;
}
std::string git;
std::istringstream in(spec);
std::string part;
std::vector<std::pair<std::string, std::string>> options;
while (std::getline(in, part, ';'))
{
const auto eq = part.find('=');
if (eq == std::string::npos)
continue;
std::string key = part.substr(0, eq);
std::string value = part.substr(eq + 1);
if (key == "git" || key == "url")
git = value;
else
options.push_back({key, value});
}
if (!git.empty())
args.push_back(git);
for (const auto &[key, value] : options)
{
if (key == "tag" || key == "branch" || key == "rev" || key == "target" || key == "name" || key == "include" || key == "subdirectory")
{
args.push_back("--" + key);
args.push_back(value);
}
else if (key == "header_only" || key == "header-only")
{
if (value == "1" || value == "true" || value == "yes" || value == "on")
args.push_back("--header-only");
}
}
args.push_back("--yes");
return args;
}
int install_temp_deps_in_dir(const fs::path &dir, const std::vector<std::string> &deps)
{
std::error_code ec;
const fs::path old = fs::current_path(ec);
if (ec)
return 1;
fs::current_path(dir, ec);
if (ec)
return 1;
int rc = 0;
for (const std::string &dep : deps)
{
std::ostringstream muted;
std::streambuf *oldOut = std::cout.rdbuf(muted.rdbuf());
rc = vix::commands::InstallCommand::run(split_dep_spec(dep));
std::cout.rdbuf(oldOut);
if (rc != 0)
break;
}
fs::current_path(old, ec);
return rc;
}
fs::path make_temp_run_dep_dir()
{
std::error_code ec;
fs::path base = fs::temp_directory_path(ec);
if (ec)
base = fs::current_path();
const auto now = std::chrono::steady_clock::now().time_since_epoch().count();
fs::path dir = base / ("vix-run-deps-" + std::to_string(static_cast<long long>(now)));
fs::create_directories(dir, ec);
return dir;
}
int run_with_temporary_deps(Options opt)
{
if (!opt.singleCpp || opt.cppFile.empty())
{
error("--dep is supported for single C++ files");
return 2;
}
if (opt.saveTempDeps)
{
for (const std::string &dep : opt.tempDeps)
{
const int rc = vix::commands::InstallCommand::run(split_dep_spec(dep));
if (rc != 0)
return rc;
}
opt.tempDeps.clear();
return run_script_mode(opt);
}
const fs::path original = fs::absolute(opt.cppFile).lexically_normal();
const fs::path tempDir = make_temp_run_dep_dir();
const fs::path tempSource = tempDir / original.filename();
std::error_code ec;
fs::copy_file(original, tempSource, fs::copy_options::overwrite_existing, ec);
if (ec)
{
error("cannot prepare temporary run source: " + ec.message());
return 1;
}
{
std::ofstream app(tempDir / "vix.app", std::ios::binary | std::ios::trunc);
app << "name = \"vix-temp-run\"\n";
app << "type = \"executable\"\n";
app << "standard = \"c++20\"\n";
app << "sources = [\"" << original.filename().string() << "\"]\n";
}
const int installRc = install_temp_deps_in_dir(tempDir, opt.tempDeps);
if (installRc != 0)
{
fs::remove_all(tempDir, ec);
return installRc;
}
opt.cppFile = tempSource;
opt.tempDeps.clear();
const int rc = run_script_mode(opt);
fs::remove_all(tempDir, ec);
return rc;
}
std::string trim_copy_local(std::string s)
{
auto is_ws = [](unsigned char c)
{ return c == ' ' || c == '\t' || c == '\n' || c == '\r'; };
while (!s.empty() && is_ws(static_cast<unsigned char>(s.back())))
s.pop_back();
std::size_t i = 0;
while (i < s.size() && is_ws(static_cast<unsigned char>(s[i])))
++i;
s.erase(0, i);
return s;
}
static void warn_if_env_file_missing(const fs::path &projectDir, const Options &opt)
{
std::error_code ec;
const fs::path envFile = projectDir / ".env";
if (fs::exists(envFile, ec) && !ec)
return;
const fs::path envExample = projectDir / ".env.example";
if (!fs::exists(envExample, ec) || ec)
return;
if (!opt.envHint)
return;
hint(".env not found.");
step("cp .env.example .env");
}
std::string to_dep_folder_name(const std::string &pkg)
{
std::string s = pkg;
const auto at = s.find('@');
if (at != std::string::npos)
s = s.substr(0, at);
const auto slash = s.find('/');
if (slash == std::string::npos)
return s;
return s.substr(0, slash) + "." + s.substr(slash + 1);
}
std::vector<std::string> parse_manifest_dep_packages_v1(const fs::path &manifestFile)
{
std::ifstream ifs(manifestFile);
std::vector<std::string> out;
if (!ifs)
return out;
bool inDeps = false;
std::string line;
while (std::getline(ifs, line))
{
line = trim_copy_local(line);
if (line.empty() || line[0] == '#')
continue;
if (line.size() >= 2 && line.front() == '[' && line.back() == ']')
{
const std::string sec = line.substr(1, line.size() - 2);
inDeps = (sec == "deps");
continue;
}
if (!inDeps)
continue;
if (line.rfind("packages", 0) != 0)
continue;
const auto eq = line.find('=');
if (eq == std::string::npos)
continue;
std::string rhs = trim_copy_local(line.substr(eq + 1));
const auto lb = rhs.find('[');
const auto rb = rhs.rfind(']');
if (lb == std::string::npos || rb == std::string::npos || rb <= lb)
continue;
std::string arr = rhs.substr(lb + 1, rb - lb - 1);
for (std::size_t i = 0; i < arr.size(); ++i)
{
if (arr[i] != '"')
continue;
const std::size_t j = arr.find('"', i + 1);
if (j == std::string::npos)
break;
out.push_back(arr.substr(i + 1, j - i - 1));
i = j;
}
}
return out;
}
static int build_project_with_vix_build(
const fs::path &projectDir,
const Options &opt,
fs::path &outExecutable)
{
namespace detail = vix::commands::RunCommand::detail;
std::string requestedTarget;
if (!opt.appName.empty() &&
opt.appName != projectDir.filename().string())
{
requestedTarget = opt.appName;
}
const fs::path buildDir = projectDir / "build-ninja";
std::ostringstream cmd;
#ifdef _WIN32
cmd << "cmd /C \"cd /D "
<< detail::quote(projectDir.string())
<< " && vix build --build-target ";
if (!requestedTarget.empty())
cmd << detail::quote(requestedTarget);
else
cmd << "all";
#else
cmd << "cd "
<< detail::quote(projectDir.string())
<< " && vix build --build-target ";
if (!requestedTarget.empty())
cmd << detail::quote(requestedTarget);
else
cmd << "all";
#endif
if (!opt.preset.empty())
cmd << " --preset " << detail::quote(opt.preset);
if (opt.jobs > 0)
cmd << " -j " << opt.jobs;
if (opt.verbose)
cmd << " -v";
else
cmd << " -q";
if (opt.clean)
cmd << " --clean";
if (opt.withSqlite)
cmd << " --with-sqlite";
if (opt.withMySql)
cmd << " --with-mysql";
#ifdef _WIN32
cmd << "\"";
#endif
const int rawCode =
detail::run_cmd_live_filtered(
cmd.str(),
"Building project");
const int buildExit =
detail::normalize_exit_code(rawCode);
if (buildExit == 130)
{
hint("Program interrupted by user.");
return 0;
}
if (buildExit != 0)
return buildExit;
auto exePath =
detail::resolve_runnable_executable(
buildDir,
requestedTarget);
if (!exePath)
{
if (!requestedTarget.empty())
{
error("Built executable not found for target: " + requestedTarget);
hint("Resolved build directory: " + buildDir.string());
detail::print_runnable_executable_candidates(buildDir);
return 1;
}
const auto candidates = detail::find_runnable_executables(buildDir, false);
if (candidates.empty())
{
error("No runnable executable found.");
hint("Resolved build directory: " + buildDir.string());
hint("The project may only build libraries, tests, or non-runnable targets.");
return 1;
}
error("Multiple runnable executables found.");
hint("Run one explicitly:");
for (const auto &p : candidates)
step(" vix run " + detail::runnable_executable_display_path(p));
return 1;
}
outExecutable = *exePath;
return 0;
}
void apply_manifest_auto_deps_includes(Options &opt, const fs::path &manifestFile)
{
const fs::path manifestDir = manifestFile.parent_path();
const fs::path depsRoot = manifestDir / ".vix" / "deps";
if (!fs::exists(depsRoot))
return;
const auto pkgs = parse_manifest_dep_packages_v1(manifestFile);
if (pkgs.empty())
return;
auto already_has_I = [&](const std::string &inc) -> bool
{
const std::string flag = "-I" + inc;
for (const auto &f : opt.scriptFlags)
{
if (f == flag)
return true;
if (f.rfind("-I", 0) == 0 && f.substr(2) == inc)
return true;
}
return false;
};
for (const auto &p : pkgs)
{
const std::string folder = to_dep_folder_name(p);
const fs::path inc = depsRoot / folder / "include";
std::error_code ec;
if (!fs::exists(inc, ec) || ec)
continue;
const std::string incStr = inc.string();
if (!already_has_I(incStr))
opt.scriptFlags.push_back("-I" + incStr);
}
}
void clear_terminal_if_enabled()
{
if (!should_clear_terminal_now())
return;
std::cout << "\033[2J\033[H" << std::flush;
}
struct RunProgress
{
using Clock = std::chrono::steady_clock;
bool enabled = false;
int total = 0;
int current = 0;
std::string currentLabel;
Clock::time_point phaseStart{};
RunProgress(int totalSteps, bool enableUi)
: enabled(enableUi), total(totalSteps)
{
}
void phase_start(const std::string &label)
{
++current;
currentLabel = label;
phaseStart = Clock::now();
if (!enabled)
return;
std::cout << std::endl;
info("┏ [" + std::to_string(current) + "/" +
std::to_string(total) + "] " + label);
}
void phase_done(const std::string &label, const std::string &extra = {})
{
const auto end = Clock::now();
const auto ms =
std::chrono::duration_cast<std::chrono::milliseconds>(end - phaseStart).count();
if (!enabled)
return;
std::string msg =
"┗ [" + std::to_string(current) + "/" +
std::to_string(total) + "] " + label;
if (!extra.empty())
msg += " - " + extra;
std::ostringstream oss;
oss << std::fixed << std::setprecision(2)
<< (static_cast<double>(ms) / 1000.0);
msg += " (" + oss.str() + "s)";
success(msg);
}
};
void enable_line_buffered_stdout_for_apps()
{
#ifndef _WIN32
::setenv("VIX_STDOUT_MODE", "line", 1);
#endif
}
bool is_executable_file(const fs::path &p)
{
std::error_code ec{};
if (!fs::is_regular_file(p, ec) || ec)
return false;
#ifdef _WIN32
return p.extension() == ".exe";
#else
auto perms = fs::status(p, ec).permissions();
if (ec)
return false;
using pr = fs::perms;
return (perms & pr::owner_exec) != pr::none ||
(perms & pr::group_exec) != pr::none ||
(perms & pr::others_exec) != pr::none;
#endif
}
bool looks_like_test_binary(const fs::path &p)
{
const std::string n = p.filename().string();
return n.find("_test") != std::string::npos ||
n.find("_tests") != std::string::npos ||
n.rfind("test_", 0) == 0;
}
std::optional<fs::path> find_single_test_binary(const fs::path &buildDir)
{
std::error_code ec{};
if (!fs::exists(buildDir, ec) || ec)
return std::nullopt;
std::vector<fs::path> candidates;
for (auto it = fs::directory_iterator(buildDir, ec);
!ec && it != fs::directory_iterator();
++it)
{
const auto &p = it->path();
if (!is_executable_file(p))
continue;
if (!looks_like_test_binary(p))
continue;
candidates.push_back(p);
}
if (candidates.size() == 1)
return candidates.front();
return std::nullopt;
}
void ensure_mode_env_for_run(const Options &opt)
{
const char *cur = vix::utils::vix_getenv("VIX_MODE");
if (cur && *cur)
return;
#ifdef _WIN32
_putenv_s("VIX_MODE", opt.watch ? "dev" : "run");
#else
::setenv("VIX_MODE", opt.watch ? "dev" : "run", 1);
#endif
}
void apply_docs_env(const Options &opt)
{
const bool enabled = opt.docs.has_value() && *opt.docs;
#ifdef _WIN32
_putenv_s("VIX_DOCS", enabled ? "1" : "0");
#else
::setenv("VIX_DOCS", enabled ? "1" : "0", 1);
#endif
}
void apply_common_run_environment(Options &opt)
{
if (!opt.cwd.empty())
opt.cwd = vix::commands::RunCommand::detail::normalize_cwd_if_needed(opt.cwd);
ensure_mode_env_for_run(opt);
enable_line_buffered_stdout_for_apps();
vix::commands::RunCommand::detail::apply_log_env(opt);
vix::cli::manifest::apply_env_pairs(opt.runEnv);
apply_docs_env(opt);
#ifndef _WIN32
::setenv("VIX_CLI_CLEAR", opt.clearMode.c_str(), 1);
#else
_putenv_s("VIX_CLI_CLEAR", opt.clearMode.c_str());
#endif
}
int run_script_mode(Options &opt)
{
if (opt.singleCpp && opt.watch)
return vix::commands::RunCommand::detail::run_single_cpp_watch(opt);
if (opt.singleCpp)
{
int rc = vix::commands::RunCommand::detail::run_single_cpp(opt);
if (rc == 130)
{
hint("ℹ Program interrupted by user (SIGINT).");
return 0;
}
if (rc < 0)
return -rc;
return rc;
}
return 1;
}
int run_test_binary_if_present(
const fs::path &buildDir,
const std::vector<std::string> &runArgs,
const std::string &cwd,
bool showUi)
{
auto testExe = find_single_test_binary(buildDir);
if (!testExe)
return -1;
if (showUi)
{
info("No main executable found. Detected library project; running test binary:");
step(testExe->string());
}
#ifdef _WIN32
std::string cmd = "\"" + testExe->string() + "\"";
cmd += vix::commands::RunCommand::detail::join_quoted_args_local(runArgs);
Options fakeOpt;
fakeOpt.cwd = cwd;
cmd = vix::commands::RunCommand::detail::wrap_with_cwd_if_needed(fakeOpt, cmd);
int raw = std::system(cmd.c_str());
int testExit = vix::commands::RunCommand::detail::normalize_exit_code(raw);
if (testExit == 130)
{
hint("ℹ Program interrupted by user (SIGINT).");
return 0;
}
if (testExit != 0)
{
error("Test execution failed (exit code " + std::to_string(testExit) + ").");
return testExit;
}
return 0;
#else
std::string testCmd = vix::commands::RunCommand::detail::quote(testExe->string());
testCmd += vix::commands::RunCommand::detail::join_quoted_args_local(runArgs);
Options fakeOpt;
fakeOpt.cwd = cwd;
testCmd = vix::commands::RunCommand::detail::wrap_with_cwd_if_needed(fakeOpt, testCmd);
const LiveRunResult tr =
vix::commands::RunCommand::detail::run_cmd_live_filtered_capture(
testCmd,
"",
true,
0,
false);
const int testExit = tr.exitCode;
if (testExit == 130)
{
hint("ℹ Program interrupted by user (SIGINT).");
return 0;
}
if (testExit != 0)
{
std::string log = tr.stderrText;
if (!tr.stdoutText.empty())
log += tr.stdoutText;
bool handled = false;
if (!log.empty())
{
const fs::path diagnosticPath{};
handled = vix::cli::errors::RawLogDetectors::handleRuntimeCrash(
log,
diagnosticPath,
"Test crashed");
if (!handled &&
vix::cli::errors::RawLogDetectors::handleKnownRunFailure(log, diagnosticPath))
{
handled = true;
}
if (!handled && !tr.printed_live)
std::cout << log << "\n";
}
if (!handled)
error("Test execution failed (exit code " + std::to_string(testExit) + ").");
return testExit;
}
return 0;
#endif
}
int run_executable_direct(
const fs::path &exePath,
const Options &opt,
const std::string &failureContext,
int timeoutSec,
bool instrumentedBinary = false)
{
#ifdef _WIN32
std::string runCmd = "\"" + exePath.string() + "\"";
runCmd += vix::commands::RunCommand::detail::join_quoted_args_local(opt.runArgs);
runCmd = vix::commands::RunCommand::detail::wrap_with_cwd_if_needed(opt, runCmd);
int raw = std::system(runCmd.c_str());
int runExit = vix::commands::RunCommand::detail::normalize_exit_code(raw);
if (runExit == 130)
{
hint("ℹ Program interrupted by user (SIGINT).");
return 0;
}
if (runExit != 0)
{
vix::commands::RunCommand::detail::handle_runtime_exit_code(
runExit,
failureContext,
false);
return runExit;
}
return 0;
#else
std::string runCmd = vix::commands::RunCommand::detail::quote(exePath.string());
runCmd += vix::commands::RunCommand::detail::join_quoted_args_local(opt.runArgs);
runCmd = vix::commands::RunCommand::detail::wrap_with_cwd_if_needed(opt, runCmd);
namespace replay = vix::commands::replay;
replay::ReplayRecorder recorder;
replay::ReplayRecorderConfig replayConfig{};
replayConfig.base_dir = fs::current_path();
replayConfig.cwd = fs::current_path();
replayConfig.project_dir = fs::current_path();
replayConfig.target_path = exePath;
replayConfig.mode = opt.watch ? replay::ReplayMode::Dev : replay::ReplayMode::Run;
replayConfig.target_kind = replay::ReplayTargetKind::Project;
replayConfig.command = opt.watch ? "vix dev" : "vix run";
replayConfig.resolved_command = runCmd;
replayConfig.app_args = opt.runArgs;
replayConfig.watch = opt.watch;
replayConfig.replayable = true;
std::string replayErr;
const bool replayEnabled =
opt.replay && recorder.begin(replayConfig, replayErr);
replay::ReplayCapture replayCapture;
if (replayEnabled)
replayCapture.attach(&recorder);
const bool useSanRuntime =
instrumentedBinary ||
vix::commands::RunCommand::detail::want_any_sanitizer(
opt.enableSanitizers,
opt.enableUbsanOnly,
opt.enableThreadSanitizer);
const LiveRunResult rr =
vix::commands::RunCommand::detail::run_cmd_live_filtered_capture(
runCmd,
"",
true,
timeoutSec,
useSanRuntime,
false,
replayEnabled ? &replayCapture : nullptr);
if (replayEnabled)
{
replay::ReplayProcessResult process =
replay::make_replay_process_result(
rr.exitCode,
rr.rawStatus,
rr.terminatedBySignal,
rr.termSignal);
replay::ReplayCapturedResult captured =
replay::make_replay_captured_result(
replayCapture.output(),
process);
replay::ReplayRecorderFinish finish =
replay::make_replay_finish_from_capture(captured);
std::string finishErr;
(void)recorder.finish(finish, finishErr);
}
int runExit = rr.exitCode;
if (runExit == 130)
{
hint("ℹ Program interrupted by user (SIGINT).");
return 0;
}
if (runExit != 0)
{
std::string log = rr.stderrText;
if (!rr.stdoutText.empty())
log += rr.stdoutText;
bool handled = false;
if (!log.empty())
{
const fs::path diagnosticPath =
opt.singleCpp
? opt.cppFile
: fs::path{};
handled = vix::cli::errors::RawLogDetectors::handleRuntimeCrash(
log,
diagnosticPath,
failureContext);
if (!handled &&
vix::cli::errors::RawLogDetectors::handleKnownRunFailure(log, diagnosticPath))
{
handled = true;
}
if (!handled && !rr.printed_live)
std::cout << log << "\n";
}
if (!handled)
error(failureContext + " (exit code " + std::to_string(runExit) + ").");
return runExit;
}
return 0;
#endif
}
[[maybe_unused]] int run_resolved_project(
const app::AppProjectResolveResult &resolved,
const Options &opt,
bool showUi)
{
using namespace vix::commands::RunCommand::detail;
const fs::path buildDir = resolved.userProjectDir / "build-ninja";
RunProgress progress(3, showUi);
{
std::error_code ec;
fs::create_directories(buildDir, ec);
if (ec)
{
error("Unable to create build directory: " + ec.message());
return 1;
}
}
const bool alreadyConfigured = has_cmake_cache(buildDir);
progress.phase_start("Configure project");
if (alreadyConfigured)
{
progress.phase_done("Configure project", "cache already present");
}
else
{
std::ostringstream oss;
#ifdef _WIN32
oss << "cmd /C \"cmake"
<< " --log-level=WARNING"
<< " -S " << quote(resolved.cmakeSourceDir.string())
<< " -B " << quote(buildDir.string())
<< " -G Ninja"
<< "\"";
#else
oss << "cmake"
<< " --log-level=WARNING"
<< " -S " << quote(resolved.cmakeSourceDir.string())
<< " -B " << quote(buildDir.string())
<< " -G Ninja";
#endif
const LiveRunResult cr = run_cmd_live_filtered_capture(
oss.str(),
"",
false,
0,
false);
const int code = cr.exitCode;
if (code != 0)
{
std::string log = cr.stderrText;
if (!cr.stdoutText.empty())
log += cr.stdoutText;
if (!log.empty())
{
const bool handled = vix::cli::ErrorHandler::printBuildErrors(
log,
resolved.cmakeListsPath,
"CMake configure failed");
if (!handled)
std::cout << log << "\n";
}
error("CMake configure failed.");
hint("Generated source directory: " + resolved.cmakeSourceDir.string());
hint("Build directory: " + buildDir.string());
return code != 0 ? code : 2;
}
progress.phase_done("Configure project", "completed");
}
progress.phase_start("Build project");