Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ steps = [
["vpt", "stat-file", ".nvmrc", "--assert", "file"],
]

[[case]]
name = "node_version_pin_reports_created_then_updated"
vp = "global"
seed-runtime = false
steps = [
{ argv = ["vp", "env", "pin", "22.13.0", "--target", "node-version", "--no-install"], comment = "a new .node-version is reported as created" },
{ argv = ["vp", "env", "pin", "24.11.0", "--no-install", "--force"], comment = "overwriting the existing .node-version is reported as updated" },
["vpt", "print-file", ".node-version"],
]

[[case]]
name = "dev_engines_keeps_priority_over_nvmrc"
vp = "global"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
VITE+ - The Unified Toolchain for the Web

✓ Pinned Node.js version to 22.13.0
Updated .nvmrc in <workspace>
Created .nvmrc in <workspace>
note: Version will be downloaded on first use.
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
VITE+ - The Unified Toolchain for the Web

✓ Pinned Node.js version to 22.13.0
Created .node-version in <workspace>
Updated .node-version in <workspace>
note: Version will be downloaded on first use.
```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# node_version_pin_reports_created_then_updated

## `vp env pin 22.13.0 --target node-version --no-install`

a new .node-version is reported as created

```
VITE+ - The Unified Toolchain for the Web

✓ Pinned Node.js version to 22.13.0
Created .node-version in <workspace>
note: Version will be downloaded on first use.
```

## `vp env pin 24.11.0 --no-install --force`

overwriting the existing .node-version is reported as updated

```
VITE+ - The Unified Toolchain for the Web

✓ Pinned Node.js version to 24.11.0
Updated .node-version in <workspace>
note: Version will be downloaded on first use.
```

## `vpt print-file .node-version`

```
24.11.0
```
29 changes: 16 additions & 13 deletions crates/vp_global_cli/src/commands/env/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,15 @@ fn print_pin_success(input_version: &str, resolved_version: &str, was_alias: boo
}
}

/// Print which pin file was written, distinguishing a new file from an overwrite.
fn print_pin_file_written(file_name: &str, existed: bool, cwd: &AbsolutePathBuf) {
let action = if existed { "Updated" } else { "Created" };
vp_shared::output::print_stdout_line(format_args!(
" {action} {file_name} in {}",
cwd.as_path().display()
));
}

/// Pin by writing the `.node-version` file.
///
/// Returns `true` when the pin was written, `false` when nothing changed
Expand All @@ -393,7 +402,8 @@ async fn pin_node_version_file(
let node_version_path = cwd.join(NODE_VERSION_FILE);

// Check if .node-version already exists
if tokio::fs::try_exists(&node_version_path).await.unwrap_or(false) {
let exists = tokio::fs::try_exists(&node_version_path).await.unwrap_or(false);
if exists {
let existing_content = tokio::fs::read_to_string(&node_version_path).await?;
let existing_version = existing_content.trim();
if !confirm_overwrite_pin(
Expand All @@ -410,11 +420,7 @@ async fn pin_node_version_file(
tokio::fs::write(&node_version_path, format!("{resolved_version}\n")).await?;

print_pin_success(input_version, resolved_version, was_alias);
vp_shared::output::print_stdout_line(format_args!(
" Created {} in {}",
NODE_VERSION_FILE,
cwd.as_path().display()
));
print_pin_file_written(NODE_VERSION_FILE, exists, cwd);

// If a devEngines.runtime range is declared and no longer satisfied, offer to
// sync it in interactive terminals and warn otherwise (rfcs/dev-engines.md)
Expand Down Expand Up @@ -442,9 +448,9 @@ async fn pin_nvmrc_file(
force: bool,
) -> Result<bool, Error> {
let path = cwd.join(NVMRC_FILE);
let mut content = match tokio::fs::read_to_string(&path).await {
Ok(content) => content,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => String::new(),
let (mut content, exists) = match tokio::fs::read_to_string(&path).await {
Ok(content) => (content, true),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => (String::new(), false),
Err(error) => return Err(error.into()),
};
if let Some(span) = nvmrc_version_span(&content)? {
Expand All @@ -468,10 +474,7 @@ async fn pin_nvmrc_file(
}
tokio::fs::write(&path, content).await?;
print_pin_success(input_version, resolved_version, was_alias);
vp_shared::output::print_stdout_line(format_args!(
" Updated {NVMRC_FILE} in {}",
cwd.as_path().display()
));
print_pin_file_written(NVMRC_FILE, exists, cwd);
Ok(true)
}

Expand Down
2 changes: 2 additions & 0 deletions rfcs/env-command.md
Original file line number Diff line number Diff line change
Expand Up @@ -1481,13 +1481,15 @@ $ vp env pin 22.13.0
.node-version already exists with version 20.18.0
Overwrite with 22.13.0? (y/n): y
✓ Pinned Node.js version to 22.13.0
Updated .node-version in /Users/user/projects/my-app
```

Use `--force` to skip confirmation:

```bash
$ vp env pin 22.13.0 --force
✓ Pinned Node.js version to 22.13.0
Updated .node-version in /Users/user/projects/my-app
```

When the target is already pinned to the same version, the command no-ops (with or without `--force`):
Expand Down