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

fix: js/ts dependency inference with file suffix #22041

Merged
merged 6 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/notes/2.26.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ yarn: 1.22.22

[Prettier](https://www.pantsbuild.org/stable/reference/subsystems/prettier) default version was upgraded from [2.6.2](https://github.com/prettier/prettier/releases/tag/2.6.2) to [3.5.2](https://github.com/prettier/prettier/releases/tag/3.5.2).

Fixed a bug on dependency inference to correctly handle imports with file suffixes (e.g., `.generated.js`).

#### Python

Some deprecations have expired and been removed:
Expand Down Expand Up @@ -122,6 +124,10 @@ The `experiemental_test_shell_command` target type is no longer experimental and

For the `tfsec` linter, the deprecation of support for leading `v`s in the `version` and `known_versions` field has expired and been removed. Write `1.28.13` instead of `v1.28.13`.

#### TypeScript

Fixed a bug on dependency inference to correctly handle imports with file suffixes (e.g., `.generated.ts`).

#### S3

The S3 backend now creates new AWS credentials when the `AWS_` environment variables change. This allows credentials to be updated without restarting the Pants daemon.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,13 @@ async def _prepare_inference_metadata(address: Address, file_path: str) -> Infer

def _add_extensions(file_imports: frozenset[str], file_extensions: tuple[str, ...]) -> PathGlobs:
extensions = file_extensions + tuple(f"/index{ext}" for ext in file_extensions)
valid_file_extensions = set(file_extensions)
return PathGlobs(
string
for file_import in file_imports
for string in (
[file_import]
if PurePath(file_import).suffix
if PurePath(file_import).suffix in valid_file_extensions
else [f"{file_import}{ext}" for ext in extensions]
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,31 @@ def test_infers_js_dependencies_via_config_and_extension_less_imports(
assert set(addresses) == {Address("root/project/src/components", relative_file_path="index.js")}


def test_infers_js_dependencies_with_file_suffix(rule_runner: RuleRunner) -> None:
rule_runner.write_files(
{
"root/project/src/__generated__/BUILD": "javascript_sources()",
"root/project/src/__generated__/moduleA.generated.js": "",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

.generated.js is one use case, but any suffix should be handled correctly.

"root/project/src/BUILD": "javascript_sources()",
"root/project/src/index.js": dedent(
"""\
import { x } from "./__generated__/moduleA.generated";
"""
),
}
)

index_tgt = rule_runner.get_target(Address("root/project/src", relative_file_path="index.js"))
addresses = rule_runner.request(
InferredDependencies,
[InferJSDependenciesRequest(JSSourceInferenceFieldSet.create(index_tgt))],
).include

assert set(addresses) == {
Address("root/project/src/__generated__", relative_file_path="moduleA.generated.js"),
}


def test_infers_js_dependencies_with_compiled_typescript_modules(rule_runner: RuleRunner) -> None:
rule_runner.write_files(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,33 @@ def test_infers_typescript_file_imports_dependencies_parent_dirs(rule_runner: Ru
}


def test_infers_typescript_file_imports_dependencies_with_file_suffix(
rule_runner: RuleRunner,
) -> None:
rule_runner.write_files(
{
"root/project/src/__generated__/BUILD": "typescript_sources()",
"root/project/src/__generated__/moduleA.generated.ts": "",
"root/project/src/BUILD": "typescript_sources()",
"root/project/src/index.ts": dedent(
"""\
import { x } from "./__generated__/moduleA.generated";
"""
),
}
)

index_tgt = rule_runner.get_target(Address("root/project/src", relative_file_path="index.ts"))
addresses = rule_runner.request(
InferredDependencies,
[InferTypeScriptDependenciesRequest(TypeScriptSourceInferenceFieldSet.create(index_tgt))],
).include

assert set(addresses) == {
Address("root/project/src/__generated__", relative_file_path="moduleA.generated.ts"),
}


def test_unmatched_ts_dependencies_error_unowned_behaviour(rule_runner: RuleRunner) -> None:
rule_runner.set_options(["--nodejs-infer-unowned-dependency-behavior=error"])
rule_runner.write_files(
Expand Down
Loading