0

I am using compare-object to compare a here-string (reference object) to the contents of a file via "get-content" (difference object). The side indicators say that the first line in each is different, even though they look the same and I manually removed all white spaces from both sources. The file content is created with the here-string itself, so I am at a loss as to why it thinks the first line is different:

$GitIgnorePath = 'file'
$NewGitIgnore = @"
#  Local .terraform directories
**/.terraform/*
123
"@

Set-Content -path $GitIgnorePath -value $NewGitIgnore
$CurrentGitIgnore = Get-Content -Path $GitIgnorePath -Raw
Compare-Object $NewGitIgnore $CurrentGitIgnore


InputObject                                            SideIndicator
-----------       
#  Local .terraform directories...                          =>
#  Local .terraform directories...                          <=

If I change '123' in $NewGitIgnore to "1234" and run compare-object again, I get the same results.

js2010
  • 23,033
  • 6
  • 64
  • 66
egrok
  • 55
  • 4
  • How does $NewGitignore get a value? What is the value? – Walter Mitty May 22 '21 at 10:48
  • 1
    Given that they’re quite short strings, have you tried doing a hex dump of both and inspecting manually with something like ```Format-HexString```, or using a ```for( )``` loop to compare character by character to find where they differ? It might be something like CRLF vs LF line ending for example. – mclayton May 22 '21 at 11:04
  • Set-Content will write lines with CRLF line endings on Windows, but here-strings generally uses LF line ending. Your identical strings aren't. Should be easy to see by examining the length of `$NewGitIgnore` and `$CurrentGitIgnore`. – Bacon Bits May 22 '21 at 11:26
  • 2
    Does this answer your question? [Set-Content appends a newline (line break, CRLF) at the end of my file](https://stackoverflow.com/questions/45266461/set-content-appends-a-newline-line-break-crlf-at-the-end-of-my-file): `Set-Content -NoNewLine` – iRon May 22 '21 at 12:18
  • @BaconBits, good point about the potential difference, but note that here-strings defined _in a file_ invariably use that file's newline format. (Interactively defined ones indeed always use LF-only newlines.) – mklement0 May 22 '21 at 13:01

1 Answers1

0

The $currentgitIgnore string from the file has an extra /r/n (0D 0A) at the end. Set-content -nonewline would get rid of that. $NewGitIgnore has no line ending at the end. They are one string each, which is unusual, not arrays of strings. The file is also malformed, having unix line endings (\n only) in all but the last line.

$NewGitIgnore | format-hex

   Label: String (System.String) <3CF480C1>

          Offset Bytes                                           Ascii
                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 23 20 20 4C 6F 63 61 6C 20 2E 74 65 72 72 61 66 #  Local .terraf
0000000000000010 6F 72 6D 20 64 69 72 65 63 74 6F 72 69 65 73 0A orm directories�
0000000000000020 2A 2A 2F 2E 74 65 72 72 61 66 6F 72 6D 2F 2A 0A **/.terraform/*�
0000000000000030 31 32 33                                        123


$currentgitIgnore | format-hex

   Label: String (System.String) <5B75072F>

          Offset Bytes                                           Ascii
                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 23 20 20 4C 6F 63 61 6C 20 2E 74 65 72 72 61 66 #  Local .terraf
0000000000000010 6F 72 6D 20 64 69 72 65 63 74 6F 72 69 65 73 0A orm directories�
0000000000000020 2A 2A 2F 2E 74 65 72 72 61 66 6F 72 6D 2F 2A 0A **/.terraform/*�
0000000000000030 31 32 33 0D 0A                                  123��

A more typical comparison with string arrays would look like this, with equal string arrays. The file will end up being windows text (/r/n at the end of every line). Note that with compare-object's default infinite syncwindow, the order of the lines doesn't matter.

$GitIgnorePath = 'file'
$NewGitIgnore = '#  Local .terraform directories',
'**/.terraform/*',
'123'

Set-Content -path $GitIgnorePath -value $NewGitIgnore
$CurrentGitIgnore = Get-Content -Path $GitIgnorePath

Compare-Object $NewGitIgnore $CurrentGitIgnore -IncludeEqual

InputObject                     SideIndicator
-----------                     -------------
#  Local .terraform directories ==
**/.terraform/*                 ==
123                             ==
js2010
  • 23,033
  • 6
  • 64
  • 66