-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathinterop.v
More file actions
1402 lines (1304 loc) · 47.3 KB
/
Copy pathinterop.v
File metadata and controls
1402 lines (1304 loc) · 47.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2025 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
module main
import json2
import os
import strings
import time
// v_compiler_exe is the absolute path to the V compiler executable, resolved
// once at startup. Using an absolute path (instead of relying on PATH at exec
// time) makes child-process invocation deterministic and shell-free.
const v_compiler_exe = resolve_v_compiler_exe()
fn resolve_v_compiler_exe() string {
return os.find_abs_path_of_executable('v') or { 'v' }
}
// compiler_is_available reports whether the V compiler was resolved to a real
// executable path at startup. When false, compiler-backed features cannot work
// and the failure should be surfaced to the user rather than masked as empty
// results (P1-03).
fn compiler_is_available() bool {
return v_compiler_exe != 'v' && os.exists(v_compiler_exe)
}
// hex_nibble returns the numeric value of a single hex digit, or none.
fn hex_nibble(c u8) ?u8 {
return match c {
`0`...`9` { u8(c - `0`) }
`a`...`f` { u8(c - `a` + 10) }
`A`...`F` { u8(c - `A` + 10) }
else { none }
}
}
// percent_decode decodes %XX escapes in a URI path component into raw bytes.
// Invalid escapes are left untouched so the function never fails on odd input.
fn percent_decode(s string) string {
if !s.contains('%') {
return s
}
mut out := []u8{cap: s.len}
mut i := 0
for i < s.len {
c := s[i]
if c == `%` && i + 2 < s.len {
hi := hex_nibble(s[i + 1]) or {
out << c
i++
continue
}
lo := hex_nibble(s[i + 2]) or {
out << c
i++
continue
}
out << u8(hi * 16 + lo)
i += 3
continue
}
out << c
i++
}
return out.bytestr()
}
// path_needs_escape reports whether a byte must be percent-encoded in a URI
// path. Unreserved characters (RFC 3986) plus the path separators '/' are kept.
fn path_byte_needs_escape(c u8) bool {
return match c {
`a`...`z`, `A`...`Z`, `0`...`9`, `-`, `.`, `_`, `~`, `/`, `:` { false }
else { true }
}
}
// percent_encode_path percent-encodes a filesystem path for use in a file URI,
// preserving '/' separators and drive-letter colons.
fn percent_encode_path(s string) string {
mut sb := strings.new_builder(s.len + 8)
for c in s.bytes() {
if path_byte_needs_escape(c) {
sb.write_string('%')
sb.write_string(c.hex().to_upper())
} else {
sb.write_u8(c)
}
}
return sb.str()
}
// uri_to_path converts a `file:` DocumentUri to a local filesystem path.
// It percent-decodes the path component, strips an empty authority, drops any
// query/fragment, and normalizes Windows drive paths (/C:/... -> C:/...).
// Non-`file:` URIs and bare paths are returned unchanged.
fn uri_to_path(uri string) string {
if uri == '' {
return ''
}
if !uri.starts_with('file:') {
return uri
}
mut rest := uri[5..] // strip 'file:'
mut authority := ''
if rest.starts_with('//') {
rest = rest[2..]
if slash := rest.index('/') {
authority = rest[..slash]
rest = rest[slash..]
} else {
// Authority only, no path component.
authority = rest
rest = ''
}
}
// Strip fragment then query (these are only meaningful when unescaped).
if hash := rest.index('#') {
rest = rest[..hash]
}
if q := rest.index('?') {
rest = rest[..q]
}
decoded := percent_decode(rest)
// An empty or `localhost` authority denotes the local machine (RFC 8089), so
// the path is local: file://localhost/tmp/main.v -> /tmp/main.v. A genuine
// remote authority is preserved as a UNC path (//server/share/...).
if authority != '' && authority.to_lower() != 'localhost' {
return '//' + authority + decoded
}
// Windows drive path: /C:/Users/... -> C:/Users/...
if decoded.len > 2 && decoded[0] == `/` && decoded[2] == `:` {
return decoded[1..]
}
return decoded
}
// path_is_within_with_case reports whether `path` lies inside `dir` using the
// requested case sensitivity. Both arguments are expected to use '/' separators.
fn path_is_within_with_case(path string, dir string, case_insensitive bool) bool {
if dir == '' {
return false
}
mut p := path
mut d := dir.trim_right('/')
if case_insensitive {
p = p.to_lower()
d = d.to_lower()
}
if d == '' {
// dir was the filesystem root.
return p.starts_with('/')
}
return p == d || p.starts_with(d + '/')
}
// path_is_within reports whether `path` lies inside directory `dir`, using a
// boundary-aware comparison so that e.g. /foo/barley is NOT treated as inside
// /foo/bar (P1-01). Windows filesystem paths are compared case-insensitively.
fn path_is_within(path string, dir string) bool {
$if windows {
return path_is_within_with_case(path, dir, true)
}
return path_is_within_with_case(path, dir, false)
}
// path_relative_to_with_case returns `path` relative to `dir` using the
// requested case sensitivity. It slices by the original prefix length so paths
// that differ only in casing still produce a valid relative path.
fn path_relative_to_with_case(path string, dir string, case_insensitive bool) ?string {
if !path_is_within_with_case(path, dir, case_insensitive) {
return none
}
d := dir.trim_right('/')
if d == '' {
return path.trim_string_left('/')
}
if path.len == d.len {
return ''
}
return path[d.len..].trim_string_left('/')
}
// path_relative_to returns `path` relative to `dir` using platform filesystem
// case rules.
fn path_relative_to(path string, dir string) ?string {
$if windows {
return path_relative_to_with_case(path, dir, true)
}
return path_relative_to_with_case(path, dir, false)
}
// path_to_uri converts a local filesystem path to a `file:` DocumentUri,
// percent-encoding characters that are not allowed unescaped in a URI path.
fn path_to_uri(path string) string {
if path == '' {
return 'file:///'
}
mut normalized := os.to_slash(path)
// Windows drive letter: C:/Users/... -> /C:/Users/... so the URI keeps a
// leading slash before the authority-less path.
if normalized.len >= 2 && normalized[1] == `:` {
normalized = '/' + normalized
}
encoded := percent_encode_path(normalized)
if encoded.starts_with('/') {
return 'file://' + encoded
}
return 'file:///' + encoded
}
// make_unique_temp_path returns a collision-resistant temp file path in the
// system temp dir, tagged with the caller's purpose, the pid, and a nanosecond
// timestamp, so concurrent requests for same-named files never overwrite one
// another (P1-12).
fn make_unique_temp_path(tag string, real_path string) string {
ext := os.file_ext(real_path)
safe_ext := if ext == '' { '.v' } else { ext }
name := os.file_name(real_path)
base := if name.contains('.') { name.all_before_last('.') } else { name }
return os.join_path(os.temp_dir(),
'${tag}_${os.getpid()}_${time.now().unix_nano()}_${base}${safe_ext}')
}
fn make_singlefile_temp_path(temp_root string, real_path string, purpose string) string {
root := if temp_root != '' { temp_root } else { os.temp_dir() }
ext := os.file_ext(real_path)
safe_ext := if ext == '' { '.v' } else { ext }
tag := if purpose == '' { 'work' } else { purpose }
return os.join_path(root, 'vls_${tag}_${os.getpid()}_${time.now().unix_nano()}${safe_ext}')
}
// Argument-vector builders for the V compiler. Every value is passed as a
// separate argv element to os.Process, so no shell metacharacter (spaces,
// `$()`, backticks, quotes, `;`, `&`, `|`, `%`, ...) in a filename or path can
// alter the executed command. There is no shell involved at any point.
fn build_v_check_args_single(file_to_check string) []string {
return ['-w', '-vls-mode', '-check', '-json-errors', '-nocolor', file_to_check]
}
fn build_v_check_args_multifile() []string {
return ['-w', '-check', '-json-errors', '-nocolor', '.']
}
fn build_v_line_info_args_multifile(rel_file string, line_info string) []string {
return ['-w', '-check', '-json-errors', '-nocolor', '-vls-mode', '-line-info',
'${rel_file}:${line_info}', '.']
}
fn build_v_line_info_args_single(file_to_check string, line_info string, compile_target string) []string {
return ['-w', '-check', '-json-errors', '-nocolor', '-vls-mode', '-line-info',
'${file_to_check}:${line_info}', compile_target]
}
fn build_v_fmt_args(temp_file string) []string {
return ['fmt', '-inprocess', '-w', temp_file]
}
// Sentinel exit code returned when a compiler invocation is killed for
// exceeding compiler_timeout_ms. 124 matches the coreutils `timeout` convention.
const compiler_exit_timeout = 124
// compiler_timeout_ms bounds how long any single compiler/formatter invocation
// may run before it is force-killed, so a hung or runaway `v` process can never
// freeze the server indefinitely (partial P0-04). Overridable via VLS_TIMEOUT_MS.
const compiler_timeout_ms = resolve_compiler_timeout_ms()
fn resolve_compiler_timeout_ms() i64 {
env := os.getenv('VLS_TIMEOUT_MS')
if env != '' {
n := env.i64()
if n > 0 {
return n
}
}
return 30_000
}
// run_v_argv executes the V compiler with the given argument vector in
// `work_folder` (set on the child process, never via a process-global chdir).
// stdout and stderr are merged into one combined buffer because the compiler
// writes its `-json-errors` / `-line-info` output to STDERR; returning stdout
// alone would silently drop every diagnostic. The child is killed if it exceeds
// compiler_timeout_ms. This is the single, shell-free entry point for all
// compiler invocations.
fn run_v_argv(args []string, work_folder string) os.Result {
if work_folder != '' && !os.is_dir(work_folder) {
msg := 'Working dir does not exist: ${work_folder}'
log(msg)
return os.Result{
exit_code: 1
output: msg
}
}
mut p := os.new_process(v_compiler_exe)
p.set_args(args)
if work_folder != '' {
p.set_work_folder(work_folder)
}
p.set_redirect_stdio()
p.run()
// The V compiler writes its `-json-errors` / `-line-info` output to STDERR,
// so both streams are captured into one combined buffer (equivalent to the
// shell `2>&1` the previous implementation relied on). Returning stdout alone
// would silently drop every diagnostic.
mut out := strings.new_builder(1024)
start_ms := time.now().unix_milli()
mut timed_out := false
// Drain both pipes on every iteration and enforce the deadline. `pipe_read`
// is non-blocking (it polls the fd and returns none immediately when no data
// is pending), so a child that writes only to stderr, or one that hangs
// silently, never wedges this loop: the stderr drain still runs so a large
// payload cannot fill the pipe and deadlock the child, and the timeout check
// below still fires to kill a stuck process.
for p.is_alive() {
mut got_data := false
if chunk := p.pipe_read(.stdout) {
out.write_string(chunk)
got_data = true
}
if chunk := p.pipe_read(.stderr) {
out.write_string(chunk)
got_data = true
}
if time.now().unix_milli() - start_ms > compiler_timeout_ms {
log('v invocation exceeded ${compiler_timeout_ms}ms; killing child')
p.signal_kill()
timed_out = true
break
}
if !got_data {
// Avoid busy-spinning while the child is compiling.
time.sleep(time.millisecond)
}
}
out.write_string(p.stdout_slurp())
out.write_string(p.stderr_slurp())
p.wait()
code := p.code
p.close()
if timed_out {
return os.Result{
exit_code: compiler_exit_timeout
output: ''
}
}
return os.Result{
exit_code: code
output: out.str()
}
}
fn cleanup_compilation_temp(temp_project_dir string, singlefile_tmppath string) {
if temp_project_dir != '' {
os.rmdir_all(temp_project_dir) or { log('Failed to clean up temp project dir: ${err}') }
} else if singlefile_tmppath != '' {
os.rm(singlefile_tmppath) or { log('Failed to remove temp file: ${err}') }
}
}
struct CompilationOverlay {
source_root string
source_display_root string
temp_root string
source_work_dir string
temp_work_dir string
temp_source_file string
}
// normalize_overlay_path converts native Windows separators before paths enter
// the overlay's slash-based containment and relative-path helpers.
fn normalize_overlay_path_with_windows_rules(path string, windows bool) string {
if windows {
return path.replace('\\', '/')
}
return path
}
fn normalize_overlay_path(path string) string {
$if windows {
return normalize_overlay_path_with_windows_rules(path, true)
}
return normalize_overlay_path_with_windows_rules(path, false)
}
// compilation_overlay_root returns the broadest source root needed to preserve
// local module imports. A v.mod project is overlaid from its root; loose modules
// retain the historical same-directory scope. The lexical path is preserved so
// a nested directory symlink keeps its project-relative position.
fn compilation_overlay_root(source_path string) string {
normalized_source_path := normalize_overlay_path(source_path)
work_dir := normalize_overlay_path(os.dir(normalized_source_path))
project_root := find_project_root(work_dir)
normalized_project_root := normalize_overlay_path(project_root)
if normalized_project_root != '' && normalized_project_root != '/'
&& path_is_within(normalized_source_path, normalized_project_root) {
return normalized_project_root
}
return work_dir
}
// overlay_relative_path prefers the lexical hierarchy supplied by the client.
// Canonical paths are only a fallback for equivalent aliases such as macOS
// `/tmp` and `/private/tmp`; a nested symlink must retain its lexical segment.
fn overlay_relative_path(path string, root string) ?string {
normalized_path := normalize_overlay_path(path)
normalized_root := normalize_overlay_path(root)
if rel := path_relative_to(normalized_path, normalized_root) {
return rel
}
canonical_path := normalize_overlay_path(os.real_path(normalized_path))
canonical_root := normalize_overlay_path(os.real_path(normalized_root))
return path_relative_to(canonical_path, canonical_root)
}
fn should_use_compilation_overlay(real_path string, open_file_count int) bool {
work_dir := os.dir(real_path)
return find_project_root(work_dir) != '' || open_file_count > 1
|| has_sibling_v_files(work_dir, real_path)
}
// prepare_compilation_overlay builds a temporary project view in which every
// open buffer is materialized and unchanged project paths are symlinked back to
// disk. The compiler runs in the overlaid counterpart of the source module.
fn (mut app App) prepare_compilation_overlay(real_path string) !CompilationOverlay {
source_path := normalize_overlay_path(real_path)
source_root := compilation_overlay_root(source_path)
source_display_root := source_root
source_work_dir := normalize_overlay_path(os.dir(source_path))
temp_root_unresolved := app.write_tracked_files_to_temp(source_root)!
temp_root := normalize_overlay_path(os.real_path(temp_root_unresolved))
symlink_untracked_files(source_root, source_work_dir, temp_root, app.open_files) or {
os.rmdir_all(temp_root) or {}
return error('Failed to populate compilation overlay: ${err}')
}
work_rel := overlay_relative_path(source_work_dir, source_root) or {
os.rmdir_all(temp_root) or {}
return error('Source work directory is outside overlay root: ${source_work_dir}')
}
file_rel := overlay_relative_path(source_path, source_root) or {
os.rmdir_all(temp_root) or {}
return error('Source file is outside overlay root: ${source_path}')
}
temp_work_dir := if work_rel == '' {
temp_root
} else {
os.join_path(temp_root, work_rel)
}
if !os.exists(temp_work_dir) {
os.mkdir_all(temp_work_dir) or {
os.rmdir_all(temp_root) or {}
return error('Failed to create overlay work directory ${temp_work_dir}: ${err}')
}
}
return CompilationOverlay{
source_root: source_root
source_display_root: source_display_root
temp_root: temp_root
source_work_dir: source_work_dir
temp_work_dir: temp_work_dir
temp_source_file: os.join_path(temp_root, file_rel)
}
}
// source_path_from_overlay maps compiler paths in the temporary project back to
// their original source paths.
fn source_path_from_overlay_with_windows_rules(reported_path string, overlay CompilationOverlay, windows bool) string {
mut candidate := normalize_overlay_path_with_windows_rules(reported_path, windows)
if candidate.starts_with('./') || candidate.starts_with('.\\') {
candidate = os.join_path(overlay.temp_work_dir, candidate[2..])
} else if !os.is_abs_path(candidate) {
candidate = os.join_path(overlay.temp_work_dir, candidate)
}
candidate = normalize_overlay_path_with_windows_rules(candidate, windows)
temp_root := normalize_overlay_path_with_windows_rules(overlay.temp_root, windows)
if path_is_within_with_case(candidate, temp_root, windows) {
rel := path_relative_to_with_case(candidate, temp_root, windows) or { return candidate }
return normalize_overlay_path_with_windows_rules(os.join_path(overlay.source_display_root,
rel), windows)
}
return candidate
}
fn source_path_from_overlay(reported_path string, overlay CompilationOverlay) string {
$if windows {
return source_path_from_overlay_with_windows_rules(reported_path, overlay, true)
}
return source_path_from_overlay_with_windows_rules(reported_path, overlay, false)
}
fn (mut app App) run_v_check(path string, text string) []JsonError {
real_path := uri_to_path(path)
working_dir := os.dir(real_path)
mut temp_project_dir := ''
mut file_to_check := ''
mut compile_target := ''
mut use_multifile := false
mut singlefile_tmppath := ''
mut overlay := CompilationOverlay{}
// Check the diagnostics cache before invoking the compiler.
content_hash := text.hash()
gen := app.project_generation(path)
if cached := app.diag_cache[path] {
if cached.content_hash == content_hash && cached.generation == gen {
log('Returning cached diagnostics for ${path}')
return cached.errors
}
}
log('running v.exe check for ${real_path}')
log('Open files count: ${app.open_files.len}')
if should_use_compilation_overlay(real_path, app.open_files.len) {
overlay = app.prepare_compilation_overlay(real_path) or {
log('Failed to prepare compilation overlay: ${err}')
CompilationOverlay{}
}
if overlay.temp_root != '' {
temp_project_dir = overlay.temp_root
file_to_check = overlay.temp_source_file
compile_target = overlay.temp_work_dir
use_multifile = true
log('temp_project_dir=${temp_project_dir}, file_to_check=${file_to_check}, compile_target=${compile_target}')
}
}
if !use_multifile {
log('USING SINGLEFILE')
singlefile_tmppath = make_singlefile_temp_path(app.temp_dir, real_path, 'check')
os.write_file(singlefile_tmppath, text) or {
log('Failed to write temp file ${singlefile_tmppath}: ${err}')
return []
}
file_to_check = singlefile_tmppath
compile_target = singlefile_tmppath
}
mut cmd_args := []string{}
if use_multifile {
cmd_args = build_v_check_args_multifile()
log('MULTIFILE CMD - compile_target=${compile_target}): v ${cmd_args.join(' ')}')
} else {
cmd_args = build_v_check_args_single(file_to_check)
log('SINGLEFILE CMD: v ${cmd_args.join(' ')}')
}
exec_dir := if use_multifile { compile_target } else { working_dir }
x := run_v_argv(cmd_args, exec_dir)
log('Check - RUN RES ${x}')
cleanup_compilation_temp(temp_project_dir, singlefile_tmppath)
json_errors := json2.decode[[]JsonError](x.output) or {
log('failed to parse json ${err}')
return []
}
// error filtlering
if use_multifile {
mut filtered_errors := []JsonError{}
for err in json_errors {
err_file := source_path_from_overlay(err.path, overlay)
if normalized_index_path(err_file) == normalized_index_path(real_path) {
updated_err := JsonError{
path: real_path
message: err.message
line_nr: err.line_nr
col: err.col
len: err.len
level: err.level
}
filtered_errors << updated_err
log('INCLUDING ERROR from err_file=${err_file}: ${err.message}')
} else {
log('EXCLUDING ERROR from err_file=${err_file} real_path=${real_path}')
}
}
log('FILTERED ERRORS: ${filtered_errors.len} of ${json_errors.len}')
app.diag_cache[path] = DiagCacheEntry{
content_hash: content_hash
generation: gen
errors: filtered_errors
}
return filtered_errors
}
log('JSON ERRORS: ${json_errors.len}')
app.diag_cache[path] = DiagCacheEntry{
content_hash: content_hash
generation: gen
errors: json_errors
}
return json_errors
}
fn (mut app App) write_tracked_files_to_temp(working_dir string) !string {
log('WRITING ${app.open_files.len} tracked files to temp directory')
// create subdir
temp_project_dir := os.join_path(app.temp_dir, 'project_${time.now().unix_nano()}')
os.mkdir_all(temp_project_dir) or { return error('Failed to create temp project dir: ${err}') }
// write file structure
for uri, content in app.open_files {
file_path := normalize_overlay_path(uri_to_path(uri))
normalized_working := normalize_overlay_path(working_dir)
// Skip files outside the working dir. On Windows the containment check is
// case-insensitive, while the returned path preserves its original case.
mut rel_path := overlay_relative_path(file_path, normalized_working) or {
log('SKIPPING FILE: ${file_path}')
continue
}
if rel_path == '' {
rel_path = os.file_name(file_path)
}
temp_file_path := os.join_path(temp_project_dir, rel_path)
// create parent dir
temp_file_dir := os.dir(temp_file_path)
os.mkdir_all(temp_file_dir) or {
log('Failed to create dir ${temp_file_dir}: ${err}')
continue
}
// write file
os.write_file(temp_file_path, content) or {
log('Failed to write ${temp_file_path}: ${err}')
continue
}
log('WROTE FILE: ${temp_file_path}')
}
return temp_project_dir
}
// has_sibling_v_files reports whether the directory of the current file holds
// another `.v` file, i.e. the file is part of a multi-file V module. This uses a
// shallow directory listing rather than a full recursive tree walk: V modules
// are per-directory, and recursively walking (e.g. a huge workspace or /tmp) on
// every diagnostics cycle was a major, unnecessary cost.
fn has_sibling_v_files(working_dir string, current_file string) bool {
cur_name := os.file_name(current_file)
entries := os.ls(working_dir) or { return false }
for entry in entries {
if entry == cur_name {
continue
}
if entry.ends_with('.v') {
full := os.join_path(working_dir, entry)
if os.is_file(full) {
return true
}
}
}
return false
}
type OverlayLinkFn = fn (string, string) !
const overlay_copy_excluded_dirs = ['.git', '.hg', '.svn', '.cache', '.idea', '.vscode', '.vmodules',
'node_modules', 'thirdparty', '_build', 'build', 'target']
// These limits apply across the entire fallback overlay, not per directory.
const overlay_copy_max_files = 4096
const overlay_copy_max_bytes = u64(64 * 1024 * 1024)
struct OverlayCopyBudget {
max_files int
max_bytes u64
mut:
files int
bytes u64
}
fn new_overlay_copy_budget() OverlayCopyBudget {
return OverlayCopyBudget{
max_files: overlay_copy_max_files
max_bytes: overlay_copy_max_bytes
}
}
fn (mut budget OverlayCopyBudget) try_reserve(path string) bool {
if budget.files >= budget.max_files {
return false
}
size := os.file_size(path)
if size > budget.max_bytes || budget.bytes > budget.max_bytes - size {
return false
}
budget.files++
budget.bytes += size
return true
}
fn overlay_path_in_with_case(path string, paths []string, case_insensitive bool) bool {
for candidate in paths {
if path_is_within_with_case(path, candidate, case_insensitive)
&& path_is_within_with_case(candidate, path, case_insensitive) {
return true
}
}
return false
}
fn overlay_path_in(path string, paths []string) bool {
$if windows {
return overlay_path_in_with_case(path, paths, true)
}
return overlay_path_in_with_case(path, paths, false)
}
fn overlay_path_has_descendant_with_case(path string, paths []string, case_insensitive bool) bool {
for candidate in paths {
if path_is_within_with_case(candidate, path, case_insensitive) {
return true
}
}
return false
}
fn overlay_path_has_descendant(path string, paths []string) bool {
$if windows {
return overlay_path_has_descendant_with_case(path, paths, true)
}
return overlay_path_has_descendant_with_case(path, paths, false)
}
fn create_overlay_symlink(source_path string, target_path string) ! {
os.symlink(source_path, target_path)!
}
fn create_overlay_hard_link(source_path string, target_path string) ! {
os.link(source_path, target_path)!
}
fn materialize_overlay_file_with_linker(source_path string, target_path string, link_fn OverlayLinkFn, mut budget OverlayCopyBudget) !bool {
link_fn(source_path, target_path) or {
if !budget.try_reserve(source_path) {
return false
}
os.cp(source_path, target_path)!
}
return true
}
fn materialize_overlay_file(source_path string, target_path string, mut budget OverlayCopyBudget) !bool {
return materialize_overlay_file_with_linker(source_path, target_path, create_overlay_hard_link, mut
budget)
}
fn symlink_untracked_files(source_root string, source_module_dir string, temp_dir string, tracked_files map[string]string) ! {
symlink_untracked_files_with_linker(source_root, source_module_dir, temp_dir, tracked_files,
create_overlay_symlink)!
}
fn symlink_untracked_files_with_linker(source_root string, source_module_dir string, temp_dir string, tracked_files map[string]string, link_fn OverlayLinkFn) ! {
log('SYMLINKING FROM ${source_root} TO ${temp_dir}')
mut tracked_rel_paths := []string{}
normalized_source_root := normalize_overlay_path(source_root)
normalized_module_dir := normalize_overlay_path(source_module_dir)
for uri, _ in tracked_files {
file_path := normalize_overlay_path(uri_to_path(uri))
if rel := overlay_relative_path(file_path, normalized_source_root) {
tracked_rel_paths << normalize_overlay_path(rel)
}
}
local_import_dirs := local_import_rel_dirs(normalized_source_root, normalized_module_dir,
tracked_files)
mut copy_budget := new_overlay_copy_budget()
thirdparty_references := referenced_thirdparty_rel_paths(normalized_source_root,
normalized_module_dir, tracked_files, local_import_dirs)
materialize_referenced_thirdparty_inputs(normalized_source_root, temp_dir,
thirdparty_references, mut copy_budget)!
symlink_untracked_tree(normalized_source_root, normalized_source_root, temp_dir, '',
tracked_rel_paths, local_import_dirs, link_fn, mut copy_budget)!
}
// local_import_rel_dirs returns project-local module directories imported by
// tracked buffers, including their local import closure. These directories must
// be real directories in the overlay: the V checker resolves symlinked local
// module files back outside the temporary project and gd^ can then stop at the
// import declaration instead of the requested symbol.
fn local_import_rel_dirs(source_root string, source_module_dir string, tracked_files map[string]string) []string {
mut pending := []string{}
for _, content in tracked_files {
pending << parse_imports(content)
}
mut seen_modules := map[string]bool{}
mut seen_dirs := map[string]bool{}
mut result := []string{}
for pending.len > 0 {
module_path := pending.pop()
if module_path == '' || module_path in seen_modules {
continue
}
seen_modules[module_path] = true
rel_dir := module_path.replace('.', '/')
mut module_dir := os.join_path(source_module_dir, rel_dir)
if !os.is_dir(module_dir) {
module_dir = os.join_path(source_root, rel_dir)
}
if !os.is_dir(module_dir) {
continue
}
normalized_module_dir := normalize_overlay_path(module_dir)
normalized_rel := overlay_relative_path(normalized_module_dir, source_root) or { continue }
if normalized_rel == '' || normalized_rel in seen_dirs {
continue
}
seen_dirs[normalized_rel] = true
result << normalized_rel
for entry in os.ls(module_dir) or { [] } {
if !entry.ends_with('.v') || entry.ends_with('_test.v') {
continue
}
content := os.read_file(os.join_path(module_dir, entry)) or { continue }
pending << parse_imports(content)
}
}
return result
}
fn parse_embed_file_literal_paths(content string) []string {
marker := r'$embed_file'
mut result := []string{}
mut search_start := 0
for search_start < content.len {
offset := content[search_start..].index(marker) or { break }
mut pos := search_start + offset + marker.len
for pos < content.len && content[pos].is_space() {
pos++
}
if pos >= content.len || content[pos] != `(` {
search_start = pos
continue
}
pos++
for pos < content.len && content[pos].is_space() {
pos++
}
mut is_raw := false
if pos + 1 < content.len && content[pos] == `r` && content[pos + 1] in [`'`, `"`] {
is_raw = true
pos++
}
if pos >= content.len || content[pos] !in [`'`, `"`] {
search_start = pos
continue
}
quote := content[pos]
pos++
path_start := pos
for pos < content.len {
if !is_raw && content[pos] == `\\` && pos + 1 < content.len {
pos += 2
continue
}
if content[pos] == quote {
result << content[path_start..pos]
pos++
break
}
pos++
}
search_start = pos
}
return result
}
fn parse_vmodroot_thirdparty_paths(content string) []string {
marker := '@VMODROOT/thirdparty'
mut result := []string{}
mut search_start := 0
for search_start < content.len {
offset := content[search_start..].index(marker) or { break }
start := search_start + offset
mut end := start + marker.len
for end < content.len && !content[end].is_space()
&& content[end] !in [`'`, `"`, `)`, `]`, `}`, `,`, `;`] {
end++
}
result << content[start..end]
search_start = end
}
return result
}
fn resolve_thirdparty_overlay_reference(reference string, source_file string, source_root string) ?string {
mut source_path := if reference.starts_with('@VMODROOT/') {
os.join_path(source_root, reference['@VMODROOT/'.len..])
} else if reference == '@VMODROOT' {
source_root
} else if os.is_abs_path(reference) || reference.starts_with('@VEXEROOT') {
return none
} else {
os.join_path(os.dir(source_file), reference)
}
source_path = normalize_overlay_path(os.norm_path(source_path))
rel := overlay_relative_path(source_path, source_root) or { return none }
normalized_rel := normalize_overlay_path(os.norm_path(rel))
if normalized_rel != 'thirdparty' && !normalized_rel.starts_with('thirdparty/') {
return none
}
if !os.exists(source_path) {
return none
}
return normalized_rel
}
fn referenced_thirdparty_rel_paths(source_root string, source_module_dir string, tracked_files map[string]string, local_import_dirs []string) []string {
mut source_contents := map[string]string{}
for uri, content in tracked_files {
source_path := normalize_overlay_path(uri_to_path(uri))
overlay_relative_path(source_path, source_root) or { continue }
source_contents[source_path] = content
}
mut module_dirs := [source_module_dir]
for rel_dir in local_import_dirs {
module_dirs << os.join_path(source_root, rel_dir)
}
mut seen_dirs := map[string]bool{}
for module_dir in module_dirs {
normalized_dir := normalize_overlay_path(module_dir)
if normalized_dir in seen_dirs {
continue
}
seen_dirs[normalized_dir] = true
for entry in os.ls(module_dir) or { [] } {
if !entry.ends_with('.v') || entry.ends_with('_test.v') {
continue
}
source_path := normalize_overlay_path(os.join_path(module_dir, entry))
if source_path in source_contents {
continue
}
source_contents[source_path] = os.read_file(source_path) or { continue }
}
}
mut references := map[string]bool{}
for source_file, content in source_contents {
mut candidates := parse_embed_file_literal_paths(content)
candidates << parse_vmodroot_thirdparty_paths(content)
for candidate in candidates {
rel := resolve_thirdparty_overlay_reference(candidate, source_file, source_root) or {
continue
}
references[rel] = true
}
}
mut result := references.keys()
result.sort()
return result
}
fn is_overlay_compilation_file(path string) bool {
name := os.file_name(path)
if name == 'v.mod' {
return true
}
for suffix in ['.v', '.vsh', '.vv', '.c', '.h', '.cc', '.cpp', '.cxx', '.hpp', '.m', '.mm',
'.s', '.asm', '.js', '.a', '.o', '.so', '.dylib', '.dll', '.lib'] {
if name.ends_with(suffix) {
return true
}
}
return false
}
fn copy_bounded_overlay_entry_pass(source_path string, target_path string, compilation_files bool, mut budget OverlayCopyBudget, mut visited map[string]bool) !int {
if os.is_dir(source_path) {
real_path := normalize_overlay_path(os.real_path(source_path))
if real_path in visited {
return 0
}
name := os.file_name(source_path)
if name in overlay_copy_excluded_dirs {
return 0
}
visited[real_path] = true
mut copied := 0
for entry in os.ls(source_path)! {
copied += copy_bounded_overlay_entry_pass(os.join_path(source_path, entry), os.join_path(target_path,
entry), compilation_files, mut budget, mut visited)!
}
return copied
}
if !os.is_file(source_path) || is_overlay_compilation_file(source_path) != compilation_files {
return 0
}
if os.exists(target_path) || os.is_link(target_path) {
return 0
}
if !budget.try_reserve(source_path) {
return 0
}