@@ -44,7 +44,6 @@ import (
4444
4545// Set via x_defs.
4646var goRootFile = ""
47- var testedModuleName = ""
4847
4948const (
5049 // Standard Bazel exit codes.
@@ -322,23 +321,34 @@ func setupWorkspace(args Args, files []string) (dir string, cleanup func() error
322321
323322 // Copy or link the files for the tested repository.
324323 testedRepoDir := filepath .Join (execDir , "tested_repo" )
324+ singleRepoPrefix := ""
325325 for _ , f := range files {
326- if ! strings .HasPrefix (f , "_main/" ) {
327- return "" , cleanup , fmt .Errorf ("unexpected data file from a non-main repo: %s" , f )
326+ if singleRepoPrefix == "" {
327+ singleRepoPrefix = f [:strings .Index (f , "/" )+ 1 ]
328+ } else if ! strings .HasPrefix (f , singleRepoPrefix ) {
329+ return "" , cleanup , fmt .Errorf ("data files from more than one repo are unsupported, got %s and %s" , singleRepoPrefix , f [:strings .Index (f , "/" )+ 1 ])
328330 }
329331 srcPath , err := runfiles .Rlocation (f )
330332 if err != nil {
331333 return "" , cleanup , fmt .Errorf ("unknown runfile %s: %v" , f , err )
332334 }
333- dstPath := filepath .Join (testedRepoDir , strings .TrimPrefix (f , "_main/" ))
335+ dstPath := filepath .Join (testedRepoDir , strings .TrimPrefix (f , singleRepoPrefix ))
334336 if err := copyOrLink (dstPath , srcPath ); err != nil {
335337 return "" , cleanup , fmt .Errorf ("copying %s to %s: %v" , srcPath , dstPath , err )
336338 }
337339 }
340+ testedRepoModulePath := filepath .Join (testedRepoDir , "MODULE.bazel" )
341+ testedModuleName := ""
342+ if _ , err := os .Stat (testedRepoModulePath ); err == nil {
343+ testedModuleName , err = loadName (testedRepoModulePath )
344+ if err != nil {
345+ return "" , cleanup , fmt .Errorf ("loading module name: %v" , err )
346+ }
347+ }
338348 testedRepoWorkspacePath := filepath .Join (testedRepoDir , "WORKSPACE" )
339349 testedModuleRepoName := testedModuleName
340350 if _ , err = os .Stat (testedRepoWorkspacePath ); err == nil {
341- testedModuleRepoName , err = loadWorkspaceName ( filepath . Join ( testedRepoDir , "WORKSPACE" ) )
351+ testedModuleRepoName , err = loadName ( testedRepoWorkspacePath )
342352 if err != nil {
343353 return "" , cleanup , fmt .Errorf ("loading workspace name: %v" , err )
344354 }
@@ -451,19 +461,19 @@ func extractTxtar(dir, txt string) error {
451461 return nil
452462}
453463
454- func loadWorkspaceName ( workspacePath string ) (string , error ) {
455- workspaceData , err := os .ReadFile (workspacePath )
464+ func loadName ( bazelFilePath string ) (string , error ) {
465+ content , err := os .ReadFile (bazelFilePath )
456466 if err != nil {
457467 return "" , err
458468 }
459- nameRe := regexp .MustCompile (`(?m)^workspace\( \s*name\s*=\s*("[^"]*"|'[^']*')\s*,?\s*\)\s*$` )
460- match := nameRe .FindSubmatchIndex (workspaceData )
469+ nameRe := regexp .MustCompile (`(?m)^(?: \s*|workspace\() name\s*=\s*("[^"]*"|'[^']*')\s*,?\s*\)? \s*$` )
470+ match := nameRe .FindSubmatchIndex (content )
461471 if match == nil {
462- return "" , fmt .Errorf ("%s: workspace name not set" , workspacePath )
472+ return "" , fmt .Errorf ("%s: name not set" , bazelFilePath )
463473 }
464- name := string (workspaceData [match [2 ]+ 1 : match [3 ]- 1 ])
474+ name := string (content [match [2 ]+ 1 : match [3 ]- 1 ])
465475 if name == "" {
466- return "" , fmt .Errorf ("%s: workspace name is empty" , workspacePath )
476+ return "" , fmt .Errorf ("%s: name is empty" , bazelFilePath )
467477 }
468478 return name , nil
469479}
0 commit comments