Skip to content

fix(bff): make cross-project client declarations portable#8735

Open
omarshibli wants to merge 3 commits into
web-infra-dev:mainfrom
omarshibli:fix/bff-crossproject-client-dts-portability
Open

fix(bff): make cross-project client declarations portable#8735
omarshibli wants to merge 3 commits into
web-infra-dev:mainfrom
omarshibli:fix/bff-crossproject-client-dts-portability

Conversation

@omarshibli

@omarshibli omarshibli commented Jul 1, 2026

Copy link
Copy Markdown

Summary

With bff.crossProject: true and declaration: true, consuming projects cannot resolve the generated <package>/api/* client types, failing with errors like:

error TS2307: Cannot find module '@shared/index' or its corresponding type declarations.

Two independent defects compound here:

  1. Path aliases leak into every emitted declaration. compileByTs applies the tsconfig-paths transformer only as a before custom transformer, and TypeScript runs those solely on the JS emit — declaration emit keeps aliases exactly as authored (Alias for TypeScript declaration emitting microsoft/TypeScript#30952). So the emitted JS gets require("../shared/index") while the sibling .d.ts keeps from '@shared/index'.
  2. The cross-project client copy breaks relative specifiers. clientGenerator copies each handler's declaration from dist/<lambda>/… into dist/client/… verbatim. The copy lands one directory shallower than its origin, so the relative specifiers TypeScript emitted for the origin (including inline import("…") types) no longer resolve from the copy.

Fix

Each defect is fixed in the layer that creates it:

  • @modern-js/server-utils — the existing tsconfig-paths transformer now also runs on declaration emit (afterDeclarations), reusing the same path matcher. The declaration variant additionally handles inline import("…") types (ImportTypeNode), reads specifiers via .text (declaration nodes are synthesized without source positions, so getText() is unavailable), and keeps specifiers extensionless, matching tsc's own declaration output.
  • @modern-js/plugin-bff — the client generator re-bases relative specifiers onto the copy's location at copy time, where the origin and destination of each file are both known.

Because both fixes live where dev and build converge, dev output is fixed as well, and no post-build processing step is needed.

Real output from the bff-corss-project integration fixture, before → after:

// dist/api/lambda/index.d.ts (origin)
import type { ApiMessage } from '@shared/index';        // before
import type { ApiMessage } from "../../shared/index";   // after

// dist/client/index.d.ts (generated client, one directory shallower)
import type { ApiMessage } from '@shared/index';        // before (verbatim copy)
import type { ApiMessage } from "../shared/index";      // after (re-based)

Bare package specifiers (e.g. import("@modern-js/plugin-bff/server")) are left untouched.

Behavior notes

All server-utils compile consumers (the BFF api compile and app-tools' server compile) now get alias-free declarations when declaration is enabled. The previously emitted aliased specifiers were unresolvable outside the project, and the JS emit already received the identical rewrite, so this is strictly a fix rather than a behavior change consumers could have relied on.

Test plan

  • packages/server/utils/tests/ts.test.ts — new declaration-emit case: an aliased named import and an inline import("…") type are rewritten to relative specifiers; existing JS-emit cases unchanged.
  • packages/cli/plugin-bff/tests/clientGenerator.test.ts — unit tests for specifier re-basing (from / import(…) / require(…) forms; bare specifiers untouched; no-op when origin and target match).
  • tests/integration/bff-corss-projectbff-api-app now has an aliased type in a handler signature; a new prod-suite test walks every generated dist-1/client/**/*.d.ts, asserts no path alias leaks, and asserts every relative specifier resolves inside the dist tree.
  • Verified locally: server-utils and plugin-bff unit suites plus the full bff client-app in prod integration describe are green.

Related Links

Checklist

  • I have added changeset via pnpm run change.
  • I have updated the documentation. (N/A — bug fix, no API change)
  • I have added tests to cover my changes.

@changeset-bot

changeset-bot Bot commented Jul 1, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cdfa1c3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 118 packages
Name Type
@modern-js/plugin-bff Patch
@modern-js/server-utils Patch
bff-api-app Patch
bff-client-app Patch
bff-indep-client-app Patch
bff-hono Patch
deploy-server Patch
pure-esm-project Patch
basic-app-rstest Patch
integration-storybook Patch
@modern-js/server Patch
@modern-js/app-tools Patch
@modern-js/adapter-rstest Patch
@modern-js/plugin-ssg Patch
@modern-js/plugin-styled-components Patch
@modern-js/plugin-i18n Patch
@modern-js/image Patch
@modern-js/runtime Patch
@modern-js/plugin-polyfill Patch
@integration-test/alias-set Patch
app-document Patch
async-entry-test Patch
tmp Patch
integration-clean-dist-path Patch
integration-compatibility Patch
integration-custom-dist-path Patch
custom-file-system-entry Patch
integration-custom-template Patch
deploy Patch
dev-server Patch
integration-disable-html Patch
entries-app-builder Patch
app-custom-entries Patch
app-custom-routes-runtime Patch
app-custom Patch
app-entry Patch
app-route Patch
app-entry-server Patch
i18n-app-csr-html-lang Patch
i18n-app Patch
i18n-app-ssr-html-lang Patch
i18n-app-ssr Patch
i18n-custom-i18n-wrapper Patch
i18n-mf-app-provider Patch
i18n-mf-component-provider Patch
i18n-mf-consumer Patch
i18n-routes Patch
i18n-routes-ssr Patch
@integration-test/image-component Patch
main-entry-name Patch
nonce Patch
routes-match Patch
routes Patch
app-rsbuild-hooks Patch
rsc-csr-app Patch
rsc-csr-routes Patch
rsc-ssr-app Patch
rsc-ssr-routes Patch
basic-app-rstest-browser Patch
runtime-custom-plugin Patch
runtime-custom-config-plugin Patch
select-mul-entry-test Patch
select-one-entry-test Patch
server-config Patch
server-json-script Patch
server-monitors Patch
server-prod Patch
server-routes Patch
@source-code-build/app Patch
ssg-fixtures-mega-list-routes Patch
ssg-fixtures-nested-routes Patch
ssg-fixtures-simple Patch
ssg-fixtures-web-server Patch
ssr-base-async-entry-test Patch
ssr-base-async-pre-entry-test Patch
ssr-base-json-test Patch
ssr-base-test Patch
ssr-base-fallback-test Patch
init Patch
ssr-base-loadable Patch
ssr-partial-test Patch
rsc-closing-tags-test Patch
ssr-script-loading Patch
ssr-useid-test Patch
ssr-streaming-inline-test Patch
ssr-streaming-lazy-test Patch
ssr-streaming-test Patch
styled-components-stream Patch
styled-components-string Patch
integration-tailwindcss-v2 Patch
integration-tailwindcss-v3 Patch
integration-tailwindcss-v4-tools Patch
integration-tailwindcss-v4 Patch
tmp-dir Patch
write-to-dist Patch
@modern-js/bundle-diff-benchmark Patch
@modern-js/main-doc Patch
@modern-js/tsconfig Patch
@modern-js/builder Patch
@modern-js/plugin-data-loader Patch
@modern-js/render Patch
@modern-js/bff-core Patch
@modern-js/bff-runtime Patch
@modern-js/server-core Patch
@modern-js/create-request Patch
@modern-js/prod-server Patch
@modern-js/server-runtime Patch
@modern-js/create Patch
@modern-js/i18n-utils Patch
@modern-js/plugin Patch
@modern-js/runtime-utils Patch
@modern-js/sandpack-react Patch
@modern-js/types Patch
@modern-js/utils Patch
@modern-js/rslib Patch
@scripts/prebundle Patch
@scripts/rstest-config Patch
@scripts/release-node Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify

netlify Bot commented Jul 1, 2026

Copy link
Copy Markdown

Deploy Preview for modernjs-byted ready!

Name Link
🔨 Latest commit cdfa1c3
🔍 Latest deploy log https://app.netlify.com/projects/modernjs-byted/deploys/6a51332fc773690008dbf6dc
😎 Deploy Preview https://deploy-preview-8735--modernjs-byted.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 99 (🟢 up 3 from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@omarshibli omarshibli force-pushed the fix/bff-crossproject-client-dts-portability branch 2 times, most recently from 941facd to 164e525 Compare July 2, 2026 10:58
@omarshibli omarshibli changed the title fix(plugin-bff): make cross-project client declarations portable fix(bff): make cross-project client declarations portable Jul 2, 2026
@omarshibli omarshibli force-pushed the fix/bff-crossproject-client-dts-portability branch from 164e525 to af4e073 Compare July 2, 2026 11:37
@omarshibli omarshibli marked this pull request as ready for review July 2, 2026 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: BFF crossProject client .d.ts unresolvable for consumers (leaked path aliases + depth-shifted relative imports)

1 participant