Always that run the command `vercel env pull` is adding an extra rule `.env*local` to `.gitignore`!

:memo: Description

This is not a critical bug but is little boring have to remove this extra rule always that I added a new rule to vercel.

:mag_right: Details

  • CLI Version: 37.12.1
  • OS: Microsoft Windows 11 Pro

:camera: Evidence

evidence

:paw_prints: Steps to reproduce the issue

  1. Using an OS that has end-of-line crlf by default like Windows
  2. Run vercel env pull Into a project that already has a the vercel rules into the .gitignore

:test_tube: What’s the expected result?

Only add the rule .env*.local when the .gitignore does not have it.

:x: What’s the actual result?

Is adding the same rule multiple times.

🫚 Root cause

This is happeing because this condition in this file packages/cli/src/util/link/add-to-gitignore.ts this is the code:

const EOL = gitIgnore.includes('\r\n') ? '\r\n' : os.EOL;
// ..
if (!gitIgnore.split(EOL).includes(ignore)) { /* add the rule */

In some cases this condition can be translated to:

const EOL = gitIgnore.includes('\r\n') ? '\r\n' : '\r\n';

For example in my case my file is using lf = \n but my os use crlf = \r\n by default that is this condition will never be true.

:white_check_mark: Solution

I did a few changes in this code that I would like to check first here to avoid a forgotten PR!

Changes

  1. Move this file add-to-gitignore.ts from src/util/link to src/util since it is not only used in link command but also in env command.

  2. Change the code to check as an string. It is good for two reasons:

    • The .gitignore can have multiples rules at the same line
    • Doesn’t matter what is the end-of-line
if (gitIgnore.includes(ignore)) return isGitIgnoreUpdated;
  1. The last change that I would like to check if it is ok :thinking: or is too much. I created a function to get the dominant end-of-line and use it when add a new line. This is the code:
function getDominantEOL(text: string) {
  const lines = (text.match(/\r\n|\n/g) ?? []) as Array<'\r\n' | '\n'>;

  const {
    '\n': LFCount,
    '\r\n': CRLFCount,
  } = lines.reduce(
    (counter, lineEnding) => {
      counter[lineEnding] += 1;
      return counter;
    },
    { '\n': 0, '\r\n': 0 },
  );

  const dominantEOL = LFCount > CRLFCount ? '\n' : '\r\n';

  return LFCount === CRLFCount ? os.EOL : dominantEOL;
}

It can be simpler checking if there is any kind of end-of-line otherwise it uses the os end-of-line:

function getEOL(text: string) {
  if (text.includes('\r\n')) return '\r\n';
  if (text.includes('\n')) return '\n';
  return os.EOL;
}

I will open the PR soon and leave the link to it here!
If you have any suggestions I will be happy to apply it :blush:

Hey!

Thank you for taking the time to write to us with such in-depth detail. We greatly appreciate your feedback and ideas. I have forwarded your message to the appropriate team for them to adequately address.

In the meantime, I would highly suggest creating a Pull request. This way, our team can thoroughly review your suggestions and work on implementing them if deemed appropriate. We always welcome feedback from our community and we’re more than happy to review any contributions.

Thank you!

1 Like

@swarnava Thank you, for your so fast anwser :cupid:! I created it here

3 Likes

I have forwarded your PR as well internally. Feel free to give me a nudge if you don’t hear back from us soon. Thank you for your help!

1 Like

Thanks for your contribution, @codermarcos! :smile:

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.