#465·plop

Append action removes literal "$`" due to special replacement pattern handling

Author: checcheCreated Mar 29, 2025Updated Mar 29, 2025

Description:

When using the append action in Plop, if the target file already contains the literal "$`"(dollar + backquote), it gets removed during the append operation. This issue appears to occur in the implementation of the append action, where String.replace interprets "$" as a special replacement pattern (representing the text preceding the match) rather than as a literal string.

The problem is placed when regular expression strings are written in JavaScript source code.

Steps to Reproduce:

input.txt

txt
$`$`

plopfile.ts

typescript
import type { NodePlopAPI } from "plop";

export default function (plop: NodePlopAPI) {
  plop.setGenerator("test", {
    description: "test",
    prompts: [
      {
        type: "input",
        name: "name",
        message: "input name",
      },
    ],
    actions: [
      {
        type: "append",
        path: "input.txt",
        template: 'console.log("{{name}}");',
      },
    ],
  });
}

Actual Outcome

txt


console.log("hoge");

Additional Context:

The issue seems to be in the implementation of the append action logic in append.js A potential fix might involve escaping all $ characters (e.g., replacing $ with $$) in the replacement string to ensure they remain intact.