Skip to content

Commit 17a0001

Browse files
committed
refactor: simplify loop over first-party links in importers
1 parent 6de1caf commit 17a0001

File tree

1 file changed

+53
-62
lines changed

1 file changed

+53
-62
lines changed

npm/private/npm_translate_lock_generate.bzl

Lines changed: 53 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -155,71 +155,62 @@ sh_binary(
155155
importer_deps_map[link_package] = pkg_deps
156156

157157
# Look for first-party links in importers
158-
for import_path, importer in importers.items():
159-
link_package = helpers.link_package(root_package, import_path)
160-
161-
prod_deps = importer.get("deps", {})
162-
all_deps = importer.get("all_deps", {})
163-
164-
dev_deps = {}
165-
for dep_name, dep_version in all_deps.items():
166-
if dep_name not in prod_deps:
167-
dev_deps[dep_name] = dep_version
168-
169-
for deps_type, deps in [("link_packages", prod_deps), ("link_dev_packages", dev_deps)]:
170-
for dep_package, dep_version in deps.items():
171-
if dep_version.startswith("file:"):
172-
dep_key = "{}+{}".format(dep_package, dep_version)
173-
if not dep_key in fp_links.keys():
174-
# Ignore file: dependencies on packages such as file: tarballs
175-
# TODO(3.0): remove with pnpm <v9
176-
if dep_version in packages:
177-
continue
178-
179-
# pnpm >=v9 where packages always have name@version
180-
if "{}@{}".format(dep_package, dep_version) in packages:
181-
continue
182-
183-
msg = "Expected to file: referenced package {} in first-party links {}".format(dep_key, fp_links.keys())
184-
fail(msg)
158+
for link_package, importer_deps in importer_deps_map.items():
159+
for dep_package, dep in importer_deps.items():
160+
deps_type = "link_dev_packages" if dep["dev"] else "link_packages"
161+
dep_version = dep["version"]
162+
if dep_version.startswith("file:"):
163+
dep_key = "{}+{}".format(dep_package, dep_version)
164+
if not dep_key in fp_links.keys():
165+
# Ignore file: dependencies on packages such as file: tarballs
166+
# TODO(3.0): remove with pnpm <v9
167+
if dep_version in packages:
168+
continue
169+
170+
# pnpm >=v9 where packages always have name@version
171+
if "{}@{}".format(dep_package, dep_version) in packages:
172+
continue
173+
174+
msg = "Expected to file: referenced package {} in first-party links {}".format(dep_key, fp_links.keys())
175+
fail(msg)
176+
if deps_type not in fp_links[dep_key]:
177+
fp_links[dep_key][deps_type] = {}
178+
fp_links[dep_key][deps_type][link_package] = True
179+
elif dep_version.startswith("link:"):
180+
dep_link = dep_version[len("link:"):]
181+
dep_path = helpers.link_package(root_package, dep_link)
182+
dep_key = "{}+{}".format(dep_package, dep_path)
183+
if fp_links.get(dep_key, False):
185184
if deps_type not in fp_links[dep_key]:
186185
fp_links[dep_key][deps_type] = {}
187186
fp_links[dep_key][deps_type][link_package] = True
188-
elif dep_version.startswith("link:"):
189-
dep_link = dep_version[len("link:"):]
190-
dep_path = helpers.link_package(root_package, dep_link)
191-
dep_key = "{}+{}".format(dep_package, dep_path)
192-
if fp_links.get(dep_key, False):
193-
if deps_type not in fp_links[dep_key]:
194-
fp_links[dep_key][deps_type] = {}
195-
fp_links[dep_key][deps_type][link_package] = True
196-
else:
197-
transitive_deps = {}
198-
raw_deps = {}
199-
if importers.get(dep_link, False):
200-
raw_deps = importers.get(dep_link).get("deps")
201-
for raw_package, raw_version in raw_deps.items():
202-
package_store_name = utils.package_store_name(raw_package, raw_version)
203-
dep_store_target = """"//{root_package}:{package_store_root}/{{}}/{package_store_name}".format(name)""".format(
204-
root_package = root_package,
205-
package_store_name = package_store_name,
206-
package_store_root = utils.package_store_root,
207-
)
208-
if dep_store_target not in transitive_deps:
209-
transitive_deps[dep_store_target] = [raw_package]
210-
else:
211-
transitive_deps[dep_store_target].append(raw_package)
212-
213-
for dep_store_target in transitive_deps.keys():
214-
transitive_deps[dep_store_target] = ",".join(transitive_deps[dep_store_target])
215-
fp_links[dep_key] = {
216-
"package": dep_package,
217-
"path": dep_path,
218-
"link_packages": {},
219-
"link_dev_packages": {},
220-
"deps": transitive_deps,
221-
}
222-
fp_links[dep_key][deps_type][link_package] = True
187+
else:
188+
transitive_deps = {}
189+
raw_deps = {}
190+
if importers.get(dep_link, False):
191+
raw_deps = importers.get(dep_link).get("deps")
192+
for raw_package, raw_version in raw_deps.items():
193+
package_store_name = utils.package_store_name(raw_package, raw_version)
194+
dep_store_target = """"//{root_package}:{package_store_root}/{{}}/{package_store_name}".format(name)""".format(
195+
root_package = root_package,
196+
package_store_name = package_store_name,
197+
package_store_root = utils.package_store_root,
198+
)
199+
if dep_store_target not in transitive_deps:
200+
transitive_deps[dep_store_target] = [raw_package]
201+
else:
202+
transitive_deps[dep_store_target].append(raw_package)
203+
204+
for dep_store_target in transitive_deps.keys():
205+
transitive_deps[dep_store_target] = ",".join(transitive_deps[dep_store_target])
206+
fp_links[dep_key] = {
207+
"package": dep_package,
208+
"path": dep_path,
209+
"link_packages": {},
210+
"link_dev_packages": {},
211+
"deps": transitive_deps,
212+
}
213+
fp_links[dep_key][deps_type][link_package] = True
223214

224215
npm_link_packages_const = """_LINK_PACKAGES = {link_packages}""".format(
225216
link_packages = str(link_packages),

0 commit comments

Comments
 (0)