In this article, we will see how to solve "kustomize error: accumulating resources: accumulation err must be a directory so that it can be used as a build root" error which usually occurs when you try to deploy some application, services, patches etc in Kubernetes cluster using kustomize tool. Kustomize is basically a free and open source Kubernetes native configuration management tool frequently used in deploying and managing the configuration in Kubernetes cluster. It is famously used to deploy configuration in multiple environments using same bases and overlays file structure without any need to create separate bases and overlays file structure for every environments. You can check more about this tool on official website.
I personally uses this tool a lot for configuration management so I always get some kind of error. This time I am getting an error where kustomize is failing to detect the path of a file which is already there but I am still unable to deploy that manifest file. I am sure you must be also facing same kind of problem. So here, I am going to explain you how you can quickly fix this error by showing an example from my use case.

Solved "kustomize error: accumulating resources: accumulation err must be a directory so that it can be used as a build root"
Also Read: Solved "New pods keep on launching in kubernetes even though the replica is set to 1"
Before deep diving into my issue, I would like to show the problematic file tools.yaml is currently present under bases path but is not getting detected by Kustomize tool when I try to deploy this.
kustomize | |---bases | |-- web/cyberithub/example_app | |-- web/cyberithub/component/tools.yaml | |---overlays |-- web/cyberithub/example_app/kustomization.yaml |-- web/cyberithub/component
Here is the structure of kustomization.yaml file present under kustomize/overlays/web/cyberithub/example_app path.
---
apiVersion: kustomize.config..k8s.io/v1beta1
kind: Kustomization
namespace: cyber-test-env
resources:
- ../../../bases/web/cyberithub/component/tools.yaml
When I was trying to build and deploy my application in kubernetes cluster using below kustomize command then I noticed "error: accumulating resources: accumulation err must be a directory so that it can be used as a build root" error on output.
cyberithub@macos1066 % kustomize build --enable-alpha-plugins --enable-exec kustomize/overlays/web/cyberithub/example_app Error: accumulating resources: accumulation err='accumulating resources from '../../../bases/web/cyberithub/component/tools.yaml':security; file '/Users/cyberithub/Desktop/testApp/kustomize/bases/web/cyberithub/component/tools.yaml' is not in or below /Users/cyberithub/Desktop/testApp/kustomize/overlays/web/cyberithub/example_app'': '/Users/cyberithub/Desktop/testApp/kustomize/bases/web/cyberithub/component/tools.yaml' must be a directory so that it can be used as a build root
As visible from above error message, this is occurring because kustomize trying to load the files from outside Kustomization root directory. Without this option, by default kustomize would load all configuration from internal Kustomization root directory path. But in case, if you have some files present outside the designated path then you will get this error. So to fix this error, we need to allow kustomize to load files from outside the current Kustomization root directory path by using --load-restrictor LoadRestrictionsNone option as shown below.
cyberithub@macos1066 % kustomize build --enable-alpha-plugins --enable-exec --load-restrictor LoadRestrictionsNone kustomize/overlays/web/cyberithub/example_app
In my case, above command resulted in deployment of all files without any further error. This confirms that issue is fixed now. Sometimes in few cases even if the file is located under internal Kustomization root directory path, during deployment it fails with same error. In those cases also, you have to use the same --load-restrictor LoadRestrictionsNone option to fix the issue. But it is important to understand here that this is not a regular option to use. This is simply because it reduces the portability of your configuration and might expose critical files to unregular build configuration. It is only to be used when it is required to include local files present under outside Kustomization root directory path.
Hope this makes sense and helps you solving your issue as well. Please let me know in comment box if it does not help and you are still getting the same error. We will work together in solving your issue as well.