Skip to content

Commit be51601

Browse files
parse_env_file discard utf8 bom (#2638)
* parse_env_file discard utf8 bom * powershell 5 may add the BOM even when explicitly using utf8 * add test + apply to GITHUB_PATH as well * fix it * fix powershel 5 syntax * misc * fixup * fix wrong subaction --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent be1b6ee commit be51601

File tree

7 files changed

+110
-1
lines changed

7 files changed

+110
-1
lines changed

pkg/container/parse_env_file.go

+8
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,16 @@ func parseEnvFile(e Container, srcPath string, env *map[string]string) common.Ex
2525
return err
2626
}
2727
s := bufio.NewScanner(reader)
28+
firstLine := true
2829
for s.Scan() {
2930
line := s.Text()
31+
if firstLine {
32+
firstLine = false
33+
// skip utf8 bom, powershell 5 legacy uses it for utf8
34+
if len(line) >= 3 && line[0] == 239 && line[1] == 187 && line[2] == 191 {
35+
line = line[3:]
36+
}
37+
}
3038
singleLineEnv := strings.Index(line, "=")
3139
multiLineEnv := strings.Index(line, "<<")
3240
if singleLineEnv != -1 && (multiLineEnv == -1 || singleLineEnv < multiLineEnv) {

pkg/runner/run_context.go

+8
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,16 @@ func (rc *RunContext) UpdateExtraPath(ctx context.Context, githubEnvPath string)
520520
return err
521521
}
522522
s := bufio.NewScanner(reader)
523+
firstLine := true
523524
for s.Scan() {
524525
line := s.Text()
526+
if firstLine {
527+
firstLine = false
528+
// skip utf8 bom, powershell 5 legacy uses it for utf8
529+
if len(line) >= 3 && line[0] == 239 && line[1] == 187 && line[2] == 191 {
530+
line = line[3:]
531+
}
532+
}
525533
if len(line) > 0 {
526534
rc.addPath(ctx, line)
527535
}

pkg/runner/runner_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ func TestRunEventHostEnvironment(t *testing.T) {
429429
tables = append(tables, []TestJobFileInfo{
430430
{workdir, "windows-prepend-path", "push", "", platforms, secrets},
431431
{workdir, "windows-add-env", "push", "", platforms, secrets},
432+
{workdir, "windows-prepend-path-powershell-5", "push", "", platforms, secrets},
433+
{workdir, "windows-add-env-powershell-5", "push", "", platforms, secrets},
432434
{workdir, "windows-shell-cmd", "push", "", platforms, secrets},
433435
}...)
434436
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
runs:
2+
using: composite
3+
steps:
4+
- run: |
5+
echo $env:GITHUB_ENV
6+
echo "kEy=n/a" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
7+
shell: powershell
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
on:
2+
push:
3+
jobs:
4+
test:
5+
runs-on: windows-latest
6+
steps:
7+
- run: |
8+
echo $env:GITHUB_ENV
9+
echo "key=val" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
10+
echo "key2<<EOF" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
11+
echo "line1" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
12+
echo "line2" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
13+
echo "EOF" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
14+
cat $env:GITHUB_ENV
15+
shell: powershell
16+
- run: |
17+
ls env:
18+
if($env:key -ne 'val') {
19+
echo "Unexpected value for `$env:key: $env:key"
20+
exit 1
21+
}
22+
if($env:key2 -ne "line1`nline2") {
23+
echo "Unexpected value for `$env:key2: $env:key2"
24+
exit 1
25+
}
26+
shell: powershell
27+
- run: |
28+
echo $env:GITHUB_ENV
29+
echo "KEY=test" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8
30+
# Defect missing append, test is broken!!!
31+
echo "Key=expected" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8
32+
shell: powershell
33+
- name: Assert GITHUB_ENV is merged case insensitive
34+
run: exit 1
35+
if: env.KEY != 'expected' || env.Key != 'expected' || env.key != 'expected'
36+
shell: powershell
37+
- name: Assert step env is merged case insensitive
38+
run: exit 1
39+
if: env.KEY != 'n/a' || env.Key != 'n/a' || env.key != 'n/a'
40+
env:
41+
KeY: 'n/a'
42+
shell: powershell
43+
- uses: actions/checkout@v3
44+
- uses: ./windows-add-env-powershell-5/action
45+
- name: Assert composite env is merged case insensitive
46+
run: exit 1
47+
if: env.KEY != 'n/a' || env.Key != 'n/a' || env.key != 'n/a'
48+
shell: powershell

pkg/runner/testdata/windows-add-env/push.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
steps:
77
- run: |
88
echo $env:GITHUB_ENV
9-
echo "key=val" > $env:GITHUB_ENV
9+
echo "key=val" >> $env:GITHUB_ENV
1010
echo "key2<<EOF" >> $env:GITHUB_ENV
1111
echo "line1" >> $env:GITHUB_ENV
1212
echo "line2" >> $env:GITHUB_ENV
@@ -24,7 +24,9 @@ jobs:
2424
}
2525
- run: |
2626
echo $env:GITHUB_ENV
27+
# Defect missing append
2728
echo "KEY=test" > $env:GITHUB_ENV
29+
# Defect missing append, test is broken!!!
2830
echo "Key=expected" > $env:GITHUB_ENV
2931
- name: Assert GITHUB_ENV is merged case insensitive
3032
run: exit 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
on:
2+
push:
3+
defaults:
4+
run:
5+
shell: powershell
6+
jobs:
7+
test:
8+
runs-on: windows-latest
9+
steps:
10+
- run: |
11+
mkdir build
12+
echo '@echo off' | Out-File -FilePath build/test.cmd -Encoding utf8
13+
echo 'echo Hi' | Out-File -FilePath build/test.cmd -Encoding utf8 -Append
14+
mkdir build2
15+
echo '@echo off' | Out-File -FilePath build/test2.cmd -Encoding utf8
16+
echo 'echo test2' | Out-File -FilePath build/test2.cmd -Encoding utf8 -Append
17+
- run: |
18+
echo '${{ tojson(runner) }}'
19+
ls
20+
echo '${{ github.workspace }}'
21+
working-directory: ${{ github.workspace }}\build
22+
- run: |
23+
echo $env:GITHUB_PATH
24+
echo '${{ github.workspace }}\build' | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
25+
cat $env:GITHUB_PATH
26+
- run: |
27+
echo $env:PATH
28+
test
29+
- run: |
30+
echo "PATH=$env:PATH;${{ github.workspace }}\build2" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
31+
- run: |
32+
echo $env:PATH
33+
test
34+
test2

0 commit comments

Comments
 (0)