Skip to content

Commit 7d0fefd

Browse files
authored
Merge pull request #944 from SteveL-MSFT/utf8-bom
Fix removing UTF-8 BOM from file
2 parents 82b9135 + a7c6f34 commit 7d0fefd

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

dsc/locales/en-us.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,4 @@ failedToGetParentPath = "Error reading config path parent"
135135
dscConfigRootAlreadySet = "The current value of DSC_CONFIG_ROOT env var will be overridden"
136136
settingDscConfigRoot = "Setting DSC_CONFIG_ROOT env var as"
137137
stdinNotAllowedForBothParametersAndInput = "Cannot read from STDIN for both parameters and input."
138+
removingUtf8Bom = "Removing UTF-8 BOM from input"

dsc/src/util.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,13 @@ pub fn get_input(input: Option<&String>, file: Option<&String>, parameters_from_
475475
} else {
476476
match std::fs::read_to_string(path) {
477477
Ok(input) => {
478-
input
478+
// check if it contains UTF-8 BOM and remove it
479+
if input.as_bytes().starts_with(&[0xEF, 0xBB, 0xBF]) {
480+
info!("{}", t!("util.removingUtf8Bom"));
481+
input[3..].to_string()
482+
} else {
483+
input
484+
}
479485
},
480486
Err(err) => {
481487
error!("{}: {err}", t!("util.failedToReadFile"));

dsc/tests/dsc_args.tests.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,22 @@ resources:
262262
$stderr | Should -Match '.*?--file.*?'
263263
}
264264

265+
It 'file containing UTF-8 BOM should be read correctly' {
266+
$yaml = @'
267+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
268+
resources:
269+
- name: Echo
270+
type: Microsoft.DSC.Debug/Echo
271+
properties:
272+
output: hello
273+
'@
274+
Set-Content -Path "$TestDrive/utf8bom.yaml" -Value $yaml -Encoding utf8BOM
275+
$out = dsc config get --file "$TestDrive/utf8bom.yaml" | ConvertFrom-Json
276+
$LASTEXITCODE | Should -Be 0
277+
$out.results[0].type | Should -BeExactly 'Microsoft.DSC.Debug/Echo'
278+
$out.results[0].result.actualState.output | Should -BeExactly 'hello'
279+
}
280+
265281
It 'Get operation on the adapter itself should fail' {
266282
dsc resource get -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
267283
$LASTEXITCODE | Should -Be 2

0 commit comments

Comments
 (0)