Skip to content

feat(validate): keep the file of each resource and show it in the results - #661

Merged
tampakrap merged 3 commits into
crossplane:mainfrom
tampakrap:theo/feat_validate_sourced
Dec 4, 2026
Merged

tampakrap merged 3 commits into
crossplane:mainfrom
tampakrap:theo/feat_validate_sourced

Conversation

@tampakrap

Copy link
Copy Markdown
Collaborator

Description of your changes

A loaded manifest doesn't say which file it came from, and a validation result names a resource only by its kind and metadata.name. In a directory of many files, that doesn't tell the user which file to fix, and two resources with the same name in different files look the same.

Let the loaders and the validator carry the file that each resource came from, and let the text output show it. This is groundwork for crossplane test lint, which reports the file of each test suite it checks. Nothing else calls this yet, so no output changes: source is left out of the JSON and YAML output, and the text lines have no prefix, when the file isn't known. crossplane validate never sets it.

Commits:

  1. feat(load): add DecodeDocuments to keep the file of each manifest. DecodeDocuments turns the documents of a document loader (NewDocumentLoader, NewExactDocumentLoader) into manifests the same way the other loaders do, and keeps the file of each one. A manifest and its file are returned together as a sourced.Resource. That type is in a package of its own (pkg/load/sourced) because pkg/load and pkg/validate both use it, and neither should import the other. The existing loaders are unchanged.
  2. feat(validate): record the file of each resource in its result. SchemaValidateSourced is SchemaValidate for sourced.Resources, and puts the file in a new source field of the result, shown in JSON and YAML output. SchemaValidate is unchanged.
  3. feat(validate): show the file of a resource in the text output. When a result has a file, its lines start with it after the marker, for example [x] dir/a.yaml: schema validation error .... Results without a file are printed as before.

Related:

I have:

  • Read and followed Crossplane's contribution process.
  • Read and followed Crossplane's AI policy and confirmed a human stands behind this PR.
  • Run ./nix.sh flake check to ensure this PR is ready for review.
  • Added or updated unit tests.
  • Linked a PR or a docs tracking issue to document this change.
  • Added backport release-x.y labels to auto-backport this PR.

The loaders return manifests, and a manifest does not say which file it
came from. So a command that loads the files of a directory cannot tell
the user which file a problem is in, and two resources with the same
name in different files cannot be told apart.

Add DecodeDocuments, which turns the documents that a document loader
returns (NewDocumentLoader, NewExactDocumentLoader) into manifests the
same way the other loaders do, and keeps the file of each one. A
manifest and its file are returned together as a sourced.Resource, a
type in a package of its own because pkg/load and pkg/validate both use
it.

Nothing uses it yet, and the existing loaders are unchanged.

Signed-off-by: Theo Chatzimichos <tampakrap@gmail.com>
A validation result names a resource by its kind and its metadata.name.
In a directory of many files that does not say which file to fix, and
two resources with the same name look the same.

Add SchemaValidateSourced, which is SchemaValidate for
sourced.Resources, and put the file of each resource in its result, as a
new source field (shown in the JSON and YAML output). SchemaValidate does
not change, and neither does the output of anything that calls it: the
field is left out when the file is not known.

Signed-off-by: Theo Chatzimichos <tampakrap@gmail.com>
The text output names a resource by its kind and its metadata.name only.
When the file of a resource is known (see SchemaValidateSourced), start
its lines with it, after the marker:

  [x] dir/a.yaml: schema validation error ...

Results without a file are printed as before, so the output of
crossplane validate does not change.

Signed-off-by: Theo Chatzimichos <tampakrap@gmail.com>
@tampakrap
tampakrap requested review from a team and jcogilvie as code owners December 4, 2026 13:37
@tampakrap
tampakrap requested review from a team, adamwg and phisco and a balanced review from Copilot and removed request for a team December 4, 2026 13:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the source-file tracking changes.

No open issues 🎉

What changed in this PR

Adds source-file tracking from manifest loading through validation output, laying groundwork for crossplane test lint without changing existing crossplane validate output.

Changes:

  • Introduces sourced resources and document decoding that preserves file paths.
  • Adds source-aware validation and optional source fields in structured output.
  • Prefixes text results with known source paths and adds focused tests.
File Description
pkg/​validate/​validate.go Adds source-aware validation entry point.
pkg/​validate/​validate_test.go Tests source mapping and input order.
pkg/​validate/​types.go Adds optional result source field.
pkg/​validate/​output/​render.go Prefixes text results with source paths.
pkg/​validate/​output/​render_test.go Tests output with and without sources.
pkg/​load/​sourced/​sourced.go Defines a resource paired with its source.
pkg/​load/​loader.go Decodes documents while preserving sources.
pkg/​load/​loader_test.go Tests decoding, pipeline inputs, and errors.

Review effort: Balanced

@tampakrap tampakrap changed the title theo/feat validate sourced feat(validate): keep the file of each resource and show it in the results Dec 4, 2026
@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.54839% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.65%. Comparing base (c53eb9c) to head (e7eb45f).
⚠️ Report is 14 commits behind head on main.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread pkg/load/loader_test.go Outdated

func TestDecodeDocumentsError(t *testing.T) {
got, err := DecodeDocuments([]Document{{Source: "bad.yaml", Raw: []byte("name: ci\njobs: {}\n")}}, false)
if err == nil || !strings.Contains(err.Error(), "cannot parse YAML manifest") {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we try to avoid string checks on errors in favor of sentinel errors; is that doable here?

that should also make this case slide into the table-driven test as a row vs. a separate test

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed an alternative, still not a sentinel because the error comes from Unmarshal, but it is closer to what you propose so that we don't have a string check

@tampakrap
tampakrap force-pushed the theo/feat_validate_sourced branch from bcd2155 to e7eb45f Compare December 4, 2026 20:54
Copilot AI balanced review requested due to automatic review settings December 4, 2026 20:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the source-file tracking changes.

No open issues 🎉

Review effort: Balanced

@tampakrap
tampakrap merged commit e4ce7c1 into crossplane:main Dec 4, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants