Skip to content

fix: handle '%' in filenames when expanding templates - #901

Open
ethanstoner wants to merge 1 commit into
stevearc:masterfrom
ethanstoner:fix/percent-in-filename
Open

ethanstoner wants to merge 1 commit into
stevearc:masterfrom
ethanstoner:fix/percent-in-filename

Conversation

@ethanstoner

Copy link
Copy Markdown

Bug

Formatting or running :ConformInfo on a buffer whose path contains a % (e.g. my%20notes.md) fails with:

runner.lua:521: invalid capture index

Root cause

string.gsub treats % in a string replacement as special (%0-%9 are capture references, %% is a literal percent). The file path was passed straight in as the replacement string in two places:

  • build_context: template:gsub("$RANDOM", ...):gsub("$FILENAME", basename) when building the temp file name (stdin = false)
  • build_cmd: $FILENAME, $DIRNAME and $EXTENSION replacements when args is a string

$RELATIVE_FILEPATH already used a function replacement, so it was unaffected. List-style args do not use gsub and were also fine.

Change

Use function replacements for those gsub calls. The return value of a replacement function is inserted literally, so % in paths no longer gets interpreted. No other gsub calls in the plugin take path-derived replacement strings.

Tests

Added to tests/runner_spec.lua:

  • build_context temp file name with a %20 filename and directory (failed with invalid capture index before the fix)
  • build_cmd string args $FILENAME $DIRNAME $EXTENSION with a %20 path (failed with invalid capture index before the fix)
  • build_cmd list args with a %20 path (already passed; regression guard)

./run_tests.sh on nvim 0.12 (macOS): all new tests pass. Three tests fail identically on unmodified master in my environment, so they are unrelated to this change: runner e2e sets the correct output, fuzzer formats correctly, injected formatter inline.ts.

stylua --check lua tests and luacheck lua tests pass.

Fixes #725

Generated with Claude Code on behalf of @ethanstoner.

@github-actions
github-actions Bot requested a review from stevearc September 17, 2026 02:46
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.

bug: Formatting with markdownlint-cli2 invalid capture index error

1 participant