Skip to content

Fix #2538: Preserve original filename when using --front-matter#2613

Open
cobyfrombrooklyn-bot wants to merge 1 commit intomikefarah:masterfrom
cobyfrombrooklyn-bot:fix-issue-2538
Open

Fix #2538: Preserve original filename when using --front-matter#2613
cobyfrombrooklyn-bot wants to merge 1 commit intomikefarah:masterfrom
cobyfrombrooklyn-bot:fix-issue-2538

Conversation

@cobyfrombrooklyn-bot
Copy link

When using --front-matter, yq extracts YAML front matter into a temporary file and replaces args[0] with the temp path. This caused the filename operator to return the temp file path (e.g., /var/folders/.../temp131236935) instead of the original filename.

Changes

  • pkg/yqlib/utils.go: Added SetFilenameAlias/ClearFilenameAliases/resolveFilename for mapping temp paths to original filenames
  • pkg/yqlib/utils.go: readDocuments now resolves filename aliases before setting candidateNode.filename
  • pkg/yqlib/stream_evaluator.go: Evaluate now resolves filename aliases
  • cmd/evaluate_all_command.go: Registers alias when front matter replaces the filename
  • cmd/evaluate_sequence_command.go: Same

Test

Added TestFrontMatterFilenamePreserved in front_matter_test.go that:

  1. Creates a front matter file, splits it
  2. Registers a filename alias for the temp file
  3. Reads documents using the temp file path
  4. Verifies candidateNode.filename contains the original path, not the temp path

Fails without the fix (undefined functions), passes with it.

Full test suite passes. Tested locally on macOS ARM (Apple Silicon).

Fixes #2538

When using --front-matter, yq creates a temporary file for the
extracted YAML content but replaces the original filename in args
with the temp file path. This caused the 'filename' operator to
return the temp file path instead of the original filename.

Added a filename alias mechanism: when front matter processing
replaces the file path, it registers the original filename as an
alias. The readDocuments and stream evaluator functions resolve
aliases before setting candidateNode.filename.

Fixes mikefarah#2538
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.

Incorrect value for filename when using --front-matter

1 participant