Skip to content

Commit 4fbc55a

Browse files
authored
v3: clean up transform helpers/inits; cmd/v: fix v3.v bootstrap dispatch on macOS (#28157)
1 parent 4420bbb commit 4fbc55a

16 files changed

Lines changed: 821 additions & 732 deletions

File tree

cmd/v/macos_v3_args.c.v

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@ fn macos_v3_non_compilation_command(command string) bool {
1919
'interpret', 'get', 'translate']
2020
}
2121

22+
// is_macos_v3_compiler_bootstrap reports whether `normalized_path` targets the
23+
// `vlib/v3/v3.v` compiler bootstrap, which must build with the compatibility
24+
// compiler rather than the embedded V3 driver. It matches the repo-relative path
25+
// and any path ending in it, and also resolves a bare `v3.v` (for example when
26+
// invoked from inside `vlib/v3`) through its real path, so the bootstrap is
27+
// recognized regardless of the working directory. Both dispatch gates rely on
28+
// it, so keep the detection in one place to stop them drifting.
29+
@[markused]
30+
fn is_macos_v3_compiler_bootstrap(normalized_path string) bool {
31+
if normalized_path == 'vlib/v3/v3.v' || normalized_path.ends_with('/vlib/v3/v3.v') {
32+
return true
33+
}
34+
// Only a file literally named `v3.v` can be the bootstrap; skip the real-path
35+
// resolution (a filesystem lookup) for every other compilation target.
36+
if os.base(normalized_path) != 'v3.v' {
37+
return false
38+
}
39+
real_path := os.real_path(normalized_path).replace('\\', '/').trim_right('/')
40+
return real_path == 'vlib/v3/v3.v' || real_path.ends_with('/vlib/v3/v3.v')
41+
}
42+
2243
// macos_v3_force_requested reports whether `-new-compiler` should hand this
2344
// invocation to the embedded V3 compiler. It gates on `-old-compiler`
2445
// precedence, options/modes V3 cannot honor yet, and whether the command is an
@@ -44,7 +65,7 @@ fn macos_v3_force_requested(command string, prefs &pref.Preferences) bool {
4465
normalized_path := prefs.path.replace('\\', '/').trim_right('/')
4566
if normalized_path == 'cmd/v' || normalized_path.starts_with('cmd/v/')
4667
|| normalized_path.contains('/cmd/v/') || normalized_path.ends_with('/cmd/v')
47-
|| normalized_path == 'vlib/v3/v3.v' || normalized_path.ends_with('/vlib/v3/v3.v') {
68+
|| is_macos_v3_compiler_bootstrap(normalized_path) {
4869
return false
4970
}
5071
return command in ['run', 'build'] || prefs.is_script || os.is_dir(prefs.path)

cmd/v/macos_v3_darwin.c.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fn is_macos_v3_relevant_command(command string, prefs &pref.Preferences) bool {
9595
// modes use V3 by default.
9696
if normalized_path == 'cmd/v' || normalized_path.starts_with('cmd/v/')
9797
|| normalized_path.contains('/cmd/v/') || normalized_path.ends_with('/cmd/v')
98-
|| normalized_path == 'vlib/v3/v3.v' || normalized_path.ends_with('/vlib/v3/v3.v')
98+
|| is_macos_v3_compiler_bootstrap(normalized_path)
9999
|| is_macos_v3_internal_tool_bootstrap(normalized_path, os.getenv('VCHILD') == 'true') {
100100
return false
101101
}

cmd/v/macos_v3_test.v

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,47 @@ fn test_macos_v3_relevant_command_selects_user_compilation_and_tests() {
217217
}
218218
}
219219

220+
fn test_macos_v3_compiler_bootstrap_is_detected_from_any_cwd() {
221+
// Repo-relative and absolute spellings are recognized directly.
222+
assert is_macos_v3_compiler_bootstrap('vlib/v3/v3.v')
223+
assert is_macos_v3_compiler_bootstrap('/home/user/v/vlib/v3/v3.v')
224+
// Non-bootstrap targets stay on the V3 path.
225+
assert !is_macos_v3_compiler_bootstrap('main.v')
226+
assert !is_macos_v3_compiler_bootstrap('cmd/v')
227+
assert !is_macos_v3_compiler_bootstrap('some/other/place/v3.v')
228+
229+
// A bare `v3.v` invoked from inside vlib/v3 must resolve to the bootstrap so
230+
// it builds with the compatibility compiler instead of the embedded V3 driver.
231+
// Use an isolated <tmp>/vlib/v3/v3.v so this exercises the real-path
232+
// resolution unconditionally, independent of where this test file lives.
233+
root := os.join_path(os.real_path(os.vtmp_dir()), 'macos_v3_bootstrap_${os.getpid()}')
234+
v3_dir := os.join_path(root, 'vlib', 'v3')
235+
other_dir := os.join_path(root, 'elsewhere')
236+
os.rmdir_all(root) or {}
237+
os.mkdir_all(v3_dir) or { panic(err) }
238+
os.mkdir_all(other_dir) or { panic(err) }
239+
defer {
240+
os.rmdir_all(root) or {}
241+
}
242+
os.write_file(os.join_path(v3_dir, 'v3.v'), 'module main\n') or { panic(err) }
243+
os.write_file(os.join_path(other_dir, 'v3.v'), 'module main\n') or { panic(err) }
244+
245+
saved := os.getwd()
246+
defer {
247+
os.chdir(saved) or {}
248+
}
249+
os.chdir(v3_dir) or { panic(err) }
250+
bare := is_macos_v3_compiler_bootstrap('v3.v')
251+
dotted := is_macos_v3_compiler_bootstrap('./v3.v')
252+
// A bare `v3.v` that is not under vlib/v3 must stay on the V3 path.
253+
os.chdir(other_dir) or { panic(err) }
254+
non_bootstrap := is_macos_v3_compiler_bootstrap('v3.v')
255+
os.chdir(saved) or {}
256+
assert bare
257+
assert dotted
258+
assert !non_bootstrap
259+
}
260+
220261
fn test_macos_v3_dispatch_allows_the_implicit_gc_default() {
221262
$if macos {
222263
implicit_gc, _ := pref.parse_args_and_show_errors([], ['', 'main.v'], false)

0 commit comments

Comments
 (0)