Skip to content
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
2 changes: 2 additions & 0 deletions DSPOT-PATCHES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ committed `.pants.bootstrap`. There's no wrapper script.
| 3 | JS inference reads a bundler query's file: `./App.tsx?raw`, `./logo.svg?url` | Branch `dspot/js-infer-bundler-query` (on patch 2's), posted the same way | Drop it when a Pants release we pin contains the upstream fix. |
| 4 | A TypeScript `.js` specifier infers the source it compiles from: `./errors.js` -> `errors.ts`/`.tsx`/`.d.ts`, `./View.jsx` -> `View.tsx` | Branch `dspot/js-infer-ts-js-specifier` (on patch 3's), posted the same way | Drop it when a Pants release we pin contains the upstream fix. |
| 5 | TypeScript check keeps the discovered named/root Node project and a sandbox-relative wrapper path (`9b4d1aed7`, `cfe58a301`) | Branch `dspot/ts-check-named-root-project` on this fork, on Pants main (60df85631); the issue and PR are posted by a person, as for patches 2-4 | Drop it when a Pants release we pin contains the equivalent named-project/root-wrapper fix. |
| 6 (next build) | Preserve TypeScript compiler inputs: imported JavaScript, exported declarations, JSON resources and module-specific declarations; recognize `.mts`/`.cts` sources and declarations in TypeScript source/test default globs, preserving TSX extensions | Internal branch `codex-r/typescript-input-closure`; coordinator/owner owns external review and submission | Drop it when a Pants release we pin contains equivalent compiler-input and export-inference fixes. |
| 7 (next build) | TypeScript check builds the configs owning the selected sources, with their declared project references, rather than every member of a Node resolve | Internal branch `codex-r/typescript-input-closure`; coordinator/owner owns external review and submission | Drop it when a Pants release we pin separates TypeScript programs from Node resolves. |

## Rules for this branch

Expand Down
15 changes: 13 additions & 2 deletions src/python/pants/backend/javascript/dependency_inference/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from pants.backend.typescript.target_types import TS_FILE_EXTENSIONS
from pants.backend.typescript.tsconfig import ParentTSConfigRequest, TSConfig, find_parent_ts_config
from pants.build_graph.address import Address
from pants.core.target_types import FileSourceField, ResourceSourceField
from pants.core.util_rules.unowned_dependency_behavior import (
UnownedDependencyError,
UnownedDependencyUsage,
Expand Down Expand Up @@ -103,7 +104,12 @@ async def infer_node_package_dependencies(
)
js_targets = await resolve_targets(**implicitly(Addresses(candidate_js_files)))
return InferredDependencies(
tgt.address for tgt in js_targets if tgt.has_field(JSRuntimeSourceField)
tgt.address
for tgt in js_targets
if any(
tgt.has_field(field)
for field in (JSRuntimeSourceField, FileSourceField, ResourceSourceField)
)
)


Expand Down Expand Up @@ -164,7 +170,12 @@ async def _prepare_inference_metadata(address: Address, file_path: str) -> Infer

# TypeScript resolves a `.js` or `.jsx` specifier to the source it compiles from, see
# https://www.typescriptlang.org/docs/handbook/modules/reference.html#file-extension-substitution
_TYPESCRIPT_SOURCES_OF_SPECIFIER = {".js": (".ts", ".tsx", ".d.ts"), ".jsx": (".tsx",)}
_TYPESCRIPT_SOURCES_OF_SPECIFIER = {
".js": (".ts", ".tsx", ".d.ts"),
".jsx": (".tsx",),
".mjs": (".mts", ".d.mts"),
".cjs": (".cts", ".d.cts"),
}


def _file_candidates(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,18 +647,20 @@ def test_infers_bin_package_json_field_js_source_dependency(rule_runner: RuleRun
assert set(addresses) == {Address("src/js/bin", relative_file_path="index.js")}


@pytest.mark.parametrize(
"exports", ("lib/index.js", {".": "lib/index.js"}, {"lib": "lib/index.js"})
)
@pytest.mark.parametrize("source", ["index.js", "index.d.ts", "schema.json"])
@pytest.mark.parametrize("export_form", ["string", "root", "subpath"])
def test_infers_exports_package_json_field_js_source_dependency(
rule_runner: RuleRunner, exports: str | dict[str, str]
rule_runner: RuleRunner, source: str, export_form: str
) -> None:
path = f"lib/{source}"
exports = path if export_form == "string" else {"." if export_form == "root" else "lib": path}
build = "javascript_sources()" if source == "index.js" else f'files(sources=["{source}"])'
rule_runner.write_files(
{
"src/js/BUILD": "package_json()",
"src/js/package.json": given_package("ham", "0.0.1", exports=exports),
"src/js/lib/BUILD": "javascript_sources()",
"src/js/lib/index.js": "",
"src/js/lib/BUILD": build,
f"src/js/lib/{source}": "",
}
)

Expand All @@ -668,7 +670,7 @@ def test_infers_exports_package_json_field_js_source_dependency(
[InferNodePackageDependenciesRequest(NodePackageInferenceFieldSet.create(pkg_tgt))],
).include

assert set(addresses) == {Address("src/js/lib", relative_file_path="index.js")}
assert set(addresses) == {Address("src/js/lib", relative_file_path=source)}


@pytest.mark.parametrize("exports", ("lib/*.js", {".": "lib/*.js"}, {"lib": "lib/*.js"}))
Expand Down
1 change: 1 addition & 0 deletions src/python/pants/backend/tsx/goals/tailor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def rule_runner() -> RuleRunner:
def test_find_putative_tsx_targets(
rule_runner: RuleRunner, files: dict, putative_map: list[ClassifiedSources]
) -> None:
files.update({"src/unowned/not_jsx.mtsx": "", "src/unowned/not_jsx.ctsx": ""})
rule_runner.write_files(files)
putative_targets = rule_runner.request(
PutativeTargets,
Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/backend/tsx/target_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from __future__ import annotations

from pants.backend.typescript.target_types import (
TS_FILE_EXTENSIONS,
TypeScriptDependenciesField,
TypeScriptSourceField,
TypeScriptTestSourceField,
Expand All @@ -25,7 +24,8 @@
)
from pants.util.strutil import help_text

TSX_FILE_EXTENSIONS = tuple(f"{ext}x" for ext in TS_FILE_EXTENSIONS)
# Module-specific .mts/.cts files do not have JSX counterparts.
TSX_FILE_EXTENSIONS = (".tsx", ".d.tsx")


class TSXDependenciesField(TypeScriptDependenciesField):
Expand Down
7 changes: 7 additions & 0 deletions src/python/pants/backend/typescript/goals/BUILD
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# Copyright 2023 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
python_sources()
python_test_utils(name="test_utils")
python_tests(
name="tests",
timeout=180,
overrides={
"check_test.py": {"dependencies": ["//src/python/pants/backend/typescript:test_resources"]},
"check_inputs_test.py": {
"dependencies": ["//src/python/pants/backend/typescript:test_resources"]
},
"check_selection_test.py": {
"dependencies": ["//src/python/pants/backend/typescript:test_resources"]
},
},
)
Loading