diff --git a/internal/pathmeta/vfox_toml.go b/internal/pathmeta/vfox_toml.go index 2dc60c83..6de2d6e9 100644 --- a/internal/pathmeta/vfox_toml.go +++ b/internal/pathmeta/vfox_toml.go @@ -367,23 +367,18 @@ func (t *ToolConfig) MarshalInline() string { return fmt.Sprintf("{%s}", strings.Join(parts, ", ")) } -// formatAttributeValue formats an attribute value based on its type +// formatAttributeValue formats an attribute value based on its type. +// Only booleans and canonical integers are written bare; every other value is +// quoted, because a bare "17.0", "007" or "nan" would be read back as a +// different value (or fail to parse) instead of the string it was. func formatAttributeValue(value string) string { - // Check if it's a boolean if value == "true" || value == "false" { return value } - // Check if it's an integer - if _, err := strconv.ParseInt(value, 10, 64); err == nil { + if n, err := strconv.ParseInt(value, 10, 64); err == nil && strconv.FormatInt(n, 10) == value { return value } - // Check if it's a float - if _, err := strconv.ParseFloat(value, 64); err == nil { - return value - } - - // Default: quote as string return strconv.Quote(value) } diff --git a/internal/pathmeta/vfox_toml_test.go b/internal/pathmeta/vfox_toml_test.go index 14c41829..61c90df8 100644 --- a/internal/pathmeta/vfox_toml_test.go +++ b/internal/pathmeta/vfox_toml_test.go @@ -519,3 +519,31 @@ func TestToolConfig_MarshalInline(t *testing.T) { }) } } + +// String attributes that merely look like numbers must survive a save and +// reload unchanged, and must never produce a file that cannot be parsed. +func TestVfoxToml_StringAttrRoundTrip(t *testing.T) { + values := []string{"17.0", "1.50", "007", "1e3", "1_000", "+5", "nan", "inf", "21", "true", "openjdk"} + for _, value := range values { + t.Run(value, func(t *testing.T) { + dir := t.TempDir() + config := NewVfoxToml() + config.SetToolWithAttr("java", "21", Attr{"dist": value}) + if err := config.SaveTo(dir); err != nil { + t.Fatalf("SaveTo() error: %v", err) + } + + loaded, err := LoadVfoxToml(DetermineConfigPath(dir)) + if err != nil { + t.Fatalf("LoadVfoxToml() error: %v", err) + } + tool, ok := loaded.GetToolConfig("java") + if !ok { + t.Fatal("java not found after reload") + } + if got := tool.Attr["dist"]; got != value { + t.Errorf("dist = %q after round trip, want %q", got, value) + } + }) + } +}