Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RCS patch wrongly applied results in incorrect file #299

Open
guillaumebrunerie opened this issue Apr 29, 2024 · 1 comment
Open

RCS patch wrongly applied results in incorrect file #299

guillaumebrunerie opened this issue Apr 29, 2024 · 1 comment

Comments

@guillaumebrunerie
Copy link

Consider the following file in a Javascript project with Prettier and the plugin https://github.com/IanVS/prettier-plugin-sort-imports (which makes Prettier automatically sort imports):

import {
    firstLongImport,
    secondLongImport,
    thirdLongImport,
} from "longDependency";
import { fourthImport } from "dependency";

Formatting it in Emacs with Apheleia results in the following invalid code:

import {
import { fourthImport } from "dependency";
    firstLongImport,
    secondLongImport,
    thirdLongImport,
} from "longDependency";

(the expected behavior is that fourthImport is moved to the first line).

Running apheleia-npx from the terminal with the correct arguments correctly formats the file, so the issue seems to be in Apheleia, not in the formatter.
Here is the RCS diff I get, running diff in the terminal with the same arguments as Apheleia):

a0 1
import { fourthImport } from "dependency";
d6 1

Could it be that the a0 line isn’t handled correctly?

@guillaumebrunerie
Copy link
Author

guillaumebrunerie commented Apr 29, 2024

I believe it can be fixed with the following patch (it is indeed the a0 which is not handled properly).

diff --git a/apheleia-rcs.el b/apheleia-rcs.el
index e462921..3d73181 100644
--- a/apheleia-rcs.el
+++ b/apheleia-rcs.el
@@ -100,7 +100,7 @@ contains the patch."
              ;; Account for the off-by-one error in the RCS patch spec
              ;; (namely, text is added *after* the line mentioned in
              ;; the patch).
-             (when (eq (alist-get 'command command) 'addition)
+             (when (and (eq (alist-get 'command command) 'addition) (> (alist-get 'start command) 0))
                (forward-line))
              (push `(marker . ,(point-marker)) command)
              (push command commands)

I’m not sure how to run the tests to make sure it doesn’t break anything else, running make fmt-build FORMATTERS=prettier-javascript fails with the error

build-image.bash: tagging apheleia-formatters:latest
build-image.bash: will install all formatters by default
test/formatters/build-image.bash: line 30: args[@]: unbound variable

I suspect the script is being run in Zsh, no idea why or how to make it run in Bash instead. Not sure anymore, running the script directly with Bash makes no difference. I’m on Mac OS Sonoma if it matters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant