From 924d9f97ce849b8f431de0d6c1aa083288cd2e9e Mon Sep 17 00:00:00 2001 From: Aaron Date: Sat, 29 Aug 2026 21:35:17 +0300 Subject: [PATCH 1/2] fix: include mismatched PHP namespaces in package error Surface the package name, distinct namespaces, and per-file namespace mapping so casing typos are easier to locate in large proto packages. Signed-off-by: amarkdotdev Signed-off-by: Aaron --- src/CodeGenerator.php | 12 +++- tests/Unit/CodeGeneratorNamespaceTest.php | 73 +++++++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/CodeGeneratorNamespaceTest.php diff --git a/src/CodeGenerator.php b/src/CodeGenerator.php index 2fca0cf1b..87a87cb7e 100644 --- a/src/CodeGenerator.php +++ b/src/CodeGenerator.php @@ -152,12 +152,20 @@ public static function generate( $mixinRpcNames = []; $serviceYamlConfig = new ServiceYamlConfig($serviceYaml); $transportType = Transport::parseTransport($transport); - foreach ($byPackage as [$_, $singlePackageFileDescs]) { + foreach ($byPackage as [$package, $singlePackageFileDescs]) { + $fileNamespaces = $singlePackageFileDescs + ->map(fn ($x) => sprintf('%s (%s)', $x->getName(), ProtoHelpers::getNamespace($x))); $namespaces = $singlePackageFileDescs ->map(fn ($x) => ProtoHelpers::getNamespace($x)) ->distinct(); if (count($namespaces) > 1) { - throw new Exception('All files in the same package must have the same PHP namespace'); + throw new Exception(sprintf( + 'All files in the same package must have the same PHP namespace; ' . + 'package "%s" has mismatched namespaces [%s] from files: %s', + $package, + $namespaces->join(', '), + $fileNamespaces->join(', ') + )); } // Full protobuf names, e.g. google.cloud.foo.Bar. diff --git a/tests/Unit/CodeGeneratorNamespaceTest.php b/tests/Unit/CodeGeneratorNamespaceTest.php new file mode 100644 index 000000000..3c74a7293 --- /dev/null +++ b/tests/Unit/CodeGeneratorNamespaceTest.php @@ -0,0 +1,73 @@ +fileDesc('tests/Unit/a.proto', 'testing.ns_mismatch', 'Testing\\NsMismatchA'); + $fileB = $this->fileDesc('tests/Unit/b.proto', 'testing.ns_mismatch', 'Testing\\NsMismatchB'); + + try { + iterator_to_array(CodeGenerator::generate( + Vector::new([$fileA, $fileB]), + Vector::new([$fileA->getName(), $fileB->getName()]), + null, + false, + null, + null, + null, + )); + $this->fail('Expected Exception was not thrown'); + } catch (Exception $e) { + $this->assertStringContainsString( + 'All files in the same package must have the same PHP namespace', + $e->getMessage() + ); + $this->assertStringContainsString('package "testing.ns_mismatch"', $e->getMessage()); + $this->assertStringContainsString('Testing\\NsMismatchA', $e->getMessage()); + $this->assertStringContainsString('Testing\\NsMismatchB', $e->getMessage()); + $this->assertStringContainsString('tests/Unit/a.proto', $e->getMessage()); + $this->assertStringContainsString('tests/Unit/b.proto', $e->getMessage()); + } + } + + private function fileDesc(string $name, string $package, string $phpNamespace): FileDescriptorProto + { + $opts = new FileOptions(); + $opts->setPhpNamespace($phpNamespace); + + $file = new FileDescriptorProto(); + $file->setName($name); + $file->setPackage($package); + $file->setSyntax('proto3'); + $file->setOptions($opts); + $file->setSourceCodeInfo(new SourceCodeInfo()); + return $file; + } +} From c926f79eaa0bd529fd05c3170df7db68a04a0f8c Mon Sep 17 00:00:00 2001 From: Aaron Date: Sat, 19 Sep 2026 21:52:46 +0300 Subject: [PATCH 2/2] test: update FakeMessage mergeFromString for protobuf 5.x Signed-off-by: Aaron --- tests/Unit/autoload.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/Unit/autoload.php b/tests/Unit/autoload.php index 5252100a5..b4cf3e95f 100644 --- a/tests/Unit/autoload.php +++ b/tests/Unit/autoload.php @@ -17,6 +17,7 @@ declare(strict_types=1); use function Opis\Closure\{serialize, unserialize}; +use Google\Protobuf\Internal\CodedInputStream; if (!isset($GLOBALS['gapic_php_test_temp_files'])) { $GLOBALS['gapic_php_test_temp_files'] = []; @@ -75,15 +76,15 @@ public function serializeToString() return $result; } - public function mergeFromString($s) + public function mergeFromString($data, $recursion_limit = CodedInputStream::DEFAULT_RECURSION_LIMIT) { $pos = 0; - while ($pos < strlen($s)) { - $colon1 = strpos($s, ':', $pos); - $colon2 = strpos($s, ':', $colon1 + 1); - $name = substr($s, $pos, $colon1 - $pos); - $valueLen = (int)substr($s, $colon1 + 1, $colon2 - $colon1 - 1); - $value = unserialize(substr($s, $colon2 + 1, $valueLen)); + while ($pos < strlen($data)) { + $colon1 = strpos($data, ':', $pos); + $colon2 = strpos($data, ':', $colon1 + 1); + $name = substr($data, $pos, $colon1 - $pos); + $valueLen = (int)substr($data, $colon1 + 1, $colon2 - $colon1 - 1); + $value = unserialize(substr($data, $colon2 + 1, $valueLen)); $this->$name = $value; $pos = $colon2 + 1 + $valueLen; }