From cd01e3ed94b77a9d181e45606670dc155b71e248 Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Fri, 4 Oct 2024 08:03:53 +0800 Subject: [PATCH] fix non-ASCII pathname under Windows --- vlib/v/builder/builder_test.v | 33 ++++++++++++++++++++++++++++++++- vlib/v/builder/cc.v | 5 ++++- vlib/v/builder/msvc_windows.v | 5 ++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/vlib/v/builder/builder_test.v b/vlib/v/builder/builder_test.v index a12579eca23f43..cf6720cac121e1 100644 --- a/vlib/v/builder/builder_test.v +++ b/vlib/v/builder/builder_test.v @@ -4,9 +4,10 @@ import os const vexe = @VEXE const test_path = os.join_path(os.vtmp_dir(), 'run_check') +const test_path2 = os.join_path(test_path, '测试目录') fn testsuite_begin() { - os.mkdir_all(test_path) or {} + os.mkdir_all(test_path2) or {} } fn testsuite_end() { @@ -43,3 +44,33 @@ fn test_conditional_executable_removal() { dump(after_second_run___) assert executable in after_second_run___ } + +fn test_windows_ansi_path_name() { + os.chdir(test_path2)! + os.write_file('测试.v', 'fn main(){\n\tprintln("Hello World!")\n}\n')! + + mut executable := '测试' + $if windows { + executable += '.exe' + } + + original_file_list_ := os.ls(test_path2)! + dump(original_file_list_) + assert executable !in original_file_list_ + + assert os.execute('${os.quoted_path(vexe)} run .').output.trim_space() == 'Hello World!' + after_run_file_list := os.ls(test_path2)!.filter(os.exists(it)) + dump(after_run_file_list) + assert executable !in after_run_file_list + + assert os.execute('${os.quoted_path(vexe)} . -o ${executable}').exit_code == 0 + assert os.execute('./${executable}').output.trim_space() == 'Hello World!' + after_compilation__ := os.ls(test_path2)! + dump(after_compilation__) + assert executable in after_compilation__ + + assert os.execute('${os.quoted_path(vexe)} run .').output.trim_space() == 'Hello World!' + after_second_run___ := os.ls(test_path2)! + dump(after_second_run___) + assert executable in after_second_run___ +} diff --git a/vlib/v/builder/cc.v b/vlib/v/builder/cc.v index 7100ed7e763238..5d6287bfcc3f35 100644 --- a/vlib/v/builder/cc.v +++ b/vlib/v/builder/cc.v @@ -9,6 +9,7 @@ import v.pref import v.util import v.vcache import term +import encoding.iconv const c_std = 'c99' const c_std_gnu = 'gnu99' @@ -666,7 +667,9 @@ pub fn (mut v Builder) cc() { response_file_content = str_args.replace('\\', '\\\\') rspexpr := '@${response_file}' cmd = '${v.quote_compiler_name(ccompiler)} ${os.quoted_path(rspexpr)}' - os.write_file(response_file, response_file_content) or { + // `LOCAL` encoding: ANSI/CP_ACP for Windows; UTF-8 for Linux + // Windows use ANSI encoding for path/filename + iconv.write_file_encoding(response_file, response_file_content, 'LOCAL', false) or { verror('Unable to write to C response file "${response_file}"') } } diff --git a/vlib/v/builder/msvc_windows.v b/vlib/v/builder/msvc_windows.v index fb75c4716be139..485d2d3a646d5a 100644 --- a/vlib/v/builder/msvc_windows.v +++ b/vlib/v/builder/msvc_windows.v @@ -4,6 +4,7 @@ import os import time import v.util import v.cflag +import encoding.iconv #flag windows -l shell32 #flag windows -l dbghelp @@ -357,7 +358,9 @@ pub fn (mut v Builder) cc_msvc() { v.dump_c_options(a) args := a.join(' ') // write args to a file so that we dont smash createprocess - os.write_file(out_name_cmd_line, args) or { + // `LOCAL` encoding: ANSI/CP_ACP for Windows; UTF-8 for Linux + // Windows use ANSI encoding for path/filename + iconv.write_file_encoding(out_name_cmd_line, args, 'LOCAL', false) or { verror('Unable to write response file to "${out_name_cmd_line}"') } cmd := '"${r.full_cl_exe_path}" "@${out_name_cmd_line}"'