diff --git a/src/cmd/main.go b/src/cmd/main.go index 11f5475..0c8c494 100644 --- a/src/cmd/main.go +++ b/src/cmd/main.go @@ -28,6 +28,18 @@ import ( func init() { verifycache.SetCertifiedVerifyKeyLogging(!license.IsCommunityBuild()) + resolver.OnLegacyLatestResolved = func(resolved string) { + fmt.Fprintf(os.Stderr, "Warning: latest resolved to v%s under LatestIncludesPrerelease policy (legacy tip-of-index).\n", resolved) + log.WarnStructured("nvm.resolution.legacy_latest", log.StructuredPayload{ + "ResolvedVersion": resolved, + "LatestMode": "legacy", + "User": log.Actor(), + "sid": log.ActorSid(), + "hostname": log.Hostname(), + "correlation_id": log.NewCorrelationID(), + "Detail": "LatestIncludesPrerelease policy is active; prefer nvm install alpha or a pinned version", + }) + } } var ( diff --git a/src/commands/alias/add.go b/src/commands/alias/add.go index 718a7a8..c40a40a 100644 --- a/src/commands/alias/add.go +++ b/src/commands/alias/add.go @@ -17,7 +17,7 @@ var ( "lts": true, "last": true, "daily": true, // Reserved for future - "alpha": true, // Reserved for future + "alpha": true, // Built-in: newest -alpha* catalog release "beta": true, // Reserved for future "prerelease": true, // Reserved for future "link": true, diff --git a/src/commands/install/install.go b/src/commands/install/install.go index 8f7b695..94102e1 100644 --- a/src/commands/install/install.go +++ b/src/commands/install/install.go @@ -20,7 +20,7 @@ type Install struct { Insecure bool `flag:"insecure" help:"Accept invalid TLS/SSL certs from download sources."` RelaxDeadlines constant.RelaxDeadlines `optional:"" placeholder:"MS" help:"Relax network deadlines for this command. Omit a value to triple the configured budgets, or pass milliseconds."` Deadlines constant.DeadlineFlags `embed:""` - constant.ArgVersion + constant.ArgVersionOptional } func (s *Install) Run(ctx *kong.Context) error { @@ -42,7 +42,7 @@ func (s *Install) Run(ctx *kong.Context) error { NoCache: s.NoCache, Force: s.Force, CacheDir: cacheRoot, - Versions: s.Version, + Versions: defaultInstallVersions(s.Version), AllowInsecure: s.Insecure, CopyModulesFrom: s.CopyFrom, ModulesFrom: s.From, @@ -50,3 +50,11 @@ func (s *Install) Run(ctx *kong.Context) error { LocalOnly: cfg.LocalInstallOnly, }) } + +// defaultInstallVersions makes bare `nvm install` equivalent to `nvm install latest`. +func defaultInstallVersions(versions []string) []string { + if len(versions) == 0 { + return []string{"latest"} + } + return versions +} diff --git a/src/commands/install/install_test.go b/src/commands/install/install_test.go new file mode 100644 index 0000000..62d85f1 --- /dev/null +++ b/src/commands/install/install_test.go @@ -0,0 +1,27 @@ +package install + +import ( + "reflect" + "testing" +) + +func TestDefaultInstallVersionsEmptyIsLatest(t *testing.T) { + got := defaultInstallVersions(nil) + want := []string{"latest"} + if !reflect.DeepEqual(got, want) { + t.Fatalf("defaultInstallVersions(nil) = %#v, want %#v", got, want) + } + + got = defaultInstallVersions([]string{}) + if !reflect.DeepEqual(got, want) { + t.Fatalf("defaultInstallVersions([]) = %#v, want %#v", got, want) + } +} + +func TestDefaultInstallVersionsPreservesExplicit(t *testing.T) { + in := []string{"24", "lts", "alpha"} + got := defaultInstallVersions(in) + if !reflect.DeepEqual(got, in) { + t.Fatalf("defaultInstallVersions(%#v) = %#v, want same slice", in, got) + } +} diff --git a/src/constant/flag_version.go b/src/constant/flag_version.go index d521f5c..82af0fc 100644 --- a/src/constant/flag_version.go +++ b/src/constant/flag_version.go @@ -1,9 +1,9 @@ package constant type ArgVersion struct { - Version []string `arg:"version" required:"" help:"The version of Node.js to install (e.g. latest, lts, lts/iron, x.x.x, x.x, x)."` + Version []string `arg:"version" required:"" help:"The version of Node.js to install (e.g. latest, alpha, lts, lts/iron, x.x.x, x.x, x). Omitting the version on nvm install installs latest (stable)."` } type ArgVersionOptional struct { - Version []string `arg:"version" optional:"" help:"The version of Node.js to install (e.g. latest, lts, lts/iron, x.x.x, x.x, x)."` + Version []string `arg:"version" optional:"" help:"The version of Node.js to install (e.g. latest, alpha, lts, lts/iron, x.x.x, x.x, x). Omit to install latest (stable)."` } diff --git a/src/installer/installer.go b/src/installer/installer.go index 353854a..2833b18 100644 --- a/src/installer/installer.go +++ b/src/installer/installer.go @@ -152,6 +152,16 @@ func Install(cfg InstallConfig) error { } status.Done() + + if failures.Load() == 0 && status.TotalInstalled > 0 { + for version := range dedupe { + if strings.EqualFold(strings.TrimSpace(version), "alpha") { + fmt.Println("Note: alpha releases are unstable and may change without notice.") + break + } + } + } + if failures.Load() > 0 { return fmt.Errorf("%d version(s) failed to install", failures.Load()) } @@ -345,6 +355,11 @@ func install( if npmVersion, ok := installedNpmVersion(txn.installDir); ok { extras["NpmVersion"] = npmVersion } + if strings.EqualFold(strings.TrimSpace(version), "latest") && + resolver.LatestIncludesPrerelease != nil && + resolver.LatestIncludesPrerelease() { + extras["LatestMode"] = "legacy" + } log.LogSystemChanged("install", nodeVersion, txn.installDir, log.OutcomeSucceeded, "", extras) } diff --git a/src/log/audit.go b/src/log/audit.go index c8b83c8..ddf4d84 100644 --- a/src/log/audit.go +++ b/src/log/audit.go @@ -1,6 +1,9 @@ package log -import "strings" +import ( + "common/resolver" + "strings" +) const ( OutcomeSucceeded = "Succeeded" @@ -17,9 +20,15 @@ func LogSystemChanged(action, nodeVersion, resolvedPath, outcome, detail string, return } + releaseType := "stable" + if resolver.IsUnstableVersion(nodeVersion) { + releaseType = "unstable" + } + payload := StructuredPayload{ "Action": action, "NodeVersion": nodeVersion, + "ReleaseType": releaseType, "Outcome": outcome, "User": Actor(), "sid": ActorSid(), diff --git a/src/test/commands/cli_integration_test.go b/src/test/commands/cli_integration_test.go index d6f32ef..4c64ab3 100644 --- a/src/test/commands/cli_integration_test.go +++ b/src/test/commands/cli_integration_test.go @@ -250,11 +250,10 @@ func TestCLIUnknownCommand(t *testing.T) { func TestCLIInstallTypoAliasParses(t *testing.T) { sb := clitest.NewSandbox(t) + // `instal` is an install typo alias. Bare install defaults to latest, so a + // missing version is no longer a parse/run error by itself. _, stderr, err := sb.Execute("instal") - if err == nil { - t.Fatal("Execute(instal) expected run error without version, got nil") - } - if strings.Contains(strings.ToLower(err.Error()), "unknown command") { + if err != nil && strings.Contains(strings.ToLower(err.Error()), "unknown command") { t.Fatalf("Execute(instal) error = %v, typo alias should resolve to install", err) } _ = stderr