-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathconfig.rs
More file actions
1172 lines (986 loc) · 47.4 KB
/
Copy pathconfig.rs
File metadata and controls
1172 lines (986 loc) · 47.4 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
//! Configuration and version resolution for the env command.
//!
//! This module provides:
//! - VP_HOME path resolution
//! - Version resolution with priority order
//! - Config file management
use serde::{Deserialize, Serialize};
use vite_js_runtime::{
NodeProvider, VersionSource, normalize_version, read_package_json, resolve_node_version,
};
use vite_path::{AbsolutePath, AbsolutePathBuf};
use crate::error::Error;
/// Config file name
const CONFIG_FILE: &str = "config.json";
/// Shim mode determines how shims resolve tools.
#[derive(Serialize, Deserialize, Clone, Copy, Debug, Default, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ShimMode {
/// Shims always use vite-plus managed Node.js
#[default]
Managed,
/// Shims prefer system Node.js, fallback to managed if not found
SystemFirst,
}
/// User configuration stored in VP_HOME/config.json
#[derive(Serialize, Deserialize, Default, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Config {
/// Default Node.js version when no project version file is found
#[serde(default, skip_serializing_if = "Option::is_none")]
pub default_node_version: Option<String>,
/// Shim mode for tool resolution
#[serde(default, skip_serializing_if = "is_default_shim_mode")]
pub shim_mode: ShimMode,
}
/// Check if shim mode is the default (for skip_serializing_if)
fn is_default_shim_mode(mode: &ShimMode) -> bool {
*mode == ShimMode::Managed
}
/// Version resolution result
#[derive(Debug)]
pub struct VersionResolution {
/// The resolved version string (e.g., "20.18.0")
pub version: String,
/// The source of the version (e.g., ".node-version", "engines.node", "default")
pub source: String,
/// Path to the source file (if applicable)
pub source_path: Option<AbsolutePathBuf>,
/// Project root directory (if version came from a project file)
pub project_root: Option<AbsolutePathBuf>,
/// Whether the original version spec was a range (e.g., "20", "^20.0.0", "lts/*")
/// Range versions should use time-based cache expiry instead of mtime-only validation
pub is_range: bool,
}
/// Get the VP_HOME directory path.
///
/// Uses `VP_HOME` environment variable if set, otherwise defaults to `~/.vite-plus`.
pub fn get_vp_home() -> Result<AbsolutePathBuf, Error> {
Ok(vite_shared::get_vp_home()?)
}
/// Get the bin directory path (~/.vite-plus/bin/).
pub fn get_bin_dir() -> Result<AbsolutePathBuf, Error> {
Ok(get_vp_home()?.join("bin"))
}
/// Get the packages directory path (~/.vite-plus/packages/).
pub fn get_packages_dir() -> Result<AbsolutePathBuf, Error> {
Ok(get_vp_home()?.join("packages"))
}
/// Get the tmp directory path for staging (~/.vite-plus/tmp/).
pub fn get_tmp_dir() -> Result<AbsolutePathBuf, Error> {
Ok(get_vp_home()?.join("tmp"))
}
/// Get the node_modules directory path for a package.
///
/// npm uses different layouts on Unix vs Windows:
/// - Unix: `<prefix>/lib/node_modules/<package>`
/// - Windows: `<prefix>/node_modules/<package>`
///
/// This function probes both paths and returns the one that exists,
/// falling back to the platform default if neither exists.
pub fn get_node_modules_dir(prefix: &AbsolutePath, package_name: &str) -> AbsolutePathBuf {
// Try Unix layout first (lib/node_modules)
let unix_path = prefix.join("lib").join("node_modules").join(package_name);
if unix_path.as_path().exists() {
return unix_path;
}
// Try Windows layout (node_modules)
let win_path = prefix.join("node_modules").join(package_name);
if win_path.as_path().exists() {
return win_path;
}
// Neither exists - return platform default (for pre-creation checks)
#[cfg(windows)]
{
win_path
}
#[cfg(not(windows))]
{
unix_path
}
}
/// Get the config file path.
pub fn get_config_path() -> Result<AbsolutePathBuf, Error> {
Ok(get_vp_home()?.join(CONFIG_FILE))
}
/// Load configuration from disk.
pub async fn load_config() -> Result<Config, Error> {
let config_path = get_config_path()?;
if !tokio::fs::try_exists(&config_path).await.unwrap_or(false) {
return Ok(Config::default());
}
let content = tokio::fs::read_to_string(&config_path).await?;
let config: Config = serde_json::from_str(&content)?;
Ok(config)
}
/// Save configuration to disk.
pub async fn save_config(config: &Config) -> Result<(), Error> {
let config_path = get_config_path()?;
let vite_plus_home = get_vp_home()?;
// Ensure directory exists
tokio::fs::create_dir_all(&vite_plus_home).await?;
let content = serde_json::to_string_pretty(config)?;
tokio::fs::write(&config_path, content).await?;
Ok(())
}
/// Environment variable for per-shell session Node.js version override.
/// Set by `vp env use` command.
pub const VERSION_ENV_VAR: &str = vite_shared::env_vars::VP_NODE_VERSION;
/// Session version file name, written by `vp env use` so shims work without the shell eval wrapper.
pub const SESSION_VERSION_FILE: &str = ".session-node-version";
/// Get the path to the session version file (~/.vite-plus/.session-node-version).
pub fn get_session_version_path() -> Result<AbsolutePathBuf, Error> {
Ok(get_vp_home()?.join(SESSION_VERSION_FILE))
}
/// Read the session version file. Returns `None` if the file is missing or empty.
pub async fn read_session_version() -> Option<String> {
let path = get_session_version_path().ok()?;
let content = tokio::fs::read_to_string(&path).await.ok()?;
let trimmed = content.trim().to_string();
if trimmed.is_empty() { None } else { Some(trimmed) }
}
/// Read the session version file synchronously. Returns `None` if the file is missing or empty.
pub fn read_session_version_sync() -> Option<String> {
let path = get_session_version_path().ok()?;
let content = std::fs::read_to_string(path.as_path()).ok()?;
let trimmed = content.trim().to_string();
if trimmed.is_empty() { None } else { Some(trimmed) }
}
/// Write the resolved version to the session version file.
pub async fn write_session_version(version: &str) -> Result<(), Error> {
let path = get_session_version_path()?;
// Ensure parent directory exists
if let Some(parent) = path.parent() {
tokio::fs::create_dir_all(parent).await?;
}
tokio::fs::write(&path, version).await?;
Ok(())
}
/// Delete the session version file. Ignores "not found" errors.
pub async fn delete_session_version() -> Result<(), Error> {
let path = get_session_version_path()?;
match tokio::fs::remove_file(&path).await {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e.into()),
}
}
/// Resolve Node.js version for a directory.
///
/// Resolution order:
/// 0. `VP_NODE_VERSION` env var (session override from `vp env use`)
/// 1. `.session-node-version` file (session override written by `vp env use` for shell-wrapper-less environments)
/// 2. `.node-version` file in current or parent directories
/// 3. `package.json#devEngines.runtime` in current or parent directories
/// 4. `package.json#engines.node` in current or parent directories
/// 5. User default from config.json
/// 6. Latest LTS version
pub async fn resolve_version(cwd: &AbsolutePath) -> Result<VersionResolution, Error> {
// Session override via environment variable (set by `vp env use`)
if let Some(env_version) = vite_shared::EnvConfig::get().node_version {
let env_version = env_version.trim();
if !env_version.is_empty() {
return Ok(VersionResolution {
version: env_version.to_string(),
source: VERSION_ENV_VAR.into(),
source_path: None,
project_root: None,
is_range: false,
});
}
}
// Session override via file (written by `vp env use` for shell-wrapper-less environments)
if let Some(session_version) = read_session_version().await {
return Ok(VersionResolution {
version: session_version,
source: SESSION_VERSION_FILE.into(),
source_path: get_session_version_path().ok(),
project_root: None,
is_range: false,
});
}
resolve_version_from_files(cwd).await
}
/// Resolve Node.js version from project files only (skipping session overrides).
///
/// This is used by `vp env use` without arguments to revert to file-based resolution.
pub async fn resolve_version_from_files(cwd: &AbsolutePath) -> Result<VersionResolution, Error> {
let provider = NodeProvider::new();
// Use shared version resolution with directory walking
let resolution = resolve_node_version(cwd, true)
.await
.map_err(|e| Error::ConfigError(e.to_string().into()))?;
if let Some(resolution) = resolution {
// Validate version before attempting resolution
// If invalid, warning is printed by normalize_version and we fall through to defaults
if let Some(validated) =
normalize_version(&resolution.version.clone().into(), &resolution.source.to_string())
{
// Detect if the original version spec was a range (not exact)
// This includes partial versions (20, 20.18), semver ranges (^20.0.0), LTS aliases, and "latest"
let is_range = NodeProvider::is_version_alias(&validated)
|| !NodeProvider::is_exact_version(&validated);
let resolved = resolve_version_string(&validated, &provider).await?;
return Ok(VersionResolution {
version: resolved,
source: resolution.source.to_string(),
source_path: resolution.source_path,
project_root: resolution.project_root,
is_range,
});
}
// Invalid version from a project source - try lower-priority sources in the same directory.
// This mirrors the fallback logic in download_runtime_for_project().
// - NodeVersionFile: try devEngines.runtime, then engines.node
// - DevEnginesRuntime: try engines.node
if matches!(
resolution.source,
VersionSource::NodeVersionFile | VersionSource::DevEnginesRuntime
) {
if let Some(project_root) = &resolution.project_root {
let package_json_path = project_root.join("package.json");
if let Ok(Some(pkg)) = read_package_json(&package_json_path).await {
// Try devEngines.runtime (only when falling back from .node-version)
if matches!(resolution.source, VersionSource::NodeVersionFile) {
if let Some(dev_engines) = pkg
.dev_engines_runtime("node")
.and_then(|r| r.version.clone())
.and_then(|v| normalize_version(&v, "devEngines.runtime"))
{
let resolved = resolve_version_string(&dev_engines, &provider).await?;
let is_range = NodeProvider::is_lts_alias(&dev_engines)
|| !NodeProvider::is_exact_version(&dev_engines);
return Ok(VersionResolution {
version: resolved,
source: "devEngines.runtime".into(),
source_path: Some(package_json_path),
project_root: Some(project_root.clone()),
is_range,
});
}
}
// Try engines.node
if let Some(engines_node) = pkg
.engines
.as_ref()
.and_then(|e| e.node.clone())
.and_then(|v| normalize_version(&v, "engines.node"))
{
let resolved = resolve_version_string(&engines_node, &provider).await?;
let is_range = NodeProvider::is_lts_alias(&engines_node)
|| !NodeProvider::is_exact_version(&engines_node);
return Ok(VersionResolution {
version: resolved,
source: "engines.node".into(),
source_path: Some(package_json_path),
project_root: Some(project_root.clone()),
is_range,
});
}
}
}
}
// Invalid version and no valid package.json sources - fall through to user default or LTS
}
// CLI-specific: Check user default from config
let config = load_config().await?;
if let Some(default_version) = config.default_node_version {
let resolved = resolve_version_alias(&default_version, &provider).await?;
// Check if default is an alias or range
let is_alias = matches!(default_version.to_lowercase().as_str(), "lts" | "latest");
let is_range = is_alias
|| NodeProvider::is_lts_alias(&default_version)
|| !NodeProvider::is_exact_version(&default_version);
return Ok(VersionResolution {
version: resolved,
source: "default".into(),
// Don't set source_path for aliases (lts, latest) so cache can refresh
source_path: if is_alias { None } else { Some(get_config_path()?) },
project_root: None,
is_range,
});
}
// CLI-specific: Fall back to latest LTS
let version = provider.resolve_latest_version().await?;
Ok(VersionResolution {
version: version.to_string(),
source: "lts".into(),
source_path: None,
project_root: None,
is_range: true, // LTS fallback is always a range (re-resolve periodically)
})
}
/// Resolve a version string to an exact version.
async fn resolve_version_string(version: &str, provider: &NodeProvider) -> Result<String, Error> {
// Check for LTS alias first (lts/*, lts/iron, lts/-1)
if NodeProvider::is_lts_alias(version) {
let resolved = provider.resolve_lts_alias(version).await?;
return Ok(resolved.to_string());
}
// Check for "latest" alias - resolves to absolute latest version (including non-LTS)
if NodeProvider::is_latest_alias(version) {
let resolved = provider.resolve_absolute_latest_version().await?;
return Ok(resolved.to_string());
}
// If it's already an exact version, use it directly
if NodeProvider::is_exact_version(version) {
// Strip v prefix if present (e.g., "v20.18.0" -> "20.18.0")
let normalized = version.strip_prefix('v').unwrap_or(version);
return Ok(normalized.to_string());
}
// Resolve from network (semver ranges)
let resolved = provider.resolve_version(version).await?;
Ok(resolved.to_string())
}
/// Resolve version alias (lts, latest) to an exact version.
///
/// Wraps resolution errors with a user-friendly message showing valid examples.
pub async fn resolve_version_alias(
version: &str,
provider: &NodeProvider,
) -> Result<String, Error> {
let result = match version.to_lowercase().as_str() {
"lts" => {
let resolved = provider.resolve_latest_version().await?;
Ok(resolved.to_string())
}
"latest" => {
let resolved = provider.resolve_absolute_latest_version().await?;
Ok(resolved.to_string())
}
_ => resolve_version_string(version, provider).await,
};
result.map_err(|e| match e {
Error::RuntimeDownload(
vite_js_runtime::Error::SemverRange(_)
| vite_js_runtime::Error::NoMatchingVersion { .. },
) => Error::Other(
format!(
"Invalid Node.js version: \"{version}\"\n\n\
Valid examples:\n \
vp env use 20 # Latest Node.js 20.x\n \
vp env use 20.18.0 # Exact version\n \
vp env use lts # Latest LTS version\n \
vp env use latest # Latest version"
)
.into(),
),
other => other,
})
}
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use vite_js_runtime::VersionSource;
use vite_path::AbsolutePathBuf;
use super::*;
#[test]
fn test_get_node_modules_dir_probes_unix_layout() {
let temp_dir = TempDir::new().unwrap();
let prefix = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
// Create Unix layout
let unix_path = temp_dir.path().join("lib").join("node_modules").join("test-pkg");
std::fs::create_dir_all(&unix_path).unwrap();
let result = get_node_modules_dir(&prefix, "test-pkg");
assert!(
result.as_path().ends_with("lib/node_modules/test-pkg"),
"Should find Unix layout: {}",
result.as_path().display()
);
}
#[test]
fn test_get_node_modules_dir_probes_windows_layout() {
let temp_dir = TempDir::new().unwrap();
let prefix = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
// Create Windows layout (no lib/)
let win_path = temp_dir.path().join("node_modules").join("test-pkg");
std::fs::create_dir_all(&win_path).unwrap();
let result = get_node_modules_dir(&prefix, "test-pkg");
assert!(
result.as_path().ends_with("node_modules/test-pkg")
&& !result.as_path().to_string_lossy().contains("lib/node_modules"),
"Should find Windows layout: {}",
result.as_path().display()
);
}
#[test]
fn test_get_node_modules_dir_prefers_unix_layout_when_both_exist() {
let temp_dir = TempDir::new().unwrap();
let prefix = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
// Create both layouts
let unix_path = temp_dir.path().join("lib").join("node_modules").join("test-pkg");
let win_path = temp_dir.path().join("node_modules").join("test-pkg");
std::fs::create_dir_all(&unix_path).unwrap();
std::fs::create_dir_all(&win_path).unwrap();
let result = get_node_modules_dir(&prefix, "test-pkg");
// Unix layout is checked first
assert!(
result.as_path().ends_with("lib/node_modules/test-pkg"),
"Should prefer Unix layout when both exist: {}",
result.as_path().display()
);
}
#[test]
fn test_get_node_modules_dir_returns_platform_default_when_neither_exists() {
let temp_dir = TempDir::new().unwrap();
let prefix = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
// Don't create any directories
let result = get_node_modules_dir(&prefix, "test-pkg");
#[cfg(windows)]
assert!(
result.as_path().ends_with("node_modules/test-pkg")
&& !result.as_path().to_string_lossy().contains("lib/node_modules"),
"Should return Windows default: {}",
result.as_path().display()
);
#[cfg(not(windows))]
assert!(
result.as_path().ends_with("lib/node_modules/test-pkg"),
"Should return Unix default: {}",
result.as_path().display()
);
}
#[test]
fn test_get_node_modules_dir_handles_scoped_packages() {
let temp_dir = TempDir::new().unwrap();
let prefix = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
// Create Unix layout for scoped package
let unix_path = temp_dir.path().join("lib").join("node_modules").join("@scope").join("pkg");
std::fs::create_dir_all(&unix_path).unwrap();
let result = get_node_modules_dir(&prefix, "@scope/pkg");
assert!(
result.as_path().ends_with("lib/node_modules/@scope/pkg"),
"Should find scoped package: {}",
result.as_path().display()
);
}
#[tokio::test]
async fn test_resolve_version_from_node_version_file() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let _guard = vite_shared::EnvConfig::test_guard(vite_shared::EnvConfig::for_test());
// Create .node-version file
tokio::fs::write(temp_path.join(".node-version"), "20.18.0\n").await.unwrap();
let resolution = resolve_version(&temp_path).await.unwrap();
assert_eq!(resolution.version, "20.18.0");
assert_eq!(resolution.source, ".node-version");
assert!(resolution.source_path.is_some());
}
#[tokio::test]
async fn test_resolve_version_walks_up_directory() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let _guard = vite_shared::EnvConfig::test_guard(vite_shared::EnvConfig::for_test());
// Create .node-version in parent
tokio::fs::write(temp_path.join(".node-version"), "20.18.0\n").await.unwrap();
// Create subdirectory
let subdir = temp_path.join("subdir");
tokio::fs::create_dir(&subdir).await.unwrap();
let resolution = resolve_version(&subdir).await.unwrap();
assert_eq!(resolution.version, "20.18.0");
assert_eq!(resolution.source, ".node-version");
}
#[tokio::test]
async fn test_resolve_version_from_engines_node() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
// Create package.json with engines.node
// Also create an empty .node-version to stop walk-up from finding parent project's version
let package_json = r#"{"engines":{"node":"20.18.0"}}"#;
tokio::fs::write(temp_path.join("package.json"), package_json).await.unwrap();
// Use resolve_node_version directly with walk_up=false to test engines.node specifically
let resolution = resolve_node_version(&temp_path, false)
.await
.map_err(|e| Error::ConfigError(e.to_string().into()))
.unwrap()
.unwrap();
assert_eq!(&*resolution.version, "20.18.0");
assert_eq!(resolution.source, VersionSource::EnginesNode);
}
#[tokio::test]
async fn test_resolve_version_from_dev_engines() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
// Create package.json with devEngines.runtime
let package_json = r#"{"devEngines":{"runtime":{"name":"node","version":"20.18.0"}}}"#;
tokio::fs::write(temp_path.join("package.json"), package_json).await.unwrap();
// Use resolve_node_version directly with walk_up=false to test devEngines specifically
let resolution = resolve_node_version(&temp_path, false)
.await
.map_err(|e| Error::ConfigError(e.to_string().into()))
.unwrap()
.unwrap();
assert_eq!(&*resolution.version, "20.18.0");
assert_eq!(resolution.source, VersionSource::DevEnginesRuntime);
}
#[tokio::test]
async fn test_resolve_version_node_version_takes_priority() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let _guard = vite_shared::EnvConfig::test_guard(vite_shared::EnvConfig::for_test());
// Create both .node-version and package.json with engines.node
tokio::fs::write(temp_path.join(".node-version"), "22.0.0\n").await.unwrap();
let package_json = r#"{"engines":{"node":"20.18.0"}}"#;
tokio::fs::write(temp_path.join("package.json"), package_json).await.unwrap();
let resolution = resolve_version(&temp_path).await.unwrap();
// .node-version should take priority
assert_eq!(resolution.version, "22.0.0");
assert_eq!(resolution.source, ".node-version");
}
#[tokio::test]
async fn test_resolve_version_string_strips_v_prefix() {
let provider = NodeProvider::new();
// Test that v-prefixed exact versions are normalized
let result = resolve_version_string("v20.18.0", &provider).await.unwrap();
assert_eq!(result, "20.18.0", "v prefix should be stripped from exact versions");
}
#[tokio::test]
#[ignore] // Requires running outside of any Node.js project (walk-up finds .node-version)
async fn test_resolve_version_alias_default_no_source_path() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let _guard = vite_shared::EnvConfig::test_guard(
vite_shared::EnvConfig::for_test_with_home(temp_dir.path()),
);
let config = Config { default_node_version: Some("lts".to_string()), ..Default::default() };
save_config(&config).await.unwrap();
// Create empty dir to resolve version in (no .node-version)
let test_dir = temp_path.join("test-project");
tokio::fs::create_dir_all(&test_dir).await.unwrap();
let resolution = resolve_version(&test_dir).await.unwrap();
assert_eq!(resolution.source, "default");
assert!(resolution.source_path.is_none(), "Alias defaults should not have source_path");
}
#[tokio::test]
#[ignore] // Requires running outside of any Node.js project (walk-up finds .node-version)
async fn test_resolve_version_exact_default_has_source_path() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let _guard = vite_shared::EnvConfig::test_guard(
vite_shared::EnvConfig::for_test_with_home(temp_dir.path()),
);
let config =
Config { default_node_version: Some("20.18.0".to_string()), ..Default::default() };
save_config(&config).await.unwrap();
// Create empty dir to resolve version in (no .node-version)
let test_dir = temp_path.join("test-project");
tokio::fs::create_dir_all(&test_dir).await.unwrap();
let resolution = resolve_version(&test_dir).await.unwrap();
assert_eq!(resolution.source, "default");
assert!(resolution.source_path.is_some(), "Exact version defaults should have source_path");
}
#[tokio::test]
async fn test_resolve_version_invalid_node_version_falls_through_to_lts() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let _guard = vite_shared::EnvConfig::test_guard(
vite_shared::EnvConfig::for_test_with_home(temp_dir.path()),
);
// Create .node-version file with invalid version
tokio::fs::write(temp_path.join(".node-version"), "invalid-version\n").await.unwrap();
// resolve_version should NOT fail - it should fall through to LTS
let resolution = resolve_version(&temp_path).await.unwrap();
// Should fall through to LTS since the .node-version is invalid
// and no user default is configured
assert_eq!(resolution.source, "lts");
assert!(resolution.source_path.is_none());
assert!(resolution.is_range, "LTS fallback should be marked as range");
}
#[tokio::test]
async fn test_resolve_version_invalid_node_version_falls_through_to_default() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let _guard = vite_shared::EnvConfig::test_guard(
vite_shared::EnvConfig::for_test_with_home(temp_dir.path()),
);
// Create .node-version file with invalid version
tokio::fs::write(temp_path.join(".node-version"), "not-a-version\n").await.unwrap();
// Create config with a default version
let config =
Config { default_node_version: Some("20.18.0".to_string()), ..Default::default() };
save_config(&config).await.unwrap();
// resolve_version should NOT fail - it should fall through to user default
let resolution = resolve_version(&temp_path).await.unwrap();
// Should fall through to user default since .node-version is invalid
assert_eq!(resolution.source, "default");
assert_eq!(resolution.version, "20.18.0");
}
#[tokio::test]
async fn test_resolve_version_invalid_node_version_falls_through_to_engines_node() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let _guard = vite_shared::EnvConfig::test_guard(
vite_shared::EnvConfig::for_test_with_home(temp_dir.path()),
);
// Create .node-version file with invalid version (typo or unsupported alias)
tokio::fs::write(temp_path.join(".node-version"), "laetst\n").await.unwrap();
// Create package.json with valid engines.node
let package_json = r#"{"engines":{"node":"^20.18.0"}}"#;
tokio::fs::write(temp_path.join("package.json"), package_json).await.unwrap();
// resolve_version should NOT fail - it should fall through to engines.node
let resolution = resolve_version(&temp_path).await.unwrap();
// Should fall through to engines.node since .node-version is invalid
assert_eq!(resolution.source, "engines.node");
// Version should be resolved from ^20.18.0 (a 20.x version)
assert!(
resolution.version.starts_with("20."),
"Expected version to start with '20.', got: {}",
resolution.version
);
}
#[tokio::test]
async fn test_resolve_version_invalid_node_version_falls_through_to_dev_engines() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let _guard = vite_shared::EnvConfig::test_guard(
vite_shared::EnvConfig::for_test_with_home(temp_dir.path()),
);
// Create .node-version file with invalid version
tokio::fs::write(temp_path.join(".node-version"), "invalid\n").await.unwrap();
// Create package.json with devEngines.runtime but no engines.node
let package_json = r#"{"devEngines":{"runtime":{"name":"node","version":"^20.18.0"}}}"#;
tokio::fs::write(temp_path.join("package.json"), package_json).await.unwrap();
// resolve_version should NOT fail - it should fall through to devEngines.runtime
let resolution = resolve_version(&temp_path).await.unwrap();
// Should fall through to devEngines.runtime since .node-version is invalid
assert_eq!(resolution.source, "devEngines.runtime");
// Version should be resolved from ^20.18.0 (a 20.x version)
assert!(
resolution.version.starts_with("20."),
"Expected version to start with '20.', got: {}",
resolution.version
);
}
#[tokio::test]
async fn test_resolve_version_invalid_engines_node_falls_through_to_dev_engines() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let _guard = vite_shared::EnvConfig::test_guard(
vite_shared::EnvConfig::for_test_with_home(temp_dir.path()),
);
// Create package.json with invalid engines.node but valid devEngines.runtime
// No .node-version file — resolve_node_version returns EnginesNode source
let package_json = r#"{"engines":{"node":"invalid"},"devEngines":{"runtime":{"name":"node","version":"^20.18.0"}}}"#;
tokio::fs::write(temp_path.join("package.json"), package_json).await.unwrap();
// resolve_version should fall through from invalid engines.node to devEngines.runtime
let resolution = resolve_version(&temp_path).await.unwrap();
assert_eq!(resolution.source, "devEngines.runtime");
assert!(
resolution.version.starts_with("20."),
"Expected version to start with '20.', got: {}",
resolution.version
);
}
#[tokio::test]
async fn test_resolve_version_latest_alias_in_node_version() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let _guard = vite_shared::EnvConfig::test_guard(vite_shared::EnvConfig::for_test());
// Create .node-version file with "latest" alias
tokio::fs::write(temp_path.join(".node-version"), "latest\n").await.unwrap();
let resolution = resolve_version(&temp_path).await.unwrap();
// Should resolve from .node-version
assert_eq!(resolution.source, ".node-version");
// "latest" is a range (should be re-resolved periodically)
assert!(resolution.is_range, "'latest' should be marked as a range");
// Version should be at least v20.x
assert!(
resolution.version.starts_with("2") || resolution.version.starts_with("3"),
"Expected version to be at least v20.x, got: {}",
resolution.version
);
}
#[tokio::test]
async fn test_resolve_version_env_var_takes_priority() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let _guard = vite_shared::EnvConfig::test_guard(vite_shared::EnvConfig {
node_version: Some("22.0.0".into()),
..vite_shared::EnvConfig::for_test()
});
// Create .node-version file
tokio::fs::write(temp_path.join(".node-version"), "20.18.0\n").await.unwrap();
let resolution = resolve_version(&temp_path).await.unwrap();
// VP_NODE_VERSION should take priority over .node-version
assert_eq!(resolution.version, "22.0.0");
assert_eq!(resolution.source, VERSION_ENV_VAR);
assert!(resolution.source_path.is_none());
assert!(!resolution.is_range);
}
/// Verify that the env var source is accepted by `vp env install` (no-arg) source validation.
/// This is a regression test for a bug where `vp env use 24` followed by `vp env install`
/// would fail with "No Node.js version found in current project."
#[tokio::test]
async fn test_env_var_source_accepted_by_install_validation() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let _guard = vite_shared::EnvConfig::test_guard(vite_shared::EnvConfig {
node_version: Some("22.0.0".into()),
..vite_shared::EnvConfig::for_test()
});
let resolution = resolve_version(&temp_path).await.unwrap();
// The install command uses this match to validate sources.
// VERSION_ENV_VAR must be accepted alongside project-file sources.
let accepted = matches!(
resolution.source.as_str(),
".node-version" | "engines.node" | "devEngines.runtime" | VERSION_ENV_VAR
);
assert!(
accepted,
"Install source validation should accept '{}' but it was rejected",
resolution.source
);
assert_eq!(resolution.version, "22.0.0");
}
// ── Session version file tests ──
#[tokio::test]
async fn test_write_and_read_session_version() {
let temp_dir = TempDir::new().unwrap();
let _guard = vite_shared::EnvConfig::test_guard(
vite_shared::EnvConfig::for_test_with_home(temp_dir.path()),
);
// Write a session version
write_session_version("22.0.0").await.unwrap();
// Read it back (async)
let version = read_session_version().await;
assert_eq!(version.as_deref(), Some("22.0.0"));
// Read it back (sync)
let version_sync = read_session_version_sync();
assert_eq!(version_sync.as_deref(), Some("22.0.0"));
}
#[tokio::test]
async fn test_read_session_version_returns_none_when_missing() {
let temp_dir = TempDir::new().unwrap();
let _guard = vite_shared::EnvConfig::test_guard(
vite_shared::EnvConfig::for_test_with_home(temp_dir.path()),
);
assert!(read_session_version().await.is_none());
assert!(read_session_version_sync().is_none());
}
#[tokio::test]
async fn test_read_session_version_returns_none_for_empty_file() {
let temp_dir = TempDir::new().unwrap();
let _guard = vite_shared::EnvConfig::test_guard(
vite_shared::EnvConfig::for_test_with_home(temp_dir.path()),
);
// Write empty content
let path = get_session_version_path().unwrap();
tokio::fs::create_dir_all(path.parent().unwrap()).await.unwrap();
tokio::fs::write(&path, "").await.unwrap();
assert!(read_session_version().await.is_none());
assert!(read_session_version_sync().is_none());
// Also test whitespace-only content
tokio::fs::write(&path, " \n ").await.unwrap();
assert!(read_session_version().await.is_none());
assert!(read_session_version_sync().is_none());
}
#[tokio::test]
async fn test_read_session_version_trims_whitespace() {
let temp_dir = TempDir::new().unwrap();
let _guard = vite_shared::EnvConfig::test_guard(
vite_shared::EnvConfig::for_test_with_home(temp_dir.path()),
);
write_session_version("20.18.0").await.unwrap();
// Overwrite with whitespace-padded content
let path = get_session_version_path().unwrap();
tokio::fs::write(&path, " 20.18.0 \n").await.unwrap();
assert_eq!(read_session_version().await.as_deref(), Some("20.18.0"));
assert_eq!(read_session_version_sync().as_deref(), Some("20.18.0"));
}
#[tokio::test]
async fn test_delete_session_version() {
let temp_dir = TempDir::new().unwrap();
let _guard = vite_shared::EnvConfig::test_guard(
vite_shared::EnvConfig::for_test_with_home(temp_dir.path()),
);
// Write then delete
write_session_version("22.0.0").await.unwrap();
assert!(read_session_version().await.is_some());
delete_session_version().await.unwrap();
assert!(read_session_version().await.is_none());
}
#[tokio::test]
async fn test_delete_session_version_ignores_missing_file() {
let temp_dir = TempDir::new().unwrap();
let _guard = vite_shared::EnvConfig::test_guard(
vite_shared::EnvConfig::for_test_with_home(temp_dir.path()),
);
// Deleting a non-existent file should succeed
let result = delete_session_version().await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_resolve_version_session_file_takes_priority_over_node_version() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let _guard = vite_shared::EnvConfig::test_guard(vite_shared::EnvConfig {
is_ci: cfg!(windows),
..vite_shared::EnvConfig::for_test_with_home(temp_dir.path())
});
// Create .node-version file
tokio::fs::write(temp_path.join(".node-version"), "20.18.0\n").await.unwrap();
// Write session version file
write_session_version("22.0.0").await.unwrap();
let resolution = resolve_version(&temp_path).await.unwrap();
// Session file should take priority over .node-version
assert_eq!(resolution.version, "22.0.0");
assert_eq!(resolution.source, SESSION_VERSION_FILE);
assert!(resolution.source_path.is_some());
assert!(!resolution.is_range);
// Clean up
delete_session_version().await.unwrap();
}
#[tokio::test]
async fn test_resolve_version_env_var_takes_priority_over_session_file() {
let temp_dir = TempDir::new().unwrap();
let temp_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap();
let _guard = vite_shared::EnvConfig::test_guard(vite_shared::EnvConfig {
node_version: Some("24.0.0".into()),
vite_plus_home: Some(temp_dir.path().into()),
..vite_shared::EnvConfig::for_test()
});
// Write session version file with different version
write_session_version("22.0.0").await.unwrap();
let resolution = resolve_version(&temp_path).await.unwrap();
// Env var should take priority over session file
assert_eq!(resolution.version, "24.0.0");
assert_eq!(resolution.source, VERSION_ENV_VAR);