1"use strict";(self.webpackChunkopa_website=self.webpackChunkopa_website||[]).push([[19238],{28453:(e,r,l)=>{l.d(r,{R:()=>i,x:()=>s});var t=l(96540);const n={},o=t.createContext(n);function i(e){const r=t.useContext(o);return t.useMemo((function(){return"function"==typeof e?e(r):{...r,...e}}),[r,e])}function s(e){let r;return r=e.disableParentContext?"function"==typeof e.components?e.components(n):e.components||n:i(e.components),t.createElement(o.Provider,{value:r},e.children)}},41025:(e,r,l)=>{l.r(r),l.d(r,{assets:()=>a,contentTitle:()=>s,default:()=>c,frontMatter:()=>i,metadata:()=>t,toc:()=>d});const t=JSON.parse('{"id":"errors/rego-type-error/multiple-default-rules","title":"rego_type_error: multiple default rules {name} found","description":"This error is emitted by the type checker during the compilation stage when multiple default definitions are found","source":"@site/docs/errors/rego-type-error/multiple-default-rules.md","sourceDirName":"errors/rego-type-error","slug":"/errors/rego-type-error/multiple-default-rules","permalink":"/docs/errors/rego-type-error/multiple-default-rules","draft":false,"unlisted":false,"tags":[],"version":"current","frontMatter":{"sidebar_label":"multiple default rules","image":"/img/opa-errors.png"},"sidebar":"docsSidebar","previous":{"title":"multiple default rules {name} found","permalink":"/docs/errors/rego-type-error/multiple-default-rules-name-found"},"next":{"title":"unsafe built-in function calls in expression: {name}","permalink":"/docs/errors/rego-type-error/unsafe-built-in-function-calls-in-expression-name"}}');var n=l(74848),o=l(28453);const i={sidebar_label:"multiple default rules",image:"/img/opa-errors.png"},s="rego_type_error: multiple default rules {name} found",a={},d=[{value:"Examples",id:"examples",level:2},{value:"How To Fix It",id:"how-to-fix-it",level:2}];function u(e){const r={admonition:"admonition",code:"code",h1:"h1",h2:"h2",header:"header",p:"p",pre:"pre",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",...(0,o.R)(),...e.components};return(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)(r.header,{children:(0,n.jsxs)(r.h1,{id:"rego_type_error-multiple-default-rules-name-found",children:[(0,n.jsx)(r.code,{children:"rego_type_error"}),": multiple default rules ",(0,n.jsx)(r.code,{children:"{name}"})," found"]})}),"\n",(0,n.jsxs)(r.p,{children:["This error is emitted by the type checker during the compilation stage when multiple ",(0,n.jsx)(r.code,{children:"default"})," definitions are found\nfor one rule. The error message will show the rule e.g. ",(0,n.jsx)(r.code,{children:"data.example.allow"})," that has multiple definitions, but the line\nnumber for the error will only show the first location."]}),"\n",(0,n.jsxs)(r.table,{children:[(0,n.jsx)(r.thead,{children:(0,n.jsxs)(r.tr,{children:[(0,n.jsx)(r.th,{children:"Stage"}),(0,n.jsx)(r.th,{children:"Category"}),(0,n.jsx)(r.th,{children:"Message"})]})}),(0,n.jsx)(r.tbody,{children:(0,n.jsxs)(r.tr,{children:[(0,n.jsx)(r.td,{children:(0,n.jsx)(r.code,{children:"compilation"})}),(0,n.jsx)(r.td,{children:(0,n.jsx)(r.code,{children:"rego_type_error"})}),(0,n.jsx)(r.td,{children:(0,n.jsx)(r.code,{children:"multiple default rules <rule> found"})})]})})]}),"\n",(0,n.jsx)(r.h2,{id:"examples",children:"Examples"}),"\n",(0,n.jsxs)(r.p,{children:["In the example below, the default value for the ",(0,n.jsx)(r.code,{children:"allow"})," rule is defined twice:"]}),"\n",(0,n.jsx)(r.pre,{children:(0,n.jsx)(r.code,{className:"language-rego",children:"package example\n\ndefault allow := false\n\nallow if input.admin\n\ndefault allow := false\n"})}),"\n",(0,n.jsx)(r.p,{children:"This will result in the following error:"}),"\n",(0,n.jsx)(r.pre,{children:(0,n.jsx)(r.code,{className:"language-txt",children:"1 error occurred: policy.rego:5: rego_type_error: multiple default rules data.example.allow found\n"})}),"\n",(0,n.jsx)(r.p,{children:"In this example, it's relatively easy to spot the issue, but in larger policies, it can be more challenging to find the\nduplicate definitions. It's also possible that the default is defined in a different Rego file. For example:"}),"\n",(0,n.jsx)(r.pre,{children:(0,n.jsx)(r.code,{className:"language-rego",children:"# example1.rego\npackage example\n\ndefault allow := false\n"})}),"\n",(0,n.jsx)(r.pre,{children:(0,n.jsx)(r.code,{className:"language-rego",children:"# example2.rego\npackage example\n\ndefault allow := true\n"})}),"\n",(0,n.jsx)(r.p,{children:"Using the two files produces an error like this:"}),"\n",(0,n.jsx)(r.pre,{children:(0,n.jsx)(r.code,{className:"language-shell",children:"$ opa eval data.example.allow -d example1.rego -d example2.rego\n1 error occurred: example1.rego:3: rego_type_error: multiple default rules data.example.allow foun
1d\n"})}),"\n",(0,n.jsx)(r.admonition,{type:"info",children:(0,n.jsx)(r.p,{children:"OPA will load files in lexicographical order, so temporarily renaming the file in the error message can be used to\nsearch for the other definitions."})}),"\n",(0,n.jsx)(r.h2,{id:"how-to-fix-it",children:"How To Fix It"}),"\n",(0,n.jsxs)(r.p,{children:["Fixing this error is usually straightforward: remove one of the repeated ",(0,n.jsx)(r.code,{children:"default"})," definitions if they are setting the same\ndefault value. If the intention is to set different default values, this is trickier, but perhaps you'd be better served\nby two different rules."]})]})}function c(e={}){const{wrapper:r}={...(0,o.R)(),...e.components};return r?(0,n.jsx)(r,{...e,children:(0,n.jsx)(u,{...e})}):u(e)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.