Skip to content

Commit

Permalink
build: force all Windows batch files to CRLF
Browse files Browse the repository at this point in the history
Batch files should use CRLF endings. LF endings mostly
work but in some situations they cause random errors like
goto commands failing for mysterious reasons. See
golang.org/issue/37791 for more information.

Next CL triggered one of such bug (a label was not being
recognized), so prepare for it by converting to CRLF.

This CL also touches all existing batch files to force git
to update the line endings (unfortunately, changing
.gitattributes only has effect next time the file is checked
out or modified).

Fixes #37791
Updates #9281

Change-Id: I6f9a114351cb7ac9881914400aa210c930eb8cc1
Reviewed-on: https://go-review.googlesource.com/c/go/+/96495
Run-TryBot: Giovanni Bajo <rasky@develer.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
  • Loading branch information
rasky committed Mar 22, 2020
1 parent 2910c5b commit 787e7b0
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@
# See golang.org/issue/9281

* -text

# The only exception is Windows files that must absolutely be CRLF or
# might not work. Batch files are known to have multiple bugs when run
# with LF endings. See golang.org/issue/37791 for more information.

*.bat text eol=crlf
1 change: 1 addition & 0 deletions src/all.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
:: Copyright 2012 The Go Authors. All rights reserved.
:: Use of this source code is governed by a BSD-style
:: license that can be found in the LICENSE file.

@echo off

setlocal
Expand Down
1 change: 1 addition & 0 deletions src/clean.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
:: Copyright 2012 The Go Authors. All rights reserved.
:: Use of this source code is governed by a BSD-style
:: license that can be found in the LICENSE file.

@echo off

setlocal
Expand Down
1 change: 1 addition & 0 deletions src/make.bat
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,4 @@ set GOBUILDFAIL=1
if x%GOBUILDEXIT%==x1 exit %GOBUILDFAIL%

:end

1 change: 0 additions & 1 deletion src/race.bat
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,3 @@ echo All tests passed.

:end
if x%GOBUILDEXIT%==x1 exit %GOBUILDFAIL%

1 change: 1 addition & 0 deletions src/run.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
:: Copyright 2012 The Go Authors. All rights reserved.
:: Use of this source code is governed by a BSD-style
:: license that can be found in the LICENSE file.

@echo off

:: Keep environment variables within this script
Expand Down
30 changes: 30 additions & 0 deletions test/winbatch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// run

// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Check that batch files are maintained as CRLF files (consistent behaviour
// on all operating systems). See https://github.com/golang/go/issues/37791

package main

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
)

func main() {
batches, _ := filepath.Glob(runtime.GOROOT() + "/src/*.bat")
for _, bat := range batches {
body, _ := ioutil.ReadFile(bat)
if !bytes.Contains(body, []byte("\r\n")) {
fmt.Printf("Windows batch file %s does not contain CRLF line termination.\nTry running git checkout src/*.bat to fix this.\n", bat)
os.Exit(1)
}
}
}

0 comments on commit 787e7b0

Please sign in to comment.