[FR]: Clean source in js_run_binary to prevent conflict
What is the current behavior?
js_run_binary has copy_srcs_to_bin action, this action only copy new/modified files, but do not remove deleted files.
For example, I have a.ts in a new branch, after I use js_run_binary to compile my source code, a.ts will be copied into bazel-bin directory. Later I checkout my master branch, the a.ts still exists in bazel-bin directory, following compiling may be failure cause a.ts is not compatible with master branch code.
Follow up the problem, I tried to use genrule to delete all the source code in bazel-bin, then I found js_run_binary will not copy all source code to bazel-bin again. I guess it because copy_src_to_bin use action to declare every source code, when the source code has no change, bazel treat it as cached output in analysis stage, but remove action in genrule effects in process stage.
Describe the feature
I hope there is some way to remove deleted files in bazel-bin, in bazel way.
This is a Bazel limitation. Nothing ever deletes from bazel-bin. Only bazel clean will remove stale output files which no rule produces anymore.
However, no Bazel actions should be reading those files anymore. In your example, a.ts is still declared as an input to something? Or it's non-hermetic and an action is discovering files in bazel-out that it wasn't told about? The latter case sounds like a bug in some rule which is being non-hermetic.
I ran into a similar issue but could track it down to using patch_node_fs = False, which then enables some tools (Vite in my case) to break out of the sandbox and starting to see those stale files.
I am running into a similar issue.
In my case I'm using vite, run via js_run_binary, the default values for patch_node_fs and copy_srcs_to_bin and the scenario that is causing us issues is:
- Have
file-1.ts, depend on it from another ts file and build the target - Delete
file-1.tsand createfile-1/index.tswith a diff set of exports and build the target
For us, vite is using file-1.ts instead of file-1/index.ts.
Our problem ended up getting resolved by using process.cwd() in our vite.config.ts to set up aliases.
See https://github.com/bazelbuild/examples/issues/614 for an explanation
@gauntface thanks for posting the workaround, I'll try upstreaming that fix!