From 773cfe113be8cb12a570b21f5a205db8a10679ce Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 21 Apr 2026 05:58:34 +0300 Subject: [PATCH] builtin: fix runtime.nr_jobs assert without VJOBS env variable (fixes #26798) --- vlib/runtime/runtime.v | 2 +- vlib/runtime/runtime_test.v | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/vlib/runtime/runtime.v b/vlib/runtime/runtime.v index 3e2346995..dd33dafeb 100644 --- a/vlib/runtime/runtime.v +++ b/vlib/runtime/runtime.v @@ -17,7 +17,7 @@ pub fn nr_jobs() int { // in its cgen stage. Returning 1 here, increases the chances of it working on non linux systems. return 1 } - mut cpus := nr_cpus() - 1 + mut cpus := nr_cpus() // allow for overrides, for example using `VJOBS=32 ./v test .` vjobs := os.getenv('VJOBS').int() if vjobs > 0 { diff --git a/vlib/runtime/runtime_test.v b/vlib/runtime/runtime_test.v index 01a2f8303..fd6aa452c 100644 --- a/vlib/runtime/runtime_test.v +++ b/vlib/runtime/runtime_test.v @@ -1,3 +1,4 @@ +import os import runtime fn test_physical_memory() { @@ -26,6 +27,37 @@ fn test_nr_jobs() { assert nr_jobs > 0 } +fn test_nr_jobs_matches_nr_cpus_without_vjobs() { + env := os.environ() + had_vjobs := 'VJOBS' in env + saved_vjobs := env['VJOBS'] + defer { + if had_vjobs { + os.setenv('VJOBS', saved_vjobs, true) + } else { + os.unsetenv('VJOBS') + } + } + os.unsetenv('VJOBS') + assert runtime.nr_jobs() == runtime.nr_cpus() +} + +fn test_nr_jobs_uses_vjobs_override() { + env := os.environ() + had_vjobs := 'VJOBS' in env + saved_vjobs := env['VJOBS'] + defer { + if had_vjobs { + os.setenv('VJOBS', saved_vjobs, true) + } else { + os.unsetenv('VJOBS') + } + } + expected_jobs := runtime.nr_cpus() + 1 + os.setenv('VJOBS', expected_jobs.str(), true) + assert runtime.nr_jobs() == expected_jobs +} + fn test_is_32bit() { x := runtime.is_32bit().str() println(' is_32bit: ${x}') -- 2.39.5