-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cross-module support for filesystem paths
This patch updates the controller-gen package loader to support loading packages across Go module boundaries for filesystem paths. What does this actually mean? Imagine a project with the following structure: my-project/ | |-- go.mod |-- go.sum | |-- apis/ | |-- v1alpha1/ | |-- go.mod |-- go.sum Prior to this patch, the following command, executed from the root of "my-project," would *not* include the package my-project/apis/v1alpha1: controller-gen crd \ paths=./apis/... \ crd:trivialVersions=true \ output:crd:dir=./config/crd/bases The reason the above command would not parse the types from the package my-project/apis/v1alpha1 is because it is a distinct Go module from the root module, where the command was executed. In fact, while the above command would silently fail to include those types, the following command will fail with an error: controller-gen crd \ paths=./apis/v1alpha1 \ crd:trivialVersions=true \ output:crd:dir=./config/crd/bases The above command fails because the underlying logic to load packages cannot load a path from one package when executed from the working directory of another package. With this patch, it is now possible for the first command to successfully traverse Go module boundaries for roots that are file- system paths ending with "..." with the following, high-level logic: 1. If no roots are provided then load the working directory and return early. 2. Otherwise sort the provided roots into two, distinct buckets: a. package/module names b. filesystem paths A filesystem path is distinguished from a Go package/module name by the same rules as followed by the "go" command. At a high level, a root is a filesystem path IFF it meets ANY of the following criteria: * is absolute * begins with . * begins with .. For more information please refer to the output of the command "go help packages". 3. Load the package/module roots as a single call to packages.Load. If there are no filesystem path roots then return early. 4. For filesystem path roots ending with "...", check to see if its descendants include any nested, Go modules. If so, add the directory that contains the nested Go module to the filesystem path roots. 5. Load the filesystem path roots and return the load packages for the package/module roots AND the filesystem path roots.
- Loading branch information
Showing
13 changed files
with
590 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
Copyright 2022 The Kubernetes Authors. | ||
Licensed 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. | ||
*/ | ||
|
||
package loader_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestLoader(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Loader Patching Suite") | ||
} |
Oops, something went wrong.