diff --git a/bypy/devenv.go b/bypy/devenv.go index bee71dd37..0b17d9bd0 100644 --- a/bypy/devenv.go +++ b/bypy/devenv.go @@ -204,7 +204,16 @@ func relocate_pkgconfig(path, old_prefix, new_prefix string) error { if err != nil { return err } - nraw := bytes.ReplaceAll(raw, []byte(old_prefix), []byte(new_prefix)) + replacement := new_prefix + if filepath.Ext(path) == ".pc" { + // pkg-config tokenizes Cflags/Libs on unescaped whitespace, so a + // prefix containing a space must have that space backslash-escaped, + // otherwise `pkg-config --cflags`/`--libs` silently truncates the + // path at the first space, breaking the build whenever the checkout + // lives under a directory with a space in its name. + replacement = strings.NewReplacer(`\`, `\\`, ` `, `\ `).Replace(new_prefix) + } + nraw := bytes.ReplaceAll(raw, []byte(old_prefix), []byte(replacement)) return os.WriteFile(path, nraw, 0o644) } diff --git a/bypy/devenv_test.go b/bypy/devenv_test.go new file mode 100644 index 000000000..99d0cd398 --- /dev/null +++ b/bypy/devenv_test.go @@ -0,0 +1,57 @@ +// License: GPLv3 Copyright: 2023, Kovid Goyal, + +package main + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +// When the kitty checkout lives under a directory whose name contains a +// space (a common occurrence on macOS, e.g. "Desktop/My Projects/kitty"), +// pkg-config tokenizes the Cflags/Libs lines it emits on unescaped +// whitespace. If relocate_pkgconfig writes the new, real prefix into a .pc +// file without escaping spaces, pkg-config truncates the path at the first +// space and the build fails with errors such as "hb.h file not found". +func TestRelocatePkgconfigEscapesSpacesInPcFiles(t *testing.T) { + dir := t.TempDir() + pc_path := filepath.Join(dir, "harfbuzz.pc") + old_prefix := "/sw/sw" + new_prefix := "/Users/someone/has space/kitty/dependencies/darwin-amd64" + contents := "prefix=" + old_prefix + "\nincludedir=${prefix}/include\n\nName: harfbuzz\nCflags: -I${includedir}/harfbuzz\n" + if err := os.WriteFile(pc_path, []byte(contents), 0o644); err != nil { + t.Fatal(err) + } + if err := relocate_pkgconfig(pc_path, old_prefix, new_prefix); err != nil { + t.Fatal(err) + } + raw, err := os.ReadFile(pc_path) + if err != nil { + t.Fatal(err) + } + expected_prefix_line := `prefix=/Users/someone/has\ space/kitty/dependencies/darwin-amd64` + if !strings.Contains(string(raw), expected_prefix_line) { + t.Fatalf("space in new prefix was not escaped in .pc file, got:\n%s", raw) + } + + // The same function is also used to relocate paths embedded in + // Python's _sysconfigdata_*.py, which is a plain Python string literal, + // not something pkg-config tokenizes. Escaping the space there would + // corrupt the path, so it must be left untouched. + py_path := filepath.Join(dir, "_sysconfigdata_darwin.py") + if err := os.WriteFile(py_path, []byte("prefix = '"+old_prefix+"'\n"), 0o644); err != nil { + t.Fatal(err) + } + if err := relocate_pkgconfig(py_path, old_prefix, new_prefix); err != nil { + t.Fatal(err) + } + raw, err = os.ReadFile(py_path) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(raw), "prefix = '"+new_prefix+"'") { + t.Fatalf("plain (non pkg-config) relocation should not escape spaces, got:\n%s", raw) + } +}