Skip to content
Open
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
13 changes: 5 additions & 8 deletions cmd/commands/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ func activateCmd(ctx context.Context, cmd *cli.Command) error {
// Process SDKs concurrently using errgroup
g, _ := errgroup.WithContext(ctx)

for sdkName, version := range allTools {
for sdkName := range allTools {
sdkName := sdkName // Capture loop variable
version := version // Capture loop variable

g.Go(func() error {
// Lookup SDK
Expand All @@ -101,10 +100,8 @@ func activateCmd(ctx context.Context, cmd *cli.Command) error {
return nil // Continue processing other SDKs
}

sdkVersion := sdk.Version(version)

// Get tool config with scope information (searches by priority)
toolConfig, scope, ok := chain.GetToolConfig(sdkName)
// Resolve to the highest-priority installed version across scopes
toolConfig, scope, sdkVersion, ok := resolveInstalledToolConfig(chain, sdkObj, sdkName)
if !ok {
return nil
}
Expand All @@ -121,14 +118,14 @@ func activateCmd(ctx context.Context, cmd *cli.Command) error {
// Create symlinks if needed (internal logic checks if symlink already exists)
if err := sdkObj.CreateSymlinksForScope(sdkVersion, actualScope); err != nil {
logger.Debugf("Failed to create symlinks for %s@%s (scope: %s): %v",
sdkName, version, actualScope.String(), err)
sdkName, sdkVersion, actualScope.String(), err)
return nil // Continue processing other SDKs
}

// Get environment variables pointing to symlinks
sdkEnvs, err := sdkObj.EnvKeysForScope(sdkVersion, actualScope)
if err != nil {
logger.Debugf("Failed to get env keys for %s@%s: %v", sdkName, version, err)
logger.Debugf("Failed to get env keys for %s@%s: %v", sdkName, sdkVersion, err)
return nil // Continue processing other SDKs
}

Expand Down
13 changes: 5 additions & 8 deletions cmd/commands/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,8 @@ func envFlag(cmd *cli.Command) error {
// Process SDKs concurrently using errgroup
g, _ := errgroup.WithContext(context.Background())

for sdkName, version := range allTools {
for sdkName := range allTools {
sdkName := sdkName // Capture loop variable
version := version // Capture loop variable

g.Go(func() error {
// Lookup SDK
Expand All @@ -224,10 +223,8 @@ func envFlag(cmd *cli.Command) error {
return nil // Continue processing other SDKs
}

sdkVersion := sdk.Version(version)

// Get tool config with scope information (searches by priority)
toolConfig, scope, ok := chain.GetToolConfig(sdkName)
// Resolve to the highest-priority installed version across scopes
toolConfig, scope, sdkVersion, ok := resolveInstalledToolConfig(chain, sdkObj, sdkName)
if !ok {
return nil
}
Comment thread
bytemain marked this conversation as resolved.
Expand All @@ -244,14 +241,14 @@ func envFlag(cmd *cli.Command) error {
// Create symlinks if needed (internal logic checks if symlink already exists)
if err := sdkObj.CreateSymlinksForScope(sdkVersion, actualScope); err != nil {
logger.Debugf("Failed to create symlinks for %s@%s (scope: %s): %v",
sdkName, version, actualScope.String(), err)
sdkName, sdkVersion, actualScope.String(), err)
return nil // Continue processing other SDKs
}

// Get environment variables pointing to symlinks
sdkEnvs, err := sdkObj.EnvKeysForScope(sdkVersion, actualScope)
if err != nil {
logger.Debugf("Failed to get env keys for %s@%s: %v", sdkName, version, err)
logger.Debugf("Failed to get env keys for %s@%s: %v", sdkName, sdkVersion, err)
return nil // Continue processing other SDKs
}

Expand Down
43 changes: 43 additions & 0 deletions cmd/commands/tool_resolution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2026 Han Li and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package commands

import (
"github.com/version-fox/vfox/internal/env"
"github.com/version-fox/vfox/internal/pathmeta"
"github.com/version-fox/vfox/internal/sdk"
"github.com/version-fox/vfox/internal/shared/logger"
)

func resolveInstalledToolConfig(chain env.VfoxTomlChain, sdkObj sdk.Sdk, sdkName string) (*pathmeta.ToolConfig, env.UseScope, sdk.Version, bool) {
toolConfigs := chain.GetToolConfigsByPriority(sdkName)
if len(toolConfigs) == 0 {
return nil, env.Global, "", false
}

for _, toolConfig := range toolConfigs {
version := sdk.Version(toolConfig.Config.Version)
if sdkObj.CheckRuntimeExist(version) {
return toolConfig.Config, toolConfig.Scope, version, true
}
logger.Debugf("SDK %s@%s from %s scope not installed, trying lower-priority config",
sdkName, version, toolConfig.Scope.String())
}

logger.Debugf("No installed configured version found for SDK %s", sdkName)
return nil, env.Global, "", false
}
Comment thread
bytemain marked this conversation as resolved.
26 changes: 26 additions & 0 deletions internal/env/vfox_toml_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ type chainItem struct {
scope UseScope
}

// ScopedToolConfig bundles a tool configuration with its scope.
type ScopedToolConfig struct {
Config *pathmeta.ToolConfig
Scope UseScope
}

// VfoxTomlChain is a chain of VfoxToml configs, supporting multi-config merging
type VfoxTomlChain []*chainItem

Expand Down Expand Up @@ -103,6 +109,26 @@ func (c *VfoxTomlChain) GetToolVersion(name string) (string, UseScope, bool) {
return "", Global, false
}

// GetToolConfigsByPriority returns all tool configs from high to low priority.
func (c *VfoxTomlChain) GetToolConfigsByPriority(name string) []ScopedToolConfig {
result := make([]ScopedToolConfig, 0, len(*c))
for i := len(*c) - 1; i >= 0; i-- {
item := (*c)[i]
if item == nil || item.config == nil {
continue
}
config, ok := item.config.Tools.Get(name)
if !ok {
continue
}
result = append(result, ScopedToolConfig{
Config: config,
Scope: item.scope,
})
}
return result
}

// GetByIndex returns the config at the specified index
func (c *VfoxTomlChain) GetByIndex(index int) *pathmeta.VfoxToml {
if index < 0 || index >= len(*c) {
Expand Down
34 changes: 34 additions & 0 deletions internal/env/vfox_toml_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,40 @@ func TestVfoxTomlChain(t *testing.T) {
}
})

t.Run("GetToolConfigsByPriority", func(t *testing.T) {
chain := NewVfoxTomlChain()

globalConfig := pathmeta.NewVfoxToml()
globalConfig.SetTool("golang", "1.26.0")

sessionConfig := pathmeta.NewVfoxToml()
sessionConfig.SetTool("golang", "1.25.0")

projectConfig := pathmeta.NewVfoxToml()
projectConfig.SetTool("golang", "1.24.0")

chain.Add(globalConfig, Global)
chain.Add(sessionConfig, Session)
chain.Add(projectConfig, Project)

configs := chain.GetToolConfigsByPriority("golang")
if len(configs) != 3 {
t.Fatalf("Expected 3 configs, got %d", len(configs))
}

if configs[0].Scope != Project || configs[0].Config.Version != "1.24.0" {
t.Fatalf("Expected project config first, got scope=%s version=%s", configs[0].Scope.String(), configs[0].Config.Version)
}

if configs[1].Scope != Session || configs[1].Config.Version != "1.25.0" {
t.Fatalf("Expected session config second, got scope=%s version=%s", configs[1].Scope.String(), configs[1].Config.Version)
}

if configs[2].Scope != Global || configs[2].Config.Version != "1.26.0" {
t.Fatalf("Expected global config third, got scope=%s version=%s", configs[2].Scope.String(), configs[2].Config.Version)
}
})

t.Run("GetTool not found", func(t *testing.T) {
chain := NewVfoxTomlChain()

Expand Down
Loading