diff --git a/sdk/go/common/workspace/loaders.go b/sdk/go/common/workspace/loaders.go index c1643e48f709..7bc50f27db58 100644 --- a/sdk/go/common/workspace/loaders.go +++ b/sdk/go/common/workspace/loaders.go @@ -15,8 +15,6 @@ package workspace import ( - "fmt" - "io/ioutil" "os" "sync" @@ -49,6 +47,27 @@ var policyPackProjectSingleton *policyPackProjectLoader = &policyPackProjectLoad internal: map[string]*PolicyPackProject{}, } +// readFile wraps os.ReadFile and also strips the Byte-order Mark (BOM) if present. +func readFile(path string) ([]byte, error) { + b, err := os.ReadFile(path) + if err != nil { + return nil, err + } + + // Strip BOM bytes if present to avoid problems with downstream parsing. + // References: + // https://github.com/spkg/bom + // https://en.wikipedia.org/wiki/Byte_order_mark + if len(b) >= 3 && + b[0] == 0xef && + b[1] == 0xbb && + b[2] == 0xbf { + b = b[3:] + } + + return b, nil +} + // projectLoader is used to load a single global instance of a Project config. type projectLoader struct { sync.RWMutex @@ -69,12 +88,10 @@ func (singleton *projectLoader) load(path string) (*Project, error) { return nil, err } - b, err := ioutil.ReadFile(path) + b, err := readFile(path) if err != nil { return nil, err } - fmt.Printf("***Project string(bytes):\n%s\n***End Project string(bytes)\n", string(b)) - fmt.Printf("***Project bytes:\n%v\n***End Project bytes\n", b) var project Project err = marshaller.Unmarshal(b, &project) @@ -87,7 +104,6 @@ func (singleton *projectLoader) load(path string) (*Project, error) { return nil, err } - fmt.Printf("***Project struct:\n%+v\n", project) err = project.Validate() if err != nil { return nil, err @@ -118,7 +134,7 @@ func (singleton *projectStackLoader) load(path string) (*ProjectStack, error) { } var projectStack ProjectStack - b, err := ioutil.ReadFile(path) + b, err := readFile(path) if os.IsNotExist(err) { projectStack = ProjectStack{ Config: make(config.Map), @@ -169,7 +185,7 @@ func (singleton *pluginProjectLoader) load(path string) (*PluginProject, error) return nil, err } - b, err := ioutil.ReadFile(path) + b, err := readFile(path) if err != nil { return nil, err } @@ -209,7 +225,7 @@ func (singleton *policyPackProjectLoader) load(path string) (*PolicyPackProject, return nil, err } - b, err := ioutil.ReadFile(path) + b, err := readFile(path) if err != nil { return nil, err }