Skip to content

Commit

Permalink
Detect non-js upstreams (uber-web#97)
Browse files Browse the repository at this point in the history
* detect non-js upstreams

* lint

* flow nags

* comment
  • Loading branch information
Leo Horie authored Jun 9, 2022
1 parent d8bdbb6 commit dc1a98c
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 15 deletions.
3 changes: 2 additions & 1 deletion tests/fixtures/find-changed-targets/bazel/changes.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
b/package.json
non-jazelle/BUILD.bazel
non-jazelle/BUILD.bazel
non-js/x.txt
11 changes: 11 additions & 0 deletions tests/fixtures/find-changed-targets/bazel/d/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package(default_visibility = ["//visibility:public"])

load("@jazelle//:build-rules.bzl", "web_library")

web_library(
name = "library",
deps = [
"//non-js:x"
],
srcs = [],
)
4 changes: 4 additions & 0 deletions tests/fixtures/find-changed-targets/bazel/d/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "d",
"version": "0.0.0"
}
9 changes: 9 additions & 0 deletions tests/fixtures/find-changed-targets/bazel/non-js/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
filegroup(
name = "x",
srcs = ["x.txt"],
)

filegroup(
name = "y",
srcs = ["y.txt"],
)
Empty file.
Empty file.
3 changes: 2 additions & 1 deletion tests/fixtures/find-changed-targets/bazel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"workspaces": [
"a",
"b",
"c"
"c",
"d"
]
}
11 changes: 2 additions & 9 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,15 +744,7 @@ async function testFindChangedTargets() {
const files = `${tmp}/tmp/find-changed-targets/bazel/changes.txt`;
await install({
root: `${tmp}/tmp/find-changed-targets/bazel`,
cwd: `${tmp}/tmp/find-changed-targets/bazel/a`,
});
await install({
root: `${tmp}/tmp/find-changed-targets/bazel`,
cwd: `${tmp}/tmp/find-changed-targets/bazel/b`,
});
await install({
root: `${tmp}/tmp/find-changed-targets/bazel`,
cwd: `${tmp}/tmp/find-changed-targets/bazel/c`,
cwd: `${tmp}/tmp/find-changed-targets/bazel`,
});
const targets = await findChangedTargets({root, files, format: 'targets'});
assert.deepEqual(
Expand All @@ -770,6 +762,7 @@ async function testFindChangedTargets() {
'//b:library',
'//b:lint',
'//b:test',
'//d:library', // this should appear here because it depends on non-js/x.txt
].sort()
);
}
Expand Down
18 changes: 14 additions & 4 deletions utils/find-changed-targets.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const findChangedBazelTargets = async ({root, files}) => {
batches(queryables, 1000), // batching required, else E2BIG errors
async q => {
const innerQuery = q.join(' union ');
const cmd = `${bazel} query 'let graph = kind("(web_.*|.*_test) rule", rdeps("...", ${innerQuery})) in $graph except filter("node_modules", $graph)' --output label`;
const cmd = `${bazel} query 'let graph = kind("(web_.*|.*_test|filegroup) rule", rdeps("...", ${innerQuery})) in $graph except filter("node_modules", $graph)' --output label`;
return exec(cmd, opts);
}
);
Expand Down Expand Up @@ -193,15 +193,25 @@ async function batch(root, items, fn) {
].filter(Boolean);
}

// for each folder, we typically only need to check one file,
// Optimization: For each folder, we typically only need to check one file,
// since all files will generally map to the same target
// given how jazelle generates BUILD.bazel files
// However, this is only true of js files
// For other types of targets, we need to be conservative and keep the entire list of files
const getTargetRepresentatives = files => {
const map = new Map();
for (const file of files) {
map.set(dirname(file), file);
const dir = dirname(file);
const list = map.get(dir) || map.set(dir, []).get(dir);
if (file.match(/(.jsx?|.tsx?)$/)) {
map.set(dir, [file]);
} else {
// $FlowFixMe
list.push(file);
}
}
return [...map.values()];
// $FlowFixMe
return [...map.values()].flat();
};

module.exports = {findChangedTargets};

0 comments on commit dc1a98c

Please sign in to comment.