From 508980603b39af59f0f9423958b0180112aa7503 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 10 Oct 2024 07:57:09 -0700 Subject: [PATCH] ipn/conffile: don't depend on hujson on iOS/Android Fixes #13772 Change-Id: I3ae03a5ee48c801f2e5ea12d1e54681df25d4604 Signed-off-by: Brad Fitzpatrick --- ipn/conffile/conffile.go | 18 ++++++++++++++++-- ipn/conffile/conffile_hujson.go | 20 ++++++++++++++++++++ tstest/iosdeps/iosdeps_test.go | 1 + 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 ipn/conffile/conffile_hujson.go diff --git a/ipn/conffile/conffile.go b/ipn/conffile/conffile.go index 0b4670c42..a2bafb8b7 100644 --- a/ipn/conffile/conffile.go +++ b/ipn/conffile/conffile.go @@ -8,10 +8,11 @@ import ( "bytes" "encoding/json" + "errors" "fmt" "os" + "runtime" - "github.com/tailscale/hujson" "tailscale.com/ipn" ) @@ -39,8 +40,21 @@ func (c *Config) WantRunning() bool { // from the VM's metadata service's user-data field. const VMUserDataPath = "vm:user-data" +// hujsonStandardize is set to hujson.Standardize by conffile_hujson.go on +// platforms that support config files. +var hujsonStandardize func([]byte) ([]byte, error) + // Load reads and parses the config file at the provided path on disk. func Load(path string) (*Config, error) { + switch runtime.GOOS { + case "ios", "android": + // compile-time for deadcode elimination + return nil, fmt.Errorf("config file loading not supported on %q", runtime.GOOS) + } + if hujsonStandardize == nil { + // Build tags are wrong in conffile_hujson.go + return nil, errors.New("[unexpected] config file loading not wired up") + } var c Config c.Path = path var err error @@ -54,7 +68,7 @@ func Load(path string) (*Config, error) { if err != nil { return nil, err } - c.Std, err = hujson.Standardize(c.Raw) + c.Std, err = hujsonStandardize(c.Raw) if err != nil { return nil, fmt.Errorf("error parsing config file %s HuJSON/JSON: %w", path, err) } diff --git a/ipn/conffile/conffile_hujson.go b/ipn/conffile/conffile_hujson.go new file mode 100644 index 000000000..6825a0638 --- /dev/null +++ b/ipn/conffile/conffile_hujson.go @@ -0,0 +1,20 @@ +// Copyright (c) Tailscale Inc & AUTHORS +// SPDX-License-Identifier: BSD-3-Clause + +//go:build !ios && !android + +package conffile + +import "github.com/tailscale/hujson" + +// Only link the hujson package on platforms that use it, to reduce binary size +// & memory a bit. +// +// (iOS and Android don't have config files) + +// While the linker's deadcode mostly handles the hujson package today, this +// keeps us honest for the future. + +func init() { + hujsonStandardize = hujson.Standardize +} diff --git a/tstest/iosdeps/iosdeps_test.go b/tstest/iosdeps/iosdeps_test.go index 6daa70c3b..ab69f1c2b 100644 --- a/tstest/iosdeps/iosdeps_test.go +++ b/tstest/iosdeps/iosdeps_test.go @@ -23,6 +23,7 @@ func TestDeps(t *testing.T) { "database/sql/driver": "iOS doesn't use an SQL database", "github.com/google/uuid": "see tailscale/tailscale#13760", "tailscale.com/clientupdate/distsign": "downloads via AppStore, not distsign", + "github.com/tailscale/hujson": "no config file support on iOS", }, }.Check(t) }