14

I am using Emacs to edit the files in directories watched by Webpack's development server. Every time I make a change in a file, a backup file gets created in the same directory, like .#original_filename, even though I did not save the changes in Emacs. This causes the server's watcher to register a change even though I did not make one. Hence the server reloads every time I make a change in the file, and then does another reload when I save it.

This is somewhat confusing and time consuming. Looking at Webpack's documentation, I found out about the following option:

For some systems, watching many file systems can result in a lot of CPU or memory usage. It is possible to exclude a huge folder like node_modules:

ignored: /node_modules/ 

It is also possible to use anymatch patterns:

ignored: "files/**/*.js"

So I modified my config like below, to match and ignore the files starting with .:

devServer: {
    ...
    watchOptions: {
        ignored: './src/app/**/.*',
    },
    ...
}

I restart the dev server, but the watcher still registers the backup files as changes done to the codebase. What am I doing wrong?

0

3 Answers 3

12


the proper way to do it would be to use node's native module path
your webpack.config.js should look something like this:

const path = require('path')

module.exports = {
  ...
  devServer: {
    watchOptions: {
      ignored: [
        path.resolve(__dirname, 'dist'),
        path.resolve(__dirname, 'node_modules')
      ]
    }
  },
  ...
}
0
3

I found out a solution that works after trying a couple of times. For some reason, it doesn't work when there is ./ at the beginning, which should just mean the current directory.

Changing the matching pattern to the following works:

watchOptions: {
    ignored: '**/.*',
},
1
0

Another option is to configure Emacs to write its backup files somewhere else. This is normally a good thing to do anyway.

configure window-setup-hook

;; deleting old backups
(defun my-delete-old-backups ()
  (let* ((backup-directory (expand-file-name
                            (concat user-emacs-directory "backups")))
         (auto-save-directory (concat backup-directory "/\\1")))
    (setq backup-by-copying t)      ; don't clobber symlinks
    ;; Write backup files to own directory
    (setq backup-directory-alist `(("." . ,backup-directory)))
    ;; auto-save files to same backup-directory
    (setq auto-save-file-name-transforms `((".*" ,auto-save-directory t)))
    ;; Make backups of files, even when they're in version control
    (setq vc-make-backup-files t)
    (setq delete-old-versions t)
    (message "Deleting old backup files...")
    (let ((week (* 60 60 24 7))
          (current (float-time (current-time))))
      (dolist (file (directory-files backup-directory t)) ;
        (when (and (backup-file-name-p file)
                   (> (- current (float-time (fifth (file-attributes file))))
                      week))
          (message "%s" file)
          (delete-file file))))
    (message "Deleting old backup files...done")
    (message nil)))
(add-hook 'window-setup-hook #'my-delete-old-backups)

The important bits extracted out:

(setq backup-directory (expand-file-name
                            (concat user-emacs-directory "backups"))
(setq backup-directory-alist `(("." . ,backup-directory)))
4
  • In case anyone else is still looking at this, there are two typos in the above. It should be (setq my-backup-directory (without the paren) and then the last symbol should be ,my-backup-directory instead of ,backup-directory
    – tsm
    Commented Nov 13, 2020 at 20:01
  • Fair enough! I think I copied that from my backup hook and then just took the bits needed and made those mistakes.
    – Bae
    Commented Nov 14, 2020 at 5:33
  • Filenames starting with .# are not backup files, they are lock files. I don't think it's possible to put them in another directory, but it's possible to not create them by setting create-lockfiles to nil (e.g. using directory local variables, but can also be done globally)
    – YoungFrog
    Commented Dec 17, 2020 at 13:22
  • @YoungFrog I disable them as this is for single-user configuration (setq create-lockfiles nil) and lock files are not necessary. Also this is only deleting files in the backup directory, there should be no lock files in there anyway. It doesn't delete files anywhere else
    – Bae
    Commented Dec 17, 2020 at 23:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.