Sitelet https://github.com/fsprojects/Paket/pull/2575
Skip to content

Don't change BOM for existing project files - fixes #2271 - #2575

Merged
forki merged 1 commit into
fsprojects:masterfrom
TeaDrivenDev:projectfile_bom
Aug 17, 2017
Merged

forki merged 1 commit into
fsprojects:masterfrom
TeaDrivenDev:projectfile_bom

Conversation

@TeaDrivenDev

Copy link
Copy Markdown
Contributor

As mentioned in #2271, some project files have a BOM (created by VS), others don't (from Ionide/Forge), so to know what "unchanged" means, we have to probe the original file and set the encoding for writing the XML file accordingly. We only consider UTF-8 because it seems safe to assume all project files will have that.



let save forceTouch project =
let hasUtf8Bom fileName =

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this go in another place? I kept it as a local function because it's only used here.

if File.Exists fileName then
use fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)
let bomBuffer = Array.zeroCreate 4
fs.Read(bomBuffer, 0, 4) |> ignore

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this save?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fs.Read(bomBuffer, 0, 4)
what happens if it's 3 bytes?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then the next line is most likely false or undefined.

Comparing the result with 4 would definitely make future readers more happy ;)

@TeaDrivenDev

Copy link
Copy Markdown
Contributor Author

Updated with a better way to get the bytes to compare against.

@forki What did you mean earlier with "What happens if it's 3 bytes"? If the file is shorter than what we're trying to read, FileStream.Read() will take what it can get and return the number of bytes as an integer (which we ignore). That would leave us with a byte array that is not equal to the expected BOM, and we would decide to write the project file without a BOM.

@forki

forki commented Aug 17, 2017

Copy link
Copy Markdown
Member

thx!

@forki
forki merged commit 9150a63 into fsprojects:master Aug 17, 2017
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

Successfully merging this pull request may close these issues.

3 participants