@@ -37,37 +37,63 @@ func FromGoMod(mod *modfile.File, prof *profile.Profile) error {
3737 }
3838 }
3939
40- for _ , f := range mod .Require {
41- if syn := f .Syntax ; syn != nil {
42- pol := currentDefaultPolicy
43- for _ , c := range append (syn .Before , syn .Suffix ... ) {
40+ for _ , stmt := range mod .Syntax .Stmt {
41+ lineBlock , ok := stmt .(* modfile.LineBlock )
42+ if ! ok {
43+ continue
44+ }
45+ currentBlockDefaultPolicy := currentDefaultPolicy
46+ for _ , c := range append (lineBlock .Before , lineBlock .Suffix ... ) {
47+ if tok := c .Token ; tok != "" {
48+ pol , err := policyFromComment (tok )
49+ if err != nil {
50+ err = fmt .Errorf ("failed to parse comment %+v: %w" , c , err )
51+ return err
52+ }
53+ if lineBlock .Token [0 ] != "require" {
54+ return fmt .Errorf ("policy %q is specified in a non-require block" , pol )
55+ }
56+ currentBlockDefaultPolicy = pol
57+ }
58+ }
59+ for _ , line := range lineBlock .Line {
60+ if line == nil {
61+ continue
62+ }
63+ pol := currentBlockDefaultPolicy
64+ for _ , c := range append (line .Before , line .Suffix ... ) {
4465 if tok := c .Token ; tok != "" {
4566 polFromComment , err := policyFromComment (tok )
4667 if err != nil {
4768 err = fmt .Errorf ("failed to parse comment %+v: %w" , c , err )
4869 return err
4970 }
71+ if lineBlock .Token [0 ] != "require" {
72+ return fmt .Errorf ("policy %q is specified in a non-require block" , polFromComment )
73+ }
5074 if polFromComment != "" {
5175 pol = polFromComment
5276 }
5377 }
5478 }
5579 if pol == "" {
56- pol = currentDefaultPolicy
80+ pol = currentBlockDefaultPolicy
5781 }
5882 if pol == profile .PolicyUnconfined {
5983 pol = "" // reduce map size
6084 }
61- if existPol , ok := prof .Modules [f .Mod .Path ]; ok && existPol != pol {
62- slog .Warn ("Overwriting an existing policy" , "module" , f .Mod .Path , "old" , existPol , "new" , pol )
85+ modPath := line .Token [0 ]
86+ if existPol , ok := prof .Modules [modPath ]; ok && existPol != pol {
87+ slog .Warn ("Overwriting an existing policy" , "module" , modPath , "old" , existPol , "new" , pol )
6388 }
6489 if pol == "" {
65- delete (prof .Modules , f . Mod . Path )
90+ delete (prof .Modules , modPath )
6691 } else {
67- prof .Modules [f . Mod . Path ] = pol
92+ prof .Modules [modPath ] = pol
6893 }
6994 }
7095 }
96+
7197 return nil
7298}
7399
0 commit comments