Skip to content

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

Merged
mikefarah merged 1 commit intomikefarah:masterfrom
cobyfrombrooklyn-bot:fix-issue-2538
Mar 25, 2026
Merged

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

Conversation

@cobyfrombrooklyn-bot
Copy link
Contributor

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
@mikefarah mikefarah merged commit b151522 into mikefarah:master Mar 25, 2026
3 checks passed
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Mar 26, 2026
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [mikefarah/yq](https://github.com/mikefarah/yq) | patch | `v4.52.4` → `v4.52.5` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>mikefarah/yq (mikefarah/yq)</summary>

### [`v4.52.5`](https://github.com/mikefarah/yq/releases/tag/v4.52.5)

[Compare Source](mikefarah/yq@v4.52.4...v4.52.5)

- Fix: reset TOML decoder state between files ([#&#8203;2634](mikefarah/yq#2634)) thanks [@&#8203;terminalchai](https://github.com/terminalchai)
  - Fix: preserve original filename when using --front-matter ([#&#8203;2613](mikefarah/yq#2613)) thanks [@&#8203;cobyfrombrooklyn-bot](https://github.com/cobyfrombrooklyn-bot)
  - Fix typo in filename ([#&#8203;2611](mikefarah/yq#2611)) thanks [@&#8203;alexandear](https://github.com/alexandear)
  - Bumped dependencies

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever MR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My44OS42IiwidXBkYXRlZEluVmVyIjoiNDMuODkuNiIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90IiwiYXV0b21hdGlvbjpib3QtYXV0aG9yZWQiLCJkZXBlbmRlbmN5LXR5cGU6OnBhdGNoIl19-->
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

2 participants