From aa5d1113427662b0ac5997ccba2c96b37dc49da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Minh=20Nh=E1=BA=ADt?= <86871862+minhnhatnoe@users.noreply.github.com> Date: Sun, 11 Jun 2023 03:28:18 +0700 Subject: [PATCH] Fix file access for embedded files on windows (#103) Windows uses '\' as path separator, while Linux uses '/'. However, Go's embed file system uses '/' on all platforms. --- db/migrations.go | 4 ++-- embed/doc.go | 5 +++++ go.mod | 2 +- scripts/windows/production_test.ps1 | 8 ++++++++ 4 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 embed/doc.go create mode 100644 scripts/windows/production_test.ps1 diff --git a/db/migrations.go b/db/migrations.go index 6156d266..9fb3b7dc 100644 --- a/db/migrations.go +++ b/db/migrations.go @@ -4,7 +4,7 @@ import ( "database/sql" "io/fs" "log" - "path/filepath" + "path" "regexp" "sort" @@ -44,7 +44,7 @@ func (db *DB) migrate() error { // Do migrations one by one for _, name := range versions { - sqlFile := filepath.Join(assetsSql, name+".sql") + sqlFile := path.Join(assetsSql, name+".sql") file, err := fs.ReadFile(embed.Content, sqlFile) if err != nil { return errors.Wrapf(err, "File %s", sqlFile) diff --git a/embed/doc.go b/embed/doc.go new file mode 100644 index 00000000..96266184 --- /dev/null +++ b/embed/doc.go @@ -0,0 +1,5 @@ +// Package embed puts all files in embed/assets and embed/templates +// into the executable. +// While the embed file system is cross-platform, it always exposes a +// '/'-delimited file system. +package embed diff --git a/go.mod b/go.mod index 5a2f8e29..ce595bdd 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/mattn/go-sqlite3 v1.14.0 github.com/pkg/errors v0.9.1 golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e - golang.org/x/net v0.7.0 + golang.org/x/net v0.7.0 // indirect golang.org/x/text v0.7.0 golang.org/x/tools v0.1.12 google.golang.org/appengine v1.6.5 // indirect diff --git a/scripts/windows/production_test.ps1 b/scripts/windows/production_test.ps1 new file mode 100644 index 00000000..8d9fafe2 --- /dev/null +++ b/scripts/windows/production_test.ps1 @@ -0,0 +1,8 @@ +$ErrorActionPreference = "Stop" +Remove-Item kjudge.db* + +& "scripts\windows\production_build.ps1" + +Invoke-Expression ".\kjudge $args" + +# pwsh -c scripts/windows/production_test.ps1 --sandbox=raw to run this test script