@@ -214,9 +214,13 @@ func testFlags(args []string) (packageNames, passToTest []string) {
214214
215215 explicitArgs := make ([]string , 0 , len (args ))
216216 inPkgList := false
217+ afterFlagWithoutValue := false
217218 for len (args ) > 0 {
218219 f , remainingArgs , err := cmdflag .ParseOne (& CmdTest .Flag , args )
219220
221+ wasAfterFlagWithoutValue := afterFlagWithoutValue
222+ afterFlagWithoutValue = false // provisionally
223+
220224 if errors .Is (err , flag .ErrHelp ) {
221225 exitWithUsage ()
222226 }
@@ -233,10 +237,24 @@ func testFlags(args []string) (packageNames, passToTest []string) {
233237 if nf := (cmdflag.NonFlagError {}); errors .As (err , & nf ) {
234238 if ! inPkgList && packageNames != nil {
235239 // We already saw the package list previously, and this argument is not
236- // a flag, so it — and everything after it — must be a literal argument
237- // to the test binary.
238- explicitArgs = append (explicitArgs , args ... )
239- break
240+ // a flag, so it — and everything after it — must be either a value for
241+ // a preceding flag or a literal argument to the test binary.
242+ if wasAfterFlagWithoutValue {
243+ // This argument could syntactically be a flag value, so
244+ // optimistically assume that it is and keep looking for go command
245+ // flags after it.
246+ //
247+ // (If we're wrong, we'll at least be consistent with historical
248+ // behavior; see https://golang.org/issue/40763.)
249+ explicitArgs = append (explicitArgs , nf .RawArg )
250+ args = remainingArgs
251+ continue
252+ } else {
253+ // This argument syntactically cannot be a flag value, so it must be a
254+ // positional argument, and so must everything after it.
255+ explicitArgs = append (explicitArgs , args ... )
256+ break
257+ }
240258 }
241259
242260 inPkgList = true
@@ -272,6 +290,9 @@ func testFlags(args []string) (packageNames, passToTest []string) {
272290
273291 explicitArgs = append (explicitArgs , nd .RawArg )
274292 args = remainingArgs
293+ if ! nd .HasValue {
294+ afterFlagWithoutValue = true
295+ }
275296 continue
276297 }
277298
0 commit comments