fix: bun monorepo complex deps
This commit is contained in:
@@ -96,6 +96,53 @@ def _workspace_patterns(repository_ctx, package_json):
|
||||
|
||||
return patterns
|
||||
|
||||
def _validate_catalog_shape(field, value):
|
||||
if value == None:
|
||||
return
|
||||
|
||||
if type(value) != type({}):
|
||||
fail("bun_install: `{}` must be an object".format(field))
|
||||
|
||||
if field not in ["catalogs", "workspaces.catalogs"]:
|
||||
return
|
||||
|
||||
for name, catalog in value.items():
|
||||
if type(name) != type(""):
|
||||
fail("bun_install: `catalogs` keys must be strings, got {}".format(type(name)))
|
||||
if type(catalog) != type({}):
|
||||
fail("bun_install: `catalogs.{}` must be an object".format(name))
|
||||
|
||||
def _copy_json_value(value):
|
||||
return json.decode(json.encode(value))
|
||||
|
||||
def _normalized_root_manifest(repository_ctx, package_json):
|
||||
manifest = json.decode(repository_ctx.read(package_json))
|
||||
workspaces = manifest.get("workspaces")
|
||||
|
||||
for field in ["catalog", "catalogs"]:
|
||||
manifest_value = manifest.get(field)
|
||||
_validate_catalog_shape(field, manifest_value)
|
||||
|
||||
if type(workspaces) != type({}):
|
||||
continue
|
||||
|
||||
workspace_value = workspaces.get(field)
|
||||
_validate_catalog_shape("workspaces.{}".format(field), workspace_value)
|
||||
|
||||
if workspace_value == None:
|
||||
continue
|
||||
|
||||
if manifest_value == None:
|
||||
manifest[field] = _copy_json_value(workspace_value)
|
||||
continue
|
||||
|
||||
if manifest_value != workspace_value:
|
||||
fail(
|
||||
"bun_install: `{}` conflicts with `workspaces.{}`; use one source of truth or keep both values identical".format(field, field),
|
||||
)
|
||||
|
||||
return json.encode(manifest)
|
||||
|
||||
def _materialize_workspace_packages(repository_ctx, package_json):
|
||||
package_root = package_json.dirname
|
||||
package_root_str = str(package_root)
|
||||
@@ -187,7 +234,7 @@ def _bun_install_repository_impl(repository_ctx):
|
||||
if lockfile_name not in ["bun.lock", "bun.lockb"]:
|
||||
lockfile_name = "bun.lock"
|
||||
|
||||
repository_ctx.file("package.json", repository_ctx.read(package_json))
|
||||
repository_ctx.file("package.json", _normalized_root_manifest(repository_ctx, package_json))
|
||||
repository_ctx.symlink(bun_lockfile, lockfile_name)
|
||||
_materialize_install_inputs(repository_ctx, package_json)
|
||||
_materialize_workspace_packages(repository_ctx, package_json)
|
||||
|
||||
@@ -66,12 +66,123 @@ runtime_package_dir="${{runtime_workspace}}/${{package_rel_dir}}"
|
||||
mkdir -p "${{runtime_package_dir}}"
|
||||
cp -RL "${{package_dir}}/." "${{runtime_package_dir}}/"
|
||||
|
||||
workspace_package_map="${{runtime_workspace}}/workspace-packages.tsv"
|
||||
python3 - "${{runtime_package_dir}}" >"${{workspace_package_map}}" <<'PY'
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
root = sys.argv[1]
|
||||
|
||||
for dirpath, dirnames, filenames in os.walk(root):
|
||||
dirnames[:] = [name for name in dirnames if name != "node_modules"]
|
||||
if "package.json" not in filenames:
|
||||
continue
|
||||
|
||||
manifest_path = os.path.join(dirpath, "package.json")
|
||||
try:
|
||||
with open(manifest_path, "r", encoding="utf-8") as manifest_file:
|
||||
package_name = json.load(manifest_file).get("name")
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
if isinstance(package_name, str):
|
||||
print(f"{{package_name}}\t{{dirpath}}")
|
||||
PY
|
||||
|
||||
install_repo_root=""
|
||||
if [[ -n "${{primary_node_modules}}" ]]; then
|
||||
install_repo_root="$(dirname "${{primary_node_modules}}")"
|
||||
ln -s "${{primary_node_modules}}" "${{runtime_workspace}}/node_modules"
|
||||
fi
|
||||
|
||||
workspace_package_dir_for_source() {{
|
||||
local source="$1"
|
||||
local manifest_path="${{source}}/package.json"
|
||||
local package_name=""
|
||||
local workspace_dir=""
|
||||
|
||||
if [[ ! -f "${{manifest_path}}" ]]; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
package_name="$(python3 - "${{manifest_path}}" <<'PY'
|
||||
import json
|
||||
import sys
|
||||
|
||||
try:
|
||||
with open(sys.argv[1], "r", encoding="utf-8") as manifest_file:
|
||||
package_name = json.load(manifest_file).get("name", "")
|
||||
except Exception:
|
||||
package_name = ""
|
||||
|
||||
if isinstance(package_name, str):
|
||||
print(package_name)
|
||||
PY
|
||||
)"
|
||||
|
||||
workspace_dir="$(awk -F '\t' -v name="$package_name" '$1 == name {{ print $2; exit }}' "${{workspace_package_map}}")"
|
||||
if [[ -n "${{package_name}}" && -n "${{workspace_dir}}" ]]; then
|
||||
echo "${{workspace_dir}}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
return 1
|
||||
}}
|
||||
|
||||
link_node_modules_entry() {{
|
||||
local source="$1"
|
||||
local destination="$2"
|
||||
local workspace_target=""
|
||||
|
||||
rm -rf "${{destination}}"
|
||||
workspace_target="$(workspace_package_dir_for_source "${{source}}" || true)"
|
||||
if [[ -n "${{workspace_target}}" ]]; then
|
||||
ln -s "${{workspace_target}}" "${{destination}}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [[ -L "${{source}}" ]]; then
|
||||
ln -s "$(readlink "${{source}}")" "${{destination}}"
|
||||
else
|
||||
ln -s "${{source}}" "${{destination}}"
|
||||
fi
|
||||
}}
|
||||
|
||||
mirror_node_modules_dir() {{
|
||||
local source_dir="$1"
|
||||
local destination_dir="$2"
|
||||
local entry=""
|
||||
local scoped_entry=""
|
||||
|
||||
rm -rf "${{destination_dir}}"
|
||||
mkdir -p "${{destination_dir}}"
|
||||
|
||||
shopt -s dotglob nullglob
|
||||
for entry in "${{source_dir}}"/* "${{source_dir}}"/.[!.]* "${{source_dir}}"/..?*; do
|
||||
local entry_name="$(basename "${{entry}}")"
|
||||
if [[ "${{entry_name}}" == "." || "${{entry_name}}" == ".." ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
if [[ -d "${{entry}}" && ! -L "${{entry}}" && "${{entry_name}}" == @* ]]; then
|
||||
mkdir -p "${{destination_dir}}/${{entry_name}}"
|
||||
for scoped_entry in "${{entry}}"/* "${{entry}}"/.[!.]* "${{entry}}"/..?*; do
|
||||
local scoped_name="$(basename "${{scoped_entry}}")"
|
||||
if [[ "${{scoped_name}}" == "." || "${{scoped_name}}" == ".." ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
link_node_modules_entry "${{scoped_entry}}" "${{destination_dir}}/${{entry_name}}/${{scoped_name}}"
|
||||
done
|
||||
continue
|
||||
fi
|
||||
|
||||
link_node_modules_entry "${{entry}}" "${{destination_dir}}/${{entry_name}}"
|
||||
done
|
||||
shopt -u dotglob nullglob
|
||||
}}
|
||||
|
||||
find_node_modules() {{
|
||||
local dir="$1"
|
||||
local root="$2"
|
||||
@@ -118,22 +229,39 @@ find_install_repo_node_modules() {{
|
||||
return 1
|
||||
}}
|
||||
|
||||
mirror_install_repo_workspace_node_modules() {{
|
||||
local repo_root="$1"
|
||||
local destination_root="$2"
|
||||
|
||||
while IFS= read -r install_node_modules; do
|
||||
local rel_path="${{install_node_modules#${{repo_root}}/}}"
|
||||
local destination="${{destination_root}}/${{rel_path}}"
|
||||
|
||||
mkdir -p "$(dirname "${{destination}}")"
|
||||
mirror_node_modules_dir "${{install_node_modules}}" "${{destination}}"
|
||||
done < <(find "${{repo_root}}" \
|
||||
-path "${{repo_root}}/node_modules" -prune -o \
|
||||
-type d -name node_modules -print 2>/dev/null | sort)
|
||||
}}
|
||||
|
||||
resolved_install_node_modules=""
|
||||
if [[ -n "${{install_repo_root}}" ]]; then
|
||||
resolved_install_node_modules="$(find_install_repo_node_modules "${{install_repo_root}}" "${{package_rel_dir}}" || true)"
|
||||
fi
|
||||
|
||||
if [[ -n "${{resolved_install_node_modules}}" ]]; then
|
||||
rm -rf "${{runtime_package_dir}}/node_modules"
|
||||
ln -s "${{resolved_install_node_modules}}" "${{runtime_package_dir}}/node_modules"
|
||||
mirror_node_modules_dir "${{resolved_install_node_modules}}" "${{runtime_package_dir}}/node_modules"
|
||||
else
|
||||
resolved_node_modules="$(find_node_modules "${{runtime_package_dir}}" "${{runtime_workspace}}" || true)"
|
||||
if [[ -n "${{resolved_node_modules}}" && "${{resolved_node_modules}}" != "${{runtime_package_dir}}/node_modules" ]]; then
|
||||
rm -rf "${{runtime_package_dir}}/node_modules"
|
||||
ln -s "${{resolved_node_modules}}" "${{runtime_package_dir}}/node_modules"
|
||||
mirror_node_modules_dir "${{resolved_node_modules}}" "${{runtime_package_dir}}/node_modules"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ -n "${{install_repo_root}}" ]]; then
|
||||
mirror_install_repo_workspace_node_modules "${{install_repo_root}}" "${{runtime_package_dir}}"
|
||||
fi
|
||||
|
||||
path_entries=()
|
||||
if [[ -d "${{runtime_package_dir}}/node_modules/.bin" ]]; then
|
||||
path_entries+=("${{runtime_package_dir}}/node_modules/.bin")
|
||||
@@ -216,4 +344,4 @@ declared in `package.json` and expect to run from the package directory with
|
||||
},
|
||||
executable = True,
|
||||
toolchains = ["//bun:toolchain_type"],
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user