go-embed-python icon indicating copy to clipboard operation
go-embed-python copied to clipboard

Fix for Windows codegen

Open floriaanpost opened this issue 10 months ago • 1 comments

On windows, when pip installing dependencies, the paths in the files.json file are stored with backslashes, for example: "_distutils_hack\\__init__.py". This causes issues when copying the embedded filesystem to the temp directory. It is not allowed to open an embedded file using a "Windows path", this for example: fs.ReadFile(embedFs, "_distutils_hack\\__init__.py").

A minimal example to reproduce the issue:

generate/main.go

package main

import (
	"log"
	"os"
	"strings"

	"github.com/kluctl/go-embed-python/pip"
)

const deps = `
setuptools==75.8.0
`

func main() {
	platforms := map[string][]string{
		"darwin-arm64":  {"macosx_11_0_arm64", "macosx_12_0_arm64"}, // No problems here
		"windows-amd64": {"win_amd64"},                              // Here the backslashes appear in files.json                                               
	}
	reqs := tempReqs(deps)

	for goPlatform, pipPlatforms := range platforms {
		s := strings.Split(goPlatform, "-")
		goOs, goArch := s[0], s[1]
		err := pip.CreateEmbeddedPipPackages(reqs, goOs, goArch, pipPlatforms, "data")
		if err != nil {
			log.Fatal(err)
		}
	}
}

func tempReqs(data string) string {
	f, err := os.CreateTemp(os.TempDir(), "*-requirements.txt")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	if _, err := f.Write([]byte(data)); err != nil {
		log.Fatal(err)
	}

	return f.Name()
}

main.go

package main

import (
	"log"

	"github.com/kluctl/go-embed-python/embed_util"
	"gitlab.com/floriaanpost/go-embed-bug-reproduce/data"
)

//go:generate go run generate/main.go

func main() {
	_, err := embed_util.NewEmbeddedFiles(data.Data, "testdeps")
	if err != nil {
		log.Fatal(err)
	}
}

Then init, generate and run:

go mod init github.com/floriaanpost/go-embed-bug-reproduce
go mod tidy
go generate
go run main.go

floriaanpost avatar Mar 27 '25 06:03 floriaanpost