Skip to content

Commit

Permalink
fix: js/ts dependency inference with file suffix (Cherry-pick of #22041
Browse files Browse the repository at this point in the history
…) (#22056)

# Description

Its possible that we have a file like `foo.<suffix>.js`, but when
importing that file on another .js file, dependency inference wasn't
working.

Co-authored-by: Kevin Oliveira <[email protected]>
  • Loading branch information
WorkerPants and kevin-oliveira-zocdoc authored Mar 7, 2025
1 parent 4140abd commit dcbb66f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
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": "",
"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

0 comments on commit dcbb66f

Please sign in to comment.