diff --git a/vlib/v/builder/compile.v b/vlib/v/builder/compile.v index 3d790a197075d4..b0a8dd2ce2a15a 100644 --- a/vlib/v/builder/compile.v +++ b/vlib/v/builder/compile.v @@ -134,26 +134,34 @@ fn (mut b Builder) run_compiled_executable_and_exit() { run_args << ['run', compiled_file] } run_args << b.pref.run_args - mut run_process := os.new_process(run_file) - run_process.set_args(run_args) + if b.pref.is_verbose { - println('running ${run_process.filename} with arguments ${run_process.args}') - } - // Ignore sigint and sigquit while running the compiled file, - // so ^C doesn't prevent v from deleting the compiled file. - // See also https://git.musl-libc.org/cgit/musl/tree/src/process/system.c - prev_int_handler := os.signal_opt(.int, eshcb) or { serror('set .int', err) } - mut prev_quit_handler := os.SignalHandler(eshcb) - $if !windows { // There's no sigquit on windows - prev_quit_handler = os.signal_opt(.quit, eshcb) or { serror('set .quit', err) } - } - run_process.wait() - os.signal_opt(.int, prev_int_handler) or { serror('restore .int', err) } - $if !windows { - os.signal_opt(.quit, prev_quit_handler) or { serror('restore .quit', err) } - } - ret := run_process.code - run_process.close() + println('running ${run_file} with arguments ${run_args.join(' ')}') + } + mut ret := 0 + if b.pref.use_os_system_to_run { + command_to_run := run_file + ' ' + run_args.join(' ') + ret = os.system(command_to_run) + // eprintln('> ret: ${ret:5} | command_to_run: ${command_to_run}') + } else { + mut run_process := os.new_process(run_file) + run_process.set_args(run_args) + // Ignore sigint and sigquit while running the compiled file, + // so ^C doesn't prevent v from deleting the compiled file. + // See also https://git.musl-libc.org/cgit/musl/tree/src/process/system.c + prev_int_handler := os.signal_opt(.int, eshcb) or { serror('set .int', err) } + mut prev_quit_handler := os.SignalHandler(eshcb) + $if !windows { // There's no sigquit on windows + prev_quit_handler = os.signal_opt(.quit, eshcb) or { serror('set .quit', err) } + } + run_process.wait() + os.signal_opt(.int, prev_int_handler) or { serror('restore .int', err) } + $if !windows { + os.signal_opt(.quit, prev_quit_handler) or { serror('restore .quit', err) } + } + ret = run_process.code + run_process.close() + } b.cleanup_run_executable_after_exit(compiled_file) exit(ret) } diff --git a/vlib/v/help/common/run.txt b/vlib/v/help/common/run.txt index 7a84a77e8cc3d1..a3b8b825bebecb 100644 --- a/vlib/v/help/common/run.txt +++ b/vlib/v/help/common/run.txt @@ -15,4 +15,12 @@ The exit status of run will be: * `1` if the compilation failed. * The exit code of the compiled executable otherwise. +Flags specific to `v run`: + -use-os-system-to-run + Use os.system() to run the produced executable, instead of os.new_process; + this is useful as a temporary workaround for segfaults, when running code + on macos, that may happen when xcode is updated after 2023/08/30, and for + comparing os.system with os.new_process on other systems. Usually used with: + `export VFLAGS='-use-os-system-to-run'`. + For more about build flags, see `v help build`. diff --git a/vlib/v/pref/pref.v b/vlib/v/pref/pref.v index ae377eab7488c2..6d9ad3f1cf8a8b 100644 --- a/vlib/v/pref/pref.v +++ b/vlib/v/pref/pref.v @@ -154,6 +154,7 @@ pub mut: dump_files string // `-dump-files files.txt` - let V store all V or .template file paths, that were used by the compiled program in `files.txt`, one path per line. use_cache bool // when set, use cached modules to speed up subsequent compilations, at the cost of slower initial ones (while the modules are cached) retry_compilation bool = true // retry the compilation with another C compiler, if tcc fails. + use_os_system_to_run bool // when set, use os.system() to run the produced executable, instead of os.new_process; works around segfaults on macos, that may happen when xcode is updated // TODO Convert this into a []string cflags string // Additional options which will be passed to the C compiler *before* other options. ldflags string // Additional options which will be passed to the C compiler *after* everything else. @@ -660,6 +661,9 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin '-usecache' { res.use_cache = true } + '-use-os-system-to-run' { + res.use_os_system_to_run = true + } '-nocache' { res.use_cache = false }