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

Exclusions not working? #156

Closed
asifbacchus opened this issue Jul 24, 2021 · 7 comments
Closed

Exclusions not working? #156

asifbacchus opened this issue Jul 24, 2021 · 7 comments

Comments

@asifbacchus
Copy link

What version of Livereload are you using?

0.9.3

What OS are you using?

Alpine 3.14 (official Node v.16.5)

What web browser are you using? (Browser name and specific version please)

Not a browser issue.

Expected result

Specified regex to ignore, but livereload still triggers a refresh when said files are updated.
Also triggers a refresh in default ignored directories such as .git/

Actual result

LiveReload is waiting for a browser to connect...

Protocol version: 7
Exclusions: /\.git\/.*/,/\.svn\/.*/,/\.idea\/.*/,/\.vscode\/.*/,/\.tmp/,/\.swp/]/\.git\//,/\.svn\//,/\.hg\//
Extensions: html,xml,css,js,jsx,ts,tsx,php,py
Polling: true


Watching /watch...

Saw change to /watch/.git/file

Reloading: /watch/.git/file

Steps to reproduce issue

  1. use default exclusions, start server
  2. in watched directory, mkdir .git
  3. `echo "this is a test file" >./.git/file
  4. Livereload sees the change and commences a reload.

Why is this important?

Would be nice to have exclusions work so there is less overhead in page reloads for simple things
like git commits. I seem to remember this working in earlier versions, but I only ever used the CLI.
I am using it via JS now.

Happy to provide any other details -- excellent project, btw! Thanks!

@napcs
Copy link
Owner

napcs commented Jul 26, 2021

@asifbacchus you indicate you're using it by JS now. Please send me the code you're using so I can explore it.

The default exclusions look like this btw:

    const defaultExclusions = [/\.git\//, /\.svn\//, /\.hg\//];

Your regex patterns look like they are doing a .* capture group. I don't think you need that - I think you can just follow the same pattern the defaults use.

If you leave the defaults alone, does the .git folder get ignored for you? If so, check your regex.

@napcs
Copy link
Owner

napcs commented Jul 26, 2021

@asifbacchus just a followup. These steps:

    use default exclusions, start server
    in watched directory, mkdir .git
    `echo "this is a test file" >./.git/file
    Livereload sees the change and commences a reload.

do not trigger a livereload event for me on macOS using 0.9.3 from npm. Happy to look at code.

@asifbacchus
Copy link
Author

First test I ran was literally what you suggested. I still get a notification "Reloading..." using the defaults. Super strange. As a side note, is there a way to override those defaults? I see that anything I specify is added -- might be a nice feature to be able to replace instead of append.

Here's the code I'm using:

// load livereload module
const livereload = require('livereload');

// set createServer options
const fs = require('fs');
const options = {
    port: process.env.LR_PORT,
    exts: process.env.LR_EXTS,
    exclusions: process.env.LR_EXCLUDE,
    usePolling: true,
    delay: process.env.LR_DELAY,
};

// set debugging output as per LR_DEBUG
if (process.env.LR_DEBUG === "true") {
    options.debug = true
    console.log("[Debug output ENABLED]");
}

// set HTTPS as per LR_HTTPS
if (process.env.LR_HTTPS === "true") {
    options.https = {
        cert: fs.readFileSync('/certs/fullchain.pem'),
        key: fs.readFileSync('/certs/privkey.pem')
    };
    console.log("[HTTPS mode]");
}
else {
    console.log("[HTTP mode]");
}

// start server
const lrServer = livereload.createServer(options, healthcheck);
lrServer.watch('/watch')

As you can see, I'm reading the exclusions from an environment variable. This is because everything is being run in a docker container and it's easier to specify at run-time this way. However, as I understand it, process.env.VARIABLE simply returns a string with the contents of the environment variable. So, maybe that string needs to be passed in a certain way?

The healthcheck callback is a separate function that I'm using as part of a docker deployment. I didn't include it because I can't see how it would affect this issue, but if you need to see it for whatever reason, let me know.

Thanks for looking into this -- very, very much appreciated.

@napcs
Copy link
Owner

napcs commented Jul 27, 2021

@asifbacchus Thanks for the code. Turns out the fix is easy.

The exclusions needs to be an array. You're taking it in from the args as a string.

Try splitting it on commas like this:

    exclusions: process.env.LR_EXCLUDE.split(","),

That's what the CLI version does anyway. When I applied that change to your code, it worked as expected.

With the exclusions.... no there's not currently a way to replace the default exclusions. It ends up breaking backwards compat if I changed that behavior. If I get enough requests for this I'll add a second command line option, but in the last 10 years it's never come up that people want remove the git/svn exclusions we have by default.

I'm gonna close this as I don't think this is an issue. But I am creating a new issue #157 so nobody else gets tripped up on this.

@napcs napcs closed this as completed Jul 27, 2021
@asifbacchus
Copy link
Author

Just to follow-up for the benefit of anyone else. Splitting on commas only accomplished half of what was needed. That resulted in an array of string elements which, while properly formatted, just would not work. I had to convert each element to a RegEx object.

// create array of string elements
const extraExclusions = process.env.LR_EXCLUDE.split(",");
// convert each element to a RegExp object
extraExclusions.forEach((exclusion, idx) => {
    extraExclusions[idx] = new RegExp(exclusion);
});

// set createServer options
const options = {
    port: process.env.LR_PORT,
    exts: process.env.LR_EXTS,
    exclusions: extraExclusions,
    usePolling: true,
    delay: process.env.LR_DELAY,
};

The extra advantage of this is the environment variable can be set in a simpler syntax:
LR_EXCLUDE=".somedir/,.swp$,.tmp$"

Hope this helps, thanks for the pointer and getting me set in the right direction, @napcs.

@napcs
Copy link
Owner

napcs commented Jul 29, 2021

@asifbacchus Glad you got it! And sorry I wasted some time there - yes they need to be proper regex entries. Also, you can use map() instead of forEach - see lib/command.js in this repo for how I set them in the CLI.

@asifbacchus
Copy link
Author

No worries, thanks for all the help!

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

No branches or pull requests

2 participants