From 7c9144350dc380cbaa307258e25b5002c3b6f9a9 Mon Sep 17 00:00:00 2001 From: Saadanjum0 <160885369+Saadanjum0@users.noreply.github.com> Date: Fri, 2 Oct 2026 17:47:34 +0500 Subject: [PATCH] devenv: Escape spaces when relocating pkg-config prefixes relocate_pkgconfig() substitutes the real, absolute dependency path into the downloaded .pc files by plain byte replacement. pkg-config tokenizes Cflags/Libs on unescaped whitespace, so when that path contains a space (for example a checkout under "Desktop/My Projects/kitty") the emitted -I/-L flags get truncated at the first space and the build fails with errors like "hb.h file not found", even though the dependency is right there on disk. Escape spaces (and literal backslashes) in the new prefix before writing it into a .pc file so pkg-config round-trips it correctly. The same function also relocates paths inside _sysconfigdata_*.py, which is a plain Python string rather than something pkg-config parses, so that case is left unescaped. Likely the cause of #8939: the reporter's checkout was under "3. Ressources" and the -I flags in that log are truncated at the space. --- bypy/devenv.go | 11 ++++++++- bypy/devenv_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 bypy/devenv_test.go 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) + } +}