• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pantsbuild / pants / 19525956643

20 Nov 2025 04:47AM UTC coverage: 80.302% (-0.001%) from 80.303%
19525956643

push

github

web-flow
Remove pyupgrade in favour of ruff-check (#22900)

Left ignores for anything not auto-fixable

14 of 17 new or added lines in 10 files covered. (82.35%)

1 existing line in 1 file now uncovered.

78276 of 97477 relevant lines covered (80.3%)

3.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

35.8
/src/python/pants/backend/typescript/goals/check.py
1
# Copyright 2025 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
from __future__ import annotations
1✔
5

6
import logging
1✔
7
import os
1✔
8
from dataclasses import dataclass, replace
1✔
9
from hashlib import sha256
1✔
10
from pathlib import Path
1✔
11
from typing import TYPE_CHECKING
1✔
12

13
from pants.backend.javascript.nodejs_project import AllNodeJSProjects, find_node_js_projects
1✔
14
from pants.backend.javascript.package_json import (
1✔
15
    AllPackageJson,
16
    OwningNodePackageRequest,
17
    PackageJson,
18
    all_package_json,
19
    find_owning_package,
20
)
21
from pants.backend.javascript.resolve import RequestNodeResolve, resolve_for_package
1✔
22
from pants.backend.javascript.subsystems.nodejs_tool import NodeJSToolRequest, prepare_tool_process
1✔
23
from pants.backend.javascript.target_types import JSRuntimeSourceField
1✔
24
from pants.backend.typescript.subsystem import TypeScriptSubsystem
1✔
25
from pants.backend.typescript.target_types import TypeScriptSourceField, TypeScriptTestSourceField
1✔
26
from pants.backend.typescript.tsconfig import AllTSConfigs, TSConfig, construct_effective_ts_configs
1✔
27
from pants.base.build_root import BuildRoot
1✔
28
from pants.build_graph.address import Address
1✔
29
from pants.core.goals.check import CheckRequest, CheckResult, CheckResults
1✔
30
from pants.core.target_types import FileSourceField
1✔
31
from pants.core.util_rules.system_binaries import CpBinary, FindBinary, MkdirBinary, TouchBinary
1✔
32
from pants.engine.fs import (
1✔
33
    EMPTY_DIGEST,
34
    CreateDigest,
35
    Digest,
36
    FileContent,
37
    GlobMatchErrorBehavior,
38
    PathGlobs,
39
)
40
from pants.engine.internals.graph import find_all_targets, hydrate_sources, transitive_targets
1✔
41
from pants.engine.internals.native_engine import MergeDigests
1✔
42
from pants.engine.internals.selectors import concurrently
1✔
43
from pants.engine.intrinsics import (
1✔
44
    create_digest,
45
    execute_process,
46
    merge_digests,
47
    path_globs_to_digest,
48
)
49
from pants.engine.process import Process
1✔
50
from pants.engine.rules import collect_rules, implicitly, rule
1✔
51
from pants.engine.target import (
1✔
52
    AllTargets,
53
    FieldSet,
54
    HydrateSourcesRequest,
55
    Target,
56
    TransitiveTargetsRequest,
57
)
58
from pants.engine.unions import UnionRule
1✔
59
from pants.option.global_options import GlobalOptions
1✔
60
from pants.util.frozendict import FrozenDict
1✔
61
from pants.util.logging import LogLevel
1✔
62
from pants.util.ordered_set import FrozenOrderedSet, OrderedSet
1✔
63

64
if TYPE_CHECKING:
65
    from pants.backend.javascript.nodejs_project import NodeJSProject
66

67
logger = logging.getLogger(__name__)
1✔
68

69

70
@dataclass(frozen=True)
1✔
71
class CreateTscWrapperScriptRequest:
1✔
72
    project: NodeJSProject
1✔
73
    package_output_dirs: tuple[str, ...]
1✔
74
    build_root: BuildRoot
1✔
75
    named_cache_sandbox_mount_dir: str
1✔
76
    tsc_wrapper_filename: str
1✔
77

78

79
@dataclass(frozen=True)
1✔
80
class TypeScriptCheckFieldSet(FieldSet):
1✔
81
    required_fields = (JSRuntimeSourceField,)
1✔
82

83
    sources: JSRuntimeSourceField
1✔
84

85

86
class TypeScriptCheckRequest(CheckRequest):
1✔
87
    field_set_type = TypeScriptCheckFieldSet
1✔
88
    tool_name = TypeScriptSubsystem.options_scope
1✔
89

90

91
def _build_workspace_tsconfig_map(
1✔
92
    workspaces: FrozenOrderedSet[PackageJson], all_ts_configs: list[TSConfig]
93
) -> dict[str, TSConfig]:
94
    workspace_dir_map = {os.path.normpath(pkg.root_dir): pkg for pkg in workspaces}
×
95
    workspace_dir_to_tsconfig: dict[str, TSConfig] = {}
×
96

97
    # Only consider tsconfig.json files (exclude tsconfig.build.json, tsconfig.test.json, etc.)
98
    # since tsc only reads tsconfig.json by default and we don't support the `--project` flag currently
99
    # Additionally, TSConfig class only supports tsconfig.json.
100
    tsconfig_json_files = [
×
101
        cfg for cfg in all_ts_configs if os.path.basename(cfg.path) == "tsconfig.json"
102
    ]
103

104
    for ts_config in tsconfig_json_files:
×
105
        config_dir = os.path.normpath(os.path.dirname(ts_config.path))
×
106

107
        if config_dir in workspace_dir_map:
×
108
            workspace_dir_to_tsconfig[config_dir] = ts_config
×
109

110
    return workspace_dir_to_tsconfig
×
111

112

113
def _collect_package_output_dirs(
1✔
114
    project: NodeJSProject, project_ts_configs: list[TSConfig], project_root_path: Path
115
) -> tuple[str, ...]:
116
    workspace_dir_to_tsconfig = _build_workspace_tsconfig_map(
×
117
        project.workspaces, project_ts_configs
118
    )
119

120
    package_output_dirs: OrderedSet[str] = OrderedSet()
×
121
    for workspace_pkg in project.workspaces:
×
122
        workspace_pkg_path = Path(workspace_pkg.root_dir)
×
123

124
        if workspace_pkg_path == project_root_path:
×
125
            pkg_prefix = ""
×
126
        else:
127
            relative_path = workspace_pkg_path.relative_to(project_root_path)
×
128
            pkg_prefix = f"{relative_path.as_posix()}/"
×
129

130
        pkg_tsconfig = workspace_dir_to_tsconfig.get(os.path.normpath(workspace_pkg.root_dir))
×
131

132
        if pkg_tsconfig and pkg_tsconfig.out_dir:
×
133
            resolved_out_dir = f"{pkg_prefix}{pkg_tsconfig.out_dir.lstrip('./')}"
×
134
            package_output_dirs.add(resolved_out_dir)
×
135

136
    return tuple(package_output_dirs)
×
137

138

139
def _collect_project_configs(
1✔
140
    project: NodeJSProject, all_package_jsons: AllPackageJson, all_ts_configs: AllTSConfigs
141
) -> tuple[list, list[TSConfig]]:
142
    project_package_jsons = [
×
143
        pkg for pkg in all_package_jsons if pkg.root_dir.startswith(project.root_dir)
144
    ]
145
    project_ts_configs = [
×
146
        config for config in all_ts_configs if config.path.startswith(project.root_dir)
147
    ]
148
    return project_package_jsons, project_ts_configs
×
149

150

151
async def _collect_config_files_for_project(
1✔
152
    project: NodeJSProject,
153
    targets: list[Target],
154
    project_package_jsons: list,
155
    project_ts_configs: list[TSConfig],
156
) -> list[str]:
157
    config_files = []
×
158

159
    for pkg_json in project_package_jsons:
×
160
        config_files.append(pkg_json.file)
×
161

162
    for ts_config in project_ts_configs:
×
163
        config_files.append(ts_config.path)
×
164

165
    # Add file() targets that are dependencies of JS/TS targets
166
    # Note: Package manager config files
167
    # (.npmrc, .pnpmrc, pnpm-workspace.yaml) should be dependencies of package_json targets
168
    # since they affect package installation, not TypeScript compilation directly.
169
    target_addresses = [target.address for target in targets]
×
170
    transitive_targets_result = await transitive_targets(
×
171
        TransitiveTargetsRequest(target_addresses), **implicitly()
172
    )
173

174
    for target in transitive_targets_result.closure:
×
175
        if target.has_field(FileSourceField):
×
176
            file_path = target[FileSourceField].file_path
×
177
            if file_path.startswith(project.root_dir):
×
178
                config_files.append(file_path)
×
179

180
    return config_files
×
181

182

183
@rule
1✔
184
async def create_tsc_wrapper_script(
1✔
185
    request: CreateTscWrapperScriptRequest,
186
    cp_binary: CpBinary,
187
    mkdir_binary: MkdirBinary,
188
    find_binary: FindBinary,
189
    touch_binary: TouchBinary,
190
) -> Digest:
191
    project = request.project
×
192
    package_output_dirs = request.package_output_dirs
×
193
    build_root = request.build_root
×
194
    named_cache_sandbox_mount_dir = request.named_cache_sandbox_mount_dir
×
195
    tsc_wrapper_filename = request.tsc_wrapper_filename
×
196

197
    project_abs_path = os.path.join(build_root.path, project.root_dir)
×
198
    cache_key = sha256(project_abs_path.encode()).hexdigest()
×
199

200
    project_depth = len([p for p in project.root_dir.strip("/").split("/") if p])
×
201
    cache_relative_path = (
×
202
        "../" * project_depth + named_cache_sandbox_mount_dir
203
        if project_depth > 0
204
        else named_cache_sandbox_mount_dir
205
    )
206
    project_cache_subdir = f"{cache_relative_path}/{cache_key}"
×
207

208
    package_dirs = []
×
209
    for workspace_pkg in project.workspaces:
×
210
        if workspace_pkg.root_dir != project.root_dir:
×
211
            package_relative = workspace_pkg.root_dir.removeprefix(f"{project.root_dir}/")
×
212
            package_dirs.append(package_relative)
×
213
        else:
214
            package_dirs.append(".")
×
215
    package_dirs_str = " ".join(f'"{d}"' for d in package_dirs)
×
216
    output_dirs_str = " ".join(f'"{d}"' for d in package_output_dirs)
×
217

NEW
218
    script_content = f"""#!/bin/sh
×
219
# TypeScript incremental compilation cache wrapper
220

221
# All source files in sandbox have mtime as at sandbox creation (e.g. when the process started).
222
# Without any special handling, tsc will do a fast immediate rebuild (without checking tsbuildinfo metadata).
223
# (tsc outputs: "Project 'tsconfig.json' is out of date because output 'tsconfig.tsbuildinfo' is older than input 'tsconfig.json'")
224
# What we want: tsconfig (oldest), tsbuildinfo (newer), source files (newest).
225
# See also https://github.com/microsoft/TypeScript/issues/54563
226

227
set -e
228

229
CP_BIN="{cp_binary.path}"
230
MKDIR_BIN="{mkdir_binary.path}"
231
FIND_BIN="{find_binary.path}"
232
TOUCH_BIN="{touch_binary.path}"
233

234
PROJECT_CACHE_SUBDIR="{project_cache_subdir}"
235

236
# Ensure cache directory exists (it won't on first run)
237
"$MKDIR_BIN" -p "$PROJECT_CACHE_SUBDIR" > /dev/null 2>&1
238

239
# Copy cached files to working directory
240
"$CP_BIN" -a "$PROJECT_CACHE_SUBDIR/." . > /dev/null 2>&1
241

242
# Make tsconfig files oldest
243
# This could be improved by passing a list of tsconfig files, but that might be best done if/when we have a Target for tsconfig files.
244
"$FIND_BIN" . -type f -name "tsconfig*.json" -exec "$TOUCH_BIN" -t 202001010000 {{}} +
245

246
# Run tsc
247
"$@"
248

249
# Update cache
250
# Copy .tsbuildinfo files located at package root to cache
251
for package_dir in {package_dirs_str}; do
252
    for tsbuildinfo_file in "$package_dir"/*.tsbuildinfo; do
253
        if [ -f "$tsbuildinfo_file" ]; then
254
            "$MKDIR_BIN" -p "$PROJECT_CACHE_SUBDIR/$package_dir"
255
            "$CP_BIN" "$tsbuildinfo_file" "$PROJECT_CACHE_SUBDIR/$package_dir/"
256
        fi
257
    done
258
done
259

260
# Copy output directories to cache
261
# (the output files are needed because tsc checks them against tsbuildinfo hashes when determining if a rebuild is required)
262
for output_dir in {output_dirs_str}; do
263
    if [ -d "$output_dir" ]; then
264
        output_parent="$(dirname "$output_dir")"
265
        "$MKDIR_BIN" -p "$PROJECT_CACHE_SUBDIR/$output_parent"
266
        "$CP_BIN" -r "$output_dir" "$PROJECT_CACHE_SUBDIR/$output_parent/"
267
    fi
268
done
269
"""
270

271
    script_digest = await create_digest(
×
272
        CreateDigest(
273
            [
274
                FileContent(
275
                    f"{project.root_dir}/{tsc_wrapper_filename}",
276
                    script_content.encode(),
277
                    is_executable=True,
278
                )
279
            ]
280
        ),
281
        **implicitly(),
282
    )
283

284
    return script_digest
×
285

286

287
async def _collect_project_targets(
1✔
288
    project: NodeJSProject,
289
    all_targets: AllTargets,
290
    all_projects: AllNodeJSProjects,
291
    project_ts_configs: list[TSConfig],
292
) -> list[Target]:
293
    has_check_js = any(
×
294
        ts_config.allow_js and ts_config.check_js for ts_config in project_ts_configs
295
    )
296

297
    if has_check_js:
×
298
        targets = [target for target in all_targets if target.has_field(JSRuntimeSourceField)]
×
299
    else:
300
        targets = [
×
301
            target
302
            for target in all_targets
303
            if (
304
                target.has_field(TypeScriptSourceField)
305
                or target.has_field(TypeScriptTestSourceField)
306
            )
307
        ]
308

309
    target_owning_packages = await concurrently(
×
310
        find_owning_package(OwningNodePackageRequest(target.address), **implicitly())
311
        for target in targets
312
    )
313

314
    project_targets = []
×
315
    for target, owning_package in zip(targets, target_owning_packages):
×
316
        if owning_package.target:
×
317
            package_directory = owning_package.target.address.spec_path
×
318
            target_project = all_projects.project_for_directory(package_directory)
×
319
            if target_project == project:
×
320
                project_targets.append(target)
×
321

322
    return project_targets
×
323

324

325
async def _hydrate_project_sources(
1✔
326
    project_targets: list[Target],
327
) -> Digest:
328
    workspace_target_sources = await concurrently(
×
329
        hydrate_sources(
330
            HydrateSourcesRequest(target[JSRuntimeSourceField]),
331
            **implicitly(),
332
        )
333
        for target in project_targets
334
    )
335

336
    return await merge_digests(
×
337
        MergeDigests([sources.snapshot.digest for sources in workspace_target_sources])
338
    )
339

340

341
async def _prepare_compilation_input(
1✔
342
    project: NodeJSProject,
343
    project_targets: list[Target],
344
    project_package_jsons: list,
345
    project_ts_configs: list[TSConfig],
346
    all_workspace_sources: Digest,
347
) -> Digest:
348
    config_files = await _collect_config_files_for_project(
×
349
        project, project_targets, project_package_jsons, project_ts_configs
350
    )
351
    for ts_config in project_ts_configs:
×
352
        ts_config.validate_outdir()
×
353

354
    config_digest = (
×
355
        await path_globs_to_digest(
356
            PathGlobs(config_files, glob_match_error_behavior=GlobMatchErrorBehavior.ignore),
357
            **implicitly(),
358
        )
359
        if config_files
360
        else EMPTY_DIGEST
361
    )
362

363
    return await merge_digests(MergeDigests([all_workspace_sources, config_digest]))
×
364

365

366
async def _prepare_tsc_build_process(
1✔
367
    project: NodeJSProject,
368
    subsystem: TypeScriptSubsystem,
369
    input_digest: Digest,
370
    package_output_dirs: tuple[str, ...],
371
    project_targets: list[Target],
372
    build_root: BuildRoot,
373
) -> Process:
374
    tsc_args = ("--build", *subsystem.extra_build_args)
×
375
    tsc_wrapper_filename = "__tsc_wrapper.sh"
×
376
    named_cache_local_dir = "typescript_cache"
×
377
    named_cache_sandbox_mount_dir = "._tsc_cache"
×
378

379
    project_resolve = await resolve_for_package(
×
380
        RequestNodeResolve(Address(project.root_dir)), **implicitly()
381
    )
382

383
    script_digest = await create_tsc_wrapper_script(
×
384
        CreateTscWrapperScriptRequest(
385
            project=project,
386
            package_output_dirs=package_output_dirs,
387
            build_root=build_root,
388
            named_cache_sandbox_mount_dir=named_cache_sandbox_mount_dir,
389
            tsc_wrapper_filename=tsc_wrapper_filename,
390
        ),
391
        **implicitly(),
392
    )
393

394
    tool_input = await merge_digests(MergeDigests([input_digest, script_digest]))
×
395

396
    tool_request = subsystem.request(
×
397
        args=tsc_args,
398
        input_digest=tool_input,
399
        description=f"Type-check TypeScript project {project.root_dir} ({len(project_targets)} targets)",
400
        level=LogLevel.DEBUG,
401
        project_caches=FrozenDict({named_cache_local_dir: named_cache_sandbox_mount_dir}),
402
    )
403

404
    # Uses the project's resolve as TypeScript execution requires project deps.
405
    tool_request_with_resolve: NodeJSToolRequest = replace(
×
406
        tool_request, resolve=project_resolve.resolve_name
407
    )
408

409
    process = await prepare_tool_process(tool_request_with_resolve, **implicitly())
×
410

411
    process_with_wrapper = replace(
×
412
        process,
413
        argv=(f"./{tsc_wrapper_filename}",) + process.argv,
414
        working_directory=project.root_dir,
415
        output_directories=package_output_dirs,
416
    )
417

418
    return process_with_wrapper
×
419

420

421
async def _typecheck_single_project(
1✔
422
    project: NodeJSProject,
423
    subsystem: TypeScriptSubsystem,
424
    global_options: GlobalOptions,
425
    all_targets: AllTargets,
426
    all_projects: AllNodeJSProjects,
427
    all_package_jsons: AllPackageJson,
428
    all_ts_configs: AllTSConfigs,
429
    build_root: BuildRoot,
430
) -> CheckResult:
431
    project_package_jsons, project_ts_configs = _collect_project_configs(
×
432
        project, all_package_jsons, all_ts_configs
433
    )
434

435
    project_targets = await _collect_project_targets(
×
436
        project, all_targets, all_projects, project_ts_configs
437
    )
438

439
    if not project_targets:
×
440
        return CheckResult(
×
441
            exit_code=0,
442
            stdout="",
443
            stderr="",
444
            partition_description=f"TypeScript check on {project.root_dir} (no targets)",
445
        )
446

447
    all_workspace_sources = await _hydrate_project_sources(project_targets)
×
448
    input_digest = await _prepare_compilation_input(
×
449
        project,
450
        project_targets,
451
        project_package_jsons,
452
        project_ts_configs,
453
        all_workspace_sources,
454
    )
455
    package_output_dirs = _collect_package_output_dirs(
×
456
        project, project_ts_configs, Path(project.root_dir)
457
    )
458

459
    process = await _prepare_tsc_build_process(
×
460
        project,
461
        subsystem,
462
        input_digest,
463
        package_output_dirs,
464
        project_targets,
465
        build_root,
466
    )
467
    result = await execute_process(process, **implicitly())
×
468

469
    return CheckResult.from_fallible_process_result(
×
470
        result,
471
        partition_description=f"TypeScript check on {project.root_dir} ({len(project_targets)} targets)",
472
        output_simplifier=global_options.output_simplifier(),
473
    )
474

475

476
@rule(desc="Check TypeScript compilation")
1✔
477
async def typecheck_typescript(
1✔
478
    request: TypeScriptCheckRequest,
479
    subsystem: TypeScriptSubsystem,
480
    global_options: GlobalOptions,
481
    build_root: BuildRoot,
482
) -> CheckResults:
483
    if subsystem.skip:
×
484
        return CheckResults([], checker_name=request.tool_name)
×
485

486
    field_sets = request.field_sets
×
487
    (
×
488
        all_projects,
489
        all_targets,
490
        all_package_jsons,
491
        all_ts_configs,
492
        owning_packages,
493
    ) = await concurrently(
494
        find_node_js_projects(**implicitly()),
495
        find_all_targets(),
496
        all_package_json(),
497
        construct_effective_ts_configs(),
498
        concurrently(
499
            find_owning_package(OwningNodePackageRequest(field_set.address), **implicitly())
500
            for field_set in field_sets
501
        ),
502
    )
503

504
    projects: OrderedSet[NodeJSProject] = OrderedSet()
×
505
    for owning_package in owning_packages:
×
506
        if owning_package.target:
×
507
            package_directory = owning_package.target.address.spec_path
×
508
            owning_project = all_projects.project_for_directory(package_directory)
×
509
            if owning_project:
×
510
                projects.add(owning_project)
×
511

512
    project_results = await concurrently(
×
513
        _typecheck_single_project(
514
            project,
515
            subsystem,
516
            global_options,
517
            all_targets,
518
            all_projects,
519
            all_package_jsons,
520
            all_ts_configs,
521
            build_root,
522
        )
523
        for project in projects
524
    )
525

526
    return CheckResults(project_results, checker_name=request.tool_name)
×
527

528

529
def rules():
1✔
530
    return [
1✔
531
        *collect_rules(),
532
        UnionRule(CheckRequest, TypeScriptCheckRequest),
533
    ]
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc