Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

javascript: Support Node.js subpath imports #18934

Merged
merged 5 commits into from May 13, 2023

Conversation

tobni
Copy link
Contributor

@tobni tobni commented May 7, 2023

Adds support for the inference impl to inspect package.json#imports, and backwards map this object to find dependencies known by pants.

Relevant part of Node.js docs: https://nodejs.org/api/packages.html#subpath-imports.

Copy link
Sponsor Member

@stuhood stuhood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@@ -110,8 +129,10 @@ async def infer_js_source_dependencies(
owning_targets = await Get(Targets, Addresses(owners))

non_path_string_bases = FrozenOrderedSet(
os.path.basename(non_path_string) for non_path_string in import_strings - path_strings
non_path_string.split(os.path.sep, 1)[0]
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: For split(.., 1), partition can be a little bit cleaner: https://docs.python.org/3/library/stdtypes.html#str.partition


@staticmethod
def _to_import_pattern(string: str) -> re.Pattern[str]:
return re.compile(r"^" + re.escape(string).replace(r"\*", "(.*)"))
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these supposed to match files recursively, or only within a directory? If the latter, you might want to use ([^/]*)?

Copy link
Contributor Author

@tobni tobni May 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The '*' token is a string replacement token, not a glob. So I think the answer is the former statement is correct.

See https://nodejs.org/api/packages.html#subpath-patterns, specifically this bit

* maps expose nested subpaths as it is a string replacement syntax only.

@tobni tobni force-pushed the add/support-subpath-imports branch from b857162 to 386bc38 Compare May 12, 2023 18:12
@stuhood stuhood merged commit 47ee2d2 into pantsbuild:main May 13, 2023
17 checks passed
@tobni tobni deleted the add/support-subpath-imports branch May 16, 2023 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants