"charset = utf-8" setting in .editorconfig breaks opening .jar files in emacs
.jar files in Java projects are actually .zip files, which emacs can open natively.
If I have a project with an .editorconfig file, like e.g. this project: https://github.com/steinarb/modelstore and then builds that project with "mvn clean install" and then tries opening the jar files in the modelstore/modelstore.services/target/ directory they fail to open correctly: https://www.bang.priv.no/screenshots/Screenshot%20at%202023-04-03%2007-27-13.png
I bisected my .emacs and found that the offending config was the following in my ~/.emacs:
;; editorconfig formatting support
;;;================================
(when (locate-library "editorconfig")
(editorconfig-mode 1))
Removing the above config caused jar files to open correctly in .emacs, which BTW looks like this: https://www.bang.priv.no/screenshots/Screenshot%20at%202023-04-03%2007-54-54.png
Removing the .editorconfig from the project also made .jar files open correctly in .emacs, so I bisected the .editorconfig file and found that the offending line was
charset = utf-8
Removing that line from a project's .editorconfig made .jar files open correctly
I tried fixing this by customizing editorconfig-exclude-modes, adding the following to custom-set-variables in .emacs:
'(editorconfig-exclude-modes '(Zip-Archive))
But I was unable to open .jar files in a project with .editorconfig files even with the editorconfig-exclude-modes setting in a freshly started emacs.
The editorconfig-exclude-regexps did work for me. I customized editorconfig-exclude-regexps and tried to match buffers ending with .jar:
'(editorconfig-exclude-regexps '("\\.jar$"))
And after this .jar files opened correctly in a project containing an .editorconfig file.
I removed both customizations and added a handwritten config in .emacs, and the following also works:
;; editorconfig formatting support
;;;================================
(when (locate-library "editorconfig")
(editorconfig-mode 1)
(setq editorconfig-exclude-regexps '("\\.jar$")))
Even though I found a config workaround, this probably should be fixed in the way that editorconfig doesn't touch buffers where mode-class is special.
For more detail, please see the following comments for an issue I opened for this on emacs (before I knew it was caused by editorconfig): https://debbugs.gnu.org/cgi/bugreport.cgi?bug=62630#26 https://debbugs.gnu.org/cgi/bugreport.cgi?bug=62630#29
'(editorconfig-exclude-modes '(Zip-Archive))
This doesn't look right. Do you mean archive-mode? You shouldn't enable editorconfig-mode in the archive-mode, that's why you are getting those errors?
Jen-Chieh Shen @.***>:
This doesn't look right. Do you mean
archive-mode?
I just picked the leftmost mode in the mode list when opening a .jar file, which is a zip file.
You shouldn't enable
editorconfig-modein the archive-mode, that's why you are getting those error?
I haven't enabled editorconfig-mode in archive-mode. At least not explicitly.
I had an .editorconfig file on a maven project and tried opening a .jar file in a target directory in that project, and that failed because of the .editorconfig charset setting (found by first bisecting my config to find that this was caused by editorconfig, and then bisecting the .editorconfig file to find out what setting caused it).
Anyway: see this message in the thread with the links to messages in the emacs-help mailing list, for what the emacs maintainers (at least one of them is) thinks is the way editorconfig-mode should see to stay away from buffers like this (editorconfig shouldn't touch buffers where mode-class is special): https://github.com/editorconfig/editorconfig-emacs/issues/294#issuecomment-1494756030
Wed 3 Jul 2024 00:33:50 BST
I've just run into this problem with .zip files (archive-mode). My solution: add the following to .editorconfig:
[*.{zip,jar}]
charset = unset
Neither
(setq editorconfig-exclude-modes '(archive-mode))
nor
(setq editorconfig-exclude-regexps '("\\.jar$" "\\.zip$"))
worked for me.
versions:
editorconfig: Melpa: 20240604.602 emacs: GNU Emacs 29.3 (build 1, aarch64-apple-darwin23.4.0, Carbon Version 170 AppKit 2487.5) of 2024-05-04
@rprimus
I could partially reproduce your problem. There was a regression caused by recent change, but I could solve this by setting (setq editorconfig-exclude-regexps '("\\.jar$" "\\.zip$"))...
Anyway I fixed the regression, so please try the latest version and comment if the problem still persists.