From 4339c643b20bf578cfde4990685b0a3f60890533 Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Sun, 27 Sep 2026 18:02:15 +0200 Subject: [PATCH] THRIFT-6391: Import packages that collide with -remote stub locals under an alias Client: go The Go -remote stub's main() declares locals such as client, cmd and host, which shadowed an imported package of the same name, so the stub did not compile. Apache Accumulo (client.thrift) and Apache Pegasus (package cmd) hit it. The generator now reserves those names before rendering imports, so such a package is imported under an alias; nothing changes when -remote stubs are skipped. Co-Authored-By: Claude Opus 5.5 --- .../cpp/src/thrift/generate/t_go_generator.cc | 13 +++++++++ lib/go/test/Makefile.am | 13 +++++++-- lib/go/test/RemoteShadowClient.thrift | 25 ++++++++++++++++ lib/go/test/RemoteShadowHost.thrift | 25 ++++++++++++++++ lib/go/test/RemoteShadowTest.thrift | 29 +++++++++++++++++++ 5 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 lib/go/test/RemoteShadowClient.thrift create mode 100644 lib/go/test/RemoteShadowHost.thrift create mode 100644 lib/go/test/RemoteShadowTest.thrift diff --git a/compiler/cpp/src/thrift/generate/t_go_generator.cc b/compiler/cpp/src/thrift/generate/t_go_generator.cc index 8bab9ae065..3923c1f769 100644 --- a/compiler/cpp/src/thrift/generate/t_go_generator.cc +++ b/compiler/cpp/src/thrift/generate/t_go_generator.cc @@ -457,6 +457,19 @@ void t_go_generator::init_generator() { package_dir_ = get_out_dir(); last_const_block_ = 0; + // The -remote stub's main() declares these local variables. An imported package with one of + // these names would be shadowed by them, so reserve them before any import is rendered; such a + // package is then imported under an alias in every file of this program. + if (!skip_remote_) { + for (const char* local : + {"cfg", "client", "cmd", "err", "framed", "headers", + "host", "httptrans", "iprot", "m", "oprot", "parsedUrl", + "parts", "port", "portStr", "protocol", "protocolFactory", "trans", + "urlString", "useHttp"}) { + package_identifiers_set_.insert(local); + } + } + // This set is taken from https://github.com/golang/lint/blob/master/lint.go#L692 commonInitialisms.insert("API"); commonInitialisms.insert("ASCII"); diff --git a/lib/go/test/Makefile.am b/lib/go/test/Makefile.am index 00d022f76c..cb93db160e 100644 --- a/lib/go/test/Makefile.am +++ b/lib/go/test/Makefile.am @@ -72,7 +72,10 @@ gopath: $(THRIFT) $(THRIFTTEST) \ ValidateTest.thrift \ ForwardType.thrift \ StringParseAllocationTest.thrift \ - DocCommentTest.thrift + DocCommentTest.thrift \ + RemoteShadowClient.thrift \ + RemoteShadowHost.thrift \ + RemoteShadowTest.thrift mkdir -p gopath/src grep -v 'set' $(THRIFTTEST) > ThriftTest.thrift $(THRIFT) $(THRIFTARGS) $(RECURSIVE) @@ -115,6 +118,7 @@ gopath: $(THRIFT) $(THRIFTTEST) \ $(THRIFT) $(THRIFTARGS) ForwardType.thrift $(THRIFT) $(THRIFTARGS) StringParseAllocationTest.thrift $(THRIFT) $(THRIFTARGS) DocCommentTest.thrift + $(THRIFT) $(THRIFTARGS) -r RemoteShadowTest.thrift ln -nfs ../../tests gopath/src/tests cp -r ./dontexportrwtest gopath/src touch gopath @@ -149,7 +153,9 @@ check: gopath ./gopath/src/processormiddlewaretest \ ./gopath/src/clientmiddlewareexceptiontest \ ./gopath/src/validatetest \ - ./gopath/src/forwardtypetest + ./gopath/src/forwardtypetest \ + ./gopath/src/remoteshadowtest \ + ./gopath/src/remoteshadowtest/shadow_service-remote $(GO) test github.com/apache/thrift/lib/go/thrift $(GO) test ./gopath/src/tests ./gopath/src/dontexportrwtest @@ -200,6 +206,9 @@ EXTRA_DIST = \ OptionalFieldsTest.thrift \ ProcessorMiddlewareTest.thrift \ RefAnnotationFieldsTest.thrift \ + RemoteShadowClient.thrift \ + RemoteShadowHost.thrift \ + RemoteShadowTest.thrift \ RequiredFieldTest.thrift \ ServicesTest.thrift \ StringParseAllocationTest.thrift \ diff --git a/lib/go/test/RemoteShadowClient.thrift b/lib/go/test/RemoteShadowClient.thrift new file mode 100644 index 0000000000..e185bb70b1 --- /dev/null +++ b/lib/go/test/RemoteShadowClient.thrift @@ -0,0 +1,25 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +// Its Go package is named like a local variable of the generated -remote stub. +namespace go client + +struct Info { + 1: i32 x +} diff --git a/lib/go/test/RemoteShadowHost.thrift b/lib/go/test/RemoteShadowHost.thrift new file mode 100644 index 0000000000..5146ef0d31 --- /dev/null +++ b/lib/go/test/RemoteShadowHost.thrift @@ -0,0 +1,25 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +// Its Go package is named like a local variable of the generated -remote stub. +namespace go host + +struct Addr { + 1: string name +} diff --git a/lib/go/test/RemoteShadowTest.thrift b/lib/go/test/RemoteShadowTest.thrift new file mode 100644 index 0000000000..50580ccf01 --- /dev/null +++ b/lib/go/test/RemoteShadowTest.thrift @@ -0,0 +1,29 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +// The -remote stub declares local variables such as client and host; packages +// with those names, used by the arguments below, must not be shadowed by them. +namespace go remoteshadowtest + +include "RemoteShadowClient.thrift" +include "RemoteShadowHost.thrift" + +service ShadowService { + void f(1: RemoteShadowClient.Info info, 2: RemoteShadowHost.Addr addr) +}