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

The parser does not return correct line number in Windows #11

Closed
k3ndo opened this issue Jan 2, 2022 · 1 comment · Fixed by #12
Closed

The parser does not return correct line number in Windows #11

k3ndo opened this issue Jan 2, 2022 · 1 comment · Fixed by #12
Labels
bug Something isn't working

Comments

@k3ndo
Copy link
Contributor

k3ndo commented Jan 2, 2022

The parser specifies the line number where the error occurred but it does not match the line number of the .env file. This problem only happens on Windows.

For example, I have the following .env file:

KEY1 = 1
KEY2 = 2
asdasd
KEY3 = 3
KEY4 = 4

On testing this, the parser throws the following exception:

The parser found a line that has no key-value pair format. (Actual Value: asdasd, Line: 5)

So, it doesn't make sense, the error occurred on line 3 and not line 5. The funny thing is that this behavior only occurs on Windows but not on Linux. I tested this on a Linux distro like Linux Mint and the line number is correct.

It seems that the problem is this line:

var lines = input.Split(Environment.NewLine.ToCharArray());

According to the documentation, the value that represents the NewLine property in Windows is \r\n. Split method is receiving that value and returning empty substrings.

Why does it return empty substrings? I don't know yet.

If we have such a string:

KEY1 = 1\r\nKEY2 = 2\r\nasdasd\r\nKEY3 = 3\r\nKEY4 = 4

Split method splits the string in this way:

KEY1 = 1
""
KEY2 = 2
""
asdasd
""
KEY3 = 3
""
KEY4 = 4

In Linux this problem does not happen, because the NewLine property returns \n, so the split method will not return empty substrings.

I will make a PR to solve this issue.

@MrDave1999
Copy link
Owner

MrDave1999 commented Mar 28, 2022

I found an easier solution without the need to use the StringReader class:

var newLines = new[] { "\r\n", "\n", "\r" };
var lines = input.Split(newLines, StringSplitOptions.None);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants