Skip to content

Commit 9fb0f8b

Browse files
authored
os: support .set_environment() on windows too (fix #10628) (#23996)
1 parent 30c21a0 commit 9fb0f8b

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

vlib/os/process_test.v

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// vtest flaky: true
2-
// vtest retry: 3
31
import os
42
import time
53

@@ -63,8 +61,22 @@ fn test_set_work_folder() {
6361
assert new_parent_work_folder != child_work_folder
6462
}
6563

66-
fn test_done() {
67-
exit(0)
64+
fn test_set_environment() {
65+
mut p := os.new_process(test_os_process)
66+
p.set_args(['-show_env', '-target', 'stdout'])
67+
p.set_environment({
68+
'V_OS_TEST_PORT': '1234567890'
69+
})
70+
p.set_redirect_stdio()
71+
p.wait()
72+
assert p.code == 0
73+
output := p.stdout_slurp().trim_space()
74+
p.close()
75+
$if trace_process_output ? {
76+
eprintln('p output: "${output}"')
77+
}
78+
dump(output)
79+
assert output.contains('V_OS_TEST_PORT=1234567890')
6880
}
6981

7082
fn test_run() {

vlib/os/process_windows.c.v

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,38 @@ fn (mut p Process) win_spawn_process() int {
124124
to_be_freed << work_folder_ptr
125125
}
126126

127+
mut env_block := []u16{}
128+
if p.env.len > 0 {
129+
mut env_ptr := &u16(unsafe { nil })
130+
131+
for e in p.env {
132+
// e should in `ABC=123` format
133+
env_ptr = e.to_wide()
134+
if isnil(env_ptr) {
135+
continue
136+
}
137+
mut i := 0
138+
for {
139+
character := unsafe { env_ptr[i] }
140+
if character == 0 {
141+
break
142+
}
143+
env_block << character
144+
i++
145+
}
146+
env_block << u16(0)
147+
to_be_freed << env_ptr
148+
}
149+
env_block << u16(0)
150+
creation_flags |= C.CREATE_UNICODE_ENVIRONMENT
151+
defer {
152+
unsafe { env_block.free() }
153+
}
154+
}
155+
127156
create_process_ok := C.CreateProcessW(0, voidptr(&wdata.command_line[0]), 0, 0, C.TRUE,
128-
creation_flags, 0, work_folder_ptr, voidptr(&start_info), voidptr(&wdata.proc_info))
157+
creation_flags, if env_block.len > 0 { env_block.data } else { 0 }, work_folder_ptr,
158+
voidptr(&start_info), voidptr(&wdata.proc_info))
129159
failed_cfn_report_error(create_process_ok, 'CreateProcess')
130160
if p.use_stdio_ctl {
131161
close_valid_handle(&wdata.child_stdout_write)

0 commit comments

Comments
 (0)