PageSourceSearch

https://buck2.build/assets/js/955ef9b4.9ef231b8.js

js buck2.build collected 2026-10-03 20:41:25 UTC 8,482 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunkwebsite=globalThis.webpackChunkwebsite||[]).push([[32386],{56064(e,i,n){n.r(i),n.d(i,{assets:()=>c,contentTitle:()=>a,default:()=>h,frontMatter:()=>l,metadata:()=>t,toc:()=>o});const t=JSON.parse('{"id":"concepts/visibility","title":"Visibility","description":"Visibility determines whether a target can reference","source":"@site/../docs/concepts/visibility.md","sourceDirName":"concepts","slug":"/concepts/visibility","permalink":"/docs/concepts/visibility","draft":false,"unlisted":false,"tags":[],"version":"current","frontMatter":{"id":"visibility","title":"Visibility"},"sidebar":"main","previous":{"title":"buck-out","permalink":"/docs/concepts/buck_out"},"next":{"title":"Toolchain","permalink":"/docs/concepts/toolchain"}}');var s=n(74848),r=n(28453);const l={id:"visibility",title:"Visibility"},a=void 0,c={},o=[{value:"Examples",id:"examples",level:2},{value:"Auditing visibility",id:"auditing-visibility",level:2}];function d(e){const i={a:"a",code:"code",em:"em",h2:"h2",li:"li",p:"p",pre:"pre",ul:"ul",...(0,r.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)(i.p,{children:["Visibility determines whether a ",(0,s.jsx)(i.a,{href:"/docs/concepts/glossary#target",children:"target"})," can reference\nanother target in its ",(0,s.jsx)(i.a,{href:"/docs/concepts/glossary#attribute",children:"attributes"}),". In a large project,\nyou may want to prevent developers from 'reaching across' the project and\npulling in additional code. Reducing the visibility of targets can help prevent\nthat type of behavior."]}),"\n",(0,s.jsxs)(i.p,{children:["There are two types of visibility attributes available, each of which takes a\nlist of ",(0,s.jsx)(i.a,{href:"/docs/concepts/glossary#target-pattern",children:"target patterns"}),". (Note: ",(0,s.jsx)(i.code,{children:"visibility"})," and\n",(0,s.jsx)(i.code,{children:"within_view"})," arguments may be defined using\n",(0,s.jsx)(i.a,{href:"../../rule_authors/package_files/#package",children:"package() rules"}),"):"]}),"\n",(0,s.jsxs)(i.ul,{children:["\n",(0,s.jsxs)(i.li,{children:["\n",(0,s.jsxs)(i.p,{children:[(0,s.jsx)(i.code,{children:"visibility"})," - determines which other targets can depend on a target."]}),"\n",(0,s.jsx)(i.p,{children:"This allows for controlling the products/features that may consume your code\nor the clients which your team may choose to support."}),"\n"]}),"\n",(0,s.jsxs)(i.li,{children:["\n",(0,s.jsxs)(i.p,{children:[(0,s.jsx)(i.code,{children:"within_view"})," - determines which other targets a target can depend on."]}),"\n",(0,s.jsxs)(i.p,{children:["On an individual target, this is very similar to ",(0,s.jsx)(i.code,{children:"deps"}),". If any of the\ntarget's ",(0,s.jsx)(i.code,{children:"deps"})," are not ",(0,s.jsx)(i.code,{children:"within_view"}),", the target cannot be built. For this\nreason, on an individual target, ",(0,s.jsx)(i.code,{children:"within_view"})," is less useful since for each\nadditional ",(0,s.jsx)(i.code,{children:"dep"}),", you must consider updating the within_view entries."]}),"\n",(0,s.jsxs)(i.p,{children:["However, the utility of within_view is expanded when used in conjunction with\n",(0,s.jsx)(i.a,{href:"../../rule_authors/package_files/#package",children:"package() rules"})," which allow\ndefining both ",(0,s.jsx)(i.code,{children:"visibility"})," and ",(0,s.jsx)(i.code,{children:"within_view"})," attributes for multiple targets\nin a scalable manner."]}),"\n"]}),"\n"]}),"\n",(0,s.jsx)(i.p,{children:"In general, if a target is not listed, then there may be no dependency\nrelationships as both attributes act as allowlists. However, there are some\nexceptions:"}),"\n",(0,s.jsxs)(i.ul,{children:["\n",(0,s.jsxs)(i.li,{children:[(0,s.jsxs)(i.em,{children:["Empty or Unset ",(0,s.jsx)(i.code,{children:"within_view"})," List"]}),": If the ",(0,s.jsx)(i.code,{children:"within_view"})," list is empty or\nunset, it does not impose any restrictions on which targets the current target\ncan depend on."]}),"\n",(0,s.jsxs)(i.li,{children:[(0,s.jsxs)(i.em,{children:["Empty or Unset ",(0,s.jsx)(i.co
1de,{children:"visibility"})," List"]}),": If the ",(0,s.jsx)(i.code,{children:"visibility"})," list is empty or\nunset, then only targets defined in the same\n",(0,s.jsx)(i.a,{href:"/docs/concepts/glossary#buck-file",children:"BUCK file"})," can depend upon the current target."]}),"\n",(0,s.jsxs)(i.li,{children:[(0,s.jsxs)(i.em,{children:["Special Value: ",(0,s.jsx)(i.code,{children:"'PUBLIC'"})]}),": ",(0,s.jsx)(i.code,{children:"visibility"})," can be set to a special value\n",(0,s.jsx)(i.code,{children:"'PUBLIC'"})," which makes a build rule visible to all targets. (Example below)"]}),"\n"]}),"\n",(0,s.jsxs)(i.p,{children:["In case of logically-conflicting lists, ",(0,s.jsx)(i.code,{children:"within_view"})," takes precedence over\n",(0,s.jsx)(i.code,{children:"visibility"}),". If ",(0,s.jsx)(i.code,{children:"//foo:bar"})," defines ",(0,s.jsx)(i.code,{children:"//hello:world"})," in its ",(0,s.jsx)(i.code,{children:"visibility"})," list,\nbut ",(0,s.jsx)(i.code,{children:"//hello:world"})," does not define ",(0,s.jsx)(i.code,{children:"//foo:bar"})," in its ",(0,s.jsx)(i.code,{children:"within_view"})," list, then\n",(0,s.jsx)(i.code,{children:"//hello:world"})," may not depend on ",(0,s.jsx)(i.code,{children:"//foo:bar"}),"."]}),"\n",(0,s.jsx)(i.h2,{id:"examples",children:"Examples"}),"\n",(0,s.jsx)(i.p,{children:"A common library like Guava should be able to be included by any build rule:"}),"\n",(0,s.jsx)(i.pre,{children:(0,s.jsx)(i.code,{className:"language-python",children:"prebuilt_jar(\n  name = 'guava',\n  binary_jar = 'guava-14.0.1.jar',\n  visibility = ['PUBLIC']\n)\n"})}),"\n",(0,s.jsx)(i.p,{children:"It is common to restrict the visibility of Android resources to the Java code\nthat uses it:"}),"\n",(0,s.jsx)(i.pre,{children:(0,s.jsx)(i.code,{className:"language-python",children:"android_resource(\n  name = 'ui_res',\n  res = 'res',\n  package = 'com.example',\n  visibility = ['//java/com/example/ui:ui']\n)\n"})}),"\n",(0,s.jsxs)(i.p,{children:["Or it may be simpler to make it visible to the entire directory in case\nadditional build rules are added to ",(0,s.jsx)(i.code,{children:"java/com/example/ui/BUCK"}),":"]}),"\n",(0,s.jsx)(i.pre,{children:(0,s.jsx)(i.code,{className:"language-python",children:"android_resource(\n  name = 'ui_res',\n  res = 'res',\n  package = 'com.example',\n  visibility = ['//java/com/example/ui:']\n)\n"})}),"\n",(0,s.jsxs)(i.p,{children:["Also, it is common to limit code for testing to be visible only to tests. If you\ndefine all of your Java unit tests in a folder named ",(0,s.jsx)(i.code,{children:"javatests/"})," in the root of\nyour project, then you could define the following rule to ensure that only build\nrules under ",(0,s.jsx)(i.code,{children:"javatests/"})," can depend on JUnit:"]}),"\n",(0,s.jsx)(i.pre,{children:(0,s.jsx)(i.code,{className:"language-python",children:"prebuilt_jar(\n  name = 'junit',\n  binary_jar = 'junit-4.11.jar',\n  visibility = ['//javatests/...']\n)\n"})}),"\n",(0,s.jsx)(i.p,{children:"Finally, restricting the view of a target can be useful for preventing\ndependency creep:"}),"\n",(0,s.jsx)(i.pre,{children:(0,s.jsx)(i.code,{className:"language-python",children:"java_library(\n  name = 'example',\n  visibility = ['PUBLIC',],\n  within_view = ['//foo:bar','//hello:world']\n)\n"})}),"\n",(0,s.jsx)(i.h2,{id:"auditing-visibility",children:"Auditing visibility"}),"\n",(0,s.jsxs)(i.p,{children:["Use ",(0,s.jsx)(i.a,{href:"../../users/commands/audit#buck2-audit-visibility",children:(0,s.jsx)(i.code,{children:"buck2 audit visibility"})})," to verify that no ",(0,s.jsx)(i.code,{children:"visibility"})," constraints\nare violated in the transitive dependency graph of the given targets."]}),"\n",(0,s.jsx)(i.pre,{children:(0,s.jsx)(i.code,{className:"language-sh",children:"buck2 audit visibility //myproject/...\n"})})]})}function h(e={}){const{wrapper:i}={...(0,r.R)(),...e.components};return i?(0,s.jsx)(i,{...e,children:(0,s.jsx)(d,{...e})}):d(e)}},28453(e,i,n){n.d(i,{R:()=>l,x:()=>a});var t=n(96540);const s={},r=t.createContext(s);function l(e){const i=t.useContext(r);return t.useMemo(function(){return"function"==typeof e?e(i):{...i,...e}},[i,e])}function a(e){let i;return i=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:l(e.components),t.createElement(r.Provider,{value:i},e.children)}}}]);

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.