Text file src/cmd/go/testdata/script/build_buildvcs_auto.txt

     1  # Regression test for https://go.dev/issue/51748: by default, 'go build' should
     2  # not attempt to stamp VCS information when the VCS tool is not present.
     3  
     4  [short] skip
     5  [!exec:git] skip
     6  
     7  cd sub
     8  exec git init .
     9  exec git add sub.go
    10  exec git commit -m 'initial state'
    11  cd ..
    12  
    13  exec git init
    14  exec git submodule add ./sub
    15  exec git add go.mod example.go
    16  exec git commit -m 'initial state'
    17  
    18  
    19  # Control case: with a git binary in $PATH,
    20  # 'go build' on a package in the same git repo
    21  # succeeds and stamps VCS metadata by default.
    22  
    23  go build -o example.exe .
    24  go version -m example.exe
    25  stdout '^\tbuild\tvcs=git$'
    26  stdout '^\tbuild\tvcs.modified=false$'
    27  
    28  
    29  # Building a binary from a different (nested) VCS repo should not stamp VCS
    30  # info. It should be an error if VCS stamps are requested explicitly with
    31  # '-buildvcs' (since we know the VCS metadata exists), but not an error
    32  # with '-buildvcs=auto'.
    33  
    34  go build -o sub.exe ./sub
    35  go version -m sub.exe
    36  ! stdout '^\tbuild\tvcs'
    37  
    38  ! go build -buildvcs -o sub.exe ./sub
    39  stderr '\Aerror obtaining VCS status: main package is in repository ".*" but current directory is in repository ".*"\n\tUse -buildvcs=false to disable VCS stamping.\n\z'
    40  
    41  cd ./sub
    42  go build -o sub.exe .
    43  go version -m sub.exe
    44  ! stdout '^\tbuild\tvcs'
    45  
    46  ! go build -buildvcs -o sub.exe .
    47  stderr '\Aerror obtaining VCS status: main module is in repository ".*" but current directory is in repository ".*"\n\tUse -buildvcs=false to disable VCS stamping.\n\z'
    48  cd ..
    49  
    50  
    51  # After removing 'git' from $PATH, 'go build -buildvcs' should fail...
    52  
    53  env PATH=
    54  env path=
    55  ! go build -buildvcs -o example.exe .
    56  stderr 'go: missing Git command\. See https://golang\.org/s/gogetcmd$'
    57  
    58  # ...but by default we should omit VCS metadata when the tool is missing.
    59  
    60  go build -o example.exe .
    61  go version -m example.exe
    62  ! stdout '^\tbuild\tvcs'
    63  
    64  # The default behavior can be explicitly set with '-buildvcs=auto'.
    65  
    66  go build -buildvcs=auto -o example.exe .
    67  go version -m example.exe
    68  ! stdout '^\tbuild\tvcs'
    69  
    70  # Other flag values should be rejected with a useful error message.
    71  
    72  ! go build -buildvcs=hg -o example.exe .
    73  stderr '\Ainvalid boolean value "hg" for -buildvcs: value is neither ''auto'' nor a valid bool\nusage: go build .*\nRun ''go help build'' for details.\n\z'
    74  
    75  
    76  -- go.mod --
    77  module example
    78  
    79  go 1.18
    80  -- example.go --
    81  package main
    82  
    83  func main() {}
    84  -- sub/sub.go --
    85  package main
    86  
    87  func main() {}
    88  

View as plain text