Skip to content

Fix schema generator path resolution to be independent of working directory #163

@aliok

Description

@aliok

Introduced here: #155 (comment)

Problem

The schema generator in hack/jsonschemagen/main.go uses filepath.Join("..", "..", "specs") to build the output path for schema files. This approach only works when the binary is executed from the hack/jsonschemagen directory. When run from the repository root (as done by make update-codegen and CI workflows with go run ./hack/jsonschemagen), this resolves to an incorrect path (../../specs from repo root) and causes the write operation to fail.

Impact

  • Schema generation fails when invoked from the repo root
  • CI workflows and make targets cannot successfully generate schemas
  • Inconsistent behavior depending on the current working directory

Proposed Solution

Use runtime.Caller to determine the source file's location, then derive the repository root from there before appending specs. This makes the path resolution independent of the current working directory:

_, callerFile, _, ok := runtime.Caller(0)
if !ok {
    log.Fatal("failed to determine generator location")
}
repoRoot := filepath.Join(filepath.Dir(callerFile), "..", "..")
specsDir := filepath.Join(repoRoot, "specs")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions