Emacs中compile-mode无法跳转到编译错误位置的求助
I get it, it's super frustrating when you run yarn test in Emacs' compile-mode and can't jump directly to the error location—especially when dealing with non-JS/TS files like Phaser's shader fragments. Let's break down why this happens and how to fix it:
The Root Problem
Compile-mode relies on predefined regex patterns in compilation-error-regexp-alist to parse error messages. By default, these patterns don't account for Jest's output format when it hits errors in shader files (like the .frag file in your stack trace). The error line starts with a full file path, line number, and then a weird ({ prefix that throws off the default matchers.
Step-by-Step Solutions
1. Add a Custom Error Regex for Jest Shader Errors
We'll extend Emacs' error matching rules to recognize this specific format. Pop this code into your Emacs config (like ~/.emacs.d/init.el or ~/.config/emacs/init.el):
;; Add regex to match Jest's shader error format (add-to-list 'compilation-error-regexp-alist 'jest-shader-error '("^\\([^ \n]+\\):\\([0-9]+\\) ({" 1 2))
This regex looks for lines that start with a file path ([^ \n]+), followed by a colon, line number, and then ({—exactly the pattern in your stack trace. The 1 and 2 tell Emacs which parts of the match correspond to the file path and line number, respectively.
2. Ensure Jest Outputs Full, Verbose Paths
Sometimes Jest uses relative paths or truncated output that Emacs can't parse correctly. Update your test command to force verbose output with full paths:
- Either modify your
package.jsontest script:"test": "jest 'src' --verbose" - Or run this directly in compile-mode:
yarn test --verbose
Verbose mode ensures the full file path is printed, making it easier for Emacs to locate the file.
3. Hook the Regex to Compile-Mode (Optional)
If you want this regex to only apply when running Jest tests, you can add a hook to compile-mode that loads the rule on demand:
(defun my-jest-compilation-setup () (add-to-list 'compilation-error-regexp-alist 'jest-shader-error)) (add-hook 'compilation-mode-hook 'my-jest-compilation-setup)
This way, the custom regex is active whenever you're in compile-mode, but you can tweak it to only trigger for Jest commands if needed (using compilation-major-mode checks).
4. Test the Fix
After updating your config, restart Emacs (or evaluate the code with M-x eval-region), then re-run your yarn test compile command. Move your cursor to the error line and press `C-x `` (that's a backtick) — Emacs should now jump directly to the shader file and line number.
If it still doesn't work, you can debug the regex with M-x compilation-show-error to see if Emacs is matching the error line correctly.
内容的提问来源于stack exchange,提问作者PaNaVTEC

